Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40341

Re: Is it correct this way to inherit from a list?

References <kgtbb9$3ps$1@tdi.cu.mi.it> <CALwzidm+TDHXRZwxHWo+Qbozw7SZJzqyX2KHGBF9Pgv+ytChUw@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-03-02 10:26 -0700
Subject Re: Is it correct this way to inherit from a list?
Newsgroups comp.lang.python
Message-ID <mailman.2781.1362245252.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Mar 2, 2013 at 10:22 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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.

I retract that.  On further testing, it is actually the __init__
method that initializes the list contents, not the __new__ method.  So
this is all you need:

class Vector(list):
    def __init__(self, *args):
        super(Vector, self).__init__(args)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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