>>> def make_incrementor (n): return lambda x: x + n
>>>
>>> f = make_incrementor(2)
>>> g = make_incrementor(6)
>>>
>>> print f(42), g(42)
44 48
>>>
>>> print make_incrementor(22)(33)
55
The above code defines a function "make_inrementor" that creates an anonymous function on the fly and returns it. The returned function increments its argument by the value is specified when it is created.
You can now create multiple different incrementor functions and assign them to variables, then use them independent from E Ach other. As the last statement demonstrates, you don ' t even has to assign the function anywhere--can just use it instantly a nd forget it ' s not needed anymore.
The following takes this a step further.
> >> foo = [ 2 18 9 22 17 24 8 12 27
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
> >> print map ( lambda x : Span class= "PLN" > x * 2 + 10 foo )
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139
From for notes (Wiz)
>> def make_incrementor (n): return lambda x:x + n,mamicode.com "style=" color: #ffffff "target=" _blank ">>& gt;> def make_incrementor (n): return lambda x:x + n