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

Discussion in 'Python' started by Ian Kelly, Mar 2, 2013.

  1. Ian Kelly

    Ian Kelly Guest

    On Sat, Mar 2, 2013 at 10:22 AM, Ian Kelly <> 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)
     
    Ian Kelly, Mar 2, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zhou Fan
    Replies:
    4
    Views:
    341
    Alf P. Steinbach
    Mar 23, 2006
  2. jonnytheclown

    Correct way to inherit from 3rd party class

    jonnytheclown, Feb 21, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    152
    Peter Scott
    Feb 22, 2005
  3. Peter Otten
    Replies:
    0
    Views:
    165
    Peter Otten
    Mar 2, 2013
  4. Ian Kelly
    Replies:
    0
    Views:
    129
    Ian Kelly
    Mar 2, 2013
  5. Rick Johnson
    Replies:
    3
    Views:
    123
    Jason Swails
    Mar 3, 2013
Loading...

Share This Page