function with a state

Patrick Useldinger pu.news.001 at gmail.com
Sun Mar 6 03:44:41 EST 2005


Xah Lee wrote:

> globe=0;
> def myFun():
>   globe=globe+1
>   return globe

The short answer is to use the global statement:

globe=0
def myFun():
   global globe
   globe=globe+1
   return globe

more elegant is:

globe=0
globe=myfun(globe)
def myFun(var):
   return var+1

and still more elegant is using classes and class attributes instead of 
global variables.

-pu



More information about the Python-list mailing list