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


Groups > comp.lang.python > #47364

Re: Idiomatic Python for incrementing pairs

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '(at': 0.04; 'subject:Python': 0.06; 'variables': 0.07; 'similar,': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; 'alpha,': 0.16; 'cc:name:python list': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'increment': 0.16; 'pythonic': 0.16; 'readable': 0.16; 'tuple': 0.16; 'umpteen': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'switch': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'tim': 0.29; "doesn't": 0.30; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'chase': 0.31; 'overhead': 0.31; 'another': 0.32; 'fri,': 0.33; 'updated': 0.34; 'sense': 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'charset:us-ascii': 0.36; 'two': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'jason': 0.38; 'pm,': 0.38; 'structure': 0.39; 'back': 0.62; 'more': 0.64; 'holding': 0.65; 'temporary': 0.65; 'to:addr:gmail.com': 0.65; 'worth': 0.66; 'glad': 0.83; '10:32': 0.84; 'received:50.22': 0.84; '2013': 0.98
Date Fri, 7 Jun 2013 23:10:15 -0500
From Tim Chase <python.list@tim.thechases.com>
To Jason Swails <jason.swails@gmail.com>
Subject Re: Idiomatic Python for incrementing pairs
In-Reply-To <CAEk9e3pbF1CUo_k6YGt-40ydVL-zgBrvsLfqhf0zim+L=SZ4yw@mail.gmail.com>
References <20130607213239.3e39a448@bigbox.christie.dr> <CAEk9e3pbF1CUo_k6YGt-40ydVL-zgBrvsLfqhf0zim+L=SZ4yw@mail.gmail.com>
X-Mailer Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
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.2874.1370664504.3114.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370664504 news.xs4all.nl 15898 [2001:888:2000:d::a6]:59418
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47364

Show key headers only | View raw


On 2013-06-07 23:46, Jason Swails wrote:
> On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase
> >   def calculate(params):
> >     a = b = 0
> >     if some_calculation(params):
> >       a += 1
> >     if other_calculation(params):
> >       b += 1
> >     return (a, b)
> >
> >   alpha = beta = 0
> >   temp_a, temp_b = calculate(...)
> >   alpha += temp_a
> >   beta += temp_b
> >
> > Is there a better way to do this without holding each temporary
> > result before using it to increment?
> 
> alpha = beta = 0
> alpha, beta = (sum(x) for x in zip( (alpha, beta),
> calculate(...) ) )

Yeah, I came up with something similar, but it was so opaque I fell
back to the more readable version I had above.  With only the two
variables to increment in this case, the overhead of duplicating code
doesn't seem as worth it as it might be if there were umpteen
counters being returned as a tuple and umpteen corresponding
variables being updated (at which point, it might make more sense to
switch to another data structure like a dict).  Ah well.  Glad to see
at least I'm not the only one stymied by trying to make it more
pythonic while at least keeping it readable.

-tkc

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


Thread

Re: Idiomatic Python for incrementing pairs Tim Chase <python.list@tim.thechases.com> - 2013-06-07 23:10 -0500

csiph-web