Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'subject:Python': 0.06; 'variables': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'similar,': 0.09; 'yeah,': 0.09; 'def': 0.12; '(2,': 0.16; '(3,': 0.16; '-tkc': 0.16; '0))': 0.16; 'alpha,': 0.16; 'class:': 0.16; 'increment': 0.16; 'numpy': 0.16; 'pythonic': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'tuple': 0.16; 'umpteen': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'switch': 0.26; 'least': 0.26; 'header:X-Complaints-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; 'class': 0.32; 'another': 0.32; 'fri,': 0.33; 'updated': 0.34; 'sense': 0.34; 'skip:_ 10': 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'two': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'jason': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'back': 0.62; 'more': 0.64; 'holding': 0.65; 'temporary': 0.65; 'worth': 0.66; 'glad': 0.83; '10:32': 0.84; 'complexity': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Idiomatic Python for incrementing pairs Date: Sat, 08 Jun 2013 08:47:43 +0200 Organization: None References: <20130607213239.3e39a448@bigbox.christie.dr> <20130607231015.515b3105@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b88d.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370674072 news.xs4all.nl 15931 [2001:888:2000:d::a6]:33119 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47371 Tim Chase wrote: > 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 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.)