这篇文章介绍的内容是python放射的代码实现,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
''' 放射 hasattr(obj, name_str):判断一个对象obj里是否有对应的name_str字符串的方法 getattr(obj, name_str):根据name_str字符串去获取obj对象里的对应的方法的内存地址 ''' def bulk(self): print("%s is yelling..." % self.name) class People(object): def __init__(self, name): self.name = name def talk(self): print("%s is talking..." % self.name) User = People("UserPython") choice = input(">>>:") # 判断一个对象User里是否有对应的choic = talk字符串的方法 # print(hasattr(User, choice)) #True # 根据choice字符串去获取User对象里的对应的方法的内存地址 # print(getattr(User, choice)) #<bound method People.talk of <__main__.People object at 0x0000000002741208>> if hasattr(User, choice): func = getattr(User, choice) func() else: setattr(User, choice, bulk) User.bulk(User)
相关推荐:
立即学习“Python免费学习笔记(深入)”;
以上就是Python放射的代码实现的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号