参考链接: hasattr(object, name)
参考链接: setattr(object, name, value)
参考链接: getattr(object, name[, default])
参考链接: delattr(object, name)
实验代码展示:
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>
代码实验:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> class Person():
def __init__(self, name, age):
self.name = name
self.age = age
>>>
>>> p = Person("张三",25)
SyntaxError: invalid character in identifier
>>> p = Person("张三",25)
>>> p
<__main__.Person object at 0x0000013CAF0EA088>
>>> hasattr(p, "name")
True
>>> hasattr(p, "age")
True
>>> hasattr(p, "weight")
False
>>> p.weight = "70kg"
>>> p.weight
'70kg'
>>> hasattr(p, "weight")
True
>>> setattr(p, "height", "175cm")
>>> p.height
'175cm'
>>> hasattr(p, "name")
True
>>> delattr(p, 'name')
>>> hasattr(p, "name")
False
>>>
>>> hasattr(p, "weight")
True
>>> delattr(p, 'weight')
>>> hasattr(p, "weight")
False
>>>
>>> hasattr(p, "height")
True
>>> delattr(p, 'height')
>>>
>>> hasattr(p, "height")
False
>>>
>>> p.age
25
>>> p.height
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
p.height
AttributeError: 'Person' object has no attribute 'height'
>>>
>>> hasattr(p, "age")
True
>>> delattr(p, 'age')
>>>
>>> hasattr(p, "age")
False
>>>
>>>
>>>