[Python-Dev] Re: weakref gc semantics

Tim Peters tim.peters at gmail.com
Fri Nov 5 16:05:23 CET 2004


[Greg Ewing]
>> Maybe the callback shouldn't be passed the weakref object?
>> I don't see what good that does anyway, since the weakref
>> is just an empty shell by then, isn't it?

[Jim Fulton]
> No.  It can still be used as a key. It still has it's original
> hash value and will compare equal to other weakrefs to the same
> original object. Weak-key dictionaries depend on this.

Right.

> In addition, as Tim points out, it could be a weakref-subclass instance
> and could have additional useful data.

Adn weak-value dictionaries depend on that; from weakref.py:

class KeyedRef(ref):
    """Specialized reference that includes a key corresponding to the value.

    This is used in the WeakValueDictionary to avoid having to create
    a function object for each key stored in the mapping.  A shared
    callback object can use the 'key' attribute of a KeyedRef instead
    of getting a reference to the key from an enclosing scope.

    """
    ...


More information about the Python-Dev mailing list