Monday, 15 March 2010

Can't access python global variable (really weird) -



Can't access python global variable (really weird) -

so here's simplified version of what's going on

x = [] def test(): homecoming x def init(): x.append('blah')

the issue: init() function run in separate file , updates global variable x correctly. after running test() function, value of [] instead of ['blah']. here's gets weird. if run function (whatever_file.py beingness name of these functions stored):

x = [] def test(): whatever_file import x homecoming x def init(): x.append('blah')

this works fine. ['blah'] returned. have tried putting global x in init , test function no avail well. have no clue what's going on

any help?

python's globals aren't global. each module has own globals, variable you're storing isn't visible in other module's globals.

to store value in module's namespace, set attribute on module object directly:

import math def store(x): math.x = x

python global

No comments:

Post a Comment