Search code examples
pythonlistlambdagetattr

strange behavior with lamba: getattr(obj, x) inside a list


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!?


Solution

  • 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)