Remember
delattr() removes a named attribute from an object when deletion is supported.
delattr() removes a named attribute from an object when deletion is supported.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Box:
pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))Top-level scope
class BoxObject 2 · BoxBox({})delattr() removes a named attribute from an object when deletion is supported.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Box:
pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))
|
v
DISPLAYED RESULT
False
FINAL BINDINGS
Box --> object 1 (type)
box --> object 2 (Box)
object 1: class Box
object 2: Box({})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Box:
pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))Top-level scope
class BoxObject 2 · BoxBox({})The example has finished. Compare the final output with the values and references above.
False
What do you expect this program to print?
class Box:
pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))delattr() FunctionThe delattr() function in Python is used to delete an attribute from an object. It’s useful when you want to dynamically remove properties from user-defined objects.
delattr(object, name)
object – The object whose attribute you want to deletename – A string representing the name of the attributeclass Person:
name = "Alice"
age = 25
# Delete the 'age' attribute
delattr(Person, 'age')
# Now let's try to access it
p = Person()
print(hasattr(p, 'age'))
False
class Car:
def __init__(self):
self.color = "red"
self.brand = "Toyota"
c = Car()
delattr(c, 'color')
print(hasattr(c, 'color'))
False
You can also delete attributes like this:
del object.attribute
But delattr() lets you use the attribute name as a string, which is helpful for dynamic situations.
delattr() is handy in scenarios involving reflection, metaclasses, and frameworks like Django where models are modified dynamically.
delattr(object, "attr") removes the named attributedel obj.attr but uses a string nameAttributeError if the attribute doesn’t existCreate a class Student with attributes name and grade. Delete the grade attribute using delattr() and print whether the attribute still exists.
class Student:
def __init__(self):
self.name = "Bob"
self.grade = "A"
s = Student()
delattr(s, 'grade')
print(hasattr(s, 'grade'))
False
Connect what you noticed in the example with the idea below.
delattr() removes a named attribute from an object when deletion is supported.
delattr() deletes the entire object.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.