[Python-3000] Removing __del__

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Tue Sep 26 14:24:27 CEST 2006


"Tim Peters" <tim.peters at gmail.com> writes:

> Read Modules/gc_weakref.txt for the gory details.

"It's a feature of Python's weakrefs too that when a weakref goes
away, the callback (if any) associated with it is thrown away too,
unexecuted."

I disagree with this choice. Doesn't it prevent weakrefs to be used as
finalizers?

Here is my semantics:

The core weakref constructor has three arguments: a key, a value,
and a finalizer.

(The finalizer is conceptually a function with no parameters.
In Python it's more convenient to make it a function with any arity,
along with the associated arguments.)

It's often the case that the key and the value are the same object.
The simplified form of the weakref constructor makes this assumption
and takes only a single object and a finalizer. The generic form is
needed for dictionaries with weak keys.

Creating a weak reference establishes a relationship:
- The key keeps the value alive.
- The weak reference and the finalizer are alive.

When the key dies, the relationship ends, and the finalizer is added
to a queue of finalizers to be executed.

Given a weak reference, you can obtain the value, which can possibly
return the information that the weakref is dead (None). You can also
invoke the finalizer explicitly, which also ends the relationship
(the thread is suspended if the finalizer is currently executing).
And you can kill the weak reference, ending the relationship.

I believe this is a sufficient design for most practical purposes.

See also
http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
but I disagree with the section about finalizers.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


More information about the Python-3000 mailing list