Why check if cls is the class in __subclasshook__?

Viewed 693

In the Python standard library documentation, the example implementation of __subclasshook__ is:

class MyIterable(metaclass=ABCMeta):

[...]

@classmethod
def __subclasshook__(cls, C):
    if cls is MyIterable:
        if any("__iter__" in B.__dict__ for B in C.__mro__):
            return True
    return NotImplemented

CPython's implementation of collections.abc indeed follows this format for most of the __subclasshook__ member functions it defines. What is the purpose of explicitly checking the cls argument?

1 Answers
Related