The global statement

Thomas Güttler guettler at thomas-guettler.de
Wed Jul 23 10:56:08 EDT 2003


David Hitillambeau wrote:

> Hi guys,
> As I am new to Python, i was wondering how to declare and use global
> variables. Suppose i have the following structure in the same module (same
> file):
> 
> def foo:
>   <instructions>
>   <instructions>
> def bar:
>   <instructions>
>   <instructions>
> 
> 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.

Hi David,

global BAD
BAD=1

def foo():
    global BAD
    print BAD

def bar():
    global BAD
    print BAD

foo()
bar()

If foo and bar are in the same file, 
you don't need the "global".

 thomas









More information about the Python-list mailing list