Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note in documentation for weakrefs #59617

Closed
sbt mannequin opened this issue Jul 21, 2012 · 5 comments
Closed

Note in documentation for weakrefs #59617

sbt mannequin opened this issue Jul 21, 2012 · 5 comments
Labels
docs Documentation in the Doc dir

Comments

@sbt
Copy link
Mannequin

sbt mannequin commented Jul 21, 2012

BPO 15412
Nosy @pitrou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-08-17.14:08:30.143>
created_at = <Date 2012-07-21.09:41:57.691>
labels = ['docs']
title = 'Note in documentation for weakrefs'
updated_at = <Date 2012-08-17.14:08:30.142>
user = 'https://bugs.python.org/sbt'

bugs.python.org fields:

activity = <Date 2012-08-17.14:08:30.142>
actor = 'sbt'
assignee = 'docs@python'
closed = True
closed_date = <Date 2012-08-17.14:08:30.143>
closer = 'sbt'
components = ['Documentation']
creation = <Date 2012-07-21.09:41:57.691>
creator = 'sbt'
dependencies = []
files = []
hgrepos = []
issue_num = 15412
keywords = []
message_count = 5.0
messages = ['166002', '166006', '166009', '166010', '168446']
nosy_count = 4.0
nosy_names = ['pitrou', 'docs@python', 'python-dev', 'sbt']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue15412'
versions = []

@sbt
Copy link
Mannequin Author

sbt mannequin commented Jul 21, 2012

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)

@sbt sbt mannequin assigned docspython Jul 21, 2012
@sbt sbt mannequin added the docs Documentation in the Doc dir label Jul 21, 2012
@pitrou
Copy link
Member

pitrou commented Jul 21, 2012

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).

@sbt
Copy link
Mannequin Author

sbt mannequin commented Jul 21, 2012

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.

@pitrou
Copy link
Member

pitrou commented Jul 21, 2012

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-dev
Copy link
Mannequin

python-dev mannequin commented Aug 17, 2012

New changeset 78b0f294674c by Richard Oudkerk in branch '2.7':
Issue bpo-15412: Remove erroneous note about weakrefs
http://hg.python.org/cpython/rev/78b0f294674c

New changeset 24b13be81d61 by Richard Oudkerk in branch '3.2':
Issue bpo-15412: Remove erroneous note about weakrefs
http://hg.python.org/cpython/rev/24b13be81d61

@sbt sbt mannequin closed this as completed Aug 17, 2012
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

1 participant