Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'dependency': 0.09; 'impose': 0.09; 'override': 0.09; 'seemed': 0.09; 'though...': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '(2,': 0.16; '(3,': 0.16; '0))': 0.16; 'behave': 0.16; 'cc:name:python list': 0.16; 'class:': 0.16; 'mutable': 0.16; 'numpy': 0.16; 'to:addr:web.de': 0.16; 'vectors': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'users.': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'usually': 0.31; '(although': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'example,': 0.37; 'jason': 0.38; 'frequently': 0.68; 'complexity': 0.84; 'otten': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=4BykIXirTnF3/CfGEbAkI/1tXPrqaAv7ElILv7NocfA=; b=R2snN5zkEhvgisFxvVYKl0ODVZb0FFkfFvXj13+ushFOZyjcCqUivF5pAzF8J6M9Pg wugwVqRIUubgcc9GrCJ5oi38MD05VynELpkErbjdvFE4q3z26aUbo7tLpf+lIv7L5Gn9 tm9PxkFqKzaN8vSVHQXnEglQsi8xLgen8vWFMD3qgk+Dk/tRDBoTDkJ1vZdeVAjJpgmK H8RbUE6Z2MSOvVH6KdsycXT81S+MoIESU74A8I+oyXvVLcO4c2swCPOxEZistQSSdzAE KeyMScM4py22cg8AWDaxqw5Wbk3T4+fOa62ayXUyj53VWNC2R8tLBF7Fk0gdnzniELEJ fLRA== MIME-Version: 1.0 X-Received: by 10.50.32.7 with SMTP id e7mr773088igi.73.1370691445664; Sat, 08 Jun 2013 04:37:25 -0700 (PDT) In-Reply-To: References: <20130607213239.3e39a448@bigbox.christie.dr> <20130607231015.515b3105@bigbox.christie.dr> Date: Sat, 8 Jun 2013 07:37:25 -0400 Subject: Re: Idiomatic Python for incrementing pairs From: Jason Swails To: Peter Otten <__peter__@web.de> Content-Type: multipart/alternative; boundary=047d7b10cbf1b7596a04dea2f854 Cc: python list 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370691449 news.xs4all.nl 15978 [2001:888:2000:d::a6]:38862 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47379 --047d7b10cbf1b7596a04dea2f854 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jun 8, 2013 at 2:47 AM, Peter Otten <__peter__@web.de> wrote: > > You can hide the complexity in a custom class: > > >>> class T(tuple): > ... def __add__(self, other): > ... return T((a+b) for a, b in zip(self, other)) > ... > >>> t = T((0, 0)) > >>> for pair in [(1, 10), (2, 20), (3, 30)]: > ... t += pair > ... > >>> t > (6, 60) > > (If you are already using numpy you can do the above with a numpy.array > instead of writing your own T.) > I do this frequently when I want data structures that behave like vectors but don't want to impose the numpy dependency on users. (Although I usually inherit from a mutable sequence so I can override __iadd__ and __isub__). It seemed overkill for the provided example, though... All the best, Jason --047d7b10cbf1b7596a04dea2f854 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On Sa= t, Jun 8, 2013 at 2:47 AM, Peter Otten <__peter__@web.de> wro= te:
You can hide the complexity in a custom class:

>>> class T(tuple):
... =A0 =A0 def __add__(self, other):
... =A0 =A0 =A0 =A0 =A0 =A0 return T((a+b) for a, b in zip(self, other)) ...
>>> t =3D T((0, 0))
>>> for pair in [(1, 10), (2, 20), (3, 30)]:
... =A0 =A0 t +=3D pair
...
>>> t
(6, 60)

(If you are already using numpy you can do the above with a numpy.array
instead of writing your own T.)

I do this frequently when I w= ant data structures that behave like vectors but don't want to impose t= he numpy dependency on users. (Although I usually inherit from a mutable se= quence so I can override __iadd__ and __isub__). It seemed overkill for the= provided example, though...

All the best,
Jason
--047d7b10cbf1b7596a04dea2f854--