Python Tutorials: Learn Python Constructors in Detail

Python Tutorials: Learn in Detail How to Create a Constructor in Python
Written by Paayi Tech |19-Oct-2020 | 0 Comments | 482 Views

In python object-oriented programming, there is no such concept as the constructor. But there is an alternative of constructor which is called. _init__() method. But we can't 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 the 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)

obj.printValue()

 

#output

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 an instance. We can also make a 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()

obj.printValue()

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

 

Destructor in Python:

Unlike the constructor, which initializes the values, destructor destroys the instance of the class. It destroys the object and its data member. The following example will clear the concept of the destructor.

class MyClass(object):

    def __init__(self,name):

        self.name = name

        print("Object is created")

    def __del__(self):

        print("Object is destroyed")

 

    def greet(self):

        print(f"Hello {self.name}")

 

obj = MyClass('Pikachu')

obj.greet()

del obj

 

obj.greet()

 

In the above example, we make an instance of a class and initialize the name. Then 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:

#Output

 

Object is created

Hello Pikachu

Object is destroyed

 

Traceback (most recent call last):

  File "/home/lalatoofani/program.py", line 16, in

    obj.greet()

NameError: name 'obj' is not defined

 That trackback shows that after destroying the object, it will be 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 members. The data member that we initialized in the above examples is public data members. It can be accessed from inside as well as outside of the class.

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

In the above code "name" is the 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 an 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 members 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

Comments




Related Posts



© Copyright 2020, All Rights Reserved. paayi.com

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.