global variables

Malcolm Tredinnick malcolm at commsecure.com.au
Fri Oct 12 08:49:40 EDT 2001


On Fri, Oct 12, 2001 at 02:25:36PM +0200, Björn Buth wrote:
> 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()
>     var=var+1
>      (function definition)
> print example_function(10)
> 
> will give an error: UnboundLocalError: local variable 'steine' referenced
> before assignment.

You need to tell the example_function code that var is global before you
assign to it. So it's definition will look something like

def example_function()
    global var
    var = var + 1
    ...

Cheers,
Malcolm

-- 
The hardness of butter is directly proportional to the softness of the bread.




More information about the Python-list mailing list