Manual
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.
直译
该函数与setattr()关联,参数是一个对像和一个字符串,字符串必须是对象中某个属性名称,该函数删除以此命名的属性。例如:delattr(x, ‘foobar’)等同于del x.foobar。
实例
>>> class CSDN:
def foobar(self):
print('Hello CSDNer!')
>>> CSDN().foobar()
Hello CSDNer!
>>> delattr(CSDN, 'foobar')
>>> CSDN().foobar()
Traceback (most recent call last):
File "<pyshell#153>", line 1, in <module>
CSDN().foobar()
AttributeError: 'CSDN' object has no attribute 'foobar'
本文介绍了 Python 中的 delattr 函数,详细解释了如何使用该函数来删除对象的属性,并通过实例展示了其用法及注意事项。此外,还提供了与之相关的 setattr、hasattr 和 getattr 函数的参考。
146

被折叠的 条评论
为什么被折叠?



