[Tutor] Local/Global Variable

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Jun 15 02:42:04 EDT 2004


> The eror only shows itself when you do semthing like:
>
> globalvar = 1
>
> def foo():
>   globalvar += 1
>
> The interpreter see's yoau re assignign, and decides to think it's a
local
> variable.

Normally you'd be right but in this case the actual error he got can
be found in this situation too:

def foo():
   bar += 1

And could be solved with:

def foo():
   bar = 0
   bar += 1

In both cases the += operator needs a predefined variable to
compute the result. Whether it is global or not is a variant on
the problem but not directly related.

Alan G




More information about the Tutor mailing list