delattr()函数在Python中做什么?
Pythondelattr()
的delattr(),如果对象允许它删除从对象的属性。
语法
的语法delattr()是-
delattr(object, name)
该delattr()方法有两个参数-
将delattr()不返回任何值(返回无)。它仅删除属性(如果对象允许)。
示例
class Coordinate:
x = 12
y = -7
z = 0
point1 = Coordinate()print('x = ',point1.x)
print('y = ',point1.y)
print('z = ',point1.z)
delattr(Coordinate, 'z')
print('--After deleting z attribute--')
print('x = ',point1.x)
print('y = ',point1.y)
# Raises Error
print('z = ',point1.z)输出结果
这给出了输出
Traceback (most recent call last):
('x = ', 12)
('y = ', -7)
File "C:/Users/~.py", line 28, in <module>
('z = ', 0)
print('z = ',point1.z)
--After deleting z attribute--
('x = ', 12)
AttributeError: Coordinate instance has no attribute 'z'
('y = ', -7)热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短