Make variable global

A

abcd

I have a file, "a.py"

blah = None
def go():
global blah
blah = 5
From the python interpreter I try....

....i was hoping to see "5" get printed out the second time I displayed
blah, but it doesn't. Now, if I type this same code directly into the
python interpreter it works as i was hoping. what i am missing?

thanks
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have a file, "a.py"

blah = None def go(): global blah blah = 5


...i was hoping to see "5" get printed out the second time I
displayed blah, but it doesn't. Now, if I type this same code
directly into the python interpreter it works as i was hoping.
what i am missing?

thanks
"import" imports bindings between symbols and objects into
the module from another one. When you call a function in another
module, it changes global space of module where it is defined.
The global space of the module, where caller is in, is kept from touched
except accessing through module names.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGAZYd1LDUVnWfY8gRAsdNAJ9niVqdGz1aG0NUzN+Ggmk+vnqZSgCfQB9s
SBuV/fWo8lqAytQP/QXQPXE=
=1BiL
-----END PGP SIGNATURE-----
 
G

Gary Herron

abcd said:
I have a file, "a.py"

blah = None
def go():
global blah
blah = 5


...i was hoping to see "5" get printed out the second time I displayed
blah, but it doesn't. Now, if I type this same code directly into the
python interpreter it works as i was hoping. what i am missing?
Since procedure go is defined in a.py, the global blah it refers to
is global to that module.

So import a (instead of importing * from a) and try this:

Gary Herron
 
A

aspineux

try

or

rewrite a.py like this

blah = [ None ]
def go():
global blah
blah[0] = 5 # and not blah=[5]

then
will work as expected

in case 1 ( import a )

blah in a.py and a.blah in python interpreter are the same
_REFERENCE_, they are the same

in case 2 (blah[0]=5)

blah in a.py and a.blah in python interpreter are just 2 differents
variables
that reference the _SAME_ object, but here (the difference with your
original case)
the object ( a list ) is a mutable object (you can change its value
without changing
the object. Here blah=[5] should simply make blah reference a new
object, but will
let the old one, [None] untouched, but unreachable (no more reference
to it)

BR
 
S

Steve Holden

abcd said:
I have a file, "a.py"

blah = None
def go():
global blah
blah = 5


...i was hoping to see "5" get printed out the second time I displayed
blah, but it doesn't. Now, if I type this same code directly into the
python interpreter it works as i was hoping. what i am missing?

thanks
That "blah" is global to module a, not to the whole program. You import
all names from a, which creates a blah and a go in your local namespace,
bound to the sames values those names have in module a.

When you call go() this sets a.blah to 5, but __main__.blah is still None.

It's those dratted namespaces again!

regards
Steve
 
B

Bruno Desthuilliers

abcd a écrit :
I have a file, "a.py"

blah = None
def go():
global blah
blah = 5



...i was hoping to see "5" get printed out the second time I displayed
blah, but it doesn't. Now, if I type this same code directly into the
python interpreter it works as i was hoping. what i am missing?

In Python, 'global' means 'module level'. And 'variables' are
name:eek:bject bindings in a namespace (mostly function, class or module).
The way you import blah and go from module a creates two names (blah and
go) in the current namespace, and bind these names to resp. a.blah and
a.go. The fact that go rebinds a.blah doesn't impact current namespace's
blah.

The following session may help you understanding what happens:
>>> from a import *
>>> dir() ['__builtins__', '__doc__', '__name__', 'blah', 'go']
>>> import a
>>> dir() ['__builtins__', '__doc__', '__name__', 'a', 'blah', 'go']
>>> dir(a) ['__builtins__', '__doc__', '__file__', '__name__', 'blah', 'go']
>>> go is a.go True
>>> go()
>>> blah
>>> a.blah 5
>>>

Note that rebinding a name and mutating a (mutable) object are two
distinct operations:

# b.py
blah = ['42']
def go():
blah[0] = "yo"

def gotcha():
global blah
blah = ['gotcha']
>>> from b import *
>>> import b
>>> blah ['42']
>>> blah is b.blah True
>>> go()
>>> blah ['yo']
>>> b.blah ['yo']
>>> blah is b.blah True
>>> gotcha()
>>> blah ['yo']
>>> b.blah ['gotcha']
>>>

To make a long story short:
1/ avoid globals whenever possible
2/ when using (module) globals, either use them as pseudoconstants or as
module's 'private' variables (IOW : only functions from the same module
can modify/rebind them).

HTH
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top