How does Python 3 super work in this particular multiple inheritance situation

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

   This question already has an answer here:
    * /questions/3277367/how-does-pythons-super-work-with-multiple-inheritance /questions/3277367/how-does-pythons-super-work-with-multiple-inheritance 10 answers

Answers

Python has a mechanism called Method Resolution Order (MRO) to find proper methods in class hierarchies. There are two rules in MRO:

1- dept-first left-to-right search

2- only the last occurrence of a class is considered

Now back to your example:

I. The search starts at D by adding it to the MRO list [D].

II. Class D is inherited from B and C. B is the left most parent and then comes C. So the MRO list becomes [D, B, C].

III. Then the search continues to a deeper level with is parent of B and A is added to the list. Now we have [D, B, C, A]. The search continues to parents of C with is again A. Since A is already in the list, nothing changes.

IV. The search goes to a deeper level (parents of A) and because A does not have any explicit parent, the search ends at this point.

So the final list is [D, B, C, A].

super() method uses this list to do its job and thats why A is called at the end

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/39423134/how-does-python-3-super-work-in-this-particular-multiple-inheritance-situation

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils