"fl" <rxjw...@gmail.com> a écrit dans le message de news:323866d1-b117-4785-ae24-7d04c49bc...@googlegroups.com...
Hi,

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

make_incrementor is a fonction which return a function !
and the returned function just add n to its input

it is equivalent to:

def make_incrementor(n):

   def inc(x):
       return x+n

   return inc



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?

the sort is done on the values returned by the key function
key function "lambda pair: pair[1]" returns the second element of each pair ( , 
)

so the sort is done alphabetically on the strings 'one' 'two' 'three' 'four'


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to