什么是反射
反射的概念是由 Smith 在 1982 年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)
Python 面向对象中的反射
- 通过字符串的形式操作对象的属性
- Python 中一切皆为对象,所以只要是对象都可以使用反射
- 比如:实例对象、类对象、本模块、其他模块,因为他们都能通过 对象.属性 的方式获取、调用
反射中关键的四个函数
- hasattr
- getattr
- setattr
- delattr
hasattr
def hasattr(*args, **kwargs): """ Return whether the object has an attribute with the given name. This is done by calling getattr(obj, name) and catching AttributeError. """ pass
- 返回对象是否具有具有给定名称的属性
- 这是通过调用 getattr(obj,name)并捕获AttributeError来完成的
getattr
def getattr(object, name, default=None): """ getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. """ pass
- 获取对象指定名称的属性
- getattr(x , y) 等价写法 x.y
- 当属性不存在,则返回 default 值,如果没有指定 default 就会抛出异常
setattr
def setattr(x, y, v): """ Sets the named attribute on the given object to the specified value. setattr(x, 'y', v) is equivalent to ``x.y = v'' """ pass
- 给指定对象的指定属性设置为值
- setattr(x,y,v) 等价写法 x.y = v
delattr
def delattr(x, y): """ Deletes the named attribute from the given object. delattr(x, 'y') is equivalent to ``del x.y'' """ pass
- 从指定对象中删除指定属性
- delattr(x,y) 等价写法 del x.y
反射类的成员
class PoloBlog: sum = 0 def __init__(self, name): self.name = name def test(self): print("====姓名==== ", self.name)
hasattr
blog = PoloBlog("小菠萝") # hasattr print(hasattr(blog, "name")) # 实例对象-实例属性 print(hasattr(blog, "sum")) # 实例对象-类属性 print(hasattr(PoloBlog, "sum")) # 类对象-类属性 print(hasattr(PoloBlog, "name")) # 类对象-实例属性 # 输出结果 True True True False
getattr
# getattr print(getattr(blog, "name")) # 实例对象-实例属性 print(getattr(blog, "sum")) # 实例对象-类属性 print(getattr(PoloBlog, "sum")) # 类对象-类属性 print(getattr(PoloBlog, "name", "默认值")) # 类对象-实例属性 # 输出结果 小菠萝 0 0 默认值
setattr
# 设置一个新的实例属性 setattr(blog, "age", 24) # 设置一个新的实例方法 setattr(blog, "printNameAge", lambda self: f"姓名:{self.name} 年龄:{self.age}") print(blog.__dict__) print(blog.printNameAge(blog)) # 输出结果 {'name': '小菠萝', 'age': 24, 'printNameAge': <function <lambda> at 0x10391a1f0>} 姓名:小菠萝 年龄:24
delattr
# delattr delattr(blog, "age") delattr(blog, "printNameAge") print(blog.__dict__) # 输出结果 {'name': '小菠萝'}
反射本模块的成员
除了可以检测类中有没有某个属性、方法,还可以用来检测某个模块下有没有方法、类、变量
sums = 0 def test1(): print("test") class A(): pass this_module = sys.modules[__name__] print(__name__) print(this_module) print(hasattr(this_module, "sums")) # 变量 print(hasattr(this_module, "test1")) # 方法 print(hasattr(this_module, "A")) # 类 # 输出结果 __main__ <module '__main__' from '/Users/polo/Documents/pylearn/第四章:面向对象/22_反射.py'> True True True