[issue13203] Doc: say id() is only useful for existing objects

New submission from Terry J. Reedy <tjreedy@udel.edu>: 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. ---------- assignee: docs@python components: Documentation messages: 145743 nosy: docs@python, terry.reedy priority: normal severity: normal status: open title: Doc: say id() is only useful for existing objects versions: Python 2.7, Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Georg Brandl <georg@python.org> added the comment: If we add something at all, it should be much shorter. After all, the current doc already states "unique during its lifetime". ---------- nosy: +georg.brandl, rhettinger priority: normal -> low _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Terry J. Reedy <tjreedy@udel.edu> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Changes by Éric Araujo <merwok@netwok.org>: ---------- nosy: +eric.araujo _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Ezio Melotti <ezio.melotti@gmail.com> added the comment: A FAQ entry sounds good to me. ---------- nosy: +ezio.melotti stage: -> needs patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Georg Brandl added the comment: Suggestion attached. ---------- keywords: +patch Added file: http://bugs.python.org/file32059/id_unique.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Ezio Melotti added the comment: 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". ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Roundup Robot added the comment: 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 ---------- nosy: +python-dev resolution: -> fixed stage: needs patch -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________

Roundup Robot added the comment: 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 ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue13203> _______________________________________
participants (6)
-
Ezio Melotti
-
Georg Brandl
-
Raymond Hettinger
-
Roundup Robot
-
Terry J. Reedy
-
Éric Araujo