classification
Title: Doc: say id() is only useful for existing objects
Type: Stage: resolved
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, eric.araujo, ezio.melotti, georg.brandl, python-dev, rhettinger, terry.reedy
Priority: low Keywords: patch

Created on 2011-10-17 17:50 by terry.reedy, last changed 2013-10-12 16:14 by python-dev. This issue is now closed.

Files
File name Uploaded Description Edit
id_unique.diff georg.brandl, 2013-10-12 11:46 review
Messages (9)
msg145743 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-10-17 17:50
Newbies too often do something like (3.2.2, )

>>> id(getattr(x, 'pop')) == id(x.pop)
True

and get confused by the (invalid) result, whereas

>>> a,b=getattr(x, 'pop'),x.pop
>>> id(a)==id(b)
False

works properly. I think we should add a sentence or two or three to the id() doc, such as

Since a newly created argument object is either destroyed or becomes inaccessible before the function returns, *id(obj)* is only useful and valid if *obj* exists prior to the call and therefore after its return.
The value of an expression such as *id(666)== id(667)* is arbitrary and meaningless. The id of the first int object might or might not be reused for the second one.
msg145747 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-10-17 18:36
If we add something at all, it should be much shorter. After all, the current doc already states "unique during its lifetime".
msg145752 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-10-17 18:44
I gave a maximum addition. The first sentence may be enough (or even just the second half of it). What people do not get is that the lifetime of new arg objects is the duration of the function call -- and that the output of id for departed objects is uniquely useless and invalid.
msg145766 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-10-17 23:50
I think this is over-explaining an implementation detail and it makes the docs for id() harder to understand.

Possibly there can be a FAQ entry about identity but the id() function itself is no place to go into the quirks of when new objects are created or destroyed.

Also, "getting confused" is often a step in the learning process -- it can't be taken out entirely.
msg146157 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-10-22 02:35
A FAQ entry sounds good to me.
msg199551 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-10-12 11:46
Suggestion attached.
msg199557 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-10-12 12:31
LGTM

+ created and deleted during execution of the ``id()``

If you want to be more accurate you could say "before and after" instead of "during".
msg199584 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-12 16:13
New changeset 8525cc1f342f by Georg Brandl in branch '2.7':
Closes #13203: add a FAQ section about seemingly duplicate id()s.
http://hg.python.org/cpython/rev/8525cc1f342f
msg199585 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-12 16:14
New changeset 0d5de993db66 by Georg Brandl in branch '3.3':
Closes #13203: add a FAQ section about seemingly duplicate id()s.
http://hg.python.org/cpython/rev/0d5de993db66
History
Date User Action Args
2013-10-12 16:14:53python-devsetmessages: + msg199585
2013-10-12 16:13:49python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg199584

resolution: fixed
stage: needs patch -> resolved
2013-10-12 12:31:50ezio.melottisetmessages: + msg199557
2013-10-12 11:46:58georg.brandlsetfiles: + id_unique.diff
keywords: + patch
messages: + msg199551
2011-10-22 02:35:43ezio.melottisetnosy: + ezio.melotti

messages: + msg146157
stage: needs patch
2011-10-21 22:07:18eric.araujosetnosy: + eric.araujo
2011-10-17 23:55:24rhettingersetassignee: docs@python -> rhettinger
2011-10-17 23:50:05rhettingersetmessages: + msg145766
2011-10-17 18:44:48terry.reedysetmessages: + msg145752
2011-10-17 18:36:17georg.brandlsetpriority: normal -> low
nosy: + rhettinger, georg.brandl
messages: + msg145747

2011-10-17 17:50:34terry.reedycreate