Python 2 documentation says that super() function "returns a proxy object that delegates method calls to a parent or sibling class of type."
The questions:
- What is a sibling class in Python?
- How do you delegate a method call to a sibling class?
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?
class ClassA(object):
def MethodA(self):
print "MethodA of ClassA"
class ClassB(ClassA):
def MethodB(self):
print "MethodB of ClassB"
class ClassC(ClassA):
def MethodA(self):
super(ClassC, self).MethodA()
def MethodB(self):
super(ClassC, self).MethodB()
if __name__ == '__main__':
ClassC().MethodA() # Works as expected
# Fail while trying to delegate method to a sibling.
ClassC().MethodB() # AttirbuteError: 'super' object has no attribute 'MethodB'
super()
calls in the body of A on an instance of C will delegate to B. See this question for the low-down. – Gerraldsuper()
can indeed call parent but also sibling classes: #5034403 – Braudsuper()
calls a sibling class instead of the parent class (bothB
andC
are children ofA
). A sibling class indeed means what you think it means: two classes that have the same parent class and thus they're siblings.super()
can call parent classes as well as sibling classes. Hope this helps - if not please let me know. – Braud