Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40340
| References | <kgtbb9$3ps$1@tdi.cu.mi.it> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2013-03-02 10:22 -0700 |
| Subject | Re: Is it correct this way to inherit from a list? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2780.1362244998.2939.python-list@python.org> (permalink) |
On Sat, Mar 2, 2013 at 10:02 AM, gialloporpora <gialloporpora@gmail.com> wrote:
> Hi all,
> I would like to inherit from the list native class.
> really I expected that was possible to use native list method without
> redefining them, for example the __repr__ method.
>
> I don't know if i have made something wrong, this is my code (I obmit
> customized methods that I have added):
>
> from os.path import exists
>
> class vector(list):
> def __init__(self, *args):
> self._list = list(args)
So here you have a list subclass, but instead of taking advantage of
that is-a relationship, you're creating a secondary list from the
arguments and attaching it to self._list in a has-a relationship. The
net effect is that you actually have two separate list objects here,
with some methods operating on the list itself and some operating on
the attached list. Try this instead:
class Vector(list):
def __new__(cls, *args):
return super(Vector, cls).__new__(cls, args)
def __init__(self, *args):
super(Vector, self).__init__(args)
The __new__ method here will receive the args in the style that you
want, and then pass them up the inheritance chain to the superclass
constructor, which will then just do the right thing. The __init__
method is also overridden to match the modified argspec. The
super().__init__() call is included for completeness; AFAIK it doesn't
actually do anything.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is it correct this way to inherit from a list? gialloporpora <gialloporpora@gmail.com> - 2013-03-02 18:02 +0100
Re: Is it correct this way to inherit from a list? Peter Otten <__peter__@web.de> - 2013-03-02 18:19 +0100
Re: Is it correct this way to inherit from a list? Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-02 10:22 -0700
Re: Is it correct this way to inherit from a list? Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-02 10:26 -0700
Re: Is it correct this way to inherit from a list? gialloporpora <gialloporpora@gmail.com> - 2013-03-03 03:33 +0100
Re: Is it correct this way to inherit from a list? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-02 09:46 -0800
Re: Is it correct this way to inherit from a list? gialloporpora <gialloporpora@gmail.com> - 2013-03-03 03:30 +0100
Re: Is it correct this way to inherit from a list? Chris Angelico <rosuav@gmail.com> - 2013-03-03 14:18 +1100
Re: Is it correct this way to inherit from a list? "Colin J. Williams" <cjw@ncf.ca> - 2013-03-03 09:21 -0500
Re: Is it correct this way to inherit from a list? Jason Swails <jason.swails@gmail.com> - 2013-03-03 15:40 -0500
csiph-web