Python反射
反射是什么
在计算机学中,反射式编程(英语:reflective programming)或反射(英语:reflection),是指计算机程序在运行时(runtime)可以访问、检测和修改它本身状态或行为的一种能力。用比喻来说,反射就是程序在运行的时候能够“观察”并且修改自己的行为。(摘自维基百科)
反射就是通过字符串的形式,导入模块;通过字符串的形式,去模块寻找指定函数,并执行。利用字符串的形式去对象(模块)中操作(查找/获取/删除/添加)成员,一种基于字符串的事件驱动。(来源:Python面试笔试300题 https://zhuanlan.zhihu.com/p/40724658)
反射的 Python 用法
用反射,可以在编译时不确定目标对象的属性、方法、类型等,而是在程序运行时才对目标对象进行增删改查。类比物理中的反射,由于视线遮挡,我们通过湖面的反射观察天空飞过的鸟。
【查】判断对象所属的类
远处飞来的到底是一只什么鸟?麻雀?大雁?
原来都不是,远处飞来的是一个飞机模型。
type 和 isinstance
# -*- coding: utf-8 -*-
class Bird:
pass
bird = Bird()
print(type(bird) == Bird)
# True
print(isinstance(bird, Bird))
# True
globals 和 locals
globals() 函数会以字典类型返回当前位置的全部全局变量。
locals() 函数会以字典类型返回当前位置的全部局部变量。
print(globals())
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x011EAF40>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Test/main.py', '__cached__': None, 'Bird': <class '__main__.Bird'>, 'bird': <__main__.Bird object at 0x0121E5E0>}
print(locals())
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x011EAF40>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/Test/main.py', '__cached__': None, 'Bird': <class '__main__.Bird'>, 'bird': <__main__.Bird object at 0x0121E5E0>}
hasattr 和 getattr
hasattr() 函数用于判断对象是否包含对应的属性。
getattr() 函数用于返回一个对象属性值。
print(hasattr(bird, "__class__"))
# True
print(getattr(bird, "__class__"))
# <class '__main__.Bird'>
inspect
是用来获取对象的信息,对象包括模块(往往是一个py文件)、类、方法、函数、报错追踪、帧对象和代码对象。(来源:Python库介绍(一)——inspect https://zhuanlan.zhihu.com/p/290018252)
import inspect
print(inspect.getmembers(bird))
# [('__class__', <class '__main__.Bird'>), ('__delattr__', <method-wrapper '__delattr__' of Bird object at 0x00F5E5E0>), ('__dict__', {}), ('__dir__', <built-in method __dir__ of Bird object at 0x00F5E5E0>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of Bird object at 0x00F5E5E0>), ('__format__', <built-in method __format__ of Bird object at 0x00F5E5E0>), ('__ge__', <method-wrapper '__ge__' of Bird object at 0x00F5E5E0>), ('__getattribute__', <method-wrapper '__getattribute__' of Bird object at 0x00F5E5E0>), ('__gt__', <method-wrapper '__gt__' of Bird object at 0x00F5E5E0>), ('__hash__', <method-wrapper '__hash__' of Bird object at 0x00F5E5E0>), ('__init__', <method-wrapper '__init__' of Bird object at 0x00F5E5E0>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x01B8A3D8>), ('__le__', <method-wrapper '__le__' of Bird object at 0x00F5E5E0>), ('__lt__', <method-wrapper '__lt__' of Bird object at 0x00F5E5E0>), ('__module__', '__main__'), ('__ne__', <method-wrapper '__ne__' of Bird object at 0x00F5E5E0>), ('__new__', <built-in method __new__ of type object at 0x5E187B78>), ('__reduce__', <built-in method __reduce__ of Bird object at 0x00F5E5E0>), ('__reduce_ex__', <built-in method __reduce_ex__ of Bird object at 0x00F5E5E0>), ('__repr__', <method-wrapper '__repr__' of Bird object at 0x00F5E5E0>), ('__setattr__', <method-wrapper '__setattr__' of Bird object at 0x00F5E5E0>), ('__sizeof__', <built-in method __sizeof__ of Bird object at 0x00F5E5E0>), ('__str__', <method-wrapper '__str__' of Bird object at 0x00F5E5E0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x01B8A3D8>), ('__weakref__', None)]
其他
还有很多魔术方法像 __ dir __ 、__getattribute__
,执行函数 eval、exec
。
【删】删除对象的属性
这里和物理的反射就有区别了,我们并不能通过湖面的倒影对实际的飞机模型产生影响。所以物理反射其实与内省或自省更相似。
要注意术语“反射”和“内省”(type introspection)的关系。内省(或称“自省”)机制仅指程序在运行时对自身信息(称为元数据)的检测;反射机制不仅包括要能在运行时对程序自身信息进行检测,还要求程序能进一步根据这些信息改变程序状态或结构。(摘自维基百科)
delattr 函数用于删除属性。
del 也能用来删除属性。
bird.age = 5
print(hasattr(bird, "age"))
# True
delattr(bird, "age")
print(hasattr(bird, "age"))
# False
bird.fly = 1
print(bird.fly)
# 1
del bird.fly
print(hasattr(bird, "fly"))
# False
【改】修改对象的属性
刚好手里有一个控制飞机模型的万能遥控器,不管来的是什么飞机,我们直接调转它的方向。
setattr() 函数对应函数 getattr(),用于设置属性值,该属性不一定是存在的。
print(hasattr(bird, "direction"))
# False
setattr(bird, "direction", "left")
print(bird.direction)
# left
【增】动态加载模块
假如没有万能遥控器,为每种飞机带一个遥控器,只有几个时没有问题,当超过一定数量时就无法实现了。
不使用反射,为每种飞机单独写同样的代码,虽然重复,但是也能实现需要的功能。所以反射更适用于编写较抽象、通用的框架。
python动态加载模块的方法(别人写的很详细就不再单独列举了):
- importlib.import_module
- types.ModuleType
- stevedore使用setuptools的entry points来定义并加载插件
相关文章:
python 动态导入模块,实现模块热更新 https://blog.csdn.net/weixin_40647516/article/details/126537108
Python插件之stevedore
https://blog.csdn.net/weixin_43700106/article/details/107150715