Passing all arguments of a function to another function

45,015

Solution 1

Explicit is better than implicit but if you really don't want to type a few characters:

def func1(a=1, b=2, c=3):
    func2(**locals())

locals() are all local variables, so you can't set any extra vars before calling func2 or they will get passed too.

Solution 2

Provided that the arguments to func1 are only keyword arguments, you could do this:

def func1(a=1, b=2, c=3):
    func2(**locals())

Solution 3

As others have said, using locals() might cause you to pass on more variables than intended, if func1() creates new variables before calling func2().

This is can be circumvented by calling locals() as the first thing, like so:

def func1(a=1, b=2,c=3):
    par = locals()

    d = par["a"] + par["b"]

    func2(**par)
Share:
45,015

Related videos on Youtube

roopesh
Author by

roopesh

I just love programming, and want to contribute back to the community.

Updated on November 21, 2020

Comments

  • roopesh
    roopesh over 3 years

    I want to pass all the arguments passed to a function(func1) as arguments to another function(func2) inside func1 This can be done with *args, *kwargs in the called func1 and passing them down to func2, but is there another way?

    Originally

    def func1(*args, **kwargs):
        func2(*args, **kwargs)
    

    but if my func1 signature is

    def func1(a=1, b=2, c=3):
    

    how do I send them all to func2, without using

    def func1(a=1, b=2, c=3):
        func2(a, b, c)
    

    Is there a way as in javascript callee.arguments?

    • Mark Elliot
      Mark Elliot about 14 years
    • jcao219
      jcao219 about 14 years
      I don't see how this could be useful...
    • roopesh
      roopesh about 14 years
      it's not all that useful, but sometimes I write a function with lot of args, and and bored of copying them over and over.
    • roopesh
      roopesh about 14 years
      @Mark no, not related, but thanks anyway.
    • b0fh
      b0fh about 11 years
      @jcao219 this is useful when writing generic decorators that do not care about function args
    • Marc
      Marc almost 8 years
      @jcao219 - or if you're extracting common functionality to another function, and you don't what to change the existing external calling interface
  • yantrab
    yantrab about 14 years
    This will have the flaw that if you've created local variables before calling func2, then they will also be passed to func2.
  • Bwmat
    Bwmat about 14 years
    maybe you could copy locals right at the beginning of the function?
  • roopesh
    roopesh about 14 years
    Now I remember reading about locals. I was thinking too much about vars() and couldn't remember locals(). Should have read the documents. I guess vars() can be used too.
  • Bhaskar
    Bhaskar almost 10 years
    This would not work for class functions as it will get multiple values for self
  • orome
    orome about 8 years
    @Bhaskar: Is there any way around that?
  • kuzzooroo
    kuzzooroo almost 8 years
    @Bhaskar, can you elaborate on the problem? I'm only seeing one copy of self, even when using multiple inheritance. What's the "this" that won't work, using locals() at all, or trying to save a copy of the result as a way to have a record after a local variable has been created?
  • Tom Swirly
    Tom Swirly over 7 years
    Ouch, I hate this one. The reason is that locals() is the wrong concept - you want to pass "all the parameters" not "all the local variables". All sorts of innocent changes - like adding a local variable before this line, or moving a function to be a method or vice versa - can break this code. More, the reader has to scratch their head to figure it out.
  • Nex
    Nex about 5 years
    Is there a lean general solution to pass mixed parameters from arguments and keyword arguments?
  • Hacker
    Hacker over 2 years
    Why only keyword arguments?