Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Is there a better way to code variable number of return arguments?

Reply
Thread Tools

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

 
 
Ethan Furman
Guest
Posts: n/a
 
      10-08-2009
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?


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~
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      10-08-2009
Ethan Furman <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Ethan Furman
Guest
Posts: n/a
 
      10-08-2009
Paul Rubin wrote:
> Ethan Furman <(E-Mail Removed)> 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~
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a better way to code variable number of return arguments? Dr. Phillip M. Feldman Python 1 10-08-2009 04:43 PM
Re: Parsing Binary Structures; Is there a better way / What is your way? Paul Rubin Python 5 08-06-2009 08:06 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
is there a better way to optimise this code Sid C Programming 24 08-27-2004 03:58 AM



Advertisments