"from module import *" and modifying module's top-level vars

lemke_juergen at yahoo.de lemke_juergen at yahoo.de
Sun Oct 29 15:18:06 EST 2006


Hi everyone,

I define some vars and functions in a "support" module which gets
called from my
main app module. Using Python 2.5.

I import all symbols in the support module at the top of the main
module through:

from support import *

Is there a way for me to modify a top-level ("global"?) variable
defined in module
support from the code in the main module and still have those changes
visible to
code in the support module?

Consider the following example:

# support.py

globvar = "old"

def fun():
    print "fun(): globvar==" + globvar

# main.py

from support import *
global globvar # had hoped this would do the trick (as it does when
placed inside a function call)
glob = "new"

print "top level(): glob==" +glob

fun() # defined inside support

Calling main.py outputs

top level(): glob==new
fun(): glob==old

How can I get the assignment to globvar performed inside module main.py
to be
felt by the code in support.py?

Thanks in advance!

Best

Juergen Lemke




More information about the Python-list mailing list