Concept of Constructor and Destructors

Constructors

Constructors are a crucial part of OOP and are functions that get executed when we instantiate an object of a class. We can use this to our advantage to initialize desired values for an object.

We used constructors in our earlier code, which is the __init__() function.
Consider an example

class Test_Function:

    def __init__(self, name):
        self.name = name
        print("Hello", self.name)


person1 = Test_Function("Jones")
person2 = Test_Function("Emma")
person3 = Test_Function("Jin")

 

Here we have created a class that takes a name as an argument, and prints it. Notice how we used the print statement within the init, and so the print statement stored within it gets executed as soon as we create an object of a class.

We can also have a specific function get executed when an object of a class is created, by simply calling the function in the __init__() function.

class Test_Function:

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

    def function_1(self):
        print("Function 1 has been called!")


person1 = Test_Function("Jones")

This gives us:

To call a function defined inside a class all we need to do is call it by using the ‘self’ keyword and then the dot operator. Self, as mentioned earlier is a reference to the same object itself.

Let’s consider a simple example to see how classes provide reusability.

class Payment:

    def __init__(self, name):
        self.name = name
        self.payment_route = "Net-banking"


User1 = Payment("Jon")  # Object 1
User2 = Payment("Payment")  # Object 2

print(User1.name, "and", User2.name, "have chosen", User1.payment_route, "as their method of payment.")

This gives us the output:

The above code allows us to reuse the value “payment_route” as these values are defined for an object when it is instantiated. Thus, we can create a million different objects without having to define a particular value a million times. Here since all objects have the value “payment_ route” as ‘Net-banking’ we can display it using the object of one value alone.

Destructors

As the name suggests, the destructor of a class gets called when an object is destroyed. An object gets destroyed when it is no longer referenced, or at the end of a program. Usually, when a program has finished execution, there are things we need to take care of like ending the use of other resources such as databases, caches, and so on; this is usually done within a destructor.

The destructor is accessed through the __del__() function.

Consider the following example, where we added a destructor to the above code:

class Payment:

    def __init__(self, name):
        self.name = name
        self.payment_route = "Net-banking"

    def __del__(self):
        print("Destructor called for the object of:", self.name)


User1 = Payment("Jon")  # Object 1
User2 = Payment("Payment")  # Object 2

print(User1.name, "and", User2.name, "have chosen", User1.payment_route, "as their method of payment.\n")

When we execute the above code, we should get:

As you can see the destructor only gets called when we it went out of reference, or in other words, it was called for each object when we no longer had a use for the objects.

What have we learned?

  • What are constructors?
  • Which function represents a constructor?
  • When does a constructor get called?
  • How can we implement re-usability through classes?
  • What are destructors?
  • Which function represents a destructor?
  • When does a destructor get called?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments