The Python delattr() method is an in-built method in Python that is used to delete the named attribute from the object. Attributes in Python can be of two types, instance, and class attributes. Instance attributes are the attributes that are owned by specific instances of the class. Any two instances in the class usually have different attributes.Â
On the other hand, class attributes are those which are owned by the class itself. These attributes are shared by all the instances present in the class. While using the delattr () method, permission from the object will be obtained first before deleting it.Â
delattr () ParametersÂ
Syntax of delattr () method:
delattr (object, name)
The function takes two parameters. The first is the object from which the name attribute is to be removed. The second parameter is ‘name’, which provides the name of the attribute that is to be removed.Â
Return Value from delattr ()Â
The Python delattr () function does not return any value. This is because it deletes the attribute that was passed in it. The removal of the object will happen only in the case if the object allows it.Â
Example 1: How does delattr () work?
Let us suppose that a Python program has a class with a fixed number of attributes. We will use the delattr () method to remove any one of the attributes from the class.
class LinearEq:
              x = 3
              y = -8
              z = 5
line1 = LinearEq ()
print (“Value of x = “, line1.x)
print (“Value of y = “, line1.y)
print (“Value of z = “, line1.z)
delattr (LinearEq, ‘z’)
print (“Value of x = “, line1.x)
print (“Value of y = “, line1.y)
In this program, if you try to print the value of z once it has been deleted using the delattr () method, then you will find that the program raises an error. This is because the value no longer exists in the class and thus, cannot be printed.Â
Note that in the delattr () method used in the program, the first parameter is the name of the class from which the attribute to be deleted is present. The next parameter is the name of the attribute that will be deleted.Â
Example 2: Deleting Attribute Using del OperatorÂ
Another method present in Python that works the same as Python delattr () is the del operator. It is also used to delete any object in the program. In Python, everything is an object, thus, the del keyword can be used to delete lists, variables, attributes, parts of the list, etc.Â
The program given below shows how the del operator can be used in place of the delattr () function.Â
class LinearEq:
              x = 3
              y = -8
              z = 5
line1 = LinearEq ()
print (“Value of x = “, line1.x)
print (“Value of y = “, line1.y)
print (“Value of z = “, line1.z)
del LinearEq.z
print (“Value of x = “, line1.x)
print (“Value of y = “, line1.y)
The program above will provide the same result as the previous one that uses the delattr () method. Here also, if you try to print the value of z, you will find that the program raises the attribute error that says that such an attribute does not exist.Â
Del operator is more preferred to be used because of its property of dynamic deletion. Del operator can be used to delete all sorts of objects. Also, the del operator is faster than delattr (), depending upon the computer on which the program is executed. Â
Frequently Asked Questions
What is delattr in Python?
delattr () is an in-built method in Python that is used to delete any attribute present in a class.Â
What are attributes in Python?
A class attribute in Python is the attribute that belongs to the class instead of a particular object.Â
Does Python have ATTR?
Yes, Python has ATTR which is a Python package that helps you to write accurate and concise software that does not slow down the code.Â
How do I get a list of built-in functions in Python?
To get the list of all built-in functions in Python, one can import the built-in module and use the function dir () which has the parameter as built-ins as well. This will provide you with all the functions present in the Python directory.Â
Leave a Reply