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

Christian Heimes lists at cheimes.de
Thu Oct 8 18:52:37 CEST 2009


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




More information about the Python-list mailing list