[Tutor] Hiding Superclass Methods

Denis Gomes denisg640 at gmail.com
Mon Oct 11 16:29:35 CEST 2010

Thank you both for your responses.  I do have one other question if I use
the method both of you describe. How do I go about implementing slicing and
indexing for an object in python?  A list object innately has them and that
is really why I wanted to use it.  I would appreciate it if you can point me
to something.


On Mon, Oct 11, 2010 at 4:13 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> "Denis Gomes" <denisg640 at gmail.com> wrote
>  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.
> Doing so would break the Liskofff Substitution Principle which says
> you should be able to use your subclass anywhere that the parent
> class can be used. This is a very bad thing!
> If you want to expose a reduced set of operations, rather than an
> extended set, then inheritance is the wriong solution. You should
> consider using delegation instead. Create a list indside your class
> and forward any requests for the operations you do want to the
> list object.
> The only snag with delegation in this scenartio is that you have
> to write an awful lot of one-liner methods. To get round that
> you can use setattr() and getattr() combined with a list of allowed
> operation names. Check the operation is in the list and then
> forward the request by name. That can save a lot of coding!
> HTH,
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101011/fc31141e/attachment.html>

More information about the Tutor mailing list