问题
In the following example:
class A(object):
pass
prop1 = 1
prop2 = 2
prop3 = 3
prop4 = 4
obj = A()
tmp = ['prop1', 'prop2', 'prop3', 'prop4']
getter = [ lambda: getattr(obj, x) for x in tmp ]
I am always getting 4
when calling the getter
:
[getter[i]() for i in range(4)]
#[4, 4, 4, 4]
why!?
回答1:
This is a very common problem with lambdas. Ultimately, the variable x
is looked up when the function is called, not when it is created. As such, at the end of your loop, the value of x
is 'prop4'
and all your lambdas will give you the same thing.
The commonly proposed fix is to use a default argument in your lambda. It gets evaluated when the function is created.
lambda x=x: getattr(obj,x)
来源:https://stackoverflow.com/questions/17192226/strange-behavior-with-lamba-getattrobj-x-inside-a-list