Does the operator not work on objects with the same identifier?

I run:

Python 2.7.8 (default, Oct  6 2017, 09:25:50)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2

According to the documents :

Operators isand is notverify the identity of the object: x is yis Trueif and only if, when x, and y- this is one and the same object.

To get the identifier of an object, we can use the function id


If we open a new REPL, we will see that 300they -6have the same identifier (on CPython this means that both refer to the same memory address):

>>> id(300)
94766593705400
>>> id(-6)
94766593705400

Please note that actual values ​​may differ from performance to performance, but they are always equal.

However, execution 300 is -6gives False:

>>> 300 is -6
False

I have a couple of questions:

  • ( ) 300 -6 ?
  • , 300 is -6 False?
+4
2

id(300) 300 , . id(6), 6. -300 is 6, -300 6 , .

-300 6, :

>>> a, b = -300, 6
>>> id(a)
some number
>>> id(b)
some different number; 6 is still in the other memory address.

. CPython -5 256 ( ) , .

+10

id():

"" . ( ), . id().

- (, id(300)), .

+8

Source: https://habr.com/ru/post/1691113/


All Articles