Home » Python

Constructors with examples in Python

Python constructors: Here, we are going to learn about the constructors, their types with examples in python.
Submitted by Yash Khandelwal, on March 31, 2019

A Constructor is the special method (function) used to initialize the instance variables during object creation.

Types of constructors in Python

  1. Default Constructor /Non parameterized Constructor
  2. Parameterized Constructor

How to create the Constructors?

In python, __init__ method stimulates the constructor of the class.

When we instantiate the class we call this method. It generally used when we initialize the class attributes. It is a must for a class to have the constructor, even if they rely on the default constructor.

Syntax to define a constructor:

While declaring the constructor one important thing is Constructor always has init and the word init is having two underscores ( __ ) before and after it, Like: __init__

We declare the constructor using def Keyword.

Syntax:

    def  __init__(self):
        #now all about the constructor

Here, __init__ is used for the constructor to a class. The self parameter refers to the instance of the object (like, this in C++).

Example:

class Addition:
    # Defininf a constructor
    def __init__(self):
        # with the help of self.xyz 
		# we are initializing instance variable
        self.num1=1000
        self.num2=2000
        self.num3=3000

    def result(self):
        self.num=self.num1+self.num2+self.num3
        print('Output:',self.num)


# Here we create the object for call 
# which calls the constructor
Sum = Addition()

# calling the instance method 
# using the object Sum
Sum.result()

Output

Output: 6000

1) Python Default constructor/Non Parameterized

In this above example, we are using a default constructor to assigning the default values to the variables num1, num2 and num3.

More about the default/non parameterized constructor...

We cannot create the object if we don’t have a constructor in our program. This is why when we do not declare the constructor in our program python having the inbuilt feature it does it for us.

For example: In the given below program we do not have the constructor but still, we can create the object. This is because of the default property of python it implicitly does that during compilation.

Default constructor looks like this:

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

Example:

class Addition:
    num1=1000
    num2=2000
    num3=3000

# here as we have no constructor 
# so it uses the default one.
    def result(self):
        self.num=self.num1+self.num2+self.num3
        print('Output:',self.num)


# Here we create the object for call 
# which calls the constructor
Sum = Addition()

# calling the instance method 
# using the object Sum
Sum.result()

Output

Output: 6000

2) Python Parameterized Constructor

When we declare a constructor in the way that accepts the arguments during the object creation these type of constructors are called the Parameterized Constructors.

This is used to initialize the instance members of the object.

Example:

class Student:
    # Defining a parameterized constructor having arguments
    def __init__(self,name,ids,college):
        print("This is a parmeterized costructor in python:")
        self.name=name
        self.ids=ids
        self.college=college
        
    def Display_Details(self):
        print("Student Details:")
        print("Student Name:",self.name)
        print("Student ids:",self.ids)
        print("Student college:",self.college)


# Here we create the object for call 
# which calls the constructor
student=Student("Yash",2023,"Kcc")

# calling the instance method 
# using the object student
student.Display_Details()

Output

This is a parmeterized costructor in python:
Student Details:
Student Name: Yash
Student ids: 2023
Student college: Kcc





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.