Is there a better way to code variable number of return arguments?
robert.kern at gmail.com
Fri Oct 9 04:34:33 CEST 2009
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.
> 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.
"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
More information about the Python-list