Path: csiph.com!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.06; 'expense': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'alpha,': 0.16; 'cc:name:python list': 0.16; 'imo.': 0.16; 'pythonic': 0.16; 'readability': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; '\xa0if': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'first,': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; 'message- id:@mail.gmail.com': 0.30; 'lines': 0.31; 'chase': 0.31; 'second,': 0.31; 'figure': 0.32; 'fri,': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'example,': 0.37; 'skip:o 20': 0.38; 'jason': 0.38; 'saves': 0.38; 'pm,': 0.38; 'most': 0.60; "you're": 0.61; 'holding': 0.65; 'temporary': 0.65; '10:32': 0.84; 'around,': 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=grH82Bl+9R+asdIwIVxRI+RIG9D7UMiwAVnk/UGqyho=; b=B/LUxC2bJexSDTyFpoo0mhs/TOnMICAJiODKyCyII7xTlKYgi2RL/w7/PnIX6AyTXa 0054G4eVXgbP9pPOZ21fepfQD0tk/3Ow7kUzMm1Vd4hJ1SXbCKS6H6iMRCqSHfil2RkU rY4SsXBqnaf9ZPU162HbyvkS+iGqnUY8cz/A22iorFGa9JmuyIyb1/ag+GzMToG3C9Zr q/+NbJ7PRtaU4LFoG9kSqG70rMc5WWRXiQ0Pe4D/99E7rf89p8myplmoQ+53/+ElOuto FG7wZh73GPD+Iq8ORNF4uR96iDqh8QD3FooOesYU1f2OPVDz+G1FGzh9/bhEOoDmTVoC Grng== MIME-Version: 1.0 X-Received: by 10.42.190.74 with SMTP id dh10mr606906icb.35.1370663182609; Fri, 07 Jun 2013 20:46:22 -0700 (PDT) In-Reply-To: <20130607213239.3e39a448@bigbox.christie.dr> References: <20130607213239.3e39a448@bigbox.christie.dr> Date: Fri, 7 Jun 2013 23:46:22 -0400 Subject: Re: Idiomatic Python for incrementing pairs From: Jason Swails To: Tim Chase Content-Type: multipart/alternative; boundary=20cf303dda621b368c04de9c64ec 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370663190 news.xs4all.nl 15877 [2001:888:2000:d::a6]:50768 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47362 --20cf303dda621b368c04de9c64ec Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase wrote: > Playing around, I've been trying to figure out the most pythonic way > of incrementing multiple values based on the return of a function. > Something like > > 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(...) ) ) It saves a couple lines of code, but at the expense of readability IMO. If I was reading the first, I'd know exactly what was happening immediately. If I was reading the second, it would take a bit to decipher. In this example, I don't see a better solution to what you're doing. All the best, Jason --20cf303dda621b368c04de9c64ec Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

<= /div>


On Fri, = Jun 7, 2013 at 10:32 PM, Tim Chase <python.list@tim.thechases.= com> wrote:
Playing around, I've been trying to figu= re out the most pythonic way
of incrementing multiple values based on the return of a function.
Something like

=A0 def calculate(params):
=A0 =A0 a =3D b =3D 0
=A0 =A0 if some_calculation(params):
=A0 =A0 =A0 a +=3D 1
=A0 =A0 if other_calculation(params):
=A0 =A0 =A0 b +=3D 1
=A0 =A0 return (a, b)

=A0 alpha =3D beta =3D 0
=A0 temp_a, temp_b =3D calculate(...)
=A0 alpha +=3D temp_a
=A0 beta +=3D temp_b

Is there a better way to do this without holding each temporary
result before using it to increment?

alpha =3D beta =3D 0
=
alpha, beta =3D (su= m(x) for x in zip( (alpha, beta), calculate(...) ) )

It saves a couple lines of c= ode, but at the expense of readability IMO. =A0If I was reading the first, = I'd know exactly what was happening immediately. =A0If I was reading th= e second, it would take a bit to decipher. =A0In this example, I don't = see a better solution to what you're doing.

All the best,
Jason
--20cf303dda621b368c04de9c64ec--