您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中实现回调-将可调用的引用传递给当前函数

在Python中实现回调-将可调用的引用传递给当前函数

任何定义的函数都可以通过简单地使用其名称来传递,而无需()调用末尾添加

def my_callback_func(event):
    # do stuff

o = Observable()
o.subscribe(my_callback_func)

其他示例用法

class CallbackHandler(object):
    @staticmethod
    def static_handler(event):
        # do stuff

    def instance_handler(self, event):
        # do stuff

o = Observable()

# static methods are referenced as <class>.<method>
o.subscribe(CallbackHandler.static_handler)

c = CallbackHandler()
# instance methods are <class instance>.<method>
o.subscribe(c.instance_handler)

# You can even pass lambda functions
o.subscribe(lambda event: <<something involving event>>)
python 2022/1/1 18:31:34 有352人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶