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.
That is poor style. Just return the result as a list.
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
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
It kind of depends on how the caller of your function handles the return values.
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~
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.
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~
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)
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
Thanks!
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
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
Python is very clever:
>>> (a, b), c = (1, 2), 3
>>> a, b, c
(1, 2, 3)
:D
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