Closures

Michele Simionato michele.simionato at poste.it
Fri Apr 30 00:07:48 EDT 2004


Gustavo Niemeyer <niemeyer at conectiva.com> wrote in message news:<mailman.128.1083272640.25742.python-list at python.org>...
> import sys
> 
> class Closure:
>     def __init__(self):
>         self.__dict__ = sys._getframe().f_back.f_locals
> 
> def getFunc():
>     counter = 0
>     c = Closure()
>     def count():
>         c.counter += 1
>         print c.counter
>     return count
> 
> c = getFunc()
> c()
> c()
> c()


This is a nice hack! Why not to make it available as a Cookbook recipe?

           Michele Simionato



More information about the Python-list mailing list