Re: [Tutor] Hiding Superclass Methods
On 11/10/10 23:52, Steven D'Aprano wrote: On Tue, 12 Oct 2010 01:55:10 am Adam Bark wrote: On 11/10/10 15:29, Denis Gomes wrote: 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. Denis You can use __getslice__, __setslice__ etc. methods. They're detailed in the list docstrings. Here's an example of using __getslice__ __getslice __setslice__ and __delslice__ have been deprecated since version 2.0, seven versions ago. http://docs.python.org/reference/datamodel.html#object.__getslice__ You should use __getitem__ __setitem__ and __delitem__, and detect if your argument is an integer or a slice object, and behave accordingly. http://docs.python.org/reference/datamodel.html#object.__getitem__ Oh right, 2.6 doesn't say anything about it in the docstrings. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
On Tue, 12 Oct 2010 01:55:10 am Adam Bark wrote: > On 11/10/10 15:29, Denis Gomes wrote: > > 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. > > Denis > > You can use __getslice__, __setslice__ etc. methods. They're detailed > in the list docstrings. Here's an example of using __getslice__ __getslice __setslice__ and __delslice__ have been deprecated since version 2.0, seven versions ago. http://docs.python.org/reference/datamodel.html#object.__getslice__ You should use __getitem__ __setitem__ and __delitem__, and detect if your argument is an integer or a slice object, and behave accordingly. http://docs.python.org/reference/datamodel.html#object.__getitem__ -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
I understand where to go from here. Thanks to all who responded. Appreciate it. Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
On 11 October 2010 15:55, Adam Bark wrote: > You can use __getslice__, __setslice__ etc. methods. They're detailed in > the list docstrings. Here's an example of using __getslice__ > > >>> dir([]) > ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', > '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', > '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', > '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', > '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', > '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', > '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', > 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] > >>> print [].__getslice__.__doc__ > x.__getslice__(i, j) <==> x[i:j] > Just to add, you might try using "help()" on the various objects and methods (also objects) to see more contextual and/or formatted information, e.g. help([]) or help([].__getslice__) etc. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
On 11/10/10 15:29, Denis Gomes wrote: 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. Denis You can use __getslice__, __setslice__ etc. methods. They're detailed in the list docstrings. Here's an example of using __getslice__ >>> dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> print [].__getslice__.__doc__ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. >>> print [].__setslice__.__doc__ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. >>> class TestSlice(object): ... def __getslice__(self, i, j): ... print i, j ... >>> t = TestSlice() >>> t[2:3] 2 3 HTH, Adam. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
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. Denis On Mon, Oct 11, 2010 at 4:13 AM, Alan Gauld wrote: > > "Denis Gomes" 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@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
"Denis Gomes" 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hiding Superclass Methods
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. Inheritance makes sence, when you want to reuse (almost) all methods of the superclass, like in GUI toolkits, where you typically have a base widget as superclass of a all other widgets. HTH, Jan I know I can overload the method in the derived class and raise some sort of an implementation error but that is not what I had in mind. I am also not looking to use numpy. This is strictly for learning purposes. Is there a way to hide superclass methods in python? Thanks to all, Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hiding Superclass Methods
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. I know I can overload the method in the derived class and raise some sort of an implementation error but that is not what I had in mind. I am also not looking to use numpy. This is strictly for learning purposes. Is there a way to hide superclass methods in python? Thanks to all, Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor