<div dir="ltr"><div class="gmail_default" style="color:#000000"><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com" target="_blank">python.list@tim.thechases.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Playing around, I've been trying to figure out the most pythonic way<br>
of incrementing multiple values based on the return of a function.<br>
Something like<br>
<br>
def calculate(params):<br>
a = b = 0<br>
if some_calculation(params):<br>
a += 1<br>
if other_calculation(params):<br>
b += 1<br>
return (a, b)<br>
<br>
alpha = beta = 0<br>
temp_a, temp_b = calculate(...)<br>
alpha += temp_a<br>
beta += temp_b<br>
<br>
Is there a better way to do this without holding each temporary<br>
result before using it to increment?<br></blockquote><div><br></div><div style="color:rgb(0,0,0)" class="gmail_default">alpha = beta = 0</div><div style="color:rgb(0,0,0)" class="gmail_default">alpha, beta = (sum(x) for x in zip( (alpha, beta), calculate(...) ) )</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">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.</div>
<div style="color:rgb(0,0,0)" class="gmail_default"><br></div><div style="color:rgb(0,0,0)" class="gmail_default">All the best,</div><div style="color:rgb(0,0,0)" class="gmail_default">Jason</div></div></div></div>