In general, super () takes two arguments: a class and an instance of this class. (1) The instance (self) object determines which MRO will be used to resolve any attributes. (2) The provided class defines a subset of this MRO, because super () uses only those entries in the MRO that appear after the provided class.
Thus, in the above case, when the super inside B is called from an instance of D Object, self refers to the object D (i.e. the instance of the object) and mro D (i.e. the instance of the object) is [D, B, C, A ], therefore, according to k (2), only those entries in the MRO that occur after B. will be used. [C, A]
Thus, it is recommended to use a class in which super () was used as the first argument, and the standard self as the second argument. The resulting object will save the instance namespace dictionary self, but it only retrieves the attributes that were defined in the classes found later in the MRO than the provided class.
Therefore, if we redefine B as
class B(A): def test(self): return 'B->'+super(C, self).test()
then call D (). test () will output ==> 'B-> A' explanation: from (1) above MRO self == [D, B, C, A, object] from (2) above, only those MRO entries that occur after of the provided class (i.e. C) so super will call test method A.
Kuldeep K. Rishi
source share