Assigning to global variables from within a function

Tom Plunket tomas at fancy.org
Sat Jul 12 16:49:38 EDT 2003


Psymaster wrote:

> I want to do this:
> 
> int= 0
> 
> def change_int():
>     	int += 1
> 
> change_int()

You shouldn't do that, 'int' is a built-in name.  Additionally,
you need to tell the function that the variable is a global one.

my_int = 0
def change_int():
    global my_int
    my_int += 1

change_int()

You'll find that it works fine.

-tom!




More information about the Python-list mailing list