Here is a simple explanation of super() feature in python.
super() in Python is a built-in function that provides a way to call methods and access attributes from a parent class in an object-oriented programming context, particularly in situations involving inheritance. It allows you to achieve method overriding and multiple inheritance while maintaining control over the method resolution order (MRO).
In essence, super() returns a temporary object that represents the parent class in the current class’s context. This allows you to call methods and access attributes of the parent class without explicitly naming the parent class, which is especially useful when dealing with complex class hierarchies and multiple inheritance.
The general syntax for using super() is:
class ChildClass(ParentClass):
def some_method(self):
super().method_name()
Here are some key points about super():
-
Method Resolution Order (MRO): Python follows the C3 linearization algorithm to determine the order in which classes are searched for a method or attribute.
super()respects this order and ensures that methods are called in the correct sequence. -
Passing Arguments: When using
super()to call a method from a parent class, you don’t need to passselfas an argument explicitly. Thesuper()call automatically includes the current instance as the first argument. -
Multiple Inheritance:
super()is particularly useful in multiple inheritance scenarios, where a class inherits from multiple parent classes. It helps in avoiding ambiguity when accessing methods from parent classes with the same name. -
Mixin Classes:
super()can be useful in situations where you have “mixin” classes that provide specific behavior, and you want to compose these mixins together while retaining the ability to call methods from the individual mixins. -
Flexibility:
super()provides flexibility by allowing you to control which parent class’s method you want to call. You can specify a particular parent class when callingsuper(), which enables you to navigate the class hierarchy and choose the desired method. -
Cooperative Hierarchies:
super()encourages cooperative method overriding and helps avoid code duplication by promoting a clear and systematic way of accessing and extending parent class methods.
Let us take an example code
class Bird:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} speaks"
class Sparrow(Bird):
def speak(self):
return super().speak() + " as chirp chirp"
robin = Sparrow('robin')
print(robin.speak())
In the above code, how the code works:
-
Defining the
BirdClass: TheBirdclass is defined with an__init__method that takes anameparameter and initializes the instance variablenamewith the provided name. Additionally, it has aspeakmethod that returns a string indicating that the bird is speaking. -
Defining the
SparrowClass: TheSparrowclass is derived from theBirdclass using inheritance. It overrides thespeakmethod to provide its own implementation of bird speech. In this case, it callssuper().speak()to invoke thespeakmethod of the parent class (Bird) and then appends " as chirp chirp" to indicate the specific sound of a sparrow. -
Creating an Instance of
Sparrow: An instance of theSparrowclass is created and assigned to the variablerobin. The constructor of theSparrowclass (__init__method) is called with the argument'robin'to set thenameattribute of therobininstance. -
Calling the
speakMethod: Thespeakmethod of therobininstance is called usingrobin.speak(). Here’s what happens:-
The overridden
speakmethod of theSparrowclass is executed. -
Inside the
Sparrowclass’sspeakmethod,super().speak()is called. This invokes thespeakmethod of the parent class (Bird). -
The
Birdclass’sspeakmethod returns the string"robin speaks". -
The
Sparrowclass’sspeakmethod appends " as chirp chirp" to the returned string, resulting in"robin speaks as chirp chirp".
-
-
Printing the Result: robin speaks as chirp chirp
Let us try a multiple inheritance
class Bird
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} speaks"
class FlyingBird(Bird):
def fly(self):
return f" and {self.name} can fly"
class SwimmingBird(Bird):
def ability(self):
return f" {self.name} can swim"
class Swan(SwimmingBird, FlyingBird):
def feature(self):
return super().speak() + " woo woo " + super().ability() + super().fly()
lenny = Swan('lenny')
print(lenny.feature())
Let’s break down the code step by step:
-
Defining Classes:
-
FlyingBird: This class defines a method calledfly, which returns a string indicating that the bird can fly. It inherits from theBirdclass, although theBirdclass definition isn’t provided in the code snippet. -
SwimmingBird: This class defines a method calledability, which returns a string indicating that the bird can swim. Similar toFlyingBird, it’s expected that this class inherits from theBirdclass. -
Swan: TheSwanclass inherits from bothSwimmingBirdandFlyingBirdusing multiple inheritance. It defines a method calledfeature, where you intend to usesuper()to call methods from the parent classes.
-
-
Creating an Instance:An instance of the
Swanclass is created and assigned to the variablelenny. The constructor of theSwanclass isn’t provided in the code snippet, but we’ll assume that the__init__method of theBirdclass is used to initialize thenameattribute. -
Calling the
featureMethod:Thefeaturemethod of thelennyinstance is called usinglenny.feature(). Here’s what happens:- Inside the
featuremethod of theSwanclass:-
super().speak(): This calls thespeakmethod of the parent class in the method resolution order (MRO). SinceBirdis the parent class that defines thespeakmethod, it returns a string indicating thatlennyspeaks. -
super().ability(): This calls theabilitymethod of the parent class in the MRO, which isSwimmingBirdin this case. It returns a string indicating thatlennycan swim. -
super().fly(): This calls theflymethod of the parent class in the MRO, which isFlyingBird. It returns a string indicating thatlennycan fly.
-
- Inside the
-
Concatenating Results:The results of the three
super()calls are concatenated together with spaces between them using the+operator. -
Printing the Result:The concatenated result of the
super()calls is printed using theprint()function.
Hence the output will be :
lenny speaks woo woo lenny can swim and lenny can fly
It is important note that the line :
return super().speak() + " woo woo " + super().ability() + super().fly()
first time super().speak() reference the parent class Bird() and then invoke the speak method, then second and third time, it reference to the parent classes FlyingBird() and SwimmingBird() then invokes methods ability() and fly()
So we can understand how super() can be used to access the methods from different and multiple parent classes
This demonstrates how the super() function allows you to access methods from parent classes in a multiple inheritance scenario, preserving the method resolution order and allowing for customization of behavior.
It’s important to understand that while super() is a powerful tool, it should be used with care to prevent unexpected behavior, especially in complex class hierarchies.
more Python code samples : https://github.com/jeevanism/python-learn/
