Python delattr() Function

Python delattr() Function is used to delete attributes from a class. It takes two arguments, the first one is the class object from which we want to delete, second is the name of the attribute which we want to delete.
Syntax
delattr (object, name)
Parameters
| Parameter | Description | 
|---|---|
| object | An object from which we want to delete the attribute | 
| name | The name of the attribute we want to delete | 
The delattr() method returns a complex number.
Example 1:
A class course is created with attributes name, duration, price, rating. An instance of the class is created and now we delete the rating attribute using delattr() method. Finally, we check if the rating attribute is present or not. A try block is used to handle the keyError
Python3
| classcourse:    name ="data structures using c++"    duration_months =6    price =20000    rating =5# creating an object of courseprint(course.rating)# deleting the rating attribute from objectdelattr(course, 'rating')# checking if the rating attribute is there or nottry:    print(course.rating)exceptException as e:    print(e) | 
5 type object 'course' has no attribute 'rating'
Example 2:
A class course is created with attributes name, duration, price, rating. An instance of class is created and now we delete the price attribute using delattr() method. Finally, we check if the price attribute is present or not. A try block is used to handle the keyError
Python3
| classcourse:    name ="data structures using c++"    duration_months =6    price =20000    rating =5# creating an object of courseprint(course.price)# deleting the price attribute from objectdelattr(course, 'price')# checking if the price attribute is there or nottry:    print(course.price)exceptException as e:    print(e) | 
20000 type object 'course' has no attribute 'price'
 
				 
					


