Python delattr() 函數(shù)

2019-03-16 11:22 更新

Python delattr() 函數(shù)

Python 內(nèi)置函數(shù) Python 內(nèi)置函數(shù)

描述

Python delattr 函數(shù)用于刪除屬性。

delattr(x, 'foobar') 相等于 del x.foobar。

語(yǔ)法

delattr 語(yǔ)法:

delattr(object, name)

參數(shù)

  • object:對(duì)象。
  • name:必須是對(duì)象的屬性。

返回值

無(wú)。

實(shí)例

以下實(shí)例展示了 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ā)錯(cuò)誤
print('z = ',point1.z)

輸出結(jié)果:

('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 內(nèi)置函數(shù) Python 內(nèi)置函數(shù)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)