Issue 4638: 1 is 1 is allways true while 1.0 is 1.0 may sometimes be true (original) (raw)

Created on 2008-12-12 01:46 by nassrat, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg77654 - (view) Author: Hatem (nassrat) Date: 2008-12-12 01:46
In [29]: a,b = 1.0,1.0 In [30]: a is b Out[30]: True In [31]: a = 1.0 In [32]: b = 1.0 In [33]: a is b Out[33]: False # ?!?
msg77655 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2008-12-12 01:50
this is not a bug. ask this type of question on comp.lang.python. in short: 'is' is not an equality comparison operator. it compares object instance identity.
msg77656 - (view) Author: Hatem (nassrat) Date: 2008-12-12 01:54
Really, "is" is not equality but is object equivalence, wow I did not know that. So why is the first one true MR. This is truly a bug, why is the first one optimized while the second one isn't. And how come integers are allways optimized in that sense. Go close other tickets.
msg77657 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2008-12-12 01:54
"is" is for testing object identity, not numeric equality. That "1 is 1" is always true is simply an implementation detail common to all recent versions of CPython, due to CPython caching "very small" integer objects. The language definition neither requires nor forbids this, so any program relying on it is in error. Likewise any program relying on "1.0 is 1.0" being false is also in error.
msg77658 - (view) Author: Hatem (nassrat) Date: 2008-12-12 01:58
Atleast you didnt really challenge my inelegence like greg did. If you look at my small interpreted session, is returned true the first time. Why is that. The ticket title may not have been perfect, I was trying to be sarcastic/funny. I am asking why are objects sometime the same instance while sometimes aren't for floats.
msg77660 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2008-12-12 02:02
the only intelligence i'm challenging is that this is not appropriate for a bug tracker. bring it up on a users mailing list.
msg77661 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2008-12-12 02:39
Please take requests for discussion to comp.lang.python. Many people there understand this behavior and will be happy to explain it in as much detail as you want. The bug tracker is not the place for this.
History
Date User Action Args
2022-04-11 14:56:42 admin set github: 48888
2008-12-12 02:39:31 tim.peters set messages: +
2008-12-12 02:02:05 gregory.p.smith set messages: +
2008-12-12 01:58:24 nassrat set messages: +
2008-12-12 01:54:56 tim.peters set nosy: + tim.petersmessages: +
2008-12-12 01:54:16 nassrat set messages: +
2008-12-12 01:50:42 gregory.p.smith set status: open -> closedresolution: not a bugmessages: + nosy: + gregory.p.smith
2008-12-12 01:46:01 nassrat create