Item 29: More Bound Method

class EventHandler(object):
    def __init__(self, name):
        self.name = name
    def test1(self, *args, **kwargs):
        print('test1:', self.name, *args, **kwargs)
    def test2(self, *args, **kwargs):
        print('test2:', self.name, *args, **kwargs)

# test3() is a normal function, not enclosed by class statement.
def test3(self, *args, **kwargs):
    print('test3:', self.name, *args, **kwargs)

def bind(receiver, method):
    return method.__get__(receiver, type(receiver))

objs = [EventHandler('object_' + str(i)) for i in range(5)]
methods = [EventHandler.test1, EventHandler.test2, test3]

callbacks = [bind(x, m) for m in methods for x in objs]

for i, callback in enumerate(callbacks):
    callback('invocation_' + str(i))