Python Constructor

A constructor is a special type of method (function) which is used to initialize the instance members of the class.

In C++ or Java, the constructor has the same name as its class, but it treats constructor differently in Python. It is used to create an object.

Constructors can be of two types.

  1. Parameterized Constructor
  2. Non-parameterized Constructor

Constructor definition is executed when we create the object of this class. Constructors also verify that there are enough resources for the object to perform any start-up task.

Creating the constructor in python

In Python, the method the __init__() simulates the constructor of the class. This method is called when the class is instantiated. It accepts the self-keyword as a first argument which allows accessing the attributes or method of the class.

We can pass any number of arguments at the time of creating the class object, depending upon the __init__() definition. It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor.

Consider the following example to initialize the Employee class attributes.

Example

Output:

ID: 101 
Name: John
ID: 102 
Name: David

Counting the number of objects of a class

The constructor is called automatically when we create the object of the class. Consider the following example.

Example

Output:

The number of students: 3

Python Non-Parameterized Constructor

The non-parameterized constructor uses when we do not want to manipulate the value or the constructor that has only self as an argument. Consider the following example.

Example

Python Parameterized Constructor

The parameterized constructor has multiple parameters along with the self. Consider the following example.

Example

Output:

This is parametrized constructor
Hello John

Python Default Constructor

When we do not include the constructor in the class or forget to declare it, then that becomes the default constructor. It does not perform any task but initializes the objects. Consider the following example.

Example

Output:

101 Joseph

More than One Constructor in Single class

Let's have a look at another scenario, what happen if we declare the two same constructors in the class.

Example

Output:

The Second Constructor

In the above code, the object st called the second constructor whereas both have the same configuration. The first method is not accessible by the st object. Internally, the object of the class will always call the last constructor if the class has multiple constructors.

Note: The constructor overloading is not allowed in Python.

Python built-in class functions

The built-in functions defined in the class are described in the following table.

SNFunctionDescription
1getattr(obj,name,default)It is used to access the attribute of the object.
2setattr(obj, name,value)It is used to set a particular value to the specific attribute of an object.
3delattr(obj, name)It is used to delete a specific attribute.
4hasattr(obj, name)It returns true if the object contains some specific attribute.

Example

Output:

John
23
True
AttributeError: 'Student' object has no attribute 'age'

Built-in class attributes

Along with the other attributes, a Python class also contains some built-in class attributes which provide information about the class.

The built-in class attributes are given in the below table.

SNAttributeDescription
1__dict__It provides the dictionary containing the information about the class namespace.
2__doc__It contains a string which has the class documentation
3__name__It is used to access the class name.
4__module__It is used to access the module in which, this class is defined.
5__bases__It contains a tuple including all base classes.

Example

Output:

None
{'name': 'John', 'id': 101, 'age': 22}
__main__





Latest Courses