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

Calling a Super Class Constructor in Python

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().

Similar Reads

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....

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....

Super

Python has super function which allows us to access temporary object of the super class....