反射

通过字符串映射或者修改程序运行时的状态、属性、方法, 有一下4个方法

小例子--根据用户输入调用方法:

class Dog(object):

    def __init__(self,name):
self.name = name def eat(self):
print("%s is eating..",self.name) d= Dog('二哈')
choice = input(">>:").strip()
d.choice()
=========执行结果===========
>>:eat
Traceback (most recent call last):
File "E:/pywww/day06/11.py", line 13, in <module>
d.choice
AttributeError: 'Dog' object has no attribute 'choice'

这里用户输入的只是一个字符串,所以不会把输入的内容当作类的方法执行。

最原始的办法就是加个判断,然后判断输入的字符串是不是在这个类里有这个方法,但是这种灵活度不好。所以可以用到下面这种方法。

hasattr(obj,name_str)

输入一个字符串,判断对象有没有这个方法。

class Dog(object):

    def __init__(self,name):
self.name = name def eat(self):
print("%s is eating.."% self.name) d= Dog('二哈')
choice = input(">>:").strip() #eat
print(hasattr(d,choice)) #True

getattr(obj,name_str)

如果是方法返回对象的内存地址,如果是属性直接返回该属性的值

print(getattr(d,choice)) #<bound method Dog.eat of <__main__.Dog object at 0x0000000000A72358>>

如果是一个不存在的方法会报错:AttributeError: 'Dog' object has no attribute 'aa'

既然是地址就说明加上括号就可以调用了:

class Dog(object):

    def __init__(self,name):
self.name = name def eat(self):
print("%s is eating.."% self.name) d= Dog('二哈')
choice = input(">>:").strip() #eat
getattr(d,choice)() #二哈 is eating..

以上两个方法可以组合起来使用:

if hasattr(d,choice):
getattr(d,choice)() #二哈 is eating..

也可以赋值给一个变量,然后传参给方法

class Dog(object):

    def __init__(self,name):
self.name = name def eat(self,food):
print("%s is eating.."% self.name,' like ',food) d= Dog('二哈')
choice = input(">>:").strip() #eat
if hasattr(d,choice):
func = getattr(d,choice)
func('包子') #二哈 is eating.. like 包子

setattr(x,'y',v) x.y = v

添加一个方法

def bulk(self):                                                  #先定义要添加的方法
print('%s wang wang wang' % self.name) class Dog(object): def __init__(self,name):
self.name = name def eat(self,food):
print("%s is eating.."% self.name,' like ',food) d= Dog('二哈')
choice = input(">>:").strip() #bulk
if hasattr(d,choice):
func = getattr(d,choice)
func('包子')
else:
setattr(d,choice,bulk)
d.bulk(d) #bulk 是你输入的字符串 ,这里要把d传进去,不然提示你少传一个参数进去

上面是动态装载一个方法,也可以动态装载一个属性

 setattr(d,choice,22) #age
print(getattr(d,choice)) #22

delattr(obj,name_str)

动态删除属性或方法

 delattr(d,choice)

最新文章

  1. golang的安装
  2. 网络安全——数据的加密与签名,RSA介绍
  3. POJ 1201 Intervals (差分约束,最短路)
  4. 【转】iOS 开发之协议protocal-代理传值delegate
  5. STL学习二:Vector容器
  6. HDU ACM 1098 Ignatius&amp;#39;s puzzle
  7. 浅谈sqlldr
  8. [Ruby]How to create singleton class ?
  9. 新生命组件XAgent使用心得
  10. 【Linux Tips】登陆,提示符,别名
  11. (HTTPS)web 项目如何实现https
  12. ChromeDriver,IEDriver,Firefox配置
  13. 基于Avocado 的 QData MySQL自动化测试.md
  14. (转)tomcat 修改默认访问项目名称和项目发布路径
  15. 微软BI 之SSIS 系列 - 理解Data Flow Task 中的同步与异步, 阻塞,半阻塞和全阻塞以及Buffer 缓存概念
  16. java配置slf4j日志系统
  17. react-native项目之样式总结
  18. Oracle EBS OPM 取消生产批
  19. python的特性
  20. jquery获取浏览器宽高

热门文章

  1. CF796A Buying A House 模拟
  2. nginx的使用(启动、重启、关闭)
  3. mpvue 小程序应用拖动排序
  4. H5切换至后台页面
  5. 设定计划的SMART原则
  6. MongoDB安装为windows服务
  7. ajax提交文件file 单个文件上传
  8. oracle service name connect
  9. java——线程的wait()和notify()
  10. Charles抓包使用总结