Is it correct this way to inherit from a list?

Ian Kelly ian.g.kelly at gmail.com
Sat Mar 2 18:26:44 CET 2013


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



More information about the Python-list mailing list