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

Discussion in 'Python' started by Ethan Furman, Oct 8, 2009.

  1. Ethan Furman

    Ethan Furman Guest

    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~
     
    Ethan Furman, Oct 8, 2009
    #1
    1. Advertising

  2. Ethan Furman

    Paul Rubin Guest

    Ethan Furman <> 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.
     
    Paul Rubin, Oct 8, 2009
    #2
    1. Advertising

  3. Ethan Furman

    Ethan Furman Guest

    Paul Rubin wrote:
    > Ethan Furman <> 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~
     
    Ethan Furman, Oct 8, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    863
  2. Paul Rubin
    Replies:
    5
    Views:
    440
    Hendrik van Rooyen
    Aug 6, 2009
  3. Dr. Phillip M. Feldman
    Replies:
    1
    Views:
    393
    Paul Rubin
    Oct 8, 2009
  4. Navaneeth
    Replies:
    4
    Views:
    573
    Kenny McCormack
    Nov 20, 2010
  5. Replies:
    20
    Views:
    403
    Ethan Furman
    Nov 16, 2012
Loading...

Share This Page