What is a sibling class in Python?
Python 2 documentation says that super() function “returns a proxy object that delegates method calls to a parent or sibling class of type.”
My presumption was that a sibling for a given class is a class that inherits from the same parent. I drafted the following code to see how a method call can be delegated to a sibling, but it didn’t work. What do I do or understand wrong?
After some further research and reading Python’s super() considered super! article I came to the following conclusions:
Here’s an interesting case I found while experimenting with
The code will print
If you as me wonder why MethodA of Class0 never gets printed, here is the explanation as I understand it. The MRO of the ClassC as printed with
Now if you follow the MRO the super() function of MethodA() of ClassC will call the MethodA() of ClassA, which before printing will call MethodA() of classB (since it’s next in MRO). And the MethodA() of ClassB in its turn will just print and exit since it doesn’t use super() function to delegate the method call further up the MRO chain.