Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a better way to code variable number of return arguments?

0 views
Skip to first unread message

Dr. Phillip M. Feldman

unread,
Oct 8, 2009, 12:41:31 PM10/8/09
to pytho...@python.org

I currently have a function that uses a list internally but then returns the
list items as separate return
values as follows:

if len(result)==1: return result[0]
if len(result)==2: return result[0], result[1]

(and so on). Is there a cleaner way to accomplish the same thing?
--
View this message in context: http://www.nabble.com/Is-there-a-better-way-to-code-variable-number-of-return-arguments--tp25803294p25803294.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Paul Rubin

unread,
Oct 8, 2009, 12:43:43 PM10/8/09
to
"Dr. Phillip M. Feldman" <pfel...@verizon.net> writes:
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?

That is poor style. Just return the result as a list.

Christian Heimes

unread,
Oct 8, 2009, 12:52:37 PM10/8/09
to pytho...@python.org
Dr. Phillip M. Feldman schrieb:

> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?

You can simply "return result". If you want to make sure that you return
a copy of the internal list, do "return list(result)" or "return
tuple(result)".

Christian

Jean-Michel Pichavant

unread,
Oct 8, 2009, 12:55:39 PM10/8/09
to Dr. Phillip M. Feldman, pytho...@python.org
Dr. Phillip M. Feldman wrote:
> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?
>

return tuple(result)

But you down want to do that, cause the caller will have a hell of a job
getting your result. You may want to simply return the list itself.

JM

Simon Forman

unread,
Oct 8, 2009, 1:00:25 PM10/8/09
to Dr. Phillip M. Feldman, pytho...@python.org
On Thu, Oct 8, 2009 at 12:41 PM, Dr. Phillip M. Feldman
<pfel...@verizon.net> wrote:
>
> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on).  Is there a cleaner way to accomplish the same thing?

It kind of depends on how the caller of your function handles the return values.

Ethan Furman

unread,
Oct 8, 2009, 1:05:14 PM10/8/09
to pytho...@python.org
Dr. Phillip M. Feldman wrote:
> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?

To elaborate on Paul's answer, returning the list will also unpack it if
you have it set up that way. E.g.

def func(alist):
return alist

some_list = [1, 2]
this, that = func(alist)

At least, in 2.5.4 this works. :-)

Mind you, if you don't have the correct number of return names to match
the unpacking you'll get the normal errors from that.

Hope this helps!

~Ethan~

Paul Rubin

unread,
Oct 8, 2009, 1:48:07 PM10/8/09
to
Ethan Furman <et...@stoneleaf.us> writes:
> some_list = [1, 2]
> this, that = func(alist)
>
> At least, in 2.5.4 this works. :-)

But that fails if there are fewer than two elements in the list. It's
better to just make the logic either expect a list, or if it's
implementing something like an optional value, code it up explicitly.
You may even want to return two lists, the second one possibly empty.

Ethan Furman

unread,
Oct 8, 2009, 2:05:45 PM10/8/09
to pytho...@python.org

It also fails if there are more than two elements in the list, as the
rest of my post went on to say. I myself would generally not use such a
structure, but that doesn't mean the OP doesn't have a good use case for
it. Don't forget, his original question indicated that there could be
more than two return elements also.

~Ethan~

Jack Norton

unread,
Oct 8, 2009, 2:26:37 PM10/8/09
to Dr. Phillip M. Feldman, pytho...@python.org
Dr. Phillip M. Feldman wrote:
> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?
>
How about using yield and then iterate over the answer:

def some_fun():
\t for result in some_loopable_stuff:
\t\t yield result

Then call it thusly:

for i in some_fun()
result = i

-Jack (PS, sorry to the OP, you will get two of these -- I forgot to CC
the list)

Dr. Phillip M. Feldman

unread,
Oct 8, 2009, 7:14:29 PM10/8/09
to pytho...@python.org

I'm amazed that this works. I had not realized that

x,y= [3,4]

is equivalent to

x= 3; y= 4

Python is rather clever.

Thanks!

<snip>

To elaborate on Paul's answer, returning the list will also unpack it if
you have it set up that way. E.g.

def func(alist):
return alist

some_list = [1, 2]
this, that = func(alist)

At least, in 2.5.4 this works. :-)

Mind you, if you don't have the correct number of return names to match

the unpacking you'll get the normal errors from that.

Hope this helps!

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

--
View this message in context: http://www.nabble.com/Is-there-a-better-way-to-code-variable-number-of-return-arguments--tp25803294p25813206.html

Phillip M. Feldman

unread,
Oct 8, 2009, 10:26:03 PM10/8/09
to Jack Norton, pytho...@python.org
This is an interesting alternative. If one wants to generate everything
and return it at one shot, the list approach is better, but there are
situations where generating things incrementally is preferrable, e.g.,
because the caller doesn't know a priori how many things he wants. I
will try this out.

Thanks!

Robert Kern

unread,
Oct 8, 2009, 10:34:33 PM10/8/09
to pytho...@python.org
Dr. Phillip M. Feldman wrote:
> I'm amazed that this works. I had not realized that
>
> x,y= [3,4]
>
> is equivalent to
>
> x= 3; y= 4
>
> Python is rather clever.
>
> Thanks!
>
> <snip>
>
> To elaborate on Paul's answer, returning the list will also unpack it if
> you have it set up that way. E.g.
>
> def func(alist):
> return alist
>
> some_list = [1, 2]
> this, that = func(alist)
>
> At least, in 2.5.4 this works. :-)

In just about all Python versions for all sequence types, in fact.

> Mind you, if you don't have the correct number of return names to match
> the unpacking you'll get the normal errors from that.

Yes. This is why people are suggesting that you be consistent about what you
return. This is quite different from Matlab where the interpreter knows how many
return values the caller is expecting in order to overload functions, but I
think it makes for much more understandable code.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Alan G Isaac

unread,
Oct 8, 2009, 11:42:52 PM10/8/09
to
Dr. Phillip M. Feldman writes:
> I currently have a function that uses a list internally but then returns the
> list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?


The suggestions to return result
or if needed tuple(result) are good,
if a sequence is expected.

But perhaps better if each element has separate meaning:
return a defaultdict;
document the keys.
http://docs.python.org/library/collections.html#collections.defaultdict

Cheers,
Alan Isaac

Simon Forman

unread,
Oct 9, 2009, 12:15:59 AM10/9/09
to Dr. Phillip M. Feldman, pytho...@python.org
On Thu, Oct 8, 2009 at 7:14 PM, Dr. Phillip M. Feldman
<pfel...@verizon.net> wrote:
>
> I'm amazed that this works.  I had not realized that
>
> x,y= [3,4]
>
> is equivalent to
>
> x= 3; y= 4
>
> Python is rather clever.
>
> Thanks!
>

Python is very clever:

>>> (a, b), c = (1, 2), 3
>>> a, b, c
(1, 2, 3)

:D

Hendrik van Rooyen

unread,
Oct 9, 2009, 2:36:12 AM10/9/09
to Dr. Phillip M. Feldman, pytho...@python.org
On Thursday, 8 October 2009 18:41:31 Dr. Phillip M. Feldman wrote:
> I currently have a function that uses a list internally but then returns
> the list items as separate return
> values as follows:
>
> if len(result)==1: return result[0]
> if len(result)==2: return result[0], result[1]
>
> (and so on). Is there a cleaner way to accomplish the same thing?

Why do you not change the list into a tuple and return the tuple, and let
automatic unpacking handle it?

As I see it, the problem is not in the return, but in the call - how do you
know now, which of the following to write:

answer = thing(params)
answer0,answer1 = thing(params)
answer0,answer1,answer2 = thing(params)
answer0,answer1,answer2,answer3 = thing(params)

and so on...

probably best to write:

answers = thing(params)

for answer in answers:
do something with answer

- Hendrik

0 new messages