Why we will use obj$func() often
Mel Wilson
mwilson at the-wire.com
Fri Apr 23 16:15:42 EDT 2004
In article <w8dic.28710$dZ1.11206 at fed1read04>,
"Mark Hahn" <mark at prothon.org> wrote:
>The benefits of closures are well known. I'll give you the canonical
>example, but I'm sure textbooks can give you many more (by the way, Python
>cannot do this):
>
># tested example
>def getFunc():
> counter = 0
> def count():
> &counter += 1
> print &counter
> return count
>
>c = getFunc()
>c() # prints 1
>c() # prints 2
>c() # prints 3
It can, if you spell '&' differently:
def getfunc():
def count (n=[0]):
n[0] += 1
print n[0]
return count
c = getfunc()
c()
c()
c()
Regards. Mel.
More information about the Python-list
mailing list