时间:2021-07-01 10:21:17 帮助过:95人阅读
''' 放射 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--反射/自省
以上就是Python放射的代码实现的详细内容,更多请关注Gxl网其它相关文章!