You could define a methodscaller
function that was somewhat of hybrid between what operator.itemgetter()
and operator.methodcaller()
do that created something callable that would call all the methods specified when it was called. The function itself would be generic in the sense that it can be used with any type of object and/or series of methods.
The following demonstrates what I mean:
class Foo:
def dog(self): print('dog called')
def cat(self): print('cat called')
def fish(self): print('fish called')
def moose(self): print('moose called')
def horse(self): print('horse called')
def methodscaller(*methods):
"""Return callable object that calls the methods named in its operand."""
if len(methods) == 1:
def g(obj):
return getattr(obj, methods[0])
else:
def g(obj):
return tuple(getattr(obj, method)() for method in methods)
return g
call_methods = methodscaller('dog', 'cat', 'fish', 'moose', 'horse')
call_methods(Foo())