On 5/4/07, Raymond Hettinger <[EMAIL PROTECTED]> 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.) Then classes that define __del__ won't have to be changed at all. (Of course dynamically patching a different __del__ method into the class won't have quite exactly the same semantics, but I don't really care about such a fragile and rare possibility; I care about vanilla use of __del__ methods.) -- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com