python学習時の問題を記録する(一)

2689 ワード

pythonの公式ドキュメントを見ると
lambda expression
Small anonymous functions can be created with the  lambda  keyword. This function returns the sum of its two arguments:  lambda a, b: a+b . Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope:
>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43

この例は読めないので、解釈器でmake_を実行しました.incrementor(42)の結果、これが関数であることに気づき、はっと悟った.
return lambda x: x + n
        ,x      ,x + n