[Types-sig] Why have two hierarchies? (*EUREKA!*)

Fredrik Lundh fredrik@pythonware.com
Sun, 6 Dec 1998 14:36:25 +0100


>Could a kind soul please explain to me in a couple of sentences what
>delegation *is*? Thanks. "Design Patterns" is patiently waiting for me
>beside my bed, but I'd rather have someone just tell me... Yeh, I'm 100%
>unqualified to talk about these subjects. It's more fun that way...

 class MyClass:
    def __init__(self, someoneelse):
        self.someoneelse = someoneelse
    def mymethod(self, argument):
        # explicitly delegate mymethod
        return self.someoneelse.mymethod(argument)

or, in a more flexible fashion:

class MyClass:
    def __init__(self, someoneelse):
        self.someoneelse = someoneelse
    def __getattr__(self, attribute):
        # delegate everything not handled by this instance
        return getattr(self.someoneelse, attribute)

I'm using it all the time...

Cheers /F
fredrik@pythonware.com
http://www.pythonware.com