delattr()

Allows the user to delete attributes from an object.

Syntax

delattr(object, name)

Parameters

  • object: The object to have an attribute removed.
  • name: The attribute to be removed from the object.

Example 1

Use delattr() to remove the position attribute from the Person1 class:

class Person1:
name = "Harry"
age = "35"
position = "Professor"
employee1 = Person1()
print("Employee Name: ", employee1.name)
print("Employee Age: ", employee1.age)
print("Employee Position: ", employee1.position)
delattr(Person1, "position")
print("---- UPDATE ----")
print("Employee Name: ", employee1.name)
print("Employee Age: ", employee1.age)
# Will cause an AttributeError because attribute is removed
print("Employee Position: ", employee1.position)

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy

Contributors