将一个函数的所有参数传递给另一个函数 [英] Passing all arguments of a function to another function

查看:43
本文介绍了将一个函数的所有参数传递给另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将传递给函数(func1)的所有参数作为参数传递给func1内的另一个函数(func2)这可以通过在被调用的 func1 中使用 *args, *kwargs 并将它们传递给 func2 来完成,但还有其他方法吗?>

原来

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

但是如果我的 func1 签名是

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

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

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

有没有 javascript callee.arguments 中的方法?

解决方案

显式优于隐式 但如果你真的不想输入几个字符:

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

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

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?

解决方案

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.

这篇关于将一个函数的所有参数传递给另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆