Categories
python

Python scoping

Last friday, Lau discovered an interesting edge case of python. Something that at first appeared to be a bug, but later revealed a deeper truth about the scoping of Python. I guess coming from a C++ background spoils one in some regards. Scoping in C++ is simple and uniform, I have never in many years of programming C++ got bitten by the scope of the language, something that I also can’t say about C#.

The problem boils down to how Python treats local variable and global variables. The following example, stolen and modified from here, shows the edge case in its most simple form:

a, b = (1, 2)

print(a, b)

def test():
    print(a)
    print(b)    # (A)
    #b=1       # (B)

test()

(A) works as long as (B) is commented out. The strange thing is that changing (B) into += produces the same result. Actually it is the case that += a global variable will never work, unless you have declared it in the local scope first. This comes back to the way Python threats variables in the local scope. The following link has more examples to illustrate how scoping works and also how this is different in Python 3k. As explained on Stackoverflow, using the dis functionality sheds some light on strange cases like these. All this said, the scoping has one good thing going for it. The following is quite useful and perfectly legal in Python:

i = 0

def ex1():
if i < 0:
b = 10
else:
b = 20
print b

ex1()