Python delattr() Function
Last Updated :
29 Nov, 2023
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.
Python3
class Equation:
x = 3
y = -8
z = 5
l1 = Equation()
print("Value of x = ", l1.x)
print("Value of y = ", l1.y)
print ("Value of z = ", l1.z)
delattr(Equation,'z')
print ("Value of x = ", l1.x)
print ("Value of y = ", l1.z)
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
Python3
class course:
name = "data structures using c++"
duration_months = 6
price = 20000
rating = 5
# creating an object of course
print(course.rating)
# deleting the rating attribute from object
delattr(course, 'rating')
# checking if the rating attribute is there or not
try:
print(course.rating)
except Exception as e:
print(e)
Output5
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
class course:
name = "data structures using c++"
duration_months = 6
price = 20000
rating = 5
# creating an object of course
print(course.price)
# deleting the price attribute from object
delattr(course, 'price')
# checking if the price attribute is there or not
try:
print(course.price)
except Exception as e:
print(e)
Output20000
type object 'course' has no attribute 'price'
Similar Reads
dir() function in Python The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik
3 min read
Python Built in Functions Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
Python str() function The str() function in Python is an in-built function that takes an object as input and returns its string representation. It can be used to convert various data types into strings, which can then be used for printing, concatenation, and formatting. Letâs take a simple example to converting an Intege
3 min read
delattr() and del() in Python In this article, we are going to see delattr() and del() functions in Python delattr() in Python The delattr() method is used to delete the named attribute from the object, with the prior permission of the object. Syntax: delattr(object, name): The function takes only two parameter: object:Â from wh
3 min read
Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age)
3 min read