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

2009-10-09 Thread Hendrik van Rooyen
On Thursday, 8 October 2009 18:41:31 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).

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

2009-10-08 Thread Christian Heimes
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

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

2009-10-08 Thread Jean-Michel Pichavant
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

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

2009-10-08 Thread Ethan Furman
Paul Rubin wrote: Ethan Furman et...@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

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

2009-10-08 Thread Jack Norton
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

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

2009-10-08 Thread Dr. Phillip M. Feldman
this message in context: http://www.nabble.com/Is-there-a-better-way-to-code-variable-number-of-return-arguments--tp25803294p25813206.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

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

2009-10-08 Thread Phillip M. Feldman
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

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

2009-10-08 Thread Robert Kern
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. 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):

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

2009-10-08 Thread Alan G Isaac
Dr. Phillip M. Feldman writes: 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

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

2009-10-08 Thread Simon Forman
On Thu, Oct 8, 2009 at 7:14 PM, Dr. Phillip M. Feldman pfeld...@verizon.net 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. Thanks! Python is very clever: (a, b), c = (1, 2), 3 a, b, c (1, 2, 3) :D --