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


Groups > comp.lang.python > #40421

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

References <kgtbb9$3ps$1@tdi.cu.mi.it> <1409c13b-60a6-4e87-a58c-52d5740e74d5@googlegroups.com> <kguckk$sf4$1@tdi.cu.mi.it> <kgvm96$94n$1@theodyn.ncf.ca>
Date 2013-03-03 15:40 -0500
Subject Re: Is it correct this way to inherit from a list?
From Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2826.1362343256.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Sun, Mar 3, 2013 at 9:21 AM, Colin J. Williams <cjw@ncf.ca> 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

Back to comp.lang.python | Previous | NextPrevious 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