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

John Skaller skaller@maxtal.com.au
Sun, 06 Dec 1998 21:31:26 +1000


At 04:27 6/12/98 +0100, Just van Rossum wrote:
>John Skaller wrote:
>
>>        Now .. doesn't this scheme look a bit like
>>supporting delegation ..??
>
>Could a kind soul please explain to me in a couple of sentences what
>delegation *is*? 

        It's what managers do: get someone else to do the work.
Here's an example:

        class worker:
                def typestuff(self,x): print x

        class boss:
                def __init__(self, aworker):
                        self.aworker = aworker
                def typestuff(self,x):
                        self.aworker.typestuff(x)

Basically, delegation is a more powerful version of inheritance,
in which methods are forwarded to other instances.

If Python had delegation, it would be much easier to 
implement say, a dictionary emulation, which used
an actual dictionary, but changed one or two of the methods.

At present, you'd have to forward every dictionary method call
with a wrapper (forwarding) function. This is a pain.
With 'automatic' delegation, the lookup system will
do it for you. "If you cannot find method f in my
method set, get one from the workers method set and use that".

Delegation is also what 'proxy' objects often do.
The technique is often prefered to inheritance in OO
and non-OO systems because it is more powerful:
one can dynamically change a worker object to another.
And share workers amoung bosses.

The extra power is also the main disadvantage of
delegation, especially in statically typed systems
where the static typing provides guarrantees
which are harder to enforce with delegation.

-------------------------------------------------------
John Skaller    email: skaller@maxtal.com.au
		http://www.maxtal.com.au/~skaller
		phone: 61-2-96600850
		snail: 10/1 Toxteth Rd, Glebe NSW 2037, Australia