A tuple in order to pass returned values ?

Discussion in 'Python' started by faucheuse, Oct 5, 2011.

  1. faucheuse

    faucheuse Guest

    Hi, (new to python and first message here \o/)

    I was wondering something :
    when you do : return value1, value2, value3
    It returns a tuple.

    So if I want to pass these value to a function, the function have to
    look like :
    def function(self,(value1, value2, value3)) #self because i'm working
    with classes

    I tried it, and it works perfectly, but I was wondering if it's a good
    choice to do so, if there is a problem by coding like that.

    So my question is : Is there a problem doig so ?
    faucheuse, Oct 5, 2011
    #1
    1. Advertising

  2. Am 05.10.2011 15:33, schrieb faucheuse:
    > I was wondering something :
    > when you do : return value1, value2, value3
    > It returns a tuple.


    Right.

    > So if I want to pass these value to a function, the function have to
    > look like :
    > def function(self,(value1, value2, value3))

    [...]

    No, you don't have to, but you can:

    # example functions
    def fni():
    return 1, 2
    def fno(v1, v2):
    pass

    # store result in a tuple and unpack tuple for function call
    t = fni()
    fno(*fni)

    # store results in individual values
    v1, v2 = fni()
    fno(v1, v2)


    Note that the first variant can be written in a single line, too. A
    completely different alternative is passing a tuple to the function as a
    single parameter. You can then access the elements using normal tuple
    indexing. That said, I don't see a problem with your syntax, except that
    it's a bit unusual.


    Welcome to Python!

    Uli
    Ulrich Eckhardt, Oct 5, 2011
    #2
    1. Advertising

  3. Daniel Dorani, Oct 5, 2011
    #3
  4. faucheuse

    faucheuse Guest

    Thanks for the answer.
    faucheuse, Oct 5, 2011
    #4
  5. faucheuse

    Dave Angel Guest

    On 01/-10/-28163 02:59 PM, faucheuse wrote:
    > Hi, (new to python and first message here \o/)
    >
    > I was wondering something :
    > when you do : return value1, value2, value3
    > It returns a tuple.
    >
    > So if I want to pass these value to a function, the function have to
    > look like :
    > def function(self,(value1, value2, value3)) #self because i'm working
    > with classes
    >
    > I tried it, and it works perfectly, but I was wondering if it's a good
    > choice to do so, if there is a problem by coding like that.
    >
    > So my question is : Is there a problem doig so ?
    >

    In the abstract, no. There's no relationship between the two, except
    they happen to use the same name in their respective local namespaces.

    In practice, I wouldn't do it. If the three values really comprise one
    "thing" then it makes sense for a function to expect a single thing, and
    that thing needs a name. So I'd define the function as

    def function(self, mything):
    interesting, useful, related = mything
    ... work on them

    But it's certainly possible that the writer of the first function really
    had three independent things to return, and if the second method is
    expecting those same three independent things, he should define the
    method as:

    def function(self, this, that, theother):

    Python does have magic syntax to make this sort of thing easier to work
    with, using * and **. But I seldom use them unless forced to by
    meta-concerns, such as passing unknown arguments through one method to a
    method of a superclass.

    DaveA
    Dave Angel, Oct 5, 2011
    #5
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    792
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    923
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    602
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    793
    Antoon Pardon
    Mar 30, 2005
  5. faucheuse
    Replies:
    5
    Views:
    131
    Roy Smith
    Oct 10, 2011
Loading...

Share This Page