将函数的所有参数传递给另一个函数

时间:2022-06-05 16:02:53

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?

我希望将传递给函数(func1)的所有参数作为func1中另一个函数(func2)的参数传递。这可以通过调用func1中的* args,* kwargs并将它们传递给func2来完成,但还有另一种方法?

Originally

本来

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

but if my func1 signature is

但如果我的func1签名是

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

how do I send them all to func2, without using

如何在不使用的情况下将它们全部发送到func2

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

Is there a way as in javascript callee.arguments?

有没有像javascript callee.arguments那样的方法?

2 个解决方案

#1


38  

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.

locals()都是局部变量,因此在调用func2之前不能设置任何额外的变量,否则它们也会被传递。

#2


9  

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

如果func1的参数只是关键字参数,您可以这样做:

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

#1


38  

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.

locals()都是局部变量,因此在调用func2之前不能设置任何额外的变量,否则它们也会被传递。

#2


9  

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

如果func1的参数只是关键字参数,您可以这样做:

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