On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase <python.l...@tim.thechases.com>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
-- http://mail.python.org/mailman/listinfo/python-list