Skip to content

Python del self statement

  • by

Python del self does almost nothing it only deletes the local variable that is named self. Calling __del__ manually also achieves nothing except for running that method; it doesn’t delete anything.

What does ‘del self.self ‘ in an __init__ function mean?

The del self.self simply removes the unwanted self attribute on the object named by the name self.

__del__ is a reserved function in Python that is called when the last reference to an object is being deleted or goes out of scope.

Example Deleted Object Manually

Manually deleting the object using the del keyword.

class Car:
    def __init__(self):
        print("New Car") = "BMW X3"
        self.max_speed = "220 mph"

    def display(self):
        print(f"Name: {}")
        print(f"Max Speed: {self.max_speed}")

    def __del__(self):
        print("Destroying the Car")

# creating object
myCar = Car()

# calling the display function

# manually deleting the object using the del keyword
del myCar


Python del self statement

Do comment if you have any doubts or suggestions on this Python basic tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *