full closures

Terry Reedy tjreedy at home.com
Wed Feb 27 15:37:15 EST 2002


"Michael P. Soulier" <msoulier at nortelnetworks.com_.nospam> wrote in
message news:slrna7q9m0.4pb.msoulier at pmerd071.ca.nortel.com...
>     While I know about bound methods in Python, I'm wondering if a
full
> closure can be implemented. Can it?
>
>     Example: I want to pass an arbitrary code reference into a
closure and
> have all of its variables preserved.
>
>     ie.
>
>     print "the current count is %d" % counter; counter += 1

Default arg works as long as function is called properly:

>>> def prcnt(counter = [0]):
...   print "The current count is %d" % counter[0]
...   counter[0] = counter[0] + 1
...
>>> prcnt()
The current count is 0
>>> prcnt()
The current count is 1

Terry J. Reedy






More information about the Python-list mailing list