Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47361 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2013-06-08 13:59 +1000 |
| Last post | 2013-06-08 13:59 +1000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Idiomatic Python for incrementing pairs Chris Angelico <rosuav@gmail.com> - 2013-06-08 13:59 +1000
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-06-08 13:59 +1000 |
| Subject | Re: Idiomatic Python for incrementing pairs |
| Message-ID | <mailman.2872.1370663943.3114.python-list@python.org> |
On Sat, Jun 8, 2013 at 12:32 PM, Tim Chase
<python.list@tim.thechases.com> wrote:
> 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?
Can you pass the function a list with the appropriate values in them,
and have it return them via that?
def calculate(params, sums):
if some_calculation(params):
sums[0] += 1
if other_calculation(params):
sums[1] += 1
sums = [0, 0]
calculate(..., sums)
Or use a dictionary if you'd rather they have names, either way.
ChrisA
Back to top | Article view | comp.lang.python
csiph-web