Is it possible to pass functions with arguments to another function in Python?
是否可能将带参数的函数传递给Python中的另一个函数?
Say for something like:
类似的说:
def perform(function):
return function()
But the functions to be passed will have arguments like:
但是将要通过的函数将具有如下参数:
action1()
action2(p)
action3(p,r)
6 个解决方案
#1
219
Do you mean this?
你的意思是这个吗?
def perform( fun, *args ):
fun( *args )
def action1( args ):
something
def action2( args ):
something
perform( action1 )
perform( action2, p )
perform( action3, p, r )
#2
92
This is what lambda is for:
这就是lambda表达式的作用:
def Perform(f):
f()
Perform(lambda: Action1())
Perform(lambda: Action2(p))
Perform(lambda: Action3(p, r))
#3
26
You can use the partial function from functools like so.
您可以使用像这样的函数工具的部分函数。
from functools import partial
def perform(f):
f()
perform(Action1)
perform(partial(Action2, p))
perform(partial(Action3, p, r))
Also works with keywords
也适用于关键字
perform(partial(Action4, param1=p))
#4
14
Use functools.partial, not lambdas! And ofc Perform is a useless function, you can pass around functions directly.
使用functools。部分,而不是λ!而ofc执行是一个无用的函数,你可以直接传递函数。
for func in [Action1, partial(Action2, p), partial(Action3, p, r)]:
func()
#5
6
(months later) a tiny real example where lambda is useful, partial not:
say you want various 1-dimensional cross-sections through a 2-dimensional function, like slices through a row of hills.quadf( x, f )
takes a 1-d f
and calls it for various x
.
To call it for vertical cuts at y = -1 0 1 and horizontal cuts at x = -1 0 1,
(几个月后)一个很小的真实示例,lambda是有用的,但不是部分有用的:例如,您希望通过一个二维函数来实现各种一维的横截面,就像在一排小山中切片一样。quadf(x, f)取1-d f,取不同的x,记作y = -1处的垂直切点,x = -1处的水平切点,
fx1 = quadf( x, lambda x: f( x, 1 ))
fx0 = quadf( x, lambda x: f( x, 0 ))
fx_1 = quadf( x, lambda x: f( x, -1 ))
fxy = parabola( y, fx_1, fx0, fx1 )
f_1y = quadf( y, lambda y: f( -1, y ))
f0y = quadf( y, lambda y: f( 0, y ))
f1y = quadf( y, lambda y: f( 1, y ))
fyx = parabola( x, f_1y, f0y, f1y )
As far as I know, partial
can't do this --
就我所知,部分不能这么做
quadf( y, partial( f, x=1 ))
TypeError: f() got multiple values for keyword argument 'x'
(How to add tags numpy, partial, lambda to this ?)
(如何向它添加标记numpy、partial和lambda ?)
#6
1
Here is a way to do it with a closure:
这里有一个结束的方法:
def generate_add_mult_func(func):
def function_generator(x):
return reduce(func,range(1,x))
return function_generator
def add(x,y):
return x+y
def mult(x,y):
return x*y
adding=generate_add_mult_func(add)
multiplying=generate_add_mult_func(mult)
print adding(10)
print multiplying(10)
#1
219
Do you mean this?
你的意思是这个吗?
def perform( fun, *args ):
fun( *args )
def action1( args ):
something
def action2( args ):
something
perform( action1 )
perform( action2, p )
perform( action3, p, r )
#2
92
This is what lambda is for:
这就是lambda表达式的作用:
def Perform(f):
f()
Perform(lambda: Action1())
Perform(lambda: Action2(p))
Perform(lambda: Action3(p, r))
#3
26
You can use the partial function from functools like so.
您可以使用像这样的函数工具的部分函数。
from functools import partial
def perform(f):
f()
perform(Action1)
perform(partial(Action2, p))
perform(partial(Action3, p, r))
Also works with keywords
也适用于关键字
perform(partial(Action4, param1=p))
#4
14
Use functools.partial, not lambdas! And ofc Perform is a useless function, you can pass around functions directly.
使用functools。部分,而不是λ!而ofc执行是一个无用的函数,你可以直接传递函数。
for func in [Action1, partial(Action2, p), partial(Action3, p, r)]:
func()
#5
6
(months later) a tiny real example where lambda is useful, partial not:
say you want various 1-dimensional cross-sections through a 2-dimensional function, like slices through a row of hills.quadf( x, f )
takes a 1-d f
and calls it for various x
.
To call it for vertical cuts at y = -1 0 1 and horizontal cuts at x = -1 0 1,
(几个月后)一个很小的真实示例,lambda是有用的,但不是部分有用的:例如,您希望通过一个二维函数来实现各种一维的横截面,就像在一排小山中切片一样。quadf(x, f)取1-d f,取不同的x,记作y = -1处的垂直切点,x = -1处的水平切点,
fx1 = quadf( x, lambda x: f( x, 1 ))
fx0 = quadf( x, lambda x: f( x, 0 ))
fx_1 = quadf( x, lambda x: f( x, -1 ))
fxy = parabola( y, fx_1, fx0, fx1 )
f_1y = quadf( y, lambda y: f( -1, y ))
f0y = quadf( y, lambda y: f( 0, y ))
f1y = quadf( y, lambda y: f( 1, y ))
fyx = parabola( x, f_1y, f0y, f1y )
As far as I know, partial
can't do this --
就我所知,部分不能这么做
quadf( y, partial( f, x=1 ))
TypeError: f() got multiple values for keyword argument 'x'
(How to add tags numpy, partial, lambda to this ?)
(如何向它添加标记numpy、partial和lambda ?)
#6
1
Here is a way to do it with a closure:
这里有一个结束的方法:
def generate_add_mult_func(func):
def function_generator(x):
return reduce(func,range(1,x))
return function_generator
def add(x,y):
return x+y
def mult(x,y):
return x*y
adding=generate_add_mult_func(add)
multiplying=generate_add_mult_func(mult)
print adding(10)
print multiplying(10)