Decorator pattern for new-style classes ?

George Sakkis gsakkis at rutgers.edu
Sat Apr 23 03:58:42 EDT 2005


Searching the archives for something related to the title, I found a
few relevant threads (e.g. http://tinyurl.com/avyg6 or
http://tinyurl.com/b5b6v); however they don't seem to give a
satisfactory answer, so here it goes again: What's the equivalent
new-style Delegate class ?

class Delegate:
    def __init__(self, principal):
        self._principal = principal

    def __getattr__(self,name):
        return getattr(self._principal,name)

    # overriden methods follow here

This delegates both normal and __special__ methods; unfortunately for
new style classes __getattr__ is not called for missing special
methods. The workarounds I saw or can think of are so ugly that I
prefer to to use an old-style class after a long time :-/

George




More information about the Python-list mailing list