global variables

Emile van Sebille emile at fenx.com
Fri Oct 12 09:37:05 EDT 2001


"Björn Buth" <bjoern4562000 at yahoo.de> wrote in message
news:9q6njs$17s4$1 at f1node01.rhrz.uni-bonn.de...
> Hi there,
>
> I´m experiencing a little problem due to my
> poor understanding of the way python handles
> global variables. Basically the problem is the following
> I have a programm with several variables which
> I would like to access globaly.
>
> --
>
> var=0
> def example_function()

add 'global var' here to use var from globals and not locals

>     var=var+1

Assigning to a variable within a function defines that variable as local.

>      (function definition)
> print example_function(10)
>
> will give an error: UnboundLocalError: local variable 'steine' referenced
> before assignment. Also replacing the first line by
>
> global var
> var=0
>


HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list