Python內建函式之: hasattr(object, name)、setattr(object, name, value)、getattr(object, name[, default])的用法
阿新 • • 發佈:2021-02-02
技術標籤:python基礎實驗
參考連結: hasattr(object, name)
參考連結: setattr(object, name, value)
參考連結: getattr(object, name[, default])
實驗程式碼展示:
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def printInfo(self):
print('我是Person類,我的名字是{0:},我的年齡是{1:}歲.'.format(self. name,self.age))
if __name__ == "__main__":
p = Person('林祖泉',26)
print()
print('測試物件p的資訊'.center(101,"-"))
p.printInfo()
print()
print('測試函式hasattr(object, name)和函式getattr(object, name[, default])'.center(100,"-"))
print('測試屬性name:', hasattr (p, 'name'), p.name, getattr(p, 'name', '未指定name的資訊'))
print('測試屬性age:', hasattr(p, 'age'), p.age, getattr(p, 'age', '未指定age的資訊'))
print('測試屬性height:', hasattr(p, 'height'), getattr(p, 'height', '未指定height的資訊'))
print('測試屬性weight:', hasattr(p, 'weight'), getattr(p, 'weight', '未指定weight的資訊' ))
print()
print('修改已存在的屬性'.center(100,"-"))
print('屬性修改中......')
p.name = '林麻子'
setattr(p, 'age', 888)
print('新增未存在的屬性'.center(100,"-"))
print('屬性新增中......')
p.height = '173釐米'
setattr(p, 'weight', '75公斤')
print('屬性設定完畢'.center(102,"-"))
print()
print('測試屬性 name:', hasattr(p, 'name'), p.name, getattr(p, 'name', '未指定name的資訊'))
print('測試屬性 age:', hasattr(p, 'age'), p.age, getattr(p, 'age', '未指定age的資訊'))
print('測試屬性 height:', hasattr(p, 'height'), p.height, getattr(p, 'height', '未指定height的資訊'))
print('測試屬性 weight:', hasattr(p, 'weight'), p.weight, getattr(p, 'weight', '未指定weight的資訊'))
print()
print('測試物件p的資訊'.center(101,"-"))
p.printInfo()
print()
控制檯輸出結果:
Windows PowerShell
版權所有 (C) Microsoft Corporation。保留所有權利。
嘗試新的跨平臺 PowerShell https://aka.ms/pscore6
載入個人及系統配置檔案用了 1032 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> & 'D:\Anaconda3\envs\ssd4pytorch1_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '65145' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test.py'
-----------------------------------------------測試物件p的資訊----------------------------------------------
我是Person類,我的名字是林祖泉,我的年齡是26歲.
--------------------測試函式hasattr(object, name)和函式getattr(object, name[, default])--------------------
測試屬性name: True 林祖泉 林祖泉
測試屬性age: True 26 26
測試屬性height: False 未指定height的資訊
測試屬性weight: False 未指定weight的資訊
----------------------------------------------修改已存在的屬性----------------------------------------------
屬性修改中......
----------------------------------------------新增未存在的屬性----------------------------------------------
屬性新增中......
------------------------------------------------屬性設定完畢------------------------------------------------
測試屬性 name: True 林麻子 林麻子
測試屬性 age: True 888 888
測試屬性 height: True 173釐米 173釐米
測試屬性 weight: True 75公斤 75公斤
-----------------------------------------------測試物件p的資訊----------------------------------------------
我是Person類,我的名字是林麻子,我的年齡是888歲.
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>