import traceback from Delegator import Delegator class Callable(object): def __call__(self, *args, **kw): return args, kw def method(self, *args, **kw): return args, kw ##if __name__ == '__main__': ## delegator = Delegator(Callable()) ## ## print 'Normal method call:', ## print delegator.method('one', two='two') ## ## print '"Direct" call (the underlying object is callable):', ## try: ## print delegator('one', two='two') ## except: ## print ## traceback.print_exc() ## Note that I am avoiding the use of the words 'delegate' and 'delegator' !!! if __name__ == '__main__': instance_of_callable = Callable() filter = Delegator(instance_of_callable) ## filter is a pure DelegatorNode. It has no methods of its own. print 'Normal method call:', print filter.method('one', two='two') ## since filter has no 'method' method, it calls ## through to instance_of_callable. That's fine, but filter isn't useful. print '"Direct" call (the underlying object is callable):', try: print filter('one', two='two') ## As a callable, filter does nothing on its own. When it's called, it ## passes the call through to instance_of_callable (assuming that ## the the Delegator class has a __call__() method). ## What are you trying to accomplish by the call to filter()? ## Why not just call instance_of_callable()? ## This is not how filters are implemented. The chain of authority ## machinery is a mixin. ColorDelegator should be renamed Colorizer. ## Its main job is to colorize a Text instance. Relaying the calls ## insert() and delete() down the chain is necessary, but not central. ## For filter to be useful, it has to be a class with a DelegatorNode ## mixin. And when you want it to be callable, you need to define a ## __call__() method in the Filter class. That method will do something ## in filter, and then possibly pass some action down the chain. ## See example2.py. except: print traceback.print_exc()