###### 钩子函数

```
import pluggy hookspec = pluggy.HookspecMarker('aaa')
hookimpl = pluggy.HookimplMarker('aaa') class MySpec(): @hookspec() # firstresult=True设置之后,等到第一个返回非空结果的hookimpl,就返回(hookwrapper还是正常执行)
def myhook(self, arg_one, arg_two):
pass class MyPlugin(): @hookimpl() # tryfirst=True,trylast=True定义执行顺序
def myhook(self, arg_one):
print('arg_one')
return arg_one class MyPluginOther(): @hookimpl()
def myhook(self, arg_one, arg_two):
print('arg_one, arg_two')
return arg_one, arg_two class MyPluginWrapper():
'''
hookwrapper作为每个钩子函数的上下文运行
在所有hook之前先运行
'''
@hookimpl(hookwrapper=True)
def myhook(self):
print('-------- hool wrapper before -----------') outcome = yield # 生成器会给outcome send一个pluggy.callers._Result对象 print('-------- hook wrapper after -----------')
res = outcome.get_result() # 通过get_result拿到所有钩子函数的结果
print('get all hook return ', res)
outcome.force_result(1) # 强制更改钩子函数返回结果 print('-------- hook wrapper end-----------') test1 = pluggy.PluginManager('aaa')
test1.add_hookspecs(MySpec) test1.register(MyPlugin())
test1.register(MyPluginOther()) # 后注册的先执行,收集进结果
test1.register(MyPluginWrapper()) result = test1.hook.myhook(arg_one=1, arg_two=2) # hookimpl调用,必须用关键字语法
print(result) ------------------
输出结果:
-------- hool wrapper before -----------
arg_one, arg_two
arg_one
-------- hook wrapper after -----------
get all hook return [(1, 2), 1]
-------- hook wrapper end-----------
1
```

Python 钩子函数详解的更多相关文章

  1. Vue之生命周期函数和钩子函数详解

    在学习vue几天后,感觉现在还停留在初级阶段,虽然知道怎么和后端做数据交互,但是对对vue的生命周期不甚了解.只知道简单的使用,而不知道为什么,这对后面的踩坑是相当不利的.因为我们有时候会在几个钩子函 ...

  2. vue2.0项目实战(4)生命周期和钩子函数详解

    最近的项目都使用vue2.0来开发,不得不说,vue真的非常好用,大大减少了项目的开发周期.在踩坑的过程中,因为对vue的生命周期不是特别了解,所以有时候会在几个钩子函数里做一些事情,什么时候做,在哪 ...

  3. vue生命周期钩子函数详解

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_35585701/article/ ...

  4. python super()函数详解

    引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...

  5. Python匿名函数详解

    python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的. 在Python中,lambda ...

  6. 04 python之函数详解

    一.函数初识 函数的产生:函数就是封装一个功能的代码片段. li = ['spring', 'summer', 'autumn', 'winter'] def function(): count = ...

  7. Python sorted函数详解(高级篇)

    sorted() 函数对所有可迭代的对象进行排序操作. sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 s ...

  8. python socket函数详解

    关于socket函数,每个的意义和基本功能都知道,但每次使用都会去百度,参数到底是什么,返回值代表什么意义,就是说用的少,也记得不够精确.每次都查半天,经常烦恼于此.索性都弄得清楚.通透,并记录下来, ...

  9. Python print函数详解

    1 """ 2 print(...) 3 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fals ...

随机推荐

  1. linux运行tomcat报错SEVERE: Unable to process Jar entry [avassist xxxx.class]

    tomcat的版本过低换成apache-tomcat-7.0.56以上的高版本的就可以了

  2. mysql的压缩版安装

    MYSQL压缩版 自己建立: data(位于mysql的bin一层文件夹),my.ini(文本) my.ini(下面是文本内容) [client] port=3306 default-characte ...

  3. Oracle GoldenGate to Confluent with Kafka Connect

    Confluent is a company founded by the team that built Apache Kafka. It builds a platform around Kafk ...

  4. XSS Cheat Sheet

    Basic and advanced exploits for XSS proofs and attacks. Work in progress, bookmark it. Technique Vec ...

  5. 分析一下 原型模式的 UML 类图 。 复制对象, 深浅拷贝 月经贴 ,请回避

  6. CSS-13-块级元素和行内元素

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. gerrit merge后不能提交问题

    需求:git 分支合并 问题:使用 git merge 在本地执行分支合并操作,然后想 push 到 gerrit 上评审入库,可是在提交时,提示:  ! [remote rejected] HEAD ...

  8. PYTHON经典算法-完美平方

    问题描述: 给定一个正整数n,找到若干个完全平方数(例如:1,4,9),使得 它们的和等于n,完全平方数的个数最少. 问题示例: 给出n=12,返回3,因为12=4+4+4:给出n=13,返回2,因为 ...

  9. CTF--HTTP服务--路径遍历(拿到www-data用户权限)

    开门见山 1. 扫描靶机ip,发现PCS 172.18.4.20 2. 用nmap扫描靶机开放服务及版本 3. 再扫描靶机的全部信息 4. 用nikto工具探测http服务敏感信息 5. 用dirb工 ...

  10. 获取PHP类的所有属性和所有方法,可通过反射机制

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?php   class Class1{     public $var1 = 'v ...