Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Callbacks, Delegates, ... ? What is common?

Tags:

Just want to know what's the common way to react on events in python. There are several ways in other languages like callback functions, delegates, listener-structures and so on. Is there a common way? Which default language concepts or additional modules are there and which can you recommend?

like image 865
okoman Avatar asked Jan 14 '09 17:01

okoman


People also ask

Are delegates the same as callbacks?

Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.

What is a Python callback?

In Python, a callback is simply a function or a method passed to LocalSolver. A callback takes two parameters: the LocalSolver object that triggers the event and the type of the callback. It is possible to use the same callback method or object for multiple events or multiple LocalSolver instances.

Are callbacks just events?

Callback is for asking another function to do business operations and send a result whereas events are not but asking for handover the control so that we can only handle business operation. Eg: Button click is event (We are doing business operation on click of button).

What are callbacks in oops?

Callbacks is a mechanism in Object Oriented Programming that allows an application to handle subscribed events, arising at runtime, through a listener interface. The subscribers will need to provide a concrete implementation of the interface abstract methods.


2 Answers

Personally I don't see a difference between callbacks, listeners, and delegates.

The observer pattern (a.k.a listeners, a.k.a "multiple callbacks") is easy to implement - just hold a list of observers, and add or remove callables from it. These callables can be functions, bound methods, or classes with the __call__ magic method. All you have to do is define the interface you expect from these - e.g. do they receive any parameters.

class Foo(object):   def __init__(self):     self._bar_observers = []    def add_bar_observer(self, observer):     self._bar_observers.append(observer)    def notify_bar(self, param):     for observer in self._bar_observers:       observer(param)  def observer(param):   print "observer(%s)" % param  class Baz(object):   def observer(self, param):     print "Baz.observer(%s)" % param  class CallableClass(object):   def __call__(self, param):     print "CallableClass.__call__(%s)" % param  baz = Baz()  foo = Foo()  foo.add_bar_observer(observer) # function foo.add_bar_observer(baz.observer) # bound method foo.add_bar_observer(CallableClass()) # callable instance  foo.notify_bar(3) 
like image 73
orip Avatar answered Oct 21 '22 17:10

orip


I can't speak for common approaches, but this page (actual copy is unavailable) has an implementation of the observer pattern that I like.

Here's the Internet Archive link: http://web.archive.org/web/20060612061259/http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html

like image 22
Matthew Maravillas Avatar answered Oct 21 '22 16:10

Matthew Maravillas