Modifying func_closure

Peter Otten __peter__ at web.de
Mon Jul 12 04:43:36 EDT 2004


Jacek Generowicz wrote:

>>> b) The closure is much clearer, shorter and more convenient.
>>> I have so many of these things that using the class version
>>> would make the code a real mess.

Does the following count as a closure, a class version, or a "real mess"?

class Bunch:
    def __init__(self, kw):
        self.__dict__.update(kw)

def make(a):
    c = 33
    self = Bunch(locals())
    def update(delta):
            self.a += delta
            return self.c, self.a
    return update

f = make(10)
for i in range(3):
    print f(i)

Put another way, could it be that the need for closures is just a request to
get rid of self in disguise?

Peter




More information about the Python-list mailing list