473,512 Members | 15,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1258

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1163
by: Roger Espinosa | last post by:
I'm attempting to install dbxml-1.10 on OS X 10.2.8 (gcc3.x prerelease, Python 2.3.2 built on same), and while everything builds happily, when I try to invoke import dbxml Python just ......
2
4352
by: Moon | last post by:
Seems I still haven't got the hang of all those window generating code in Javascript. I've got a page with about 15 photo thumbnails. When you click on a thumbnail a new window pops up which shows...
4
358
by: Bhavna | last post by:
Hello Does anyone know what this message could be regarding? The application seems to fall over on the following line. Dim variable1 As SqlString = Arraylist1(i The first time the code runs...
6
2344
by: Andrew Mueller | last post by:
Hello all, There is a message below 'Application Error... When querying data'. What appears to be happening is that when I perform a lot of queries in a row and bring back the information into...
2
1311
by: Barry Mossman | last post by:
Hi, What is required to unwire an event ? I wire it with: this.MyEvent += new MyEventHandler() Do I need to maintain a handle to the eventhandler created so that I can detach it ? The...
669
25400
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
894
by: HLady | last post by:
I have a sequence of clicks in my apps that are always causing the session to start again. This is definitely not being caused by a timeout, cause it always happens no matter how long since the...
38
2284
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
1
1331
by: Adrian20XX | last post by:
Hi, I don't understand why the first C (c, not c++) program compiles and the second one does not compile. Any idea what is happening with the macro pre-processor in here, why StateStart(0) can...
2
1480
by: sarandnl08 | last post by:
Hi all, I can't find any problem in this program, i getting empty string as output, why? what happening here any one help? int main() { char *p1=name; char *p2; ...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.