Python函数作为函数参数? [英] python function as a function argument?

查看:230
本文介绍了Python函数作为函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否Python函数是另一个函数的参数?
说:

 高清MYFUNC(anotherfunc,extraArgs):
    #运行anotherfunc,也传递从extraArgs的值给它
    通过

所以这基本上是两个问题:
1)它是允许的呢?
2)如果是,如何使用该函数的其他函数内?我需要使用EXEC()的eval()或者类似的东西?从来不需要惹他们。

BTW,extraArgs是anotherfunc的参数列表/元组。


解决方案

  

一个Python功能只是一个说法
  另一个函数?


 高清MYFUNC(anotherfunc,extraArgs):
    anotherfunc(* extraArgs)

要更具体...与各种参数...

 >>> DEF X(A,B):
...打印参数1%S参数2%的%(A,B)
...
>>> DEF Y(Z,T):
... Z(* T)
...
>>> Y(X,(你好,曼努埃尔))
参数1你好参数2曼努埃尔
>>>

Can a python function be an argument of another function? say:

def myfunc(anotherfunc, extraArgs):
    # run anotherfunc and also pass the values from extraArgs to it
    pass

So this is basically two questions: 1) is it allowed at all? 2) and if it is, how to use the function inside the other function? Would I need to use exec(), eval() or something like that? Never needed to mess with them.

BTW, extraArgs is a list/tuple of anotherfunc's arguments.

解决方案

Can a python function be an argument of another function?

Yes.

def myfunc(anotherfunc, extraArgs):
    anotherfunc(*extraArgs)

To be more specific ... with various arguments ...

>>> def x(a,b):
...     print "param 1 %s param 2 %s"%(a,b)
... 
>>> def y(z,t):
...     z(*t)
... 
>>> y(x,("hello","manuel"))
param 1 hello param 2 manuel
>>> 

这篇关于Python函数作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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