Python的hasattr() getattr() setattr() 函数使用方法详解
来源:互联网 发布:win10安装ubuntu共存 编辑:程序博客网 时间:2024/05/21 22:55
hasattr(object, name)
判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False。
需要注意的是name要用括号括起来
1 >>> class test(): 2 ... name="xiaohua" 3 ... def run(self): 4 ... return "HelloWord" 5 ... 6 >>> t=test() 7 >>> hasattr(t, "name") #判断对象有name属性 8 True 9 >>> hasattr(t, "run") #判断对象有run方法10 True11 >>>
getattr(object, name[,default])
获取对象object的属性或者方法,如果存在打印出来,如果不存在,打印出默认值,默认值可选。
需要注意的是,如果是返回的对象的方法,返回的是方法的内存地址,如果需要运行这个方法,
可以在后面添加一对括号。
1 >>> class test(): 2 ... name="xiaohua" 3 ... def run(self): 4 ... return "HelloWord" 5 ... 6 >>> t=test() 7 >>> getattr(t, "name") #获取name属性,存在就打印出来。 8 'xiaohua' 9 >>> getattr(t, "run") #获取run方法,存在就打印出方法的内存地址。10 <bound method test.run of <__main__.test instance at 0x0269C878>>11 >>> getattr(t, "run")() #获取run方法,后面加括号可以将这个方法运行。12 'HelloWord'13 >>> getattr(t, "age") #获取一个不存在的属性。14 Traceback (most recent call last):15 File "<stdin>", line 1, in <module>16 AttributeError: test instance has no attribute 'age'17 >>> getattr(t, "age","18") #若属性不存在,返回一个默认值。18 '18'19 >>>
setattr(object, name, values)
给对象的属性赋值,若属性不存在,先创建再赋值。
1 >>> class test(): 2 ... name="xiaohua" 3 ... def run(self): 4 ... return "HelloWord" 5 ... 6 >>> t=test() 7 >>> hasattr(t, "age") #判断属性是否存在 8 False 9 >>> setattr(t, "age", "18") #为属相赋值,并没有返回值10 >>> hasattr(t, "age") #属性存在了11 True12 >>>
一种综合的用法是:判断一个对象的属性是否存在,若不存在就添加该属性。
1 >>> class test(): 2 ... name="xiaohua" 3 ... def run(self): 4 ... return "HelloWord" 5 ... 6 >>> t=test() 7 >>> getattr(t, "age") #age属性不存在 8 Traceback (most recent call last): 9 File "<stdin>", line 1, in <module>10 AttributeError: test instance has no attribute 'age'11 >>> getattr(t, "age", setattr(t, "age", "18")) #age属性不存在时,设置该属性12 '18'13 >>> getattr(t, "age") #可检测设置成功14 '18'15 >>>
阅读全文
0 0
- Python的hasattr() getattr() setattr() 函数使用方法
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python的hasattr() getattr() setattr() 函数使用方法详解
- Python中hasattr() getattr() setattr() 函数使用方法详解
- python hasattr() getattr() setattr()函数的使用
- 人工智能资料
- excel之多个单元格同时除以一个数
- laravel5.4下的group by报错
- <p>和<br>的区别
- JMeter中引入外部的JAR包给bean shell 使用(提供三种方法)
- Python的hasattr() getattr() setattr() 函数使用方法详解
- maven_阿里云镜像仓库(可用)
- STM32在变量不被编译
- PHP每天固定时间点执行一次函数
- git常用指令
- spring3.0集成slf4j和logback
- java使用jxl创建excel表格由于行列有限制,推荐使用poi
- 机器学习理论篇1:机器学习的数学基础
- 设计模式_解释器模式(26)