Working with multiple return values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    Working with multiple return values

    In the simplest terms...
    I have a function that returns 3 values
    Code:
    >>> def sendnum():
    ... 	return 1,2,3
    and a function that takes 3 arguments.
    Code:
    >>> def receive(a=4,b=5,c=6):
    ... 	print a,b,c
    They of course do their individual jobs
    Code:
    >>> sendnum()
    (1, 2, 3)
    >>> receive(11,12,13)
    11 12 13
    Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of
    Code:
    >>> receive(sendnum())
    (1, 2, 3) 5 6
    I would get
    Code:
     >>> receive(sendnum())
    1 2 3
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by dshimer
    In the simplest terms...
    I have a function that returns 3 values
    Code:
    >>> def sendnum():
    ... 	return 1,2,3
    and a function that takes 3 arguments.
    Code:
    >>> def receive(a=4,b=5,c=6):
    ... 	print a,b,c
    They of course do their individual jobs
    Code:
    >>> sendnum()
    (1, 2, 3)
    >>> receive(11,12,13)
    11 12 13
    Is there any way to code this so that the sending function doesn't send it as a list, or that the receiving function sees it as 3 different values? So instead of
    Code:
    >>> receive(sendnum())
    (1, 2, 3) 5 6
    I would get
    Code:
     >>> receive(sendnum())
    1 2 3
    [code=Python]>>> receive(*sendnu m())
    1 2 3
    >>> [/code]

    Comment

    • dshimer
      Recognized Expert New Member
      • Dec 2006
      • 136

      #3
      Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

      Thanks

      Originally posted by bvdet
      [code=Python]>>> receive(*sendnu m())
      1 2 3
      >>> [/code]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by dshimer
        Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

        Thanks
        A function can accept a variable number of arguments if an asterisk (*) precedes the last argument in an argument list:[code=Python]>>> def sample(s, *args):
        ... print s
        ... for item in args:
        ... print item
        ...
        >>> s = 'A string'
        >>> sample(s, 'This', 'is', 'a', 'test')
        A string
        This
        is
        a
        test
        >>> aList = ('This', 'is', 'a', 'test')
        >>> sample(s, aList)
        A string
        ('This', 'is', 'a', 'test')
        >>> sample(s, *aList)
        A string
        This
        is
        a
        test
        >>> [/code]A function can also accept a variable number of keyword arguments if '**' precedes the last argument in an argument list:[code=Python]>>> def samplekw(**karg s):
        ... for key in kargs:
        ... print key, kargs[key]
        ...
        >>> samplekw(**{'ke y1': 1, 'key2': 100})
        key2 100
        key1 1
        >>> samplekw(key1=1 , key2=100)
        key2 100
        key1 1
        >>> [/code]Combined:[code=Python]>>> def sample(s, *args, **kargs):
        ... print s
        ... for item in args:
        ... print item
        ... for key in kargs:
        ... print key, kargs[key]
        ...
        >>> sample(s, *aList, **{'key1': 1, 'key2': 100})
        A string
        This
        is
        a
        test
        key2 100
        key1 1
        >>> [/code]

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by dshimer
          Fantastic, what is terminology for this, so I can study up on it? I have tried it in an system which embeds python to access CAD functions, so I have no control over the supplied objects. This works perfect.

          Thanks
          Mark Lutz calls it "varargs" on p 338 of
          Originally posted by Learning Python
          7. More argument matching examples. Here is the sort of interaction you should get, along with comments that explain the matching that goes on:[CODE=python]
          def f1(a, b): print a, b # normal args

          def f2(a, *b): print a, b # positional varargs

          f3(a, **b): print a, b # keyword varargs

          f4(a, *b, **c): print a, b, c # mixed modes

          f5(a, b=2, c=3): print a, b, c # defaults

          f6(a, b=2, *c): print a, b, c # defaults + positional varargs
          % python
          >>> f1(1, 2) # matched by position (order matters)
          1 2
          >>> f1(b=2, a=1) # matched by name (order doesn't matter)
          1 2
          >>> f2(1, 2, 3) # extra positionals collected in a tuple
          1 (2, 3)
          >>> f3(1, x=2, y=3) # extra keywords collected in a dictionary
          1 {'x': 2, 'y': 3}
          >>> f4(1, 2, 3, x=2, y=3) # extra of both kinds
          1 (2, 3) {'x': 2, 'y': 3}[/CODE]

          Comment

          Working...