Invocation of Python's descriptor protocol from super

45 Views Asked by At

In the Python Descriptor HowTo guide, it is written that:

A dotted lookup such as super(A, obj).m searches obj.__class__.__mro__ for the base class B immediately following A and then returns B.__dict__['m'].__get__(obj, A). If not a descriptor, m is returned unchanged.

This seems to suggest that the search for attribute m only happens in the __dict__ of class B, which is the base class immediately following A, however, I think it should happen in all of the base classes in the mro following A. Am I missing anything here?

1

There are 1 best solutions below

2
chepner On

The lookup is basically the usual algorithm, but with a couple of beginning steps skipped:

  1. Look for m in obj.__dict__.
  2. Look for m in type(m).__dict__.
  3. Look for m in a class in the MRO of type(m), starting with the class after A.

Once m is found, then Python checks if m is a descriptor. If it is, m.__get__ is called and its return value is the result. Otherwise, m itself is the result.