Python delattr() function

Published February 15, 2022

This function removes a specific attribute from an object. Calling the delattr() method of the python object and passing the attribute and object name as arguments will delete an attribute from the object.

Syntax

delattr(object, name)

where:

Object: the object from which the attribute will be removed.

Name: the name of the attribute to be removed.

Example

This example will display an error after   the delattr() function removes an attribute from one of the objects

class Coordinate:

  a = 6

  b = -3

  c = 2

point1 = Coordinate()

print('a = ',point1.a)

print('b = ',point1.b)

print('c = ',point1.c)

delattr(Coordinate, 'c')

print('--After deleting c attribute--')

print('a = ',point1.a)

print('b = ',point1.b)

# Display Error

print('c = ',point1.c)

 

Output

 

python delattr() function