A Python surprise: the consequences of variable scope
The comment on my AClosureConfusion entry brought up a little Python surprise that I had known about in the back of my mind but never thought about fully before: the consequences of Python's scoping rules for variables.
Ignoring closures for the moment, Python only has two scopes for variables: global to the module and local to the entire function. (Closures introduce additional scopes for the variables of the 'outer' functions.)
Well, sure, you know that. But the consequence is that any variable
in a function is 'live' for the entire function, including variables
used only as the index in
for loops and variables used only for
elements in list comprehensions. So when you write:
newlst = [x for x in lst if foobar(x)]
For the rest of the function (or the entire module) you will have an
x variable (in this case with the value of the last element of
at the time of the list comprehension).
This is a little bit surprising, at least for me, because
intellectually I usually consider such variables dead the moment the
list comprehension or
for loop is done. For example, I don't read
their value after that point.
In some languages, index variables really are dead after the loop finishes; references to them outside the loop will get some sort of 'no such variable' error. In some other languages, such as perl, such scope restriction is optional but the recommended style.