[Python-3000] PEP: Eliminate del (original) (raw)
Guido van Rossum guido at python.org
Fri May 4 19:15:19 CEST 2007
- Previous message: [Python-3000] PEP: Eliminate __del__
- Next message: [Python-3000] PEP: Eliminate __del__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.)
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/)
- Previous message: [Python-3000] PEP: Eliminate __del__
- Next message: [Python-3000] PEP: Eliminate __del__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]