Change language

Destructors in Python

|

Destructors are called when the object is destroyed. In Python, destructors are not needed as much as in C++, because Python has a garbage collector that automatically handles memory management. 
The __ destructor method in Python. It is called when all references to the object have been removed, i.e. when an object is garbage collected. 
The syntax for declaring a destructor is:

 def __del __ (self): # body of destructor 

Example 1: Here a simple example of a destructor. Using the del keyword, we removed all references to obj, so the destructor was called automatically.

# Python program to illustrate the destructor

class Employee:

 

# Initialization

def __ init __ ( self ):

  print ( ’Employee created.’ )

  

# Deleting (destructor call)

def __ del__ ( self ):

print ( ’Destructor called, Employee deleted.’ )

 

obj = Employee ()

del obj

Output:

 Employee created. Destructor called, Employee deleted. 

Note: the destructor was called after the program terminated or when all references to the object have been removed, i.e. when the reference count goes to zero, not when the object is out of scope.

Example 2: This example explains the above note. Note here that the destructor is called after "End of program ..." is printed.

# Python program to illustrate the destructor

 

class Employee:

 

# Initialization

def __ init __ ( self ):

print ( ’Employee created’ )

 

< code class = "comments"> # Call destructor

def __ del __ ( self ):

  print ( " Destructor called " )

 

def Create_obj ():

print ( ’Making Object ...’ )

obj = Employee ()

print ( ’f unction end ... ’ )

  return obj

 

print ( ’Calling Create_obj () function ...’ )

obj = Create_obj ( )

print ( ’Program End. ..’ )

Output:

 Calling Create_obj () function ... Making Object ... Employee created function end ... Program End ... Destructor called 

Example 3: Now consider the following example:

# Python destructor illustration program

  

class A:

def __ init __ ( self , bb):

self . b = bb

 

class B:

def __ init __ ( self ):

self . a = A ( self )

def __ del __ ( self ):

print ( " die " )

  

def fun ():

b = B ()

 
fun ()

Exit :

 die 

In this example, when fun () is called, it creates an instance of class B, which transfers itself to the class A which then sets a reference to class B and results in circular reference .

Normally the garbage collector in Python, which is used to detect this type of circular reference, will remove it, but in this example, using a custom destructor marks the item as "not collectable" ... 
He just doesn’t know in what order to destroy objects, so he leaves them. Therefore, if your instances participate in circular references, they will live in memory as long as the application is running.

Destructors in Python destructor: Questions

Destructors in Python Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method