hasattr(object, name)

判断一个对象(object)是否存在name属性或方法,返回boolean值,有name属性返回True, 否则返回False

In [1]: class Test(object):
...: name = 'hkey'
...: def hello(self):
...: print('hello', self.name)
...: In [2]: t = Test() In [3]: print(hasattr(t, 'name')) # 注意:属性'name'是需要引号引起来的
True In [4]: print(hasattr(t, 'hello')) # 方法也是类的属性
True

getattr(object, name[, default])

获取对象object的属性或方法(name), 如果存在打印出来,如果不存在,打印默认值,默认值可选,默认值不存在报错对象没有该属性;

In [1]: class Test(object):
...: name = 'hkey'
...: def hello(self):
...: print('hello ', self.name)
...: In [2]: t = Test() In [3]: print(getattr(t, 'name'))
hkey In [4]: print(getattr(t, 'hello'))
<bound method Test.hello of <__main__.Test object at 0x000001499524EE80>> In [5]: print(getattr(t, 'age'))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-bd4a642cfb8c> in <module>()
----> 1 print(getattr(t, 'age')) # 没有该属性或方法 AttributeError: 'Test' object has no attribute 'age' In [6]: print(getattr(t, 'age', 20)) # 设置默认值
20

setattr(object, name, values)

给对象的属性赋值,若属性不存在,先创建再赋值

# object不存在属性(age), 用 setattr 新增属性 'age'
In [1]: class Test(object):
...: name = 'hkey'
...: def hello(self):
...: print('hello ', self.name)
...: In [2]: t = Test() In [3]: print(hasattr(t, 'age'))
False In [4]: setattr(t, 'age', 20) In [5]: print(hasattr(t, 'age'))
True In [6]: print(getattr(t, 'age'))
20 # object 存在属性(name), 用 setattr 覆盖原属性值
In [1]: class Test(object):
...: name = 'hkey'
...: def hello(self):
...: print('hello ', self.name)
...: In [2]: t = Test() In [3]: print(hasattr(t, 'name')) # 使用 hasattr 检查object 是否存在'name'属性, True为存在
True In [4]: setattr(t, 'name', 'superman') # setattr 重新为'name'属性赋值为'superman' In [5]: print(getattr(t, 'name')) # 使用getattr 获取'name'属性的值
superman In [6]: print(t.name) # 直接调用实例't'的name属性
superman

从上面的实例发现,如果object已经存在属性,再次使用setattr为该属性赋值,该属性会发生变化,这里值得注意。

hasattr(), getattr(), setattr() 使用场景

* 作为反射对程序进行解耦操作 *

In [1]: class Test(object):
...: def __init__(self):
...: self.x = 1
...: self.y = 2
...:
...: def sum(self):
...: print(self.x + self.y)
...:
...: def sub(self):
...: print(self.x - self.y)
...: In [2]: t = Test() In [3]: while True:
...: cmd = input('-->').strip()
...: if hasattr(t, cmd): # 'hasattr' 判断 'cmd' 属性是否存在
...: func = getattr(t, cmd) # getattr 获取 'cmd' 属性的值
...: func() # 执行该属性
...: else:
...: setattr(t, cmd, t.sum) # 如果输入不存在的属性名, 使用 'setattr' 重新为 'cmd' 赋值为 't.sum'
...: func = getattr(t, cmd) # getattr 获取 'cmd' 属性的值
...: func() # 执行该属性
...: # 执行结果:
-->sum
3
-->sub
-1
-->dddddd
3

最新文章

  1. JS 数组去重复值
  2. QtAlgorithms
  3. c# 获取屏幕DPI
  4. CCSprite的使用方法大全
  5. 纯CSS写三角形-border法
  6. lintcode:三数之和
  7. self指向函数地址 动态调用函数的简单例子
  8. 实现textarea限制输入字数
  9. iOS开发极光推送显示 开发证书没有通过验证 是否重新上传证书?解决方法
  10. Mac下截图快捷键
  11. Java ee el表达式
  12. 17. ZooKeeper常见的分布式系统任务——屏障
  13. leveldb实现原理
  14. springboot 实战之一站式开发体验
  15. python学习日记(装饰器的补充)
  16. POJ 2672 Tarjan + 缩点 + 拓扑思想
  17. Netty 4.1 Getting Start (翻译) + Demo
  18. AndroidStudio 代码(导入类)报错但可正常运行,以及解决此问题后带来的系列问题解决
  19. vue.js not detected 解决办法-vue.js devtools 安装
  20. phpstorm的安装和使用

热门文章

  1. BZOJ4890 &amp; 洛谷3761:[TJOI2017]城市——题解
  2. BZOJ3197:[SDOI2013]刺客信条——题解
  3. Visual Studio 2010如何利用宏
  4. 什么是static?什么是final?
  5. 制定clone的用户名
  6. java入门实现转换
  7. 【BZOJ3884】上帝与集合的正确用法 [欧拉定理]
  8. 【NOIP】提高组2013 火柴排队
  9. Optimal Milking(POJ2112+二分+Dinic)
  10. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) F.猴子排序的期望