Automatic methods in new-style classes

Scott David Daniels scott.daniels at acm.org
Fri Sep 29 17:42:02 EDT 2006


Scott David Daniels wrote:
> 
> class Forwards(object):
> 
>     to_forward = set(['flush', 'read', 'write', 'close'])
> 
>     def __init__(self, backends):
>         self.backends = backends
> 
>     def forwarder(self, methodname):
>         def method(*args, **kwargs):
>             for b in self.backends:
>                 getattr(b, methodname)(*args, **kwargs)
>         return forwarder
^^^       return method  # thinko.
> 
>     def __getattr__(self, name):
>         if name in self.to_forward:
>             return self.forwarder(name)
>         raise AttributeError("%r object has no attribute %r"
>                              % (self.__class__.__name__, name))

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list