Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Automatic methods in new-style classes
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="George Sakkis, post: 1948894"] Or if you don't mind a more verbose API, you may use a decorator instead: # comment out if version<2.5 from functools import wraps def do_forall(attr): def deco(func): f_name = func.__name__ @wraps(func) # comment out if version<2.5 def wrapper(self,*args,**kwds): for b in getattr(self,attr): getattr(b,f_name)(*args,**kwds) return wrapper return deco #====== Example ================================= class Foo(object): def flush(self): print "Foo.flush" def draw(self, x, y): print "Foo.draw(%s,%s)" % (x,y) class Bar(object): def flush(self): print "Bar.flush" def draw(self, x, y): print "Bar.draw(%s,%s)" % (x,y) forall_backends = do_forall('backends') class MultiBackend(object): """Renders to multiple backends""" def __init__(self, backends): self.backends = backends @forall_backends def flush(self): 'Flush all backends' @forall_backends def draw(self, x, y): 'Draw point (x,y) on all backends' m = MultiBackend([Foo(),Bar()]) m.flush() m.draw(1,2) #====================================== HTH, George [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Automatic methods in new-style classes
Top