了解过编程的人应该对函数重写 ( override ) 不陌生,但其实,这个普适的方法并不适用于全部的应用场景。举个简单的例子,当多个项目代码贡献方都想参与同一程序的修改时,频繁的函数重写会使代码变得异常混乱,让整个项目变得难以维护。python
那么,有没有更优雅的方法可以兼顾代码的扩展性与稳定性呢?编程
有的,pytest ( python 单元测试框架 ) 的做者就意识到了这个问题。在其源码中,能够发现许多通过 @pytest.hookimpl 关键字装饰的函数,这表明这个函数是一个插件的实现,其做用是经过用插件调用的形式来替代函数重写。。windows
pytest 部分源码:app
@pytest.hookimpl(hookwrapper=True)
def pytest_load_initial_conftests(early_config: Config):
ns = early_config.known_args_namespace
if ns.capture == "fd":
_py36_windowsconsoleio_workaround(sys.stdout)
_colorama_workaround()
_readline_workaround()
pluginmanager = early_config.pluginmanager
capman = CaptureManager(ns.capture)
pluginmanager.register(capman, "capturemanager")
# make sure that capturemanager is properly reset at final shutdown
early_config.add_cleanup(capman.stop_global_capturing)
# finally trigger conftest loading but while capturing (issue93)
capman.start_global_capturing()
outcome = yield
capman.suspend_global_capture()
if outcome.excinfo is not None:
out, err = capman.read_global_capture()
sys.stdout.write(out)
sys.stderr.write(err)
复制代码
早前在 pytest 中这只是一个插件工具库,而随着这个库的日益发展, 做者把它从 pytest 中分离了出来,并将其命名为了 pluggy。框架
今天的学习内容是要用 pluggy 把当前项目里的类和函数转变为插件,来代替函数重写。ide
预估赏金:约 1500 人民币。函数
通常在开始工做前须要将任务进行拆解,划分红数个小任务。而这个任务,基本能够划分为如下三步:工具
因而在一顿废寝忘食的编码后......单元测试
任务实际耗时在 15 个小时左右。学习
任务进行过程当中,发现写代码并非最难的,难的是如何将当前项目的代码结构插件化。过去的固化编码思惟让我很难短期想明白并做出实践。大概在作了一、2次代码重构,推翻了1次原有代码后,才终于完成了这个任务。
有时候代码思惟比代码更加剧要。十分感谢此次学习让我深入理解了编程的另外一种可能性。
期待下一个学习任务,也欢迎想要一块儿学习的小伙伴多多交流。