__init__
.__init__
method is called automatically when an object is created, and it can be used to set initial values for object properties.The Syntax of a Constructor in Python is:
class ClassName:
def __init__(self, arg1, arg2, ...):
# Initialization code
# ....
self
parameter is used to refer to the current object being created.Example
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
s = Student("John Doe", 123)
print(s.name) # John Doe
print(s.roll) # 123
__del__
.__del__
method is called automatically when an object is no longer needed, and it can be used to clean up any resources used by the object.The Syntax of a Destructor in Python is:
class ClassName:
def __del__(self):
# Clean up code
self
parameter is used to refer to the current object being destroyed.class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
def __del__(self):
print("Deleting object")
s = Student("John Doe", 123)
del s # Deleting object