任何定义的函数都可以通过简单地使用其名称来传递,而无需()
在调用末尾添加:
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>>)