Search code examples
pythonmethodsidentityequality

Why don't methods have reference equality?


I had a bug where I was relying on methods being equal to each other when using is. It turns out that's not the case:

>>> class What:
...     def meth(self):
...         pass

>>> What.meth is What.meth  # This is False in Python 2
True
>>> inst = What()
>>> inst.meth is inst.meth
False

Why is that the case? It works for regular functions:

>>> def func(): pass
>>> func is func
True

See also: How does Python's attribute lookup process work?.


Solution

  • Method objects are created each time you access them. Functions act as descriptors, returning a method object when their .__get__ method is called:

    >>> What.__dict__['meth']
    <function What.meth at 0x10a6f9c80>
    >>> What.__dict__['meth'].__get__(What(), What)
    <bound method What.meth of <__main__.What object at 0x10a6f7b10>>
    

    If you're on Python 3.8 or later, you can use == equality testing instead. On Python 3.8 and later, two methods are equal if their .__self__ and .__func__ attributes are identical objects (so if they wrap the same function, and are bound to the same instance, both tested with is).

    Before 3.8, method == behaviour is inconsistent based on how the method was implemented - Python methods and one of the two C method types compare __self__ for equality instead of identity, while the other C method type compares __self__ by identity. See Python issue 1617161.

    If you need to test that the methods represent the same underlying function, test their __func__ attributes:

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