Modifying func_closure
Peter Otten
__peter__ at web.de
Mon Jul 12 08:19:13 EDT 2004
Jacek Generowicz wrote:
[me]
>> def make(a):
>> c = 33
>> self = Bunch(locals())
>> def update(delta):
>> self.a += delta
>> return self.c, self.a
>> return update
[Jacek]
> def make(a):
> c = 33
> self = Bunch(locals())
> def update(self, delta):
> self.a += delta
> return self.c, self.a
> return update
Did you spot the difference?
Now let's change the misleading name 'self' into 'outer':
class Bunch:
def __init__(self, kw):
self.__dict__.update(kw)
def make(a):
c = 33
outer = Bunch(locals())
def update(self, delta):
outer.a += delta
return outer.c, outer.a
return update
class Foo:
update = make(20)
print Foo().update(99)
There you are.
I'm not sure why, if you're using a class anyway, you aren't using it to
store method state, too.
> However, when I'm implementing functions, I'd really like to be able
> to implement functions, and not have to try to hack around here and
> there trying to make classes or their instances behave like functions,
> wherever I need them to behave like functcions ... even when my
> function happens to carry some state around.
I've no principal objections, I just didn't spot a proposition that
integrates nicely with the rest of the language.
> (I'd also like Python not to treat builtin functions and pure Python
> functions differently, but that's a different rant :-)
Here I'm with you.
Peter
More information about the Python-list
mailing list