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?

 
 
Rick Johnson
Guest
Posts: n/a
 
      03-02-2013
On Saturday, March 2, 2013 11:02:14 AM UTC-6, gialloporpora wrote:

> 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.
>
> [...]
>
> class vector(list):
> def __init__(self, *args):
> self._list = list(args)
> self._index = 0


Here is where you go wrong.

First of all why would you inherit from "list" and then create a new list as attribute, that seems a bit silly huh? If you want your custom list to "inherit" all the pre-defined methods of the python list type, then do so.

>>> class MyList(list):

pass

>>> ml = MyList()
>>> dir(ml)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> ml

[]
>>> ml.append('cat')
>>> ml

['cat']
>>> ml.extend(range(5))
>>> ml

['cat', 0, 1, 2, 3, 4]
>>> ml.sort()
>>> ml

[0, 1, 2, 3, 4, 'cat']
>>> isinstance(ml, list)

True

Quacks like a list to me. In this case you did not need to call the superclass constructor explicitly; for example.

class MyList(list):
def __init__(self):
list.__init__(self)

....is really a waste of time because you will not have any options to pass to the super.

>>> ml2 = MyList2()
>>> dir(ml2)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> ml2+[10,20,30]

[10, 20, 30]
>>> ml2

[]
>>> ml2.append('salt')
>>> ml2

['salt']
>>> isinstance(ml2, list)

True

If however you wanted to create a custom Tkinter widget, you would then need to pass the options from the derived class __init__ method into the superclass __init__ method, like this:

class MyButton(tk.Button):
def __init__(self, master, **kw):
tk.Button.__init__(self, master, **kw)

mb = MyButton(rootWindow, text='PushMe', command=helloButton)

What are you trying to achieve exactly?
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      03-03-2013
On Sun, Mar 3, 2013 at 1:30 PM, gialloporpora <(E-Mail Removed)> wrote:
> Risposta al messaggio di Rick Johnson :
>
>
>> What are you trying to achieve exactly?

>
>
>
> I would like to implement a class (vector) to works with vectors, for
> example using scalar multiplication:
> a*v = [a*v1, a*vn]
> and a dual class for dual vector (the only method that I'll change is the
> __str__ method to print it as colun.


Have you looked at NumPy? I haven't used it myself, but I understand
it's good for this sort of thing.

ChrisA
 
Reply With Quote
 
 
 
 
Colin J. Williams
Guest
Posts: n/a
 
      03-03-2013
On 02/03/2013 9:30 PM, gialloporpora wrote:
> Risposta al messaggio di Rick Johnson :
>
>> What are you trying to achieve exactly?

>
>
> I would like to implement a class (vector) to works with vectors, for
> example using scalar multiplication:
> a*v = [a*v1, a*vn]
> and a dual class for dual vector (the only method that I'll change is
> the __str__ method to print it as colun.
> Sandro

Numpy facilitates this sort of thing more efficiently than using a List.

Colin W.

 
Reply With Quote
 
Jason Swails
Guest
Posts: n/a
 
      03-03-2013
On Sun, Mar 3, 2013 at 9:21 AM, Colin J. Williams <(E-Mail Removed)> wrote:

> On 02/03/2013 9:30 PM, gialloporpora wrote:
>
>> Risposta al messaggio di Rick Johnson :
>>
>> What are you trying to achieve exactly?
>>>

>>
>>
>> I would like to implement a class (vector) to works with vectors, for
>> example using scalar multiplication:
>> a*v = [a*v1, a*vn]
>> and a dual class for dual vector (the only method that I'll change is
>> the __str__ method to print it as colun.
>> Sandro
>>

> Numpy facilitates this sort of thing more efficiently than using a List.
>


As a couple people have already pointed out, numpy is the way to go for
most scientific applications. You have also been given good advice
regarding 'properly' inheriting from 'list' by calling the list.__init__
function.

The only thing I'll add here is that you can inherit from array.array
instead of list if you want a 'truly' numeric-vector without introducing
numpy as a dependency. The advantage array.array has over list in this
instance is that it is type-restrictive for member data (it has to be
pre-declared and throws TypeError if you try to pass it a bad variable
type).

You can then proceed to override the __add__, __sub__, __mul__, and __div__
methods (and the in-place versions of these operators) to mimic vector
operations. (By default, __add__ appends the rhs to the lhs and returns a
copy of that for array.array and list).

You can avoid all this work, however, and just use numpy.ndarray instead .

Good luck,
Jason

 
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? Ian Kelly Python 0 03-02-2013 05:26 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