Page 1 of 1

General API design - callbacks

Posted: Fri Sep 27, 2013 4:38 pm
by Omega_K2
So what do you think is the best method to specify callbacks?

Just a single method or support args/kwargs?

Syntax: Select all

def myapifunc(callback):
callback()


vs


Syntax: Select all

def myapifunc(callback, callback_args=(), callback_kwargs={}):
callback(*callback_args, **callback_kwargs)



Please note that with both solutions you can use arguments, except you need a wrapper for the first like this:

Syntax: Select all

class Wrap(object):
def __init__(self, callback, args, kwargs):
self.callback = callback
self.args = args
self.kwargs = kwargs
def __call__(self):
self.callback(*self.args, **self.kwargs)

Posted: Sat Sep 28, 2013 10:00 pm
by arawra
Isn't explicit better than implicit? If you hold true to that idiom, I'd go with the former option.

Posted: Sun Sep 29, 2013 12:18 am
by satoon101
I tend to agree with arawra on this. Are there any specific examples that you wish to have that ability for? Obviously, TickDelays has that capability, because you are calling a specific function with specific arguments/keywords after the specified time. I really don't see the point in doing that in "most" situations.

Satoon