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

Adam Olsen rhamph at gmail.com
Fri May 4 20:35:28 CEST 2007


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

On 5/4/07, Steven Bethard <steven.bethard at gmail.com> wrote: > On 5/4/07, Guido van Rossum <guido at python.org> wrote: > > 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.::

Oh, you mean 'self' as passed to the callback is not the instance? That kills the whole idea (since the typical del calls self.flush() or self.close()). [..snip example using dict..] If it really has to be done this way, I think the whole PEP is doomed.

Any attempt that keeps the entire contents of dict alive is doomed. It's likely to contain a cycle back to the original object, and avoiding that is the whole point of jumping through these hoops.

I've got a metaclass that moves explicitly marked attributes and methods into a "core" object, allowing you to write code like this:

class MyFile(safedel): coreattrs = ['_fd'] def init(self, path): super(MyFile, self).init() self._fd = os.open(path, ...) @coremethod def safedel(core): core.close() @coremethod def close(core): # This method is written to be idempotent if core._fd is not None: os.close(core._fd) core._fd = None

I've submitted it to the python cookbook, but I don't know how long it'll take to get posted; it's a little on the long side at 163 lines.

The biggest limitation is you can't easily use super() in core methods, although the proposed changes to super() would probably fix this.

-- Adam Olsen, aka Rhamphoryncus



More information about the Python-3000 mailing list