The global statement

Nick Vargish nav+posts at bandersnatch.org
Fri Jul 25 15:29:19 EDT 2003


"David Hitillambeau" <edavid at intnet.mu> writes:

> I want to enable some sharing between the two functions (foo and bar)
> using one global variable in such a way that each function can have read
> and write access over it.

Using the "global" statement seems unpythonic to me, for reasons I'm
too lazy to come up with good ways to express. :^) This is what I do,
if I need something like this: 

-----------------------------
class Global:
    """Generic container for shared variables."""
    pass

def foo():
    Global.somevar = 'set in foo'

def bar():
    print Global.somevar

foo()
bar()
-----------------------------

HTH,

Nick

-- 
#  sigmask  ||  0.2  ||  20030107  ||  public domain  ||  feed this to a python
print reduce(lambda x,y:x+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')




More information about the Python-list mailing list