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:
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 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏