Calling a function with unknown arguments?

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

    #1

    Calling a function with unknown arguments?

    I can get a list of a function's arguments.
    >>def bob(a, b):
    .... return a+b
    ....
    >>bob.func_code .co_varnames
    ('a', 'b')

    Let's say that I also have a dictionary of possible arguments for this
    function.
    >>possible = {'a':10, 'b':5, 'c':-3}
    How do I proceed to call bob(a=10, b=5) with this information?

    Thanks!

  • Grant Edwards

    #2
    Re: Calling a function with unknown arguments?

    On 2007-03-07, Rob <crowell@mit.ed uwrote:
    I can get a list of a function's arguments.
    >>>def bob(a, b):
    ... return a+b
    ...
    >>>bob.func_cod e.co_varnames
    ('a', 'b')
    >
    Let's say that I also have a dictionary of possible arguments for this
    function.
    >>>possible = {'a':10, 'b':5, 'c':-3}
    >
    How do I proceed to call bob(a=10, b=5) with this information?
    bob(a=possible['a'],b=possible['b'])

    --
    Grant Edwards grante Yow! BRILL CREAM is
    at CREAM O' WHEAT in another
    visi.com DIMENSION...

    Comment

    • Gabriel Genellina

      #3
      Re: Calling a function with unknown arguments?

      En Wed, 07 Mar 2007 19:55:08 -0300, Rob <crowell@mit.ed uescribió:
      I can get a list of a function's arguments.
      >>>def bob(a, b):
      ... return a+b
      ...
      >>>bob.func_cod e.co_varnames
      ('a', 'b')
      >
      Let's say that I also have a dictionary of possible arguments for this
      function.
      >>>possible = {'a':10, 'b':5, 'c':-3}
      >
      How do I proceed to call bob(a=10, b=5) with this information?
      You're almost done. Notice that co_varnames includes local variables too;
      only the first co_argcount are positional arguments.
      If you only want to deal with positional arguments (no *args, no **kw):

      pydef bob(a,b):
      .... c = a+b
      .... return c
      ....
      pybob.func_code .co_varnames
      ('a', 'b', 'c')
      pybob.func_code .co_argcount
      2
      pyargs = dict((name, possible[name]) for name in
      bob.func_code.c o_varnames[:bo
      b.func_code.co_ argcount] if name in possible)
      pyargs
      {'a': 10, 'b': 5}
      pybob(**args)
      15

      It can handle missing arguments (if they have a default value, of course):

      pydef bob(a, k=3):
      .... return a+k
      ....
      pyargs = dict((name, possible[name]) for name in
      bob.func_code.c o_varnames[:bo
      b.func_code.co_ argcount] if name in possible)
      pyargs
      {'a': 10}
      pybob(**args)
      13

      --
      Gabriel Genellina

      Comment

      • Tombo

        #4
        Re: Calling a function with unknown arguments?

        On Mar 7, 10:55 pm, "Rob" <crow...@mit.ed uwrote:
        I can get a list of a function's arguments.>>def bob(a, b):
        >
        ... return a+b
        ...>>bob.func_c ode.co_varnames
        >
        ('a', 'b')
        >
        Let's say that I also have a dictionary of possible arguments for this
        function.
        >
        >possible = {'a':10, 'b':5, 'c':-3}
        >
        How do I proceed to call bob(a=10, b=5) with this information?
        >
        Thanks!
        You can do this with a list comprehension:
        bob(*[possible[k] for k in bob2.func_code. co_varnames])

        The LC creates a list of arg values in the same order as the var names
        for the function. Putting a * before the list when you call bob()
        turns that list into it's arguments.


        Alternativly (and this may not be useful) you can call a function with
        a dictionary of arguments:

        def bob(**kwargs):
        print kwargs

        possible = {'a' : 10, 'b':5, 'c':-3}

        >>bob(**possibl e)
        {'a': 10, 'c': -3, 'b': 5}
        >>>
        Tom

        Comment

        • Rob

          #5
          Re: Calling a function with unknown arguments?

          You can do this with a list comprehension:
          bob(*[possible[k] for k in bob2.func_code. co_varnames])
          >
          The LC creates a list of arg values in the same order as the var names
          for the function. Putting a * before the list when you call bob()
          turns that list into it's arguments.
          Thanks Tom! This turns out to be exactly what I needed.

          Comment

          Working...