Why does the \'is\' operator behave differently with non-cached integers inside and outside a function in Python?

Mary-Kate Olsen
Release: 2024-10-31 19:22:02
Original
256 people have browsed it

Why does the 'is' operator behave differently with non-cached integers inside and outside a function in Python?

The 'is' Operator's Unexpected Behavior with Non-Cached Integers

Inquiry

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)
Copy after login

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?

Explanation

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.

Code Demonstration

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
Copy after login

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
Copy after login

We see that each code object has its instance of 1000, leading to the False evaluation.

Notes

  • This observation pertains to CPython, the most widely used Python implementation.
  • Chained statements evaluate to True for 'is' because they are treated as a single code block.
  • Execution at the module level also yields True.
  • Identity checks using 'is' are not recommended for mutable objects, as they will always evaluate to False.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!