Python内置函数之: hasattr()、setattr()、getattr()、delattr()的用法

参考链接: 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
>>> 
>>> 
>>> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值