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)