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).  Is there a cleaner way to accomplish the same thing?

Why do you not change the list into a tuple and return the tuple, and let 
automatic unpacking handle it?

As I see it, the problem is not in the return, but in the call - how do you 
know now, which of the following to write:

answer = thing(params)
answer0,answer1 = thing(params)
answer0,answer1,answer2 = thing(params)
answer0,answer1,answer2,answer3 = thing(params)

and so on...

probably best to write:

answers = thing(params)

for answer in answers:
do something with answer

- Hendrik

-- 
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 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 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

-- 
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 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 same thing?
  


return tuple(result)

But you down want to do that, cause the caller will have a hell of a job 
getting your result. You may want to simply return the list itself.


JM
--
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 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 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~
--
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 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 same thing?
  

How about using yield and then iterate over the answer:

def some_fun():
\tfor 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)

--
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 Dr. Phillip M. Feldman

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):
return alist

some_list = [1, 2]
this, that = func(alist)

At least, in 2.5.4 this works.  :-)

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.

Hope this helps!

~Ethan~
-- 
http://mail.python.org/mailman/listinfo/python-list



-- 
View 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 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():
\tfor 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)




--
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 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):
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.


--
Robert Kern

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

--
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 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 same thing?



The suggestions to return result
or if needed tuple(result) are good,
if a sequence is expected.

But perhaps better if each element has separate meaning:
return a defaultdict;
document the keys.
http://docs.python.org/library/collections.html#collections.defaultdict

Cheers,
Alan Isaac
--
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 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
-- 
http://mail.python.org/mailman/listinfo/python-list