Implementing a callback in Python - passing a callable reference to the current function Implementing a callback in Python - passing a callable reference to the current function python python

Implementing a callback in Python - passing a callable reference to the current function


Any defined function can be passed by simply using its name, without adding the () on the end that you would use to invoke it:

def my_callback_func(event):    # do stuffo = Observable()o.subscribe(my_callback_func)

Other example usages:

class CallbackHandler(object):    @staticmethod    def static_handler(event):        # do stuff    def instance_handler(self, event):        # do stuffo = 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 functionso.subscribe(lambda event: <<something involving event>>)


To add to Amber's main answer above, this is a complete example of how two classes can be used to implement a callback from one to another.

I dummied this up to satisfy my own curiosity on whether it would work or not - particularly given that register only seems to be called with a function being passed, but now that I've seen this example work, the function must be passed with a context of the object to which it is attached.

class A():    def register(self,fn):        self.cb=fn        def gimme(self,val):        self.cb(val)class B():    def __init__(self, o):        self.o = o        self.o.register(self.callback)        def callback(self,val):        print(f"Got called with {val} !!")    def go(self):        self.o.gimme('test')>>> a = A()>>> b = B(a)>>> b.go()Got called with test !!