类的反射
一、概念
Python中反射是作用于类中的方法和属性,通过字符串映射或修改程序运行时的状态、属性、方法。
二、反射方法
2.1、hasattr(obj,name_str)
判断一个对象obj中是否有对应的name_str字符串的属性或者方法
class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:") # 用户输入的形式来判断是否存在该方法 print(hasattr(p, choice)) # 输出 >>:sport True # 输入其他方法 >>:walk False
# 测试属性 >>:name True >>:age False
2.1、getattr(obj,name_str)
根据字符串name_str获取obj对象中的对应方法的内存地址或者对应属性的值
class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:") print(hasattr(p, choice)) print(getattr(p, choice)) # 输出 >>:sport True <bound method Person.sport of <__main__.Person object at 0x000001AA97F2D9E8>> # 获取了该方法的内存地址
有了方法的内存地址,我们调用直接加()就行了
class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:").strip() print(hasattr(p, choice)) func = getattr(p, choice) func('football') # 输出 >>:sport True Bigberg is good at football
2.3 setattr(x,y,v)
作用:给obj对象添加一个新属性或者新方法,setattr(x, 'y', v) is equivalent to ``x.y = v''
1. 新增一个方法
def hobby(self, favour): # 在类 外又定义了一个函数hobby print('%s is fond of %s' % (self.name, favour)) class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:").strip() setattr(p, choice, hobby) #参数分别为 对象,方法/属性名, 函数名 p.hobby(p, 'football') # 调用时要传入一个self参数,所有我们要把对象p穿进去 func = getattr(p, choice) # 上面的调用方法可以,但不是很清晰 func(p, 'football') # 输出 >>:hobby Bigberg is fond of football Bigberg is fond of football
2. 新增一个属性
def hobby(self, favour): print('%s is fond of %s' % (self.name, favour)) class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:").strip() setattr(p, choice, 22) # 新增一个属性age = 22 attr = getattr(p, choice) print(attr) # 输出 >>:age 22
2.4 delattr()
删除obj对象中的属性或者方法,delattr(x, 'y') is equivalent to ``del x.y''
class Person(object): def __init__(self, name): self.name = name def sport(self, item): print("%s is good at %s" % (self.name, item)) p = Person('Bigberg') choice = input(">>:").strip() # 删除属性 delattr(p, choice) print(p.name) >>:name Traceback (most recent call last): File "G:/python/untitled/study6/反射.py", line 21, in <module> print(p.name) AttributeError: 'Person' object has no attribute 'name' # 删除方法 delattr(p, choice) p.sport('football') >>:sport Traceback (most recent call last): File "G:/python/untitled/study6/反射.py", line 21, in <module> delattr(p, choice) AttributeError: spor
三、事例
class Dog(object): def __init__(self, name): self.name = name def eat(self, food): print("{0} is eating...{1}".format(self.name, food)) d = Dog("hashiky") choice = input(">>>:").strip() if hasattr(d, choice): # 判断d对象中存在属性和方法 name_value = getattr(d, choice) # 获取属性值 print(name_value) setattr(d, choice, "hong") # 修改属性值 print(getattr(d, choice)) # 重新获取属性的值 else: setattr(d, choice, None) # 设置不存在的属性值为None v = getattr(d, choice) print(v)