Python

Constructor Example in Python

Hello in this tutorial, we will explain how a Constructor works in python programming, using examples.

1. Introduction

Constructors are used for instantiating an object and assign values to the data members of a class. In python, the method __inti()__ simulates the class constructor and is called every time the class is instantiated. It accepts the self keyword as an input argument that allows accessing the attributes or method of a class. There are two types of constructors:

  • Default constructor – Constructor that does not accept any arguments and has only one argument which is a reference to the instance being constructed known as self
  • Parameterized constructor – Constructor with parameters is known as parameterized constructor. In this, the first argument is a reference to the instance being constructed known as self and the rest are the arguments

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Constructor example in Python

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.

2.1 Default Constructor

Let us understand this with the help of a code snippet.

Default constructor example

01
02
03
04
05
06
07
08
09
10
class JCG:
    # default constructor
    def __init__(self):
        self.message = 'Hello world'
    # a method for printing data members
    def print_message(self):
        print(self.message)
# creating object of the class
obj = JCG()
obj.print_message()

If everything goes well the following output will be shown in the IDE console.

Console Output

1
Hello world

2.2 Parameterized the constructor in Python

Let us understand this with the help of a code snippet.

Parameterized constructor example

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
class Multiply:
    ele1 = 1
    ele2 = 1
    total = 0
    # parameterized constructor
    def __init__(self, x, y):
        self.ele1 = x
        self.ele2 = y
    def calculate(self):
        self.total = self.ele1 * self.ele2
    # a method for printing data members
    def display(self):
        print('ele 1 = {} '.format(self.ele1))
        print('ele 2 = {} '.format(self.ele2))
        print('Total  = {} '.format(self.total))
# Creating the class object which will invoke the parameterized
# constructor
obj = Multiply(5, 5)
obj.calculate()
obj.display()

If everything goes well the following output will be shown in the IDE console.

Console Output

1
2
3
ele 1 = 5
ele 2 = 5
Total  = 25

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • An introduction to constructors in the python programming language
  • Sample program to understand the constructors in the python programming language

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

In this article, we explained how a Constructor works in python programming, using examples.

Download
You can download the full source code of this example here: Constructor Example in Python

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button