473,385 Members | 1,730 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.

Don't understand global variables between modules

Hi all

I don't understand globals between multiple modules in a python program. I
really don't. I've narrowed it down to the following two very simple
programs a.py and b.py. When I run a.py I get the following output:

inc: 2
A: 2
inc: 3
B: 3
C: 1
I don't understand the last line at all. Why is my x variable 1 after having
been incremented twice? Is there more than one global space? Is this
backreference to the original a.py not allowed?

I could use some help.

Thanks

Bart van Deenen

a.py:
---------------------------------
import b
x=1

def inc():
global x
x+=1
print "inc: ",x

if __name__=="__main__":
b.test()
print "C: ",x

b.py:
---------------------------------
def test():
import a
a.inc()
print "A: ",a.x
a.inc()
print "B: ",a.x

Jul 18 '05 #1
4 1968
Bart wrote:
I don't understand globals between multiple modules in a python program. I
really don't. I've narrowed it down to the following two very simple
programs a.py and b.py. When I run a.py I get the following output:

inc: 2
A: 2
inc: 3
B: 3
C: 1

I don't understand the last line at all. Why is my x variable 1 after having
been incremented twice?
because running a script isn't the same thing as importing it. try adding
"print __name__" lines before your other print statements so you can see
who's printing what.
Is there more than one global space?


in this case, there are more module namespaces than you think.

this page might help (especially the "Using Modules as Scripts" section):

http://effbot.org/zone/import-confusion.htm

</F>

Jul 18 '05 #2

"Bart" <bv******@access-four-all.nl.invalid> wrote in message
news:42*********************@dreader14.news.xs4all .nl...
I don't understand globals between multiple modules in a python program.
Because there are not any. All names are bound to objects in a module
global namespace, a function local namespace, or an object attribute
namespace.

The builtins seem like and act like intermodule 'globals', but their names
are bound in a hidden module which is imported to all other modules and
treated like an extension of each module's namespace. Users can do
something similar by defining a 'myglobals' module and importing it
everywhere.
I've narrowed it down to the following two very simple
programs a.py and b.py. When I run a.py I get the following output:

inc: 2
A: 2
inc: 3
B: 3
C: 1
I don't understand the last line at all.


Don't feel too bad. While I 'know' the answer -- running anyfile.py
creates a module named '__main__' while importing it (in another module)
creates a separate module named 'anyfile' -- it did not 'click' until
reading Fredrik's hint. You created a nice, memorable example that shows
that __main__ is not anotherfile.anyfile (in this case, not b.a)!

Terry J. Reedy

Jul 18 '05 #3
Fredrik Lundh <fr*****@pythonware.com> wrote:
because running a script isn't the same thing as importing it. try adding
"print __name__" lines before your other print statements so you can see
who's printing what.
Is there more than one global space?


in this case, there are more module namespaces than you think.
this page might help (especially the "Using Modules as Scripts" section):
http://effbot.org/zone/import-confusion.htm


Thanks for your answer, and also thanks for effbot. Lots of good tips.

Bart
Jul 18 '05 #4
Hi

thanks for the answer. Coming from C and C++ this behaviour wasn't
really obvious to me. I still love Python though :-) Most elegant
language I've ever seen.

Bart
Jul 18 '05 #5

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

Similar topics

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...
10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
9
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
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...
4
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
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...
8
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.