id()

id()

  • id() is the memory address of the corresponding C object. if we do hex(id(obj))
  • It returns the identity of an object, it is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime.
>>> а = 100
>>> b = 100
>>> a is b
True

>>> a = 1000
>>> b = 1000
>>> a is b
False

Why? 

Answer: Python used integer caching for numbers between [-5, 256] inclusively
Same with strings, "String Interning"

The length of srring <= 4096, and without spaces. But for Python < 3..7 20 chars only.

>>> a = "Yang"
>>> b = "Yang"
>>> a is b
True
>>> c = "Yang Zhou"
>>> d = "Yang Zhou"
>>> c is d
False

But to cache exolicitely we can use sys.intern()

References

SuperMade with Super