Getting Started

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

Operators

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

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

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

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

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

Scope of Variables

Python - Scope of Variables

Modules

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

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

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

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
obj.greetings()

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.

Example

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
        self.fobj.write(data)

    # the del method
    def __del__(self):

        print("Inside the __del__ method.")

        # close file
        self.fobj.close()

# 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.