Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Constructors in Python
Next article icon

Calling a Super Class Constructor in Python

Last Updated : 01 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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

Next Article
Constructors in Python

R

rajutkarshai
Improve
Article Tags :
  • Python
Practice Tags :
  • python

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
  • How to call base class constructor from child class in TypeScript ?
    In this article, we will learn how we can call the base class constructor from the child class. When a class inherits the properties of another class, it is called a child class and the class whose properties are inherited is called the parent class and the whole process is called Inheritance. In In
    3 min read
  • Attaching a Decorator to All Functions within a Class in Python
    Decorators in Python allow us to add extra features to functions or methods of class without changing their code. Sometimes, we might want to apply a decorator to all methods in a class to ensure they all behave in a certain way. This article will explain how to do this step by step. Applying Decora
    2 min read
  • call() decorator in Python
    Python Decorators are important features of the language that allow a programmer to modify the behavior of a class. These features are added functionally to the existing code. This is a type of metaprogramming when the program is modified at compile time. The decorators can be used to inject modifie
    3 min read
  • Closures And Decorators In Python
    Closures and decorators are powerful features in Python that allow for more advanced and flexible code patterns. Understanding these concepts can greatly enhance your ability to write clean, efficient, and reusable code. Why Python decorators rather than closures?Python decorators are preferred over
    3 min read
  • Constructor in Java Abstract Class
    Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will g
    4 min read
  • How to Define the Constructor Outside the Class in C++?
    A constructor is a special type of member function whose task is to initialize the objects of its class. It has no return type so can't use the return keyword and it is implicitly invoked when the object is created. Constructor is also used to solve the problem of initialization. It is called after
    3 min read
  • dict() Constructor in Python
    In Python, the dict() constructor is used to create a dictionary object. A dictionary is a built-in data type that stores data in key-value pairs, similar to a real-life dictionary where each word (the key) has a meaning (the value). The dict() constructor helps you create empty dictionaries or conv
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences