Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'override': 0.07; '(it': 0.09; 'facilitates': 0.09; 'list).': 0.09; 'throws': 0.09; '(the': 0.15; 'appends': 0.16; 'exactly?': 0.16; 'in-place': 0.16; 'lhs': 0.16; 'luck,': 0.16; 'numpy': 0.16; 'rhs': 0.16; 'sandro': 0.16; 'to:name:python list': 0.16; '\xa0you': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'pointed': 0.17; '>>>': 0.18; 'variable': 0.20; 'versions': 0.20; 'sort': 0.21; 'trying': 0.21; '(by': 0.22; 'default,': 0.22; 'operations.': 0.22; 'work,': 0.22; 'example': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'efficiently': 0.29; 'vector': 0.29; 'class': 0.29; '(and': 0.32; 'implement': 0.32; 'johnson': 0.32; 'print': 0.32; 'function.': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'jason': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'list.': 0.35; 'add': 0.36; 'scientific': 0.36; 'method': 0.36; "i'll": 0.36; 'bad': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'advice': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'most': 0.61; 'skip:n 10': 0.63; 'more': 0.63; 'here': 0.65; 'subject:this': 0.84; '2013': 0.84; 'rick': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=k5UXZLpi7jGkDb+Wfxz6GY7LsoWXdeZGbWkssU2H2Xg=; b=OZYVErVyYSvtnT3wZgnwQZ8hfEkR9F3cPFkEEn6c5esNC2qdZbTkEENnzkO0NQwkVm DqTV4O7bMTmMefqxOSeDLzNsdk/JEEn2PZedkl11W5MgIFnctDCs37kXJI1S5LfCrb+K MTeRWbnzVryYXRjVa32794QKwocrcm/Tht0jmeO9+r8Ek/RjHCZa7uvF7J0u2Z/7+85F jcvV+9HfocrEaYp+sPW7CLqt7Fypxn9pdYGs/5kHGbF/tUEzS8vTJNMej67GiMQ/O+5f IgOWeblYiPBX98vpzCyQeqUN3/dgCOWbbCr7XO5sJIsYiWELg/0U63arAaZ/GCqy3/gv 7cKQ== MIME-Version: 1.0 X-Received: by 10.50.190.233 with SMTP id gt9mr761885igc.80.1362343248464; Sun, 03 Mar 2013 12:40:48 -0800 (PST) In-Reply-To: References: <1409c13b-60a6-4e87-a58c-52d5740e74d5@googlegroups.com> Date: Sun, 3 Mar 2013 15:40:48 -0500 Subject: Re: Is it correct this way to inherit from a list? From: Jason Swails To: python list Content-Type: multipart/alternative; boundary=14dae934117763134104d70b41bd X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 93 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362343256 news.xs4all.nl 6965 [2001:888:2000:d::a6]:58144 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40421 --14dae934117763134104d70b41bd Content-Type: text/plain; charset=ISO-8859-1 On Sun, Mar 3, 2013 at 9:21 AM, Colin J. Williams 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 --14dae934117763134104d70b41bd Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Sun, M= ar 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 =3D [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 o= ut, numpy is the way to go for most scientific applications. =A0You have al= so 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 inheri= t from array.array instead of list if you want a 'truly' numeric-ve= ctor without introducing numpy as a dependency. =A0The advantage array.arra= y 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. =A0(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 nump= y.ndarray instead ;).

Good luck,
Jason
--14dae934117763134104d70b41bd--