guestts

Guestts

A constructor is a one-of-a-kind function that is executed whenever a new object of that class is formed. Constructors are invoked when an object is created in a class. Constructors in Python are primarily used to initialise or assign values to the data members of a class. This is the primary function of constructors in Python. It is not possible for it to return any value other than zero.

The creation of an instance of an object is the principal function that a constructor is responsible for. When an object of a certain class is formed, the constructors for that class are called, and the responsibility of the constructors is to initialise (or assign values to) the data members of the class. When an object is created in Python, the __init__() function is called. This method is also commonly referred to as the constructor.

 

Let’s see how to use the constructor.

Syntax of Python Constructor

def __init__(self):

# initializations

 

Init is one of the reserved functions in Python. In object-oriented programming, it is known as a constructor.

Rules of Python Constructor

It starts with the def keyword, like all other functions in Python.

Following it is the word init, which has double underscores as its prefix and suffix along with a pair of brackets, i.e., __init__().

It takes an argument called self, and assigning values to the variables is a reference to the current instance of the class. It is created and passed automatically or implicitly to __init__() when the constructor is called.

Types of Constructors in Python

Parameterized Constructor

Non-Parameterized Constructor

            Default Constructor

 

1. Parameterized Constructor in Python

 

When the constructor accepts arguments along with itself, it is known as a parameterized constructor.

These arguments can be used inside the class to assign values to the data members. Let’s see an example:

Code:

class Family:  

    # Constructor – parameterized  

    members=5

    def __init__(self, count):  

        print(“This is parametrized constructor”)  

        self.members = count

    def show(self):  

        print(“No. of members is”, self.members)  

        

object = Family(10)  

object.show()   

 

Output:

 

This is parameterized constructor

No. of members is 10

 

Explanation

 

An object of the class Family is created. It has a variable known as members.

When the object is created, a parameter (here it is 10) is passed as an argument.

This parameter (10, as in the above example) is taken up by the constructor as the object is created.

The number 10 is assigned to the variable count, which is further assigned to the self-members.

The self-members can be used within the class to print the data.

 

2. Non-Parameterized Constructor in Python

 

When the constructor doesn’t accept any arguments from the object and has only one argument, self, in the constructor, it is known as a non-parameterized constructor.

This can be used to re-assign a value inside the constructor. Let’s see an example:

 

Code:

class Fruits:

    favourite = “Apple”

 

    # non-parameterized constructor

    def __init__(self):

        self.favourite = “Orange”

 

    # a method

    def show(self):

        print(self.favourite)

# Creating an object of the class

obj = Fruits()

 

# calling the instance method using the object obj

obj.show()

 

Output:

 

Orange

 

Explanation:

In the above example, we initialise our favourite fruit as “orange” inside the class. But we want to change it as soon as the object is created.

Re-assigning the value to the constructor will accomplish this. This type of constructor doesn’t take any other argument other than self. It can be used to print and check whether our value has changed.

3. Default Constructor in Python

When you do not write the constructor in the class you create, Python itself creates a constructor during the compilation of the programme.

It generates an empty constructor that has no code in it. Let’s see an example:

 

Example

 

class Assignments:

    check= “not done”

    # a method

    def is_done(self):

        print(self.check)

 

# Creating an object of the class

obj = Assignments()

 

# calling the instance method using the object obj

.is_done()

 

Output

 

not done

 

Explanation

In the above example, we check whether our assignment is done. We create an object of a class where we do not write the constructor.

Python generates a constructor when we do not provide any to initialise the instance variables. It does nothing.

Conclusion

When a new instance of a class object is created, a method known as the constructor is invoked.

The generation of the constructors in python is dependent on the programmer; if the programmer does not create them, Python will build the default constructors automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *