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 passself
as 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
Bird
Class: TheBird
class is defined with an__init__
method that takes aname
parameter and initializes the instance variablename
with the provided name. Additionally, it has aspeak
method that returns a string indicating that the bird is speaking. - Defining the
Sparrow
Class: TheSparrow
class is derived from theBird
class using inheritance. It overrides thespeak
method to provide its own implementation of bird speech. In this case, it callssuper().speak()
to invoke thespeak
method 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 theSparrow
class is created and assigned to the variablerobin
. The constructor of theSparrow
class (__init__
method) is called with the argument'robin'
to set thename
attribute of therobin
instance. - Calling the
speak
Method: Thespeak
method of therobin
instance is called usingrobin.speak()
. Here’s what happens:- The overridden
speak
method of theSparrow
class is executed. - Inside the
Sparrow
class’sspeak
method,super().speak()
is called. This invokes thespeak
method of the parent class (Bird
). - The
Bird
class’sspeak
method returns the string"robin speaks"
. - The
Sparrow
class’sspeak
method appends ” as chirp chirp” to the returned string, resulting in"robin speaks as chirp chirp"
.
- The overridden
- 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 theBird
class, although theBird
class 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 theBird
class.Swan
: TheSwan
class inherits from bothSwimmingBird
andFlyingBird
using 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
Swan
class is created and assigned to the variablelenny
. The constructor of theSwan
class isn’t provided in the code snippet, but we’ll assume that the__init__
method of theBird
class is used to initialize thename
attribute. - Calling the
feature
Method:Thefeature
method of thelenny
instance is called usinglenny.feature()
. Here’s what happens:- Inside the
feature
method of theSwan
class:super().speak()
: This calls thespeak
method of the parent class in the method resolution order (MRO). SinceBird
is the parent class that defines thespeak
method, it returns a string indicating thatlenny
speaks.super().ability()
: This calls theability
method of the parent class in the MRO, which isSwimmingBird
in this case. It returns a string indicating thatlenny
can swim.super().fly()
: This calls thefly
method of the parent class in the MRO, which isFlyingBird
. It returns a string indicating thatlenny
can 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/
Leave a comment