delattr(object, attribute) Removes the attribute of the input object
object (required ) , input object for which the attribute will be removed.
attribute ( required ), attribute to be removed.

Read more on Class Object and Attributes
Deleting an instance attribute
class Animal():
     #class attributes

     #instance attributes
    def __init__(self1,name1,age): name1

tiger=Animal("Ronald",5) # object created 
print(tiger.age) # 5
#print(tiger.age) # Error 
In above code we created an object tiger. By using delattr() we removed one attribute age. The last line ( commented ) will generate error as we already removed the attribute age before using it.

Deleting attribute of particular object.

We will create two objects and check that the delattr() will only delete for the input object only. Here in last line we can print the age of the other object lion.
print(tiger.age) # 5 
print(lion.age) # 6

Deleting class attribute

Once the class attribute is removed, it is not available to all objects.
Both the objects tiger and lion can't access the class attribute.

Using del operator

tiger=Animal("Ronald",5) # object created 
print(tiger.age) # 5
del tiger.age
print(tiger.age) # Error 
The last line will generate error as we have deleted the attribute before this line.
All Built in Functions in Python setattr() getattr() hasattr()

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer