[Python-3000] PEP: Eliminate del (original) (raw)

Steven Bethard steven.bethard at gmail.com
Fri May 4 20:02:45 CEST 2007


On 5/4/07, Guido van Rossum <guido at python.org> wrote:

On 5/4/07, Raymond Hettinger <python at rcn.com> wrote: > An encapsulating function should be added to the weakref module > so that Guido's example could be written as: > > class BufferedWriter: > > def init(self, raw): > self.raw = raw > self.buffer = "" > weakref.cleanup(self, lambda s: s.raw.write(s.buffer))

Or, instead of a new lambda, just use the unbound method: weakref.cleanup(self, self.class.flush) Important: use the dynamic class (self.class), not the static class (BufferedWriter). The distinction matters when BufferedWriter is subclassed and the subclass overrides flush(). Hm, a thought just occurred to me. Why not arrange for object.new to call [the moral equivalent of] weakref.cleanup(self, self.class.del), and get rid of the direct call to del from the destructor? (And the special-casing of objects with del in the GC module, of course.)

That seems like a good idea, though I'm still a little unclear as to how far the AttrMap should be going to look like a real instance. As it stands, you can only access items from the instance dict. That means no methods, class attributes, etc.::

import weakref def cleanup(obj, callback, reg=[]): ... class AttrMap(object): ... def init(self, map): ... self._map = map ... def getattr(self, key): ... return self._map[key] ... def wrapper(wr, mp=AttrMap(obj.dict), callback=callback): ... _reg.remove(wr) ... callback(mp) ... _reg.append(weakref.ref(obj, wrapper)) ... class Object(object): ... # note that we do this in init because in new, the ... # object has no references to it yet ... def init(self): ... super(Object, self).init() ... if hasattr(self.class, 'newdel'): ... # note we use .im_func so that we can later pass ... # any object as the "self" parameter ... cleanup(self, self.class.newdel.im_func) ... class Foo(Object): ... def flush(self): ... print 'flushing' ... def newdel(self): ... print 'deleting' ... self.flush() ... f = Foo() del f deleting Exception exceptions.KeyError: 'flush' in <function wrapper at 0x00F34630> ignored

STeVe

I'm not in-sane. Indeed, I am so far out of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy



More information about the Python-3000 mailing list