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).
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
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
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
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
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
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
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):
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
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
--
10 matches
Mail list logo