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 入住酒店文案祝福语简短