Python Reference Library →

Python delattr() Function – Delete Attribute from Object

Let’s look at delattr() function – delete attribute from object

  1. Notice

    delattr() removes a named attribute from an object when deletion is supported.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Box:
    pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))
Live state

Top-level scope

Box→Object 1
box→Object 2
Object 1 · typeclass BoxObject 2 · BoxBox({})
This step

delattr() removes a named attribute from an object when deletion is supported.

Picture delattr() function – delete attribute from object

delattr() removes a named attribute from an object when deletion is supported.

Image brief

Alt text
A diagram for Python delattr() Function – Delete Attribute from Object. Instructions: class Box: pass box = Box() box.value = 3 delattr(box, "value") print(hasattr(box, "value")). Displayed result: False. The final example bindings are Box --> object 1 (type); box --> object 2 (Box). object 1: class Box; object 2: Box({})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python delattr() Function – Delete Attribute from Object". Teaching focus: delattr() removes a named attribute from an object when deletion is supported. Depict exactly the relationships and labels in this reference diagram: 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({}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python delattr() Function – Delete Attribute from Object. Instructions: class Box: pass box = Box() box.value = 3 delattr(box, "value") print(hasattr(box, "value")). Displayed result: False. The final example bindings are Box --> object 1 (type); box --> object 2 (Box). object 1: class Box; object 2: Box({})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Box:
    pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))
Live state

Top-level scope

Box→Object 1
box→Object 2
Object 1 · typeclass BoxObject 2 · BoxBox({})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
False

What do you expect this program to print?

What does this example display?

class Box:
    pass
box = Box()
box.value = 3
delattr(box, "value")
print(hasattr(box, "value"))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

False

Follow the printed values in order. The output is: False

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

delattr() removes a named attribute from an object when deletion is supported.

delattr() removes a named attribute from an object when deletion is supported.

More examples and reference

Python delattr() Function

The 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.

Syntax

delattr(object, name)

Parameters:

  • object – The object whose attribute you want to delete
  • name – A string representing the name of the attribute

Returns:

  • Nothing. It deletes the attribute in place.

Example 1: Deleting an Attribute from an Object

class 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

Example 2: With Object Instances

class Car:
    def __init__(self):
        self.color = "red"
        self.brand = "Toyota"

c = Car()
delattr(c, 'color')

print(hasattr(c, 'color'))
False

Use Case: When is delattr() Useful?

  • When you want to remove an attribute dynamically at runtime
  • Used in metaprogramming and frameworks that modify object structure
  • Cleaning up unnecessary attributes before serialization

Equivalent Code Using del

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.

Common Errors

  • AttributeError: If the attribute doesn't exist
  • TypeError: If the attribute name is not a string

Interview Tip

delattr() is handy in scenarios involving reflection, metaclasses, and frameworks like Django where models are modified dynamically.

Summary

  • delattr(object, "attr") removes the named attribute
  • Works like del obj.attr but uses a string name
  • Raises AttributeError if the attribute doesn’t exist

Practice Problem

Create 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'))

Expected Output:

False

What to remember

Connect what you noticed in the example with the idea below.

Remember

delattr() removes a named attribute from an object when deletion is supported.

Watch out for this idea

delattr() deletes the entire object.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →