Don't understand global variables between modules

B

Bart

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
 
F

Fredrik Lundh

Bart said:
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>
 
T

Terry Reedy

Bart said:
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
 
B

Bart van Deenen

Fredrik Lundh said:
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.


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
 
B

Bart van Deenen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top