creating variable in root namespace from module

Steve Holden steve at holdenweb.com
Fri Mar 10 03:23:08 EST 2006


MakaMaka wrote:
> Hi,
> I have a scope related question that I haven't been able to find an
> answer to anywhere.  Is there a way to have a function in an imported
> module add variables to the scope of the calling script?  Basically,
> can I have the following:
> 
> #root.py
> import some_module.py
> 
> some_module.afunction() # <== create a variable x1=1 and add it to the
> scope of root.py
> 
> print x1  # <== would print 1
> 
> I know this can be accomplished by passing the what globals() returns
> into the function; however, I'm wondering if there is a way to do this
> without passing anything to the function.
> 
There are clever technical ways to do this, but what's the use case? 
There's almost certainly a better way to accomplish what you want.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd                 www.holdenweb.com
Love me, love my blog         holdenweb.blogspot.com




More information about the Python-list mailing list