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

Ethan Furman ethan at stoneleaf.us
Thu Oct 8 14:05:45 EDT 2009


Paul Rubin wrote:
> Ethan Furman <ethan at 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.

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~



More information about the Python-list mailing list