[Python-3000] PEP: Eliminate del (original) (raw)
Steven Bethard steven.bethard at gmail.com
Sat May 12 21:28:54 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/12/07, Guido van Rossum <guido at python.org> wrote:
On 5/12/07, Steven Bethard <steven.bethard at gmail.com> wrote: > And here's a version that doesn't lose updates to the finalizer attributes: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/519635 > > It replaces enablefinalizer() with a class attribute finalattrs. > >From finalize, all class attributes and methods are accessible, as > are any instance attributes specified by finalattrs. Guido's > BufferedWriter example looks like:: > > class BufferedWriter(Finalized): > finalattrs = 'buffer', 'raw' > ... > def flush(self): > self.raw.write(self.buffer) > self.buffer = b"" > > def finalize(self): > self.flush()
But can I subclass it and in the subclass override (extend) flush()? E.g. class MyWriter(BufferedWriter): def flush(self): super(MyWriter, self).flush() # Or super.flush() once PEP xxx is accepted print("Feel free to unplug the disk now")
Yep. The 'self' passed to finalize is still an instance of the same class (e.g. BufferedWriter or MyWriter). So inheritance works normally:
class BufferedWriter(Finalized): ... finalattrs = 'buffer', 'raw' ... def init(self): ... self.buffer = '' ... self.raw = 'raw' ... def flush(self): ... print 'writing:', self.buffer, 'to', self.raw ... self.buffer = '' ... def finalize(self): ... self.flush() ... class MyWriter(BufferedWriter): ... def flush(self): ... super(MyWriter, self).flush() ... print 'feel free to unplug the disk now' ... w = MyWriter() del w writing: to raw feel free to unplug the disk now
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
- Previous message: [Python-3000] PEP: Eliminate __del__
- Next message: [Python-3000] PEP: Eliminate __del__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]