python - delattr(object, name)

高洛峰
Release: 2016-10-17 14:52:44
Original
1307 people have browsed it

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']
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template