Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables


Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators


Python - If Else statement


Python - While LoopPython - For Loop


Python - NumbersPython - Number Conversion


Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method


Python - ListPython - List Methods


Python - Tuple


Python - SetPython - Set Methods


Python - DictionaryPython - Dictionary Methods


Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables


Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module


Python - Read input from keyboard


Python - File Handling

Exception Handling

Python - Exception Handling


Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Class Destructor __del__ method


python logo

In this tutorial we will learn about the class __del__ method in Python.

We learned about classes and objects in the Python - Classes and Objects tutorial. Feel free to check that out.

The __del__ method

The __del__ method is a special method of a class.

It is also called the destructor method and it is called (invoked) when the instance (object) of the class is about to get destroyed.

We use the __del__ method to clean up resources like closing a file.

In the following Python program we are creating the __del__ method inside the Awesome class.

# class
class Awesome:

    # some method
    def greetings(self):
        print("Hello World!")

    # the del method
    def __del__(self):
        print("Hello from the __del__ method.")

# object of the class
obj = Awesome()

# calling class method

The above code will print the following output.

Hello World!
Hello from the __del__ method.

Points to note!

We get the above output because when the code is about to end the class Awesome is no longer required and so, it is ready to be destroyed.

Before the class Awesome is destroyed the __del__ method is called automatically.

Garbage collection

In Python, any unused objects (like built-in types or instances of the classes) are automatically deleted (removed) from memory when they are no longer in use.

This process of freeing and reclaiming unused memory space is called Garbage Collection.

The concept of Garbage Collection is common in langauges like Java, C#, Python etc.


In the following Python program we are creating a new file and writing some text in it. Then we are closing the file in the __del__ method.

Learn more about File Handling in this tutorial.

# class
class Awesome:

    # the init method
    def __init__(self, filename):

        print("Inside the __init__ method.")

        # open file
        self.fobj = open(filename, "w")

    # method
    def writeContent(self, data):

        print("Inside the writeContent method.")

        # write the data

    # the del method
    def __del__(self):

        print("Inside the __del__ method.")

        # close file

# object
obj = Awesome("helloworld.txt")
obj.writeContent("Hello World")

On running the above code we will get the following output.

Inside the __init__ method.
Inside the writeContent method.
Inside the __del__ method.