将函数及其参数传递给另一个函数 [英] passing functions and its arguments to another function

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

问题描述

我有树型的子函数:


  • 一个没有任何参数(参数),

  • 第二个参数

  • 第三个参数(元组)



我是试图将该函数及其参数传递给另一个函数,该函数将所有子函数的结果相加并返回总和值。
该函数中的参数应该是:每个子函数的名称作为位置参数(* args)和每个子函数的参数作为键值参数(* kvargs)。

示例

  def no_arg()
def one_arg(a)
def multiple_args(a,b,c,e,f)

#执行function_results_sum:
function_results_sum(
no_arg,one_arg,multiple_args,
one_arg = 23,
multiple_args =(1,2,3,4,5))

我到目前为止做了什么:

  def no_arg():
return 5

def ident(x):
return x

def mult(x,y):
return x * y

def function_results_sum(* args,** kwargs):
return no_arg()+ ident(kwargs [ident .__ name__])+ mult(* kwargs [mult .__ name__])

上面的代码将参数传递给每个子函数,但子函数名称重新编码。我想修改当前的代码,以便能够从* args获取函数名称。在下面,我写了一个伪代码,表达了我试图实现的更多内容:

  def function_results_sum(* args,** kwargs): 
for functionName in args:
result = sum(funcionName(kwargs))
返回结果

我已经花了整整一天的时间来解决这个问题,所以请不要写信给我说:使用google并不会伤害到它;)

;

解决方案

类似这样的工作: ():
返回5

def one_arg(x):
return x

def multiple_args(x,y):
return x * y

def function_results_sum(* args,** kwargs):
result = 0
用于args中的func:
result + = func(* kwargs [ func .__ name__])
返回结果

输出:

  function_results_sum(
no_arg,one_arg,multiple_args,
no_arg =(),
one_arg =(23,),
multiple_args =(1,5))

33

你所要求的唯一区别是你必须将args放在一个元组中,然后解包为参数以后再传入。



<如果你不想提供任何没有参数函数的东西,你可以仔细检查函数名是否在kwargs中:

  def function_results_sum(* args,** kwargs):
result = 0
for func in args:
if func .__ name__ i kwargs:
result + = func(* kwargs [func .__ name__])
else:
result + = func()
返回结果


I have tree types of sub-functions:

  • one without any parameters (arguments),
  • second with one parameter
  • third with multiple parameters (tuple)

I am trying to pass that functions and its arguments to another function which sum results of all sub-functions and return the sum value. Parameters in that function should be: names of each sub-function as position arguments (*args) and arguments of each subfunction as key-value arguments (*kvargs).

Example:

 def no_arg()
 def one_arg(a)
 def multiple_args(a, b, c, e, f)

 # execution of function_results_sum:
 function_results_sum(
     no_arg, one_arg, multiple_args,
     one_arg=23,
     multiple_args=(1, 2, 3, 4, 5))

What i have done so far:

def no_arg():
    return 5

def ident(x):
    return x

def mult(x, y):
    return x * y    

def function_results_sum(*args, **kwargs):
        return no_arg() + ident(kwargs[ident.__name__]) + mult(*kwargs[mult.__name__])   

The code above is passing arguments to each sub-function, but sub-function names are hardcoded. I would like to modify the current code to be able to get function names from *args. Below I wrote a pseudocode expressing more less what i am trying to achieve:

def function_results_sum(*args, **kwargs):
    for functionName in args:
        result = sum(funcionName(kwargs))
    return result

I have already spent all day struggling with that problem, so please don't write me that "using google doesn't hurt" ;)

解决方案

Something like this would work:

def no_arg():
    return 5

def one_arg(x):
    return x

def multiple_args(x, y):
    return x * y

def function_results_sum(*args, **kwargs):
    result = 0
    for func in args:
            result += func(*kwargs[func.__name__])
    return result

Output:

function_results_sum(
    no_arg, one_arg, multiple_args,
    no_arg=(),
    one_arg=(23, ),
    multiple_args=(1,5))

33

The only difference between what you are asking is that you have to put args in a tuple to then unpack as args to pass in later.

If you dont want to have to supply anything for no argument functions, you can double check if the func name is in kwargs:

def function_results_sum(*args, **kwargs):
    result = 0
    for func in args:
        if func.__name__ i kwargs:
            result += func(*kwargs[func.__name__])
        else:
            result += func()
    return result

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

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