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


Groups > comp.lang.python > #47379

Re: Idiomatic Python for incrementing pairs

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 <jason.swails@gmail.com>
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; '&gt;&gt;&gt;': 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 <kouk25$aq7$1@ger.gmane.org>
References <20130607213239.3e39a448@bigbox.christie.dr> <CAEk9e3pbF1CUo_k6YGt-40ydVL-zgBrvsLfqhf0zim+L=SZ4yw@mail.gmail.com> <20130607231015.515b3105@bigbox.christie.dr> <kouk25$aq7$1@ger.gmane.org>
Date Sat, 8 Jun 2013 07:37:25 -0400
Subject Re: Idiomatic Python for incrementing pairs
From Jason Swails <jason.swails@gmail.com>
To Peter Otten <__peter__@web.de>
Content-Type multipart/alternative; boundary=047d7b10cbf1b7596a04dea2f854
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2883.1370691449.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Idiomatic Python for incrementing pairs Jason Swails <jason.swails@gmail.com> - 2013-06-08 07:37 -0400

csiph-web