Automatic methods in new-style classes
Scott David Daniels
scott.daniels at acm.org
Fri Sep 29 17:38:05 EDT 2006
bertgoos at yahoo.com wrote:
> Hey, I want to send commands to a list of backends:
How about something like:
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
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