Detupleize a tuple for argument list

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

    #1

    Detupleize a tuple for argument list

    Hi,

    I want to give a tuple to a function where the function
    expects the respective tuple-size number of arguments.

    The following session illustrates what I want to do and
    the respective failure.

    Python 2.4.1 (#7, Aug 3 2005, 14:55:58)
    [GCC 3.3.1 (SuSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>def foo(a, b): print a, b
    ...
    >>t = (1, 2)
    >>def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b))
    ...
    >>foo(1, 2)
    a == 1, b == 2
    >>foo(t)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: foo() takes exactly 2 arguments (1 given)
    >>>
    One way to do what I want is--of course--to call
    foo(t[0], t[1]). My actual question is if there is a
    smarter way to do it.

    The situation for me is that I take the functions from
    a library that I cannot modify. On the other side in
    my code I use the tuples.


    Best wishes
    --
    Marco Wahl


  • Luke Plant

    #2
    Re: Detupleize a tuple for argument list

    Marco Wahl wrote:
    Hi,
    >
    I want to give a tuple to a function where the function
    expects the respective tuple-size number of arguments.
    ....
    One way to do what I want is--of course--to call
    foo(t[0], t[1]). My actual question is if there is a
    smarter way to do it.
    Yes, just this:

    foo(*t)

    Luke

    Comment

    • Steven D'Aprano

      #3
      Re: Detupleize a tuple for argument list

      On Wed, 05 Jul 2006 14:01:27 +0200, Marco Wahl wrote:
      Hi,
      >
      I want to give a tuple to a function where the function
      expects the respective tuple-size number of arguments.
      >
      The following session illustrates what I want to do and
      the respective failure.
      >
      Python 2.4.1 (#7, Aug 3 2005, 14:55:58)
      [GCC 3.3.1 (SuSE Linux)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>def foo(a, b): print a, b
      ...
      >>t = (1, 2)
      >>def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b))
      ...
      >>foo(1, 2)
      a == 1, b == 2
      >>foo(t)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: foo() takes exactly 2 arguments (1 given)
      Easy: foo(*t)

      --
      Steve.

      Comment

      • Marco Wahl

        #4
        Re: Detupleize a tuple for argument list

        Marco Wahl enlightened us with:
        >>foo(t)
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        TypeError: foo() takes exactly 2 arguments (1 given)
        >
        Call foo(*t)
        Thank you very much Luke Plant, Steven D'Aprano and Sybren Stuvel.

        This was exactly what I was looking for. I'm happy now. ;-)
        --
        Marco Wahl

        Comment

        • Bruno Desthuilliers

          #5
          Re: Detupleize a tuple for argument list

          Marco Wahl wrote:
          >>Marco Wahl enlightened us with:
          >>
          >>>>>>foo(t)
          >>>
          >>Traceback (most recent call last):
          >> File "<stdin>", line 1, in ?
          >>TypeError: foo() takes exactly 2 arguments (1 given)
          >>
          >>Call foo(*t)
          >
          >
          Thank you very much Luke Plant, Steven D'Aprano and Sybren Stuvel.
          >
          This was exactly what I was looking for. I'm happy now. ;-)
          FWIW, there's something similar for dicts and named args:
          >>def foo(a, b): print str(a), str(b)
          ....
          >>kwargs = {'a' : 1, 'b': 2}
          >>foo(**kwarg s)
          1 2
          >>>


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          Working...