[Tutor] Hiding Superclass Methods

Dave Kuhlman dkuhlman at rexx.com
Mon Oct 11 16:23:17 CEST 2010


On Mon, Oct 11, 2010 at 08:25:24AM +0200, Knacktus wrote:
> 
> Am 11.10.2010 06:24, schrieb Denis Gomes:
> >Hi Everyone,
> >
> >    I have a basic python question.  I am writing an n dimensional vector
> >class by inheriting from the builtin python list object.  I want to be
> >able to hide the parent object's methods in the derived class instances.
> Why inheriting then?
> Another approach to reusing exisiting methods is to wrap them into your 
> newly defined methods. Here you would not inherit from a list, but 
> create a list in your class (composition). E.g.
> 
> class MyVector(object):
> 
>     def __init__(self):
>         self.data = []
> 
>     def append_data(self, new_data):
>         self.data.append(new_data)
> 
> Actually I use this all the time. And I used this before I knew about 
> inheritance.

It's a good technique.  And, it's called delegation.  For more on
delegation, see this:

    http://en.wikipedia.org/wiki/Delegation_(programming)

Also see Alan's message in this same thread.

- Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list