The delattr() function takes an object as a parameter and removes an attribute from the object.
delattr(object, name) #where name is the name of attribute
The function takes as a parameter an object, the attribute which is to be deleted can be passed as an optional parameter.
|Parameter||Description||Required / Optional|
|An object||The object from which the attribute to be deleted||Required|
|Attribute||The name of attribute to be removed||Optional|
The delattr() function doesn't return anything. It only removes the attribute from the object
class Student: name = "Ram" age = 22 course = "Bachelors" Student1 = Student() delattr(Student, 'age') #Removing age attribute print("Student's name is ",Student 1.name) print("Student's country is ",Student 1.country) print("Student's age is ",Student 1.age) # Raises error since age is not found
Student's name is John Student's country is Norway AttributeError: 'Student' object has no attribute 'age'
class Coordinate: x = 20 y = -15 z = 0 value1 = Coordinate() print('x = ',value1.x) print('y = ',value1.y) print('z = ',value1.z) delattr(Coordinate, 'z') print('--After deleting z attribute--') print('x = ',value1.x) print('y = ',value1.y) # Raises Error print('z = ',value1.z)
x = 20 y = -15 z = 0 --After deleting z attribute-- x = 20 y = -15 Traceback (most recent call last): File "python", line 19, in
AttributeError: 'Coordinate' object has no attribute 'z'