VOOZH about

URL: https://www.geeksforgeeks.org/python/python-delattr-function/

⇱ Python delattr() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python delattr() Function

Last Updated : 23 Jul, 2025

In Python, the delattr() function is used to delete an attribute from an object. In this article, we will learn about the Python delattr() function.

Python delattr() Syntax

delattr (object, name)

Parameters:

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.

delattr() Function in Python

Python delattr() Function is used to delete an object attribute in Python. 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. We can use delattr() to remove an attribute dynamically at runtime in Python.

How does delattr() works?

In this example, we are using delattr() to delete an object attribute in Python.

Output

Value of x = 3
Value of y = -8
Value of z = 5
Value of x = 3
ERROR!
Traceback (most recent call last):
File "<string>", line 14, in <module>
AttributeError: 'Equation' object has no attribute 'z'

Python delattr() Examples

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


Output
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


Output
20000
type object 'course' has no attribute 'price'
Comment
Article Tags: