global variable not seen (bug?)

Peter Hansen peter at engcorp.com
Wed Jan 8 12:27:12 EST 2003


Peter Hansen wrote:
> 
> "Globals" are not really *global* in Python, they are visible only
> throughout the module in which they are defined, not throughout the
> entire application.
> 
> You've defined "GLOBAL_VARIABLE" in the module used for the interactive
> interpreter (well, I think that's roughly what happens... it's close
> enough for this discussion) but someFunction() is defined in another
> module which has its _own_ "namespace" (basically, the set of names
> that it recognizes).  Even though you've imported a name from the
> "imported" namespace into your own namespace(*), you don't change
> the fact that someFunction() looks in the namespace of the "imported"
> module to find the "global".

As a followup to my own response: try this to observe the effect
(after retyping exactly what you typed before in the interactive
interpreter):

>>> import imported
>>> imported.GLOBAL_VARIABLE = 'test'
>>> someFunction()
test


Note that even though you've created a local name bound to
"imported.someFunction" in the current namespace, you can
only affect the GLOBAL_VARIABLE that that function expects
to find by directly changing the original module "imported"
the way I did above.

Does that help?

-Peter




More information about the Python-list mailing list