Python Scoping: Run it over me one more time please...
Justin Sheehy
dworkin at ccs.neu.edu
Fri Mar 24 16:18:24 EST 2000
"Warren Postma" <embed at geocities.com> writes:
> # But why can't this function modify the global Y:
> Y = 0
> def inc_Y():
> Y= (Y + 1)
> return Y
It can, if you add one line.
>>> Y = 0
>>> def inc_Y():
... global Y
... Y= (Y + 1)
... return Y
...
>>> inc_Y()
1
>>> Y
1
Python, in examining your function, saw that you are modifying Y and
decided that it is local. You just need to tell it that Y is a
global.
This sort of thing is mentioned a couple of times in the FAQ.
http://www.python.org/doc/FAQ.html#4.36
http://www.python.org/doc/FAQ.html#4.57
-Justin
More information about the Python-list
mailing list