You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Doc: say id() is only useful for existing objects #57412
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='https://github.com/rhettinger'closed_at=<Date2013-10-12.16:13:49.126>created_at=<Date2011-10-17.17:50:34.768>labels= ['docs']
title='Doc: say id() is only useful for existing objects'updated_at=<Date2013-10-12.16:14:53.055>user='https://github.com/terryjreedy'
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.
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.
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.
Activity
terryjreedy commentedon Oct 17, 2011
Newbies too often do something like (3.2.2, )
and get confused by the (invalid) result, whereas
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.
birkenfeld commentedon Oct 17, 2011
If we add something at all, it should be much shorter. After all, the current doc already states "unique during its lifetime".
terryjreedy commentedon Oct 17, 2011
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.
rhettinger commentedon Oct 17, 2011
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.
ezio-melotti commentedon Oct 22, 2011
A FAQ entry sounds good to me.
birkenfeld commentedon Oct 12, 2013
Suggestion attached.
ezio-melotti commentedon Oct 12, 2013
LGTM
id()If you want to be more accurate you could say "before and after" instead of "during".
python-dev commentedon Oct 12, 2013
New changeset 8525cc1f342f by Georg Brandl in branch '2.7':
Closes bpo-13203: add a FAQ section about seemingly duplicate id()s.
http://hg.python.org/cpython/rev/8525cc1f342f
python-dev commentedon Oct 12, 2013
New changeset 0d5de993db66 by Georg Brandl in branch '3.3':
Closes bpo-13203: add a FAQ section about seemingly duplicate id()s.
http://hg.python.org/cpython/rev/0d5de993db66