Calling a Super Class Constructor in Python
Last Updated :
01 Aug, 2020
Classes are like creating a blueprint for an object. If we want to build a building then we must have the blueprint for that, like how many rooms will be there, its dimensions and many more, so here the actual building is an object and blueprint of the building is a class.
- A Class is a user-defined data-type which has data members and member functions.
- Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behavior of the objects in a Class.
A class is defined in Python using keyword class
followed by the name of class.
Class and Object structure
Declaring object in python :When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, we need to create objects.
Syntax :
object = ClassName()
Accessing data member and member functions: They can be accessed by dot(".") operator with the object of their respective class. For example, if the object is car and we want to access the function called drive, then we will have to write car.drive()
.
Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another class. The class which get inherited is called base class or parent class. The class which inherits the other class is called child class or derived class.
Example :
person class (parent class)
teacher class (child class)
Here we can see both the classes person and teacher, and as we are inheriting the person class in teacher so we have many of the features common like every person has a name, gender, canTalk(in most of the cases), canWalk(in most of the cases) etc., so in teacher class, we don't need to implement that thing again as it is inherited by teacher class so whatever features person has teacher must have, so we can add more features like canTeach() and teacher id and many other.
So the basic idea is if any class has inherited in other class then it must have the parent class features(it's unto you if want to use you can use ) and we can add more features on them.
Constructor
Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python, the
__init__()
method is called the constructor and is always called when an object is created.
Syntax :
def __init__(self):
# body of the constructor
Super
Python has super function which allows us to access temporary object of the super class.
Use of super class :
- We need not use the base class name explicitly.
- Helps in working with multiple inheritance.
Super with Single Inheritance :
Example :
python3
# this is the class which will become
# the super class of "Subclass" class
class Class():
def __init__(self, x):
print(x)
# this is the subclass of class "Class"
class SubClass(Class):
def __init__(self, x):
# this is how we call super
# class's constructor
super().__init__(x)
# driver code
x = [1, 2, 3, 4, 5]
a = SubClass(x)
Output :
[1, 2, 3, 4, 5]
Super with Multiple Inheritance :
Example : Implement the following inheritance structure in python using the super function :
Inheritance Structure
python3
# defining class A
class A:
def __init__(self, txt):
print(txt, 'I am in A Class')
# B class inheriting A
class B(A):
def __init__(self, txt):
print(txt, 'I am in B class')
super().__init__(txt)
# C class inheriting B
class C(B):
def __init__(self, txt):
print(txt, 'I am in C class')
super().__init__(txt)
# D class inheriting B
class D(B):
def __init__(self, txt):
print(txt, 'I am in D class')
super().__init__(txt)
# E class inheriting both D and C
class E(D, C):
def __init__(self):
print( 'I am in E class')
super().__init__('hello ')
# driver code
d = E()
print('')
h = C('hi')
Output :
I am in E class
hello I am in D class
hello I am in C class
hello I am in B class
hello I am in A Class
hi I am in C class
hi I am in B class
hi I am in A Class
Similar Reads
Calling A Super Class Constructor in Scala Prerequisite - Scala ConstructorsIn Scala, Constructors are used to initialize an object's state and are executed at the time of object creation. There is a single primary constructor and all the other constructors must ultimately chain into it. When we define a subclass in Scala, we control the sup
3 min read
How to call the constructor of a parent class in JavaScript ? In this article, we learn how to call the constructor of a parent class. Before the beginning of this article, we should have a basic knowledge of javascript and some basic concepts of inheritance in javascript. Constructor: Constructors create instances of a class, which are commonly referred to as
4 min read
Constructors in Python In Python, a constructor is a special method that is called automatically when an object is created from a class. Its main role is to initialize the object by setting up its attributes or state. The method __new__ is the constructor that creates a new instance of the class while __init__ is the init
3 min read
Call Parent class method - Python In object-oriented programming in Python, the child class will inherit all the properties and methods of the parent class when the child class inherits the parent class. But there may be some cases when the child class has to call the methods of the parent class directly. Python has an easy and effe
5 min read
Call a Class Method From another Class in Python In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method f
3 min read
Wrapper Class in Python Decoration is a way to specify management code for functions and classes. Decorators themselves take the form of callable objects that process other callable objects. A Class Decorator is similar to function decorators, but they are run at the end of a class statement to rebind a class name to a cal
2 min read
Create Derived Class from Base Class Universally in Python In object-oriented programming, the concept of inheritance allows us to create a new class, known as the derived class, based on an existing class, referred to as the base class. This facilitates code reusability and structuring. Python provides a versatile way to create derived classes from base cl
3 min read
Define and Call Methods in a Python Class In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov
3 min read
self in Python class In Python, self is a fundamental concept when working with object-oriented programming (OOP). It represents the instance of the class being used. Whenever we create an object from a class, self refers to the current object instance. It is essential for accessing attributes and methods within the cla
6 min read
Data Classes in Python | Set 2 (Decorator Parameters) Prerequisite: Data Classes in Python | Set 1 In this post, we will discuss how to modify the default constructor which dataclass module virtually makes for us. dataclass() decorator - @dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False) By changing t
4 min read