Python 3 reference · Built-in Functions

Python delattr() Function

Delete a named attribute from an object. delattr(obj, 'name') does the same as del obj.name when the name is in a variable.

Python 3 Since 3.0

Syntax

delattr(object, name)

Parameters

ParameterDescription
object The object to change.
name The attribute name, as a string.

Return value

None.

Examples

Remove an attributeTry it yourself
class Config:
    pass

c = Config()
c.debug = True
delattr(c, "debug")
print(hasattr(c, "debug"))

Output

False
Remove fields by nameTry it yourself
class User:
    pass

u = User()
u.name, u.password, u.email = "ada", "secret", "[email protected]"
for field in ["password"]:
    delattr(u, field)
print(vars(u))

Output

{'name': 'ada', 'email': '[email protected]'}

Good to know

Deleting a missing attribute raises AttributeError. Use if hasattr(obj, name): first if it might not exist.

Version history

VersionChange
Python 3.0Added