function with a state

Peter Hansen peter at engcorp.com
Wed Mar 9 02:50:27 CET 2005


Xah Lee wrote:
>>def myFun(var):
>>  return var+1
>>globe = 0
>>globe = myFun(globe)
> 
> this is  intriguing. How does it work?
> not a rhetorical question, but where in the python doc can i read about
> it?

The tutorial, presumably, since there is nothing here
that isn't covered by the most basic aspects of Python.

Really, what did you think was there that was so
intriguing?  A function is defined.  It takes a
parameter and returns its value plus one.  A
variable is created with the value 0.  The
function is called, passing the value of the
variable, and the return value (remember, the
function just adds one!) is assigned to the
variable.  Now "globe" equals 1.

Again, what aspect of this startled you?

-Peter



More information about the Python-list mailing list