Constructors in Python

Learn in Detail How to Create a Constructor in Python
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 436 Views

In python object-oriented programming there is no such concept of constructor. But there is an alternative of constructor which is called. _init__() method. But we cant say it constructor because the instance of the class is initialized before. _init__() method is called. This method is used to initialize the value of data immediately after instance is created.

class MyClass(object):
    def __init__(self, a,b,c):
        self.a = a
        self.b = b
        self.c = c
    def printValue(self):
        print(f"Value of a is: {self.a}")
        print(f"Value of b is: {self.b}")
        print(f"Value of c is: {self.c}")
obj = MyClass(1,2,3)



Value of a is: 1

Value of b is: 2

Value of c is: 3


This method makes the work easy we don’t have to set the values before running the method values are initialized at creating instance. We can also make non-parameterized constructor as follows”

class MyClass(object):
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
    def printValue(self):
        print(f"Value of a is: {self.a}")
        print(f"Value of b is: {self.b}")
        print(f"Value of c is: {self.c}")
obj = MyClass()

The output will be the same. Similarly, we can make default values in constructor too just we saw in the function module.


Destructor in Python:

Unlike constructor which initialize the values destructor destroy the instance of the class. It destroys the object and its data member. Following example will clear the concept of destructor.

class MyClass(object):
    def __init__(self,name): = name
        print("Object is created")
    def __del__(self):
        print("Object is destroyed")
    def greet(self):
        print(f"Hello {}")
obj = MyClass('Pikachu')
del obj


In the above example we make a instance of a class and initialize the name. Than print the name and destroy the object. After that when we again accessed the object python interpreter will generate an error no obj is defined:



Object is created

Hello Pikachu

Object is destroyed


Traceback (most recent call last):

  File "/home/lalatoofani/", line 16, in


NameError: name 'obj' is not defined

That trackback show that after destroying the object it will removed from the memory.


Public Data Member:

Public data members are the members that can be accessed outside the class too. In python there is no such keyword for public data member. The data member that we initialized in the above examples are public data member. It can be accessed from inside as well as outside of the class.

def __init__(self,name): = name

Here name is public data member


Protected Data members:

Unlike public data members protected data members can only be accessed by the class and the class that inherits it. It cannot be accessed from outside of the class that is not inherited. To make a protected data member we have to give underscore sign before the variable name. Following is the example of protected data members:

def __init__(self,name):
        self._name = name

Here the _name is protected data members.


Private Data Members:

Private data member cannot be accessed by the class itself. It cannot be accessed from outside the class nor from the inherited class. It is private from all and cannot be seen. To make a variable private we must write “__” (two times underscore) before the variable name. Following is the example of private data members:

def __init__(self,name):
        self.__name = name

Here the name is private data member.

Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.