interactive terminal results:
In [11]: a=1111111111111111111111111111111111111111111111111111
In [12]: b=1111111111111111111111111111111111111111111111111111
In [13]: a is b
Out[13]: False
script results:
# overflow in ~ [13:55:16]
» python -c "a=1111111111111111111111111111111111111111111111111111;b=1111111111111111111111111111111111111111111111111111; print a is b"
True
Emmmmm. I think the result should be False... Because 1111111111111111111111111111111111111111111111111111
is much larger than 256
I know is
is not an equality test. But, when Python declares a variable(immutable type), like b = 1
, it will find out whether the object has been declared in the old object(a = 1
). If the object is declared(a = 1
), the variable will point directly to the old object and will not apply for new memory space.(a is b
is True
)
In [2]: a=1
In [3]: b=1
In [4]: a is b
Out[4]: True
emmmm. right?
As for “is” operator behaves unexpectedly with integers. It explains the usage of is
, but I still don't understand why these two methods lead to different results. :(
Maybe, it just depends on the Python implementation and runtime environment.