return multiple values from fuction

Discussion in 'Python' started by Jay Dorsey, Nov 6, 2003.

  1. Jay Dorsey

    Jay Dorsey Guest

    Lupe wrote:
    > hi, if someone can help me I would be grateful
    >
    > when I do
    >
    > def function
    > kjklj
    > llklç
    >
    > return variableA, variableB
    >
    > how can I assign the two return values to two distinct variables, as for ex.
    >
    > varC = variableA
    > varD = variableB
    >


    >>> def a():

    .... return "value 1", "value 2"
    ....
    >>> c, d = a()
    >>> c

    'value 1'
    >>> d

    'value 2'
    >>> e = a()
    >>> e

    ('value 1', 'value 2')

    HTH

    Jay
     
    Jay Dorsey, Nov 6, 2003
    #1
    1. Advertising

  2. Jay Dorsey

    Lupe Guest

    hi, if someone can help me I would be grateful

    when I do

    def function
    kjklj
    llklç

    return variableA, variableB

    how can I assign the two return values to two distinct variables, as for ex.

    varC = variableA
    varD = variableB

    ??
     
    Lupe, Nov 6, 2003
    #2
    1. Advertising

  3. Jay Dorsey

    Jay O'Connor Guest

    On Thu, 06 Nov 2003 19:58:55 +0000, Lupe <> wrote:

    >hi, if someone can help me I would be grateful
    >
    >when I do
    >
    >def function
    > kjklj
    > llklç
    >
    > return variableA, variableB
    >
    >how can I assign the two return values to two distinct variables, as for ex.
    >
    >varC = variableA
    >varD = variableB
    >
    >??
    >
    >

    def test ():
    ...
    return variableA, variableN


    varC, vardD = test()

    varC will contain variableA
    varD ill contain variableB
     
    Jay O'Connor, Nov 6, 2003
    #3
  4. Lupe wrote:
    > how can I assign the two return values to two distinct variables, as for ex.


    By using tuple unpacking:

    (varC, varD) = function()


    --Irmen
     
    Irmen de Jong, Nov 6, 2003
    #4
  5. Lupe wrote:

    > hi, if someone can help me I would be grateful
    >
    > when I do
    >
    > def function


    this needs of course to be

    def function():

    > kjklj
    > llklç
    >
    > return variableA, variableB
    >
    > how can I assign the two return values to two distinct variables, as for
    > ex.
    >
    > varC = variableA
    > varD = variableB


    "just do it":

    varC, varD = function()


    Alex
     
    Alex Martelli, Nov 6, 2003
    #5
  6. Alex Martelli wrote:

    >>how can I assign the two return values to two distinct variables, as for
    >>ex.
    >>
    >>varC = variableA
    >>varD = variableB

    >
    >
    > "just do it":
    >
    > varC, varD = function()


    I like that comment... "just do it"...
    I find this is also true for most other things
    that you want to do in Python.

    "How do I create a mapping between a person's last
    name and the list of telephone numbers he/she can be
    reached at?" -- "umm.. just do it?"

    { "de Jong": ['234234', '34562363'] }

    or whatever ;-)

    --Irmen
     
    Irmen de Jong, Nov 6, 2003
    #6
  7. Jay Dorsey

    Lupe Guest

    thank you

    I'm starting with Python and I find it really great!

    It's... natural!

    Lupe
     
    Lupe, Nov 6, 2003
    #7
    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. mike
    Replies:
    3
    Views:
    373
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,366
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    393
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    316
    Alf P. Steinbach
    Jul 14, 2003
  5. Cat
    Replies:
    2
    Views:
    406
    llewelly
    Aug 20, 2003
Loading...

Share This Page