[issue15412] Note in documentation for weakrefs
New submission from Richard Oudkerk <shibturn@gmail.com>: In the documentation on weakrefs there is the following quote Note: Weak references to an object are cleared before the object’s __del__() is called, to ensure that the weak reference callback (if any) finds the object still alive. But I think the weakref is always dead by the time the callback is invoked. The first paragraph from the documentation for weakref.ref(object[, callback]) contains the following: If callback is provided and not None, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized; the weak reference object will be passed as the only parameter to the callback; **the referent will no longer be available**. Which does prompt a question: what use is there for the weakref argument to the callback if it already dead? (Compare http://bugs.python.org/issue14933) ---------- assignee: docs@python components: Documentation messages: 166002 nosy: docs@python, pitrou, sbt priority: normal severity: normal status: open title: Note in documentation for weakrefs _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
Antoine Pitrou <pitrou@free.fr> added the comment:
In the documentation on weakrefs there is the following quote
Note: Weak references to an object are cleared before the object’s __del__() is called, to ensure that the weak reference callback (if any) finds the object still alive.
But I think the weakref is always dead by the time the callback is invoked.
It's true, the doc needs fixing.
Which does prompt a question: what use is there for the weakref argument to the callback if it already dead?
The weakref is "dead" but it's still a weakref, and it can be used to e.g. index a container of existing weakrefs (cf. WeakSet, WeakKeyDictionary, WeakValueDictionary). ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
Richard Oudkerk <shibturn@gmail.com> added the comment:
The weakref is "dead" but it's still a weakref, and it can be used to e.g. index a container of existing weakrefs (cf. WeakSet, WeakKeyDictionary, WeakValueDictionary).
Ah. I had assumed that since dead weakrefs were unhashable you couldn't safely use them as keys in a dict. (Presumably removal of a no longer hashable key from a dict is O(n)?) I also hadn't realize that weakrefs were subclassable. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
Antoine Pitrou <pitrou@free.fr> added the comment:
I had assumed that since dead weakrefs were unhashable you couldn't safely use them as keys in a dict. (Presumably removal of a no longer hashable key from a dict is O(n)?)
They are unhashable if you didn't hash them alive. Otherwise they retain their old hash value (which is quite useful :-)). ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
Roundup Robot added the comment: New changeset 78b0f294674c by Richard Oudkerk in branch '2.7': Issue #15412: Remove erroneous note about weakrefs http://hg.python.org/cpython/rev/78b0f294674c New changeset 24b13be81d61 by Richard Oudkerk in branch '3.2': Issue #15412: Remove erroneous note about weakrefs http://hg.python.org/cpython/rev/24b13be81d61 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
Changes by Richard Oudkerk <shibturn@gmail.com>: ---------- resolution: -> fixed stage: -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15412> _______________________________________
participants (3)
-
Antoine Pitrou -
Richard Oudkerk -
Roundup Robot