View Single Post
Old Feb 19th, 2007, 2:57 PM   #2
ptmcg
Newbie
 
Join Date: Aug 2005
Location: Austin, TX
Posts: 15
Rep Power: 0 ptmcg is on a distinguished road
You could create a class that implements __call__ that takes all of the methods and calls them in turn. Here's a version that returns all of the return values in a list, unless an exception is thrown, in which case None is returned.

-- Paul

class Multicall(object):
    def __init__(self,*args):
        # all methods must have same signature
        self.methods = args
    
    def __call__(self,*args):
        retList = [None]*len(self.methods)
        for i,m in enumerate(self.methods):
            try:
                retList[i] = m(*args)
            except:
                # change 'break' to 'continue' if you want all methods to run
                break  
        return retList

addOne = lambda x : x + 1
addTen = lambda x : x + 10
invert = lambda x : 1.0 / x
add100 = lambda x : x + 100
    
    
callAll = Multicall( addOne, addTen, invert, add100 )

print callAll( 5 )
print callAll( 0 )

"""
prints:

[6, 15, 0.20000000000000001, 105]
[1, 10, None, None]
"""
ptmcg is offline   Reply With Quote