A Python surprise: the consequences of variable scope

December 30, 2005

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 lst 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.

Comments on this page:

From at 2006-01-01 09:53:43:

Aha, ok, I bet that's true in javascript as well, explaining why ''var x = array[i];'' doesn't do the right thing!

-- nothings

By cks at 2006-01-01 11:50:27:

Yeah, Javascript is also a 'only local to the entire function' language.

It's more confusing than Python, because Javascript allows you to put the 'var blah=...' inside the for loop despite not having a just loop-wide scope. This was probably done because they wanted to allow variable declarations anywhere in the function body, not just at the start. (Python ducks the whole issue by not having variable declarations at all.)

Written on 30 December 2005.
« What I use asserts for
On Lisp's fundamental data structure »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Fri Dec 30 00:40:19 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.