473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Global variables in modules...

With a.py containing this:

========== a.py ===========
#!/usr/bin/env python
import b

g = 0

def main():
global g
g = 1
b.callb()

if __name__ == "__main__":
main()
==========================

....and b.py containing...

========= b.py =============
import a, sys

def callb():
print a.g
==========================

....can someone explain why invoking a.py prints 0?
I would have thought that the global variable 'g' of module 'a' would
be set to 1...
Mar 28 '08 #1
3 1321
tt*******@gmail.com wrote:
With a.py containing this:

========== a.py ===========
#!/usr/bin/env python
import b

g = 0

def main():
global g
g = 1
b.callb()

if __name__ == "__main__":
main()
==========================

...and b.py containing...

========= b.py =============
import a, sys

def callb():
print a.g
==========================

...can someone explain why invoking a.py prints 0?
I would have thought that the global variable 'g' of module 'a' would
be set to 1...
When you run a.py as a script it is put into the sys.modules module cache
under the key "__main__" instead of "a". Thus, when you import a the cache
lookup fails and a.py is executed again. You end up with two distinct
copies of the script and its globals:

$ python -i a.py
0
>>import __main__, a
a.g
0
>>__main__.g
1

Peter
Mar 28 '08 #2
That clears it up. Thanks.
Mar 28 '08 #3
>>>>"Peter" == Peter Otten <__*******@web.dewrites:
Petertt*******@gmail.com wrote:
[snip]
>...can someone explain why invoking a.py prints 0?
I would have thought that the global variable 'g' of module 'a' would
be set to 1...
PeterWhen you run a.py as a script it is put into the sys.modules module
Petercache under the key "__main__" instead of "a". Thus, when you import
Petera the cache lookup fails and a.py is executed again. You end up with
Petertwo distinct copies of the script and its globals
[snip]
Suggesting the following horribleness in a.py

g = 0

def main():
global g
import b
g = 1
b.callb()

if __name__ == "__main__":
import sys, __main__
sys.modules['a'] = __main__
main()
Terry
Mar 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
7
by: Aaron Deskins | last post by:
I'm trying to write program with a main.py and several functions distributed in auxiliary *.py files. I've looked through the archives on the 'global' keyword, but I'm still not sure how to get...
6
by: dkeeney | last post by:
I have a newby type question on how global variables work between modules. I have a module test2.py that defines a global variable as well as two functions to operate on that variable. A script...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...

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.