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

Question:

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

Answers:

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)
Answered By: mgilson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.