Home > Article > Backend Development > Why does the \'is\' operator behave differently with non-cached integers inside and outside a function in Python?
While experimenting with Python's interpreter, a paradox was noticed regarding the 'is' operator. Specifically, 'is' returns True when evaluated within a function but False when evaluated outside of it:
>>> def func(): ... a = 1000 ... b = 1000 ... return a is b ... >>> a = 1000 >>> b = 1000 >>> a is b, func() (False, True)
Since 'is' evaluates the object's 'id', this implies that within the 'func' function, 'a' and 'b' refer to the same int instance. However, outside of the function, they refer to different objects. Why is this the case?
The Python reference manual offers an insightful clarification:
"A block is a piece of Python program text that is executed as a unit. Each command typed interactively is a block."
Within a function, a single code block exists, containing only one object for the number 1000. Thus, 'id(a)' and 'id(b)' return the same value, resulting in a True evaluation.
Outside of the function, we have two separate code objects, each with its object for 1000. Therefore, 'id(a)' and 'id(b)' are different, leading to a False evaluation.
This quirk is not exclusive to integers. Similar results are observed with, for instance, float literals. Remember, comparing objects for identity (using 'is') is generally discouraged; instead, the equality operator ('==') should be employed.
To gain a clearer understanding, we can delve into the code objects for both cases:
Within the 'func' Function:
>>> print(dis.code_info(func)) ... Constants: 0: None 1: 1000
We have a single 'int' instance for 1000 that is assigned to both 'a' and 'b'.
Outside the 'func' Function:
>>> com1 = compile("a=1000", filename="", mode="single") >>> com2 = compile("b=1000", filename="", mode="single") >>> id(com1.co_consts[0]) == id(com2.co_consts[0]) False
We see that each code object has its instance of 1000, leading to the False evaluation.
The above is the detailed content of Why does the \'is\' operator behave differently with non-cached integers inside and outside a function in Python?. For more information, please follow other related articles on the PHP Chinese website!