Home  >  Article  >  Backend Development  >  Why Do Methods in Python Lack Reference Equality?

Why Do Methods in Python Lack Reference Equality?

DDD
DDDOriginal
2024-10-27 15:53:02392browse

 Why Do Methods in Python Lack Reference Equality?

Method Reference Equality in Python: Uncovering the Mysteries

Why do methods, seemingly bestowed with inheritance from regular functions, lack the virtue of reference equality? This perplexing phenomenon has baffled many Python programmers. Let's delve into the underlying mechanisms to understand the reasons behind this disparity.

Unlike regular functions, which hold their object identity throughout the program, method objects are dynamically created upon access. This ephemeral nature stems from their dependency on descriptors, which generate method objects when their .__get__ method is invoked. Code snippets aptly illustrate this behavior:

<code class="python">>>> What.__dict__['meth']
<function What.meth at 0x10a6f9c80>
>>> What.__dict__['meth'].__get__(What(), What)
<bound method What.meth of <__main__.What object at 0x10a6f7b10>></code>

Starting with Python 3.8, equality testing for methods has become consistent and predictable. Two methods are considered equal if both their .__self__ (the instance they are bound to) and .__func__ (the underlying function) attributes are identical objects.

However, this consistent behavior is a recent addition. Prior to Python 3.8, method equality varied depending on their implementation details. For Python methods and certain C method types, self was compared for equality, while for the other C method type, self was compared by identity. This inconsistency was eventually addressed in Python issue 1617161.

To ensure consistency, it is recommended to verify method identity using their func attributes:

<code class="python">>>> What.meth == What.meth     # functions (or unbound methods in Python 2)
True
>>> What().meth == What.meth   # bound method and function
False
>>> What().meth == What().meth # bound methods with *different* instances
False
>>> What().meth.__func__ == What().meth.__func__ # functions
True</code>

In summary, the ephemerality of method objects, coupled with historical inconsistencies in method equality, has led to the absence of reference equality for methods. However, Python 3.8 introduces a more consistent and predictable approach, allowing programmers to reason about method equality with greater confidence.

The above is the detailed content of Why Do Methods in Python Lack Reference Equality?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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