Python delattr() 関数
説明
delattrこの関数は属性を削除するために使用します。
delattr(x, 'foobar')次と同等del x.foobar。
構文
delattr 構文:
delattr(object, name)
パラメータ
- 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)
# エラーを発生させる
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'
その他の拡張
Python 組み込み関数