Dispatch table of methods with various return value types
Loris Bennett
loris.bennett at fu-berlin.de
Tue Nov 17 04:01:30 EST 2020
Hi,
I have a method for manipulating the membership of groups such as:
def execute(self, operation, users, group):
"""
Perform the given operation on the users with respect to the
group
"""
action = {
'get': self.get,
'add': self.add,
'delete': self.delete,
}
return action.get(operation)(users, group)
The 'get' action would return, say, a dict of users attribute, whereas
the 'add/delete' actions would return, say, nothing, and all actions
could raise an exception if something goes wrong.
The method which calls 'execute' has to print something to the terminal,
such as the attributes in the case of 'get' and 'OK' in the cases of
'add/delete' (assuming no exception occurred).
Is there a canonical way of dealing with a method which returns different
types of data, or should I just make all actions return the same data
structure so that I can generate a generic response?
Cheers,
Loris
--
This signature is currently under construction.
More information about the Python-list
mailing list