Re: function with a state

2005-03-09 Thread Dan Bishop
[EMAIL PROTECTED] wrote: > Xah Lee <[EMAIL PROTECTED]> wrote: > > > is it possible in Python to create a function that maintains a > > variable value? > > Yes. There's no concept of a 'static' function variable as such, but > there are many other ways to achieve the same thing. > > > globe=0; > > d

Re: function with a state

2005-03-09 Thread Xah Lee
Nevermind. I was thinking too much. :) Thanks. Xah Peter Hansen wrote: > 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

Re: function with a state

2005-03-08 Thread Peter Hansen
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

Re: function with a state

2005-03-08 Thread Xah Lee
>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? thanks. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listi

Re: function with a state

2005-03-07 Thread Reinhold Birkenfeld
Reinhold Birkenfeld wrote: > or with a default function argument: > > > class Dummy: pass > > def myFun(globe=Dummy()): > try:globe.globe += 1 > except: globe.globe = 1 > > return globe.globe A quicker way: def myFun(globe=[0]): globe[0] += 1 return globe[0] Reinhold

Re: function with a state

2005-03-06 Thread Stephen Thorne
On Sun, 06 Mar 2005 09:44:41 +0100, Patrick Useldinger <[EMAIL PROTECTED]> wrote: > 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 >retu

Re: function with a state

2005-03-06 Thread Mike Meyer
"Xah Lee" <[EMAIL PROTECTED]> writes: > the Python doc is quite stilted. Where in the python doc is a programer > supposed read about how the package/module system in Python works? > (besides the tutorial that touches it) The python docs at http://docs.python.org/ref/naming.html > are perfectly c

Re: function with a state

2005-03-06 Thread Kent Johnson
Patrick Useldinger wrote: Kent Johnson wrote: globe=0 globe=myfun(globe) def myFun(var): return var+1 This mystifies me. What is myfun()? What is var intended to be? myfun is an error ;-) should be myFun, of course. var is parameter of function myFun. If you call myFun with variable globe, all

Re: function with a state

2005-03-06 Thread Patrick Useldinger
Kent Johnson wrote: globe=0 globe=myfun(globe) def myFun(var): return var+1 This mystifies me. What is myfun()? What is var intended to be? myfun is an error ;-) should be myFun, of course. var is parameter of function myFun. If you call myFun with variable globe, all references to var will be

Re: function with a state

2005-03-06 Thread gene . tani
I believe python docs are quite *un*-stilted. Modules and packages are not complicated. Read chapter 7 of the nutshell, it's only 10 pages long. 2.3 and 2.4 didn't introduce any fundamental changes in how modules work AFAIK -- http://mail.python.org/mailman/listinfo/python-list

Re: function with a state

2005-03-06 Thread Andrew Koenig
"Xah Lee" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > globe=0; > def myFun(): > globe=globe+1 > return globe > > apparently it can't be done like that. I thought it can probably be > done by prefixing the variable with some package context... You can do this: globe=0

Re: function with a state

2005-03-06 Thread Reinhold Birkenfeld
Xah Lee wrote: > is it possible in Python to create a function that maintains a variable > value? > > something like this: > > globe=0; > def myFun(): > globe=globe+1 > return globe You could work with function attributes: def myFun(): try:myFun.globe += 1 except: myFun.globe =

Re: function with a state

2005-03-06 Thread gene . tani
and make it a singleton, viz: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 http://www.python.org/2.2.3/descrintro.html (scroll wayyy down) -- http://mail.python.org/mailman/listinfo/python-list

Re: function with a state

2005-03-06 Thread Kent Johnson
Patrick Useldinger wrote: 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 This mystifies me. What is myfun()? What is var intended to be? Kent -- http://mai

Re: function with a state

2005-03-06 Thread and-google
Xah Lee <[EMAIL PROTECTED]> wrote: > is it possible in Python to create a function that maintains a > variable value? Yes. There's no concept of a 'static' function variable as such, but there are many other ways to achieve the same thing. > globe=0; > def myFun(): > globe=globe+1 > return g

Re: function with a state

2005-03-06 Thread Patrick Useldinger
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