Shocked by python

I was shocked by the comportment of python:
B = 10
inline = lambda: B
print inline()
# OUT: 10
B = 20
print inline()
# OUT: 20
One would expect that the value of B is fixed after the declaration of the lambda function (like in functional languages).
It isn't, however.

EDIT: this doesn't work with full-fledged functions. Guess it's logical since the lambda doesn't creates a new scope, except, maybe, for it's arguments.



Leave a Reply.