Make variable global
aspineux
aspineux at gmail.com
Wed Mar 21 16:43:12 EDT 2007
try
>>> import a
>>> a.blah
>>> a.go()
>>> a.blah
or
rewrite a.py like this
blah = [ None ]
def go():
global blah
blah[0] = 5 # and not blah=[5]
then
>>> from a import *
>>> blah
>>> go()
>>> blah
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
On 21 mar, 20:55, "abcd" <codecr... at gmail.com> wrote:
> I have a file, "a.py"
>
> blah = None
> def go():
> global blah
> blah = 5
>
> >From the python interpreter I try....
> >>> from a import *
> >>> blah
> >>> go()
> >>> blah
>
> ...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
More information about the Python-list
mailing list