Here's my test script:
def main(): #global
n = 1
z = None
def addone(): #local
if not z:
n = n+1
addone()
print n
main()
I step into the addone()
function once it hits the calling line.
At this point I can only see the variable z
, but cannot see n
.
Now, if n
is referenced before assignment, shouldn't z
be too?
Similarly, if I change n=n+1
to z='hi'
, I can no longer see z
!
This is contrary to all my previous beliefs about local/global functions! The more you know, the more you know you don't know about Python.
Question(s):
Why can I see one but not the other?
Do I want to be prepending
global
to these variables I want to reassign?