什么是Hook钩子函数?
Hook钩子函数是一种回调(Callback)机制,允许程序在执行的特定点插入用户定义的代码。这种机制在事件处理、插件系统、调试工具等方面得到广泛应用。典型的Hook钩子包含预定义的事件点和用户自定义的处理函数
Hook的基本用法
1 基本结构
class Hook:def __init__(self):self.hooks = []def register(self, func):self.hooks.append(func)def run(self, *args, **kwargs):for hook in self.hooks:hook(*args, **kwargs)
# 使用示例
hook_instance = Hook()@hook_instance.register
def my_hook_function(arg):print(f"My hook function called with argument: {arg}")
# 调用钩子
hook_instance.run("Hello, Hook!")# output
>>> My hook function called with argument: Hello, Hook!
https://sitin.blog.csdn.net/article/details/135016069?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ECtr-3-135016069-blog-133174718.235%5Ev43%5Epc_blog_bottom_relevance_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ECtr-3-135016069-blog-133174718.235%5Ev43%5Epc_blog_bottom_relevance_base3&utm_relevant_index=6