470,821 Members | 2,084 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

what is happening here?

trying to figure out scoping of 'globals' in modules. So I have test.py:

glob = 1

def setglob(v):
global glob
glob = v

def getglob():
return glob
and I do this:
from test import *
glob 1 getglob() 1 glob=2
glob 2 getglob() 1 setglob(3)
getglob() 3 glob

2
Seems to me like the first time I invoke glob, it creates a new global
in my namespace, and initializes it to the value of the glob in test.py.
Seems counterintuitive!

Jul 18 '05 #1
2 1186

"dan miller (moderator, s.p.d)" <da*****@cmu.edu> wrote in message
news:c4**********@nntp.ece.cmu.edu...
trying to figure out scoping of 'globals' in modules. So I have test.py:

glob = 1

def setglob(v):
global glob
glob = v

def getglob():
return glob
and I do this:
>>> from test import *
>>> glob 1 >>> getglob() 1 >>> glob=2
>>> glob 2 >>> getglob() 1 >>> setglob(3)
>>> getglob() 3 >>> glob
2
Seems to me like the first time I invoke glob, it creates a new global
in my namespace, and initializes it to the value of the glob in test.py.
Seems counterintuitive!


Sure seems wierd. What it looks like to me is that
your import is binding three objects, but the actual
functions are still refering to whatever object is bound
in the "test" module, not in the interpreter's namespace.
In other words, they are using the global dictionary
they were compiled with, not their caller's global
dictionary.

Make sense?

John Roth

Jul 18 '05 #2
dan miller (moderator, s.p.d) wrote:
>>> from test import *
Here you create a variable glob that is a reference to the one in your test
module.
>>> glob 1


This returns the value of glob in the test module
>>> getglob() 1


Here you're calling getglob, which is in the test module.
>>> glob=2
Here's where your problem lies... You're creating a new variable glob that
*replaces* the old variable. The variable that is also called glob in the
test module is unaffected.
>>> glob 2


Now you're refering to the newly create variable, hence the value 2.
>>> getglob() 1


Here you're calling getglob, which is in the test module and hence it uses
the glob variable in test - which has the value 1.
>>> setglob(3)
>>> getglob() 3


Same with these two, they're both in test - and so use the variable glob in
test.
>>> glob

2


This still refers to glob in your local namespace, which still has the value
2.

Hope this is of some help,
Andrew
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Roger Espinosa | last post: by
4 posts views Thread by Bhavna | last post: by
6 posts views Thread by Andrew Mueller | last post: by
2 posts views Thread by Barry Mossman | last post: by
669 posts views Thread by Xah Lee | last post: by
reply views Thread by HLady | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.