Could you explain lambda function to me?

fl rxjwg98 at gmail.com
Tue Jun 2 14:14:10 EDT 2015


Hi,

I see the description of lambda at the online tutorial, but I cannot 
understand it. '42' is transferred to the function. What 'x' value should
be? I do not see it says that it is '0'. And, what is 'x'?








>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43


The second lambda example is even more obscure to me:

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]


Could you explain 'key=lambda pair: pair[1]' to me?

Python grammar seems too succinct to me.


Thanks,



More information about the Python-list mailing list