Sunday 15 January 2012

Python lambda - is it necessary in such case? -



Python lambda - is it necessary in such case? -

lambda xxx: foo(xxx) - totally same foo? used in construction map(lambda xxx: foo(xxx), my_things)

i dont see difference:

def plus(a): homecoming a+1 in map(plus, [1,2,3]): print(i) in map(lambda a: plus(a), [1,2,3]): print(i)

prints same

2 3 4

yes, that's right. lambda x: foo(x) same calling foo(x).

remember lambda anonymous function x. in case, calls foo on x , returns result of foo(x).

for example, calling (lambda x: foo(x), 60) similar doing this:

def function2(y): homecoming y*2 def function1(x): homecoming function2(x) function1(60)

python lambda

No comments:

Post a Comment