Fork me on GitHub

Project Notes

#061 Quirks - Late Bound Closures

Closures a great, but the results they produce depend on system state at the time they are called (not just when they are defined).

Notes

This is one of the classic gotchas detailed in the python-guide.

Here’s a function that returns an array of lambdas that you might expect to mulitply the given parameter by the poisiton in the list (i.e. the 0..4 produced by the range function):

def create_multipliers():
    return [lambda x : i * x for i in range(5)]

But in practice, it doesn’t work correctly:

for multiplier in create_multipliers():
    print multiplier(2)
8
8
8
8
8

This occurs because i in the lambda is only evaluated when it is called (i.e. print multiplier(2)). But by this time, the list comprehension has already been evaluated, for i in range(5) is over, and ‘i’ is 4.

A quick fix is to have the lambda bind immediately to its arguments:

def create_multipliers():
    return [lambda x, i=i : i * x for i in range(5)]

See late_bound_closures.py for a test of the problem and the fix.

$ ./late_bound_closures.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Examples

Credits and References

About LCK#61 python
Project Source on GitHub Return to the Project Catalog

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

LittleArduinoProjects LittleModelArt More on my blog