Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to implement the Observable pattern in Python for a couple of workers, and came across this helpful snippet:

class Event(object):     pass  class Observable(object):     def __init__(self):         self.callbacks = []     def subscribe(self, callback):         self.callbacks.append(callback)     def fire(self, **attrs):         e = Event()         e.source = self         for k, v in attrs.iteritems():             setattr(e, k, v)         for fn in self.callbacks:             fn(e) 

Source: Here

As i understand it, in order to subscribe, I would need to pass a callback to the function that is going to be called on fire. If the calling function was a class method, presumably I could have used self, but in the absence of this - how could I directly get a callback that can be useful for the self.callbacks.append(callback) bit?

like image 717
malangi Avatar asked Jan 14 '11 10:01

malangi


People also ask

How do I call a callback function in Python?

In the Parallel Python Module, the submit function is known as the callback function. The callback function acts as an argument for any other function. The other function in which the callback function is an argument calls the callback function in its function definition.

How are callbacks implemented?

Callbacks in C are usually implemented using function pointers and an associated data pointer. You pass your function on_event() and data pointers to a framework function watch_events() (for example). When an event happens, your function is called with your data and some event-specific data.

How do you assign a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

What is callback function explain with an example?

Note, however, that callbacks are often used to continue code execution after an asynchronous operation has completed — these are called asynchronous callbacks. A good example is the callback functions executed inside a . then() block chained onto the end of a promise after that promise fulfills or rejects.


1 Answers

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 stuff  o = 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 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>>) 
like image 186
Amber Avatar answered Sep 30 '22 04:09

Amber