Is there a better way to code variable number of return arguments?
Phillip M. Feldman
pfeldman at verizon.net
Thu Oct 8 22:26:03 EDT 2009
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!
Jack Norton wrote:
> 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)
>
More information about the Python-list
mailing list