W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Python delattr 函數(shù)用于刪除屬性。
delattr(x, 'foobar') 相等于 del x.foobar。
delattr 語法:
delattr(object, name)
無。
以下實例展示了 delattr 的使用方法:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate()
print('x = ',point1.x)
print('y = ',point1.y)
print('z = ',point1.z)
delattr(Coordinate, 'z')
print('--刪除 z 屬性后--')
print('x = ',point1.x)
print('y = ',point1.y)
# 觸發(fā)錯誤
print('z = ',point1.z)
輸出結果:
('x = ', 10)
('y = ', -5)
('z = ', 0)
--刪除 z 屬性后--
('x = ', 10)
('y = ', -5)
Traceback (most recent call last):
File "test.py", line 22, in <module>
print('z = ',point1.z)
AttributeError: Coordinate instance has no attribute 'z'
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: