Home >Backend Development >Python Tutorial >python - delattr(object, name)
delattr(object, name)
Chinese description: Delete the attribute named name of the object object. The naming of this function is really simple and easy to understand. It is similar to jquery, but the function is different. Please pay attention.
Parameter object: object.
Parameter name: attribute name string.
Version: This function is supported in all versions and is still available in python3.
English description: This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object's attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.
Code example:
>>> class Person: ... def __init__(self, name, age): ... self.name = name ... self.age = age ... >>> tom = Person("Tom", 35) >>> dir(tom) ['__doc__', '__init__', '__module__', 'age', 'name'] >>> delattr(tom, "age") >>> dir(tom) ['__doc__', '__init__', '__module__', 'name']