call f(a, *b) with f(*a, **b) ?

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

    call f(a, *b) with f(*a, **b) ?

    This question seems easy but I can't figure it out.
    Lets say there's a function:

    def f(a, *args):
    print a
    for b in args: print b

    and elsewhere in your program you have a list and a dict like this:
    args = [2, 3]
    kwargs = {'a':1}

    I'd like to get f() to print something like the following, but I can't
    figure out how.
    1
    2
  • inhahe

    #2
    Re: call f(a, *b) with f(*a, **b) ?


    "bukzor" <workitharder@g mail.comwrote in message
    news:55bc8846-107e-461a-89de-25c83cbcfa27@2g 2000hsn.googleg roups.com...
    This question seems easy but I can't figure it out.
    Lets say there's a function:
    >
    def f(a, *args):
    print a
    for b in args: print b
    >
    and elsewhere in your program you have a list and a dict like this:
    args = [2, 3]
    kwargs = {'a':1}
    >
    I'd like to get f() to print something like the following, but I can't
    figure out how.
    1
    2
    I think there's no 'standard' way to do this. but:

    import inspect
    f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args)

    i don't know if it works because i'm afraid to try it. if it doesn't the
    solution is something similar to that.


    Comment

    • inhahe

      #3
      Re: call f(a, *b) with f(*a, **b) ?

      >1
      >2
      actually, you don't want it to print 3 also? if not, then you would do
      f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args[:1])
      import inspect
      f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args)
      >

      Comment

      • bukzor

        #4
        Re: call f(a, *b) with f(*a, **b) ?

        On May 22, 5:39 pm, "inhahe" <inh...@gmail.c omwrote:
        1
        2
        >
        actually, you don't want it to print 3 also?  if not, then you would do
        f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args[:1])
        >
        import inspect
        f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args)
        >
        >
        No, that was a typo. Thanks tho.

        Comment

        • bukzor

          #5
          Re: call f(a, *b) with f(*a, **b) ?

          On May 22, 5:29 pm, "inhahe" <inh...@gmail.c omwrote:
          "bukzor" <workithar...@g mail.comwrote in message
          >
          news:55bc8846-107e-461a-89de-25c83cbcfa27@2g 2000hsn.googleg roups.com...
          >
          This question seems easy but I can't figure it out.
          Lets say there's a function:
          >
          def f(a, *args):
             print a
             for b in args: print b
          >
          and elsewhere in your program you have a list and a dict like this:
          args = [2, 3]
          kwargs = {'a':1}
          >
          I'd like to get f() to print something like the following, but I can't
          figure out how.
          1
          2
          >
          I think there's no 'standard' way to do this. but:
          >
          import inspect
          f(*map(kwargs.g et, inspect.getargs pec(f)[0])+args)
          >
          i don't know if it works because i'm afraid to try it.  if it doesn't the
          solution is something similar to that.
          That does, in fact work. Thanks! I'm a little sad that there's no
          builtin way to do it, owell.
          >>def f(a, *args):
          ... print a
          ... for b in args: print b
          ...
          >>import inspect
          >>a = [2,3]
          >>b = {'a':1}
          >>inspect.getar gspec(f)
          (['a'], 'args', None, None)
          >>map(b.get, inspect.getargs pec(f)[0])
          [1]
          >>map(b.get, inspect.getargs pec(f)[0]) + a
          [1, 2, 3]
          >>f(*map(b.ge t, inspect.getargs pec(f)[0]) + a)
          1
          2
          3

          Comment

          • Nick Craig-Wood

            #6
            Re: call f(a, *b) with f(*a, **b) ?

            bukzor <workitharder@g mail.comwrote:
            That does, in fact work. Thanks! I'm a little sad that there's no
            builtin way to do it, owell.
            >
            >def f(a, *args):
            ... print a
            ... for b in args: print b
            ...
            >import inspect
            >a = [2,3]
            >b = {'a':1}
            >inspect.getarg spec(f)
            (['a'], 'args', None, None)
            >map(b.get, inspect.getargs pec(f)[0])
            [1]
            >map(b.get, inspect.getargs pec(f)[0]) + a
            [1, 2, 3]
            >f(*map(b.get , inspect.getargs pec(f)[0]) + a)
            1
            2
            3
            If I saw that in my code I'd be wanting to get rid of it as soon as
            possible!

            I'd re-write f() to have all named arguments then the problem becomes
            easy and the answer pythonic (simple dictionary manipulation).. .

            So instead of f(a, *args) have f(a, list_of_args).

            The f(*args) syntax is tempting to use for a function which takes a
            variable number of arguments, but I usually find myself re-writing it
            to take a list because of exactly these sort of problems. In fact I'd
            be as bold to say that f(*args) is slightly un-pythonic and you should
            avoid as a user interface. It does have its uses when writing
            polymorphic code though.

            --
            Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

            Comment

            Working...