Methods and Functions

Python delattr()

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. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.