create global variables?

robert no-spam at no-spam-no-spam.com
Mon Oct 30 12:21:17 EST 2006


Alistair King wrote:
> Hi,
> 
> is there a simple way of creating global variables within a function?
> 

#module global:

def f(atom):
    global a
    a=1
    globals()[atom+'var']=2

def f():
    a=b=1
    globals().update(locals())

_global=sys.modules[__name__]

def f(atom):
    _global.a = 1
    setattr(_global,atom+'var', 2)

# all/app global 

import myglobals

def f(atom):
    myglobals.a=1
    setattr(myglobals....)


your example application seems to point towards 'odd' use of these techniques.


-robert



More information about the Python-list mailing list