Python Scoping: Run it over me one more time please...

Warren Postma embed at geocities.com
Fri Mar 24 16:03:16 EST 2000


# I understand why this does what it does:

Z  = 0
def inc_Z(Z=Z):
    Z  = (Z  + 1)
    print "local Z =",Z # does not change global Z
    return Z
inc_Z()
print "global Z=",Z # global Z never changed. this I understand.

# But why can't this function modify the global Y:
Y = 0
def inc_Y():
    Y= (Y + 1)
    return Y

# I can also surmise that Y is not visible inside inc_Y...
try:
    inc_Y() # throws exception on invocation
except:
    print "fail!"

print "global Y=",Y

# so how do I create a function that increments a global and returns it!?

# Warren Postma






More information about the Python-list mailing list