Python

Constructors – Default And Parameterized In Python

When creating an object, a constructor is a specific sort of method that is used to initialize the instance variables. This book will explain constructors, their types, and how to utilize them using examples in Python programming.

1. What is a Constructor in Python?

When creating an object of a class, the constructor is used to initialize the instance members.

For instance: In this case, the constructor initializes the instance variable num. When we create an object of the class, the constructor is called (obj in the following example).

class DemoClass:
    # constructor
    def __init__(self):
        # initializing instance variable
        self.num=100

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class. This invokes constructor
obj = DemoClass()

# calling the instance method using the object obj
obj.read_number()

Output:

100

1.1 Syntax of constructor declaration

A constructor’s name, init, is always present and is prefixed and suffixed with a double underscore (__), as seen in the example above. Like with methods, a constructor is declared with the def keyword.

def __init__(self):
    # body of the constructor

2. Types of constructors in Python

Python has two different kinds of constructors.
1. The default constructor, which we have already seen in the example above. There are no parameters accepted by this constructor.
2. parameterized constructor – A parameterized constructor is a constructor that accepts parameters.

2.1 Python – default constructor example

It should be noted that without a constructor, an object cannot be generated in our application. Because of this, Python creates a constructor for us when we forget to do so in our application. Let’s examine the following example.

Example: When we do not declare a constructor

In this example, despite the lack of a constructor, we are nevertheless able to generate an object for the class. This is due to the fact that Python inadvertently injects an empty default constructor during program compilation, and it looks like this:

def __init__(self):
    # no body, does nothing.

Source Code:

class DemoClass:
    num = 101

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
obj = DemoClass()

# calling the instance method using the object obj
obj.read_number()

Output:

101

Example: When we declare a constructor

Python does not produce a constructor in our program in this instance.

class DemoClass:
    num = 101

    # non-parameterized constructor
    def __init__(self):
        self.num = 999

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
obj = DemoClass()

# calling the instance method using the object obj
obj.read_number()

Output:

999

2.2 Python – Parameterized constructor example

The term “parameterized constructor” refers to a constructor that has been declared in such a manner that it takes parameters when creating an object. As you can see, we may send values (data) to these types of constructors when creating objects, and they utilize those values to populate the instance members of the object.

class DemoClass:
    num = 101

    # parameterized constructor
    def __init__(self, data):
        self.num = data

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
# this will invoke parameterized constructor
obj = DemoClass(55)

# calling the instance method using the object obj
obj.read_number()

# creating another object of the class
obj2 = DemoClass(66)

# calling the instance method using the object obj
obj2.read_number()

Output:

55
66
class DemoClass:
    num = 101

    # parameterized constructor
    def __init__(self, data):
        self.num = data

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
# this will invoke parameterized constructor
obj = DemoClass(55)

# calling the instance method using the object obj
obj.read_number()

# creating another object of the class
obj2 = DemoClass(66)

# calling the instance method using the object obj
obj2.read_number()

Output:

55
66
class DemoClass:
    num = 101

    # parameterized constructor
    def __init__(self, data):
        self.num = data

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
# this will invoke parameterized constructor
obj = DemoClass(55)

# calling the instance method using the object obj
obj.read_number()

# creating another object of the class
obj2 = DemoClass(66)

# calling the instance method using the object obj
obj2.read_number()

Output:

55
66

Related Articles

Leave a Reply

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

Back to top button