return multiple values from fuction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lupe

    return multiple values from fuction

    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

    ??


  • Jay O'Connor

    #2
    Re: return multiple values from fuction

    On Thu, 06 Nov 2003 19:58:55 +0000, Lupe <luis_@iname.co m> wrote:
    [color=blue]
    >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
    >
    >??
    >
    >[/color]
    def test ():
    ...
    return variableA, variableN


    varC, vardD = test()

    varC will contain variableA
    varD ill contain variableB


    Comment

    • Irmen de Jong

      #3
      Re: return multiple values from fuction

      Lupe wrote:[color=blue]
      > how can I assign the two return values to two distinct variables, as for ex.[/color]

      By using tuple unpacking:

      (varC, varD) = function()


      --Irmen

      Comment

      • Alex Martelli

        #4
        Re: return multiple values from fuction

        Lupe wrote:
        [color=blue]
        > hi, if someone can help me I would be grateful
        >
        > when I do
        >
        > def function[/color]

        this needs of course to be

        def function():
        [color=blue]
        > kjklj
        > llklç
        >
        > return variableA, variableB
        >
        > how can I assign the two return values to two distinct variables, as for
        > ex.
        >
        > varC = variableA
        > varD = variableB[/color]

        "just do it":

        varC, varD = function()


        Alex

        Comment

        • Irmen de Jong

          #5
          Re: return multiple values from fuction

          Alex Martelli wrote:
          [color=blue][color=green]
          >>how can I assign the two return values to two distinct variables, as for
          >>ex.
          >>
          >>varC = variableA
          >>varD = variableB[/color]
          >
          >
          > "just do it":
          >
          > varC, varD = function()[/color]

          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

          Comment

          • Lupe

            #6
            thank you

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

            It's... natural!

            Lupe

            Comment

            • Jay Dorsey

              #7
              Re: return multiple values from fuction

              Lupe wrote:[color=blue]
              > 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
              >[/color]
              [color=blue][color=green][color=darkred]
              >>> def a():[/color][/color][/color]
              .... return "value 1", "value 2"
              ....[color=blue][color=green][color=darkred]
              >>> c, d = a()
              >>> c[/color][/color][/color]
              'value 1'[color=blue][color=green][color=darkred]
              >>> d[/color][/color][/color]
              'value 2'[color=blue][color=green][color=darkred]
              >>> e = a()
              >>> e[/color][/color][/color]
              ('value 1', 'value 2')

              HTH

              Jay


              Comment

              Working...