re: 3 Tricky Python Nuances VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

I tried 158896587456 is 158896587456 on Python 3.6.1 and it answers True :/
(Even with huge numbers it does answer True)

 

Sorry, this is true when (at least) one of them is stored into a variable:

>>> a = 1
>>> b = 1
>>> print(a is b)
True
>>> a = 123456789
>>> b = 123456789
>>> print(a is b)
False

Indeed ! But it's weird that 123456789 is 123456789 = True, isn't it ? As if Python answers True for a is b if "a is the same as b" before testing if they are the same. Is there a reason for that ?

(123456789+0) is 123456789 = False
but 123456789 is 123456789 = True

Anyway thanks for the example and the answer !

Optimizer gets better and better. In 3.7, even 123456788+1 is 123456789 gives True. It's simply that Python realizes it can evaluate constant pure expressions before compiling to bytecode. And it folds constants, of course.

>>> compile('123456788+1 is 123456789', '', 'eval').co_consts

You'll notice there's only one 123456789 there, not two. And there are no 123456788 nor 1.

code of conduct - report abuse