a76*_*664 103 python arguments function
Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
Run Code Online (Sandbox Code Playgroud)
所以这基本上是两个问题:
BTW,extraArgs是anotherfunc参数的列表/元组.
Man*_*res 122
Python函数可以作为另一个函数的参数吗?
是.
def myfunc(anotherfunc, extraArgs):
anotherfunc(*extraArgs)
Run Code Online (Sandbox Code Playgroud)
更具体......各种论点......
>>> 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
>>>
Run Code Online (Sandbox Code Playgroud)
sab*_*ujp 32
这是使用*args(以及可选)的另一种方式**kwargs:
def a(x, y):
print x, y
def b(other, function, *args, **kwargs):
function(*args, **kwargs)
print other
b('world', a, 'hello', 'dude')
Run Code Online (Sandbox Code Playgroud)
产量
hello dude
world
Run Code Online (Sandbox Code Playgroud)
需要注意的是function,*args,**kwargs必须按照这个顺序和必须的函数调用该函数的最后的参数.
Ign*_*ams 16
Python中的函数是一流的对象.但是你的功能定义有点偏.
def myfunc(anotherfunc, extraArgs, extraKwArgs):
return anotherfunc(*extraArgs, **extraKwArgs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176975 次 |
| 最近记录: |