Constructor & Destructor in Python
Constructors in Python: Initializing Objects with Purpose
In object-oriented programming, a constructor is a special method used for initializing instances of a class. In Python, the constructor method is named init, and it is automatically called when an object is created from a class. Constructors play a crucial role in setting up the initial state of objects, allowing for proper initialization and configuration. Let’s explore the key aspects of constructors in Python.
What is a Constructor in Python?
A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is automatically called. It is used to initialize the object’s state. Constructors can also be used to perform any required initialization of the object before it is used. Constructors are not mandatory in Python, but they are used to initialize the state of an object.
How to Create a Constructor in Python?
In Python, the constructor method is named __init__
__init__
. It is automatically called when an object is created from a class. The __init__
__init__
method is used to initialize the state of an object. The __init__
__init__
method is called the constructor method because it is called when an object is created.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python constructor.py
Name: John
Roll: 1
C:\Users\username>python constructor.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
What is the Use of a Constructor in Python?
Constructors are used to initialize the state of an object. They are used to set the initial values of the instance variables of an object. They are used to initialize the state in a better way. You can directly assign values to the instance variables of an object, but it is not a good practice. It is better to use a constructor to initialize the state of an object.
Initializing Instance Variables Directly
class Student:
name
age
student1 = Student()
student1.name = 'John'
student1.age = 20
print('Name:', student1.name)
print('Age:', student1.age)
class Student:
name
age
student1 = Student()
student1.name = 'John'
student1.age = 20
print('Name:', student1.name)
print('Age:', student1.age)
Output:
C:\Users\username>python instance_variables.py
Name: John
Age: 20
C:\Users\username>python instance_variables.py
Name: John
Age: 20
In the above example, we have created two instance variables named name
name
and age
age
. We have initialized the name
name
and age
age
variables to the name
name
and age
age
values of the student1
student1
object. We have printed the name
name
and age
age
variables using the student1
student1
object. The output shows that the name
name
and age
age
variables are unique to the object.
Initializing Instance Variables Using a Constructor
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student('John', 20)
print('Name:', student1.name)
print('Age:', student1.age)
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student('John', 20)
print('Name:', student1.name)
print('Age:', student1.age)
Output:
C:\Users\username>python constructor.py
Name: John
Age: 20
C:\Users\username>python constructor.py
Name: John
Age: 20
In the above example, we have created two instance variables named name
name
and age
age
. We have initialized the name
name
and age
age
variables to the name
name
and age
age
parameters of the __init__()
__init__()
method. We have printed the name
name
and age
age
variables using the student1
student1
object. The output shows that the name
name
and age
age
variables are unique to the object.
How to Use a Constructor in Python?
In Python, the constructor method is named __init__
__init__
. It is automatically called when an object is created from a class. The __init__
__init__
method is used to initialize the state of an object. The __init__
__init__
method is called the constructor method because it is called when an object is created. You need to pass the required parameters to the __init__
__init__
method when creating an object except the self
self
parameter.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python constructor.py
Name: John
Roll: 1
C:\Users\username>python constructor.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
Types of Constructors in Python
There are two types of constructors in Python:
- Default Constructor: A default constructor is a constructor that does not take any parameters. It is used to initialize the instance variables of an object to their default values. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the default constructor because it is called when an object is created without any parameters.
- Parameterized Constructor: A parameterized constructor is a constructor that takes one or more parameters. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the parameterized constructor because it is called when an object is created with parameters.
Default Constructor in Python
A default constructor is a constructor that does not take any parameters. It is used to initialize the instance variables of an object to their default values. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the default constructor because it is called when an object is created without any parameters.
class Student:
def __init__(self):
pass
student1 = Student()
class Student:
def __init__(self):
pass
student1 = Student()
In the above example, we have created a default constructor. It does not take any parameters. It is used to initialize the instance variables of an object to their default values. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the default constructor because it is called when an object is created without any parameters.
Another example of a default constructor:
class Student:
def __init__(self):
self.name = 'John'
self.roll = 1
student1 = Student()
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self):
self.name = 'John'
self.roll = 1
student1 = Student()
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python default_constructor.py
Name: John
Roll: 1
C:\Users\username>python default_constructor.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
values of the student1
student1
object. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
Parameterized Constructor in Python
A parameterized constructor is a constructor that takes one or more parameters. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the parameterized constructor because it is called when an object is created with parameters.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python parameterized_constructor.py
Name: John
Roll: 1
C:\Users\username>python parameterized_constructor.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
Another example of a parameterized constructor:
class Employee:
def __init__(self, name, salary, department):
self.name = name
self.salary = salary
self.department = department
employee1 = Employee('John', 10000, 'IT')
print('Name:', employee1.name)
print('Salary:', employee1.salary)
print('Department:', employee1.department)
class Employee:
def __init__(self, name, salary, department):
self.name = name
self.salary = salary
self.department = department
employee1 = Employee('John', 10000, 'IT')
print('Name:', employee1.name)
print('Salary:', employee1.salary)
print('Department:', employee1.department)
Output:
C:\Users\username>python parameterized_constructor.py
Name: John
Salary: 10000
Department: IT
C:\Users\username>python parameterized_constructor.py
Name: John
Salary: 10000
Department: IT
In the above example, we have created three instance variables named name
name
, salary
salary
, and department
department
. We have initialized the name
name
, salary
salary
, and department
department
variables to the name
name
, salary
salary
, and department
department
parameters of the __init__()
__init__()
method. We have printed the name
name
, salary
salary
, and department
department
variables using the employee1
employee1
object. The output shows that the name
name
, salary
salary
, and department
department
variables are unique to the object.
Constructor with Default Parameters in Python
A constructor with default parameters is a constructor that takes one or more parameters with default values. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the constructor with default parameters because it is called when an object is created with default parameters.
class Student:
def __init__(self, name='John', roll=1):
self.name = name
self.roll = roll
student1 = Student()
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name='John', roll=1):
self.name = name
self.roll = roll
student1 = Student()
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python constructor_with_default_parameters.py
Name: John
Roll: 1
C:\Users\username>python constructor_with_default_parameters.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
Another example of a constructor with default parameters:
class Employee:
def __init__(self, name='John', salary=10000, department='IT'):
self.name = name
self.salary = salary
self.department = department
employee1 = Employee()
print('Name:', employee1.name)
print('Salary:', employee1.salary)
print('Department:', employee1.department)
class Employee:
def __init__(self, name='John', salary=10000, department='IT'):
self.name = name
self.salary = salary
self.department = department
employee1 = Employee()
print('Name:', employee1.name)
print('Salary:', employee1.salary)
print('Department:', employee1.department)
Output:
C:\Users\username>python constructor_with_default_parameters.py
Name: John
Salary: 10000
Department: IT
C:\Users\username>python constructor_with_default_parameters.py
Name: John
Salary: 10000
Department: IT
In the above example, we have created three instance variables named name
name
, salary
salary
, and department
department
. We have initialized the name
name
, salary
salary
, and department
department
variables to the name
name
, salary
salary
, and department
department
parameters of the __init__()
__init__()
method. We have printed the name
name
, salary
salary
, and department
department
variables using the employee1
employee1
object. The output shows that the name
name
, salary
salary
, and department
department
variables are unique to the object.
Constructor with Default Parameters and Parameters in Python
A constructor with default parameters and parameters is a constructor that takes one or more parameters with default values and one or more parameters without default values. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the constructor with default parameters and parameters because it is called when an object is created with default parameters and parameters.
class Student:
def __init__(self, name, roll, age=20):
self.name = name
self.roll = roll
self.age = age
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
class Student:
def __init__(self, name, roll, age=20):
self.name = name
self.roll = roll
self.age = age
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
Output:
C:\Users\username>python constructor_with_default_parameters_and_parameters.py
Name: John
Roll: 1
Age: 20
C:\Users\username>python constructor_with_default_parameters_and_parameters.py
Name: John
Roll: 1
Age: 20
In the above example, we have created three instance variables named name
name
, roll
roll
, and age
age
. We have initialized the name
name
, roll
roll
, and age
age
variables to the name
name
, roll
roll
, and age
age
parameters of the __init__()
__init__()
method. We have printed the name
name
, roll
roll
, and age
age
variables using the student1
student1
object. The output shows that the name
name
, roll
roll
, and age
age
variables are unique to the object.
Constructor with keyword arguments in Python
A constructor with keyword arguments is a constructor that takes one or more parameters with default values and one or more parameters without default values. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the constructor with keyword arguments because it is called when an object is created with keyword arguments.
class Student:
def __init__(self, name, roll, age=20):
self.name = name
self.roll = roll
self.age = age
student1 = Student(name='John', roll=1)
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
class Student:
def __init__(self, name, roll, age=20):
self.name = name
self.roll = roll
self.age = age
student1 = Student(name='John', roll=1)
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
Output:
C:\Users\username>python constructor_keyword_arguments.py
Name: John
Roll: 1
Age: 20
C:\Users\username>python constructor_keyword_arguments.py
Name: John
Roll: 1
Age: 20
In the above example, we have created three instance variables named name
name
, roll
roll
, and age
age
. We have initialized the name
name
, roll
roll
, and age
age
variables to the name
name
, roll
roll
, and age
age
parameters of the __init__()
__init__()
method. We have printed the name
name
, roll
roll
, and age
age
variables using the student1
student1
object. The output shows that the name
name
, roll
roll
, and age
age
variables are unique to the object.
Constructor Overloading in Python
Constructor overloading is a technique in which a class can have more than one constructor. It is used to initialize the instance variables of an object to the values passed to the constructor. It is automatically called when an object is created from a class. It is used to initialize the state of an object. It is called the constructor overloading because it is called when an object is created with different parameters. You can create more than one constructor in a class. You can create a constructor with default parameters and parameters but constructor signature must be different.
class Student:
def __init__(self):
self.name = 'John'
self.roll = 1
print('Default Constructor')
def __init__(self, name, roll):
self.name = name
self.roll = roll
print('Constructor 1')
def __init__(self, name, roll, age):
self.name = name
self.roll = roll
self.age = age
print('Constructor 2')
def __init__(self, name, roll, age, department):
self.name = name
self.roll = roll
self.age = age
self.department = department
print('Constructor 3')
student1 = Student()
student2 = Student('John', 1)
student3 = Student('John', 1, 20)
student4 = Student('John', 1, 20, 'IT')
class Student:
def __init__(self):
self.name = 'John'
self.roll = 1
print('Default Constructor')
def __init__(self, name, roll):
self.name = name
self.roll = roll
print('Constructor 1')
def __init__(self, name, roll, age):
self.name = name
self.roll = roll
self.age = age
print('Constructor 2')
def __init__(self, name, roll, age, department):
self.name = name
self.roll = roll
self.age = age
self.department = department
print('Constructor 3')
student1 = Student()
student2 = Student('John', 1)
student3 = Student('John', 1, 20)
student4 = Student('John', 1, 20, 'IT')
Output:
C:\Users\username>python constructor_overloading.py
Default Constructor
Constructor 1
Constructor 2
Constructor 3
C:\Users\username>python constructor_overloading.py
Default Constructor
Constructor 1
Constructor 2
Constructor 3
In the above example, we have created four constructors. We have initialized the name
name
, roll
roll
, age
age
, and department
department
variables to the name
name
, roll
roll
, age
age
, and department
department
parameters of the __init__()
__init__()
method. We have printed the name
name
, roll
roll
, age
age
, and department
department
variables using the student1
student1
, student2
student2
, student3
student3
, and student4
student4
objects. The output shows that the name
name
, roll
roll
, age
age
, and department
department
variables are unique to the object. In this example, we have created four constructors with different parameters. You can create more than one constructor in a class. You can create a constructor with default parameters and parameters but constructor signature must be different.
Constructor Chaining in Python
Constructor chaining is a technique in which a constructor calls another constructor of the same class. It is used to initialize the instance variables of an object to the values passed to the constructor.
class Student:
def __init__(self, name, roll, age):
self.name = name
self.roll = roll
self.age = age
def __init__(self, name, roll, age, department):
self(name, roll, age)
self.department = department
student1 = Student('John', 1, 20, 'IT')
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
print('Department:', student1.department)
class Student:
def __init__(self, name, roll, age):
self.name = name
self.roll = roll
self.age = age
def __init__(self, name, roll, age, department):
self(name, roll, age)
self.department = department
student1 = Student('John', 1, 20, 'IT')
print('Name:', student1.name)
print('Roll:', student1.roll)
print('Age:', student1.age)
print('Department:', student1.department)
Output:
C:\Users\username>python constructor_chaining.py
Name: John
Roll: 1
Age: 20
Department: IT
C:\Users\username>python constructor_chaining.py
Name: John
Roll: 1
Age: 20
Department: IT
In the above example, we have created two instance variables named name
name
, roll
roll
, age
age
, and department
department
. We have initialized the name
name
, roll
roll
, age
age
, and department
department
variables to the name
name
, roll
roll
, age
age
, and department
department
parameters of the __init__()
__init__()
method. We have printed the name
name
, roll
roll
, age
age
, and department
department
variables using the student1
student1
object. The output shows that the name
name
, roll
roll
, age
age
, and department
department
variables are unique to the object. In this example, we have created two constructors. We have initialized the name
name
, roll
roll
, and age
age
variables to the name
name
, roll
roll
, and age
age
parameters of the __init__()
__init__()
method. We have initialized the department
department
variable to the department
department
parameter of the __init__()
__init__()
method. We have called the first constructor from the second constructor using the self()
self()
method. We have passed the required parameters to the first constructor. We have passed the required parameters to the second constructor. We have printed the name
name
, roll
roll
, age
age
, and department
department
variables using the student1
student1
object. The output shows that the name
name
, roll
roll
, age
age
, and department
department
variables are unique to the object.
Built-In Instance Methods in Python
There are many built-in instance methods in Python. The following table lists some of the most commonly used built-in instance methods in Python.
S.No. | Method | Description |
---|---|---|
1 | getattr() getattr() | Returns the value of the specified attribute of an object. |
2 | setattr() setattr() | Sets the value of the specified attribute of an object. |
3 | delattr() delattr() | Deletes the specified attribute of an object. |
4 | hasattr() hasattr() | Returns True True if the specified attribute of an object exists, otherwise returns False False . |
getattr()
getattr()
Method in Python
The getattr()
getattr()
method returns the value of the specified attribute of an object. It takes two parameters:
object
object
: The object whose attribute value you want to get.name
name
: The name of the attribute whose value you want to get.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', getattr(student1, 'name'))
print('Roll:', getattr(student1, 'roll'))
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', getattr(student1, 'name'))
print('Roll:', getattr(student1, 'roll'))
Output:
C:\Users\username>python getattr.py
Name: John
Roll: 1
C:\Users\username>python getattr.py
Name: John
Roll: 1
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the getattr()
getattr()
method. The output shows that the name
name
and roll
roll
variables are unique to the object.
setattr()
setattr()
Method in Python
The setattr()
setattr()
method sets the value of the specified attribute of an object. It takes three parameters:
object
object
: The object whose attribute value you want to set.name
name
: The name of the attribute whose value you want to set.value
value
: The value of the attribute you want to set.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
setattr(student1, 'name', 'John Doe')
setattr(student1, 'roll', 2)
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
setattr(student1, 'name', 'John Doe')
setattr(student1, 'roll', 2)
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python setattr.py
Name: John Doe
Roll: 2
C:\Users\username>python setattr.py
Name: John Doe
Roll: 2
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have set the name
name
and roll
roll
variables using the setattr()
setattr()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
delattr()
delattr()
Method in Python
The delattr()
delattr()
method deletes the specified attribute of an object. It takes two parameters:
object
object
: The object whose attribute you want to delete.name
name
: The name of the attribute you want to delete.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
delattr(student1, 'name')
delattr(student1, 'roll')
print('Name:', getattr(student1, 'name'))
print('Roll:', getattr(student1, 'roll'))
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
delattr(student1, 'name')
delattr(student1, 'roll')
print('Name:', getattr(student1, 'name'))
print('Roll:', getattr(student1, 'roll'))
Output:
C:\Users\username>python delattr.py
Traceback (most recent call last):
File "delattr.py", line 9, in <module>
print('Name:', getattr(student1, 'name'))
AttributeError: 'Student' object has no attribute 'name'
C:\Users\username>python delattr.py
Traceback (most recent call last):
File "delattr.py", line 9, in <module>
print('Name:', getattr(student1, 'name'))
AttributeError: 'Student' object has no attribute 'name'
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have deleted the name
name
and roll
roll
variables using the delattr()
delattr()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
hasattr()
hasattr()
Method in Python
The hasattr()
hasattr()
method returns True
True
if the specified attribute of an object exists, otherwise returns False
False
. It takes two parameters:
object
object
: The object whose attribute you want to check.name
name
: The name of the attribute you want to check.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', hasattr(student1, 'name'))
print('Roll:', hasattr(student1, 'roll'))
print('Age:', hasattr(student1, 'age'))
print('Department:', hasattr(student1, 'department'))
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
student1 = Student('John', 1)
print('Name:', hasattr(student1, 'name'))
print('Roll:', hasattr(student1, 'roll'))
print('Age:', hasattr(student1, 'age'))
print('Department:', hasattr(student1, 'department'))
Output:
C:\Users\username>python hasattr.py
Name: True
Roll: True
Age: False
Department: False
C:\Users\username>python hasattr.py
Name: True
Roll: True
Age: False
Department: False
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have checked the name
name
and roll
roll
variables using the hasattr()
hasattr()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. The output shows that the name
name
and roll
roll
variables are unique to the object.
Destructor in Python
In object-oriented programming, a destructor is a special method used for destroying instances of a class. In Python, the destructor method is named __del__
__del__
, and it is automatically called when an object is destroyed. Destructors play a crucial role in cleaning up the resources used by an object, allowing for proper cleanup and disposal. Let’s explore the key aspects of destructors in Python.
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
def __del__(self):
print('Destructor called')
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
class Student:
def __init__(self, name, roll):
self.name = name
self.roll = roll
def __del__(self):
print('Destructor called')
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
Output:
C:\Users\username>python destructor.py
Name: John
Roll: 1
Destructor called
C:\Users\username>python destructor.py
Name: John
Roll: 1
Destructor called
In the above example, we have created two instance variables named name
name
and roll
roll
. We have initialized the name
name
and roll
roll
variables to the name
name
and roll
roll
parameters of the __init__()
__init__()
method. We have printed the name
name
and roll
roll
variables using the student1
student1
object. We have created a destructor named __del__
__del__
. We have printed the Destructor called
Destructor called
message using the __del__
__del__
method. The output shows that the name
name
and roll
roll
variables are unique to the object. The output also shows that the Destructor called
Destructor called
message is printed when the object is destroyed.
Usage of Destructors in Python
- Destructors are used to destroy instances of a class.
- Destructors are used to clean up the resources used by an object.
- Destructors are used to perform any required cleanup before an object is destroyed.
Conclusion
In this tutorial, we have learned about constructors in Python. We have learned how to create a constructor in Python. We have learned what is the use of a constructor in Python. We have learned how to use a constructor in Python. We have learned how to create a constructor with parameters in Python. We have learned how to create a constructor without parameters in Python. We have learned how to create a default constructor in Python. We have learned how to create a parameterized constructor in Python. We have learned how to create a constructor with default parameters in Python. We have learned how to create a constructor with default parameters and parameters in Python. We have learned how to create a constructor with keyword arguments in Python. We have learned how to create constructor overloading in Python. We have learned how to create constructor chaining in Python. We have learned about built-in instance methods in Python. For more information on constructors in Python, see the official documentation. For more tutorials like this, check out Python Central Hub.
Was this page helpful?
Let us know how we did