Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Is it correct this way to inherit from a list?

Reply
Thread Tools

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

 
 
Ian Kelly
Guest
Posts: n/a
 
      03-02-2013
On Sat, Mar 2, 2013 at 10:22 AM, Ian Kelly <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is it correct this way to inherit from a list? Rick Johnson Python 3 03-03-2013 08:40 PM
Re: Is it correct this way to inherit from a list? Ian Kelly Python 0 03-02-2013 05:22 PM
Re: Is it correct this way to inherit from a list? Peter Otten Python 0 03-02-2013 05:19 PM
How to get the correct size of inherit class? Zhou Fan C++ 4 03-23-2006 01:30 PM
Correct way to inherit from 3rd party class jonnytheclown Perl Misc 6 02-22-2005 05:51 PM



Advertisments