Demonstration of Constructors and Destructors in Python

Demonstration of Constructors and Destructors in Python 

Constructors

What are constructors: 

Constructors in Python are special methods used for initializing objects of a class. They are typically named __init__() and are automatically called when a new instance of the class is created. The primary purpose of constructors is to initialize instance variables or perform any setup that may be required before using the object.


Key points about constructors in Python:


Constructors are defined using the def __init__(self, ...). The self parameter refers to the instance of the class.

Constructors can accept arguments to initialize instance variables with initial values.

Constructors are optional. If not defined explicitly, Python provides a default constructor with no arguments.

Constructors can include any initialization code needed for the class.

Constructors can be overloaded in Python, meaning multiple constructors with different signatures can exist within a class.

class InterestCalculator:

    def __init__(self, principal, rate, time):

        self.principal = principal

        self.rate = rate

        self.time = time


    def calculate_simple_interest(self):

        interest = (self.principal * self.rate * self.time) / 100

        return interest

    

    def display_result(self):

        interest = self.calculate_simple_interest()

        print("Principal Amount:", self.principal)

        print("Rate of Interest:", self.rate)

        print("Time for loan:", self.time)

        print("Simple Interest:", interest)


# Creating an instance of the class with constructor parameters

calculator = InterestCalculator(1000, 5, 2)


# Calling the method to display the result

calculator.display_result()


By Taking User Input:

class InterestCalculator:

    def __init__(self, principal, rate, time):

        self.principal = principal

        self.rate = rate

        self.time = time


    def calculate_simple_interest(self):

        interest = (self.principal * self.rate * self.time) / 100

        return interest

    

    def display_result(self):

        interest = self.calculate_simple_interest()

        print("Principal Amount:", self.principal)

        print("Rate of Interest:", self.rate)

        print("Time for loan:", self.time)

        print("Simple Interest:", interest)


# Take user input for principal, rate, and time

principal = float(input("Enter principal amount: "))

rate = float(input("Enter rate of interest: "))

time = float(input("Enter time for loan (in years): "))


# Create an instance of the class with user input

calculator = InterestCalculator(principal, rate, time)

print()

# Call the method to display the result

calculator.display_result()



Types of Constructors:

Default Constructors

class InterestCalculator:
    def __init__(self):  # Default constructor
        # Initialize instance attributes with default values
        self.principal = 0
        self.rate = 0
        self.time = 0
    def calculate_simple_interest(self):
        interest = (self.principal * self.rate * self.time) / 100
        return interest
    
    def display_result(self):
        interest = self.calculate_simple_interest()
        print("Principal Amount:", self.principal)
        print("Rate of Interest:", self.rate)
        print("Time for loan:", self.time)
        print("Simple Interest:", interest)
# Creating an instance using the default constructor
calculator_default = InterestCalculator()
# Displaying the result (attributes initialized with default values)
calculator_default.display_result()


Parameterized Constructors:

# Parameterized constructor

class InterestCalculatorParam:

    def __init__(self, principal, rate, time):  # Parameterized constructor

        # Initialize instance attributes with provided values

        self.principal = principal

        self.rate = rate

        self.time = time


    def calculate_simple_interest(self):

        interest = (self.principal * self.rate * self.time) / 100

        return interest

    

    def display_result(self):

        interest = self.calculate_simple_interest()

        print("Principal Amount:", self.principal)

        print("Rate of Interest:", self.rate)

        print("Time for loan:", self.time)

        print("Simple Interest:", interest)


# Creating an instance using the parameterized constructor

calculator_param = InterestCalculatorParam(1000, 5, 2)


# Displaying the result (attributes initialized with provided values)

calculator_param.display_result()


Destructors in Python:


Destructors in Python are special methods used for cleaning up resources or performing finalization actions when an object is no longer needed and is about to be destroyed. They are typically named __del__() and are automatically called when an object is garbage-collected, meaning when there are no more references to the object.
Key points about destructors in Python:
Destructors are defined using the def __del__(self) syntax.
Destructors are optional. If not defined explicitly, Python provides a default destructor that does nothing.
Destructors are automatically called when an object is garbage-collected, which happens when there are no more references to the object.
Destructors are commonly used to release system resources, close files, or perform any cleanup actions before the object is destroyed.
It's important to note that in Python, garbage collection is handled automatically by the Python interpreter, so the exact timing of when a destructor is called may not be deterministic.

class InterestCalculator:
    def __init__(self, principal, rate, time):
        self.principal = principal
        self.rate = rate
        self.time = time
    def __del__(self):
        print("InterestCalculator instance is being destroyed.")
    def calculate_simple_interest(self):
        interest = (self.principal * self.rate * self.time) / 100
        return interest
    
    def display_result(self):
        interest = self.calculate_simple_interest()
        print("Principal Amount:", self.principal)
        print("Rate of Interest:", self.rate)
        print("Time for loan:", self.time)
        print("Simple Interest:", interest)
# Creating an instance of the class with constructor parameters
calculator = InterestCalculator(1000, 5, 2)
# Calling the method to display the result
calculator.display_result()
# Deleting the instance
del calculator




टिप्पणी पोस्ट करा

0 टिप्पण्या