Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47361
| References | <20130607213239.3e39a448@bigbox.christie.dr> |
|---|---|
| Date | 2013-06-08 13:59 +1000 |
| Subject | Re: Idiomatic Python for incrementing pairs |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2872.1370663943.3114.python-list@python.org> (permalink) |
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 comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idiomatic Python for incrementing pairs Chris Angelico <rosuav@gmail.com> - 2013-06-08 13:59 +1000
csiph-web