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 官宣新年祝福语简短