delattr(object,name)[Python函数]

python

delattr(object, name)

中文说明:删除object对象名为name的属性。这个函数的命名真是简单易懂啊,和jquery里面差不多,但是功能不一样哦,注意一下。

参数object:对象。

参数name:属性名称字符串。

版本:各版本中都支持该函数,python3中仍可用。

英文说明: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.

代码实例:

>>> 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"]

以上是 delattr(object,name)[Python函数] 的全部内容, 来源链接: utcz.com/z/520747.html

回到顶部