General API design - callbacks

Discuss API design here.
Omega_K2
Senior Member
Posts: 227
Joined: Sat Jul 07, 2012 3:05 am
Location: Europe
Contact:

General API design - callbacks

Postby Omega_K2 » Fri Sep 27, 2013 4:38 pm

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)
Libraries: k2tools
Plugins (any): GSRPG (soon) | Pretty Status List | MySQLAds (soon)
Plugins (game-specific): None atm

If you happen to find a bug or need help, either post in the release threads or contact me in IRC gamesurge.net:6667 / #sourcepython
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Sat Sep 28, 2013 10:00 pm

Isn't explicit better than implicit? If you hold true to that idiom, I'd go with the former option.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Sep 29, 2013 12:18 am

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

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 21 guests