[英]send arguments for function arguments in python
I have a function which receives one function argument and that function argument also receives one argument but I don't know how to pass this argument to argument function in python. 我有一个函数接收一个函数参数,该函数参数也接收一个参数,但我不知道如何将此参数传递给python中的参数函数。 Here is the sample code:
以下是示例代码:
def pr(text):
print(text)
def frun(func):
func()
frun(pr)
The question is how can I pass text
argument in frun(pr)
? 问题是如何在
frun(pr)
传递text
参数? And please consider about using keyword arguments too. 请考虑使用关键字参数。
If you only have one argument, you can do: 如果你只有一个参数,你可以这样做:
def pr(text):
print(text)
def frun(func, text):
func(text)
frun(pr, "blah")
Otherwise, you can use variable arguments "magic" like: 否则,您可以使用变量参数“magic”,如:
def pr(text):
print(text)
def frun(func, *args):
func(*args)
frun(pr, "blah")
The latter is far less readable and could lead to weird errors (in this case, it won't even work with more than one parameter passed, because pr
expects only one positional argument), still is sometimes necessary. 后者的可读性要差得多,并且可能导致奇怪的错误(在这种情况下,它甚至不能使用多个参数传递,因为
pr
只需要一个位置参数),有时仍然是必要的。 For instance, when you don't know a priori how many parameters your function will need: 例如,当你不知道先验多少个参数的功能将需要:
def pr1(text):
print(text)
def pr2(text, note):
print(text, note)
def frun(func, *args):
func(*args)
frun(pr1, "foobarbaz")
frun(pr2, "blah", "IT WORKS!")
TL;DR: Partial Application TL; DR: 部分申请
The problem boils down to passing func
's argument to the scope you want to do the evaluation in. There are a few ways of doing this. 问题归结为将
func
的参数传递给您想要进行评估的范围。有几种方法可以做到这一点。
Explicit Parameterization 显式参数化
This get's unwieldy if you have numerous functions, or deep function call trees. 如果您有许多函数或深度函数调用树,这将变得笨拙。
def pr(text):
print(text)
def frun(func, *func_args):
func(*func_args)
frun(pr, *pr_args)
Deferred Execution with Lambda 使用Lambda延迟执行
Limitation: You need all the arguments for func
available at the same time, when you create the lambda
. 限制:当您创建
lambda
时,需要同时提供func
所有参数。
def pr(text):
print(text)
def frun(func):
func()
frun(lambda: pr(*pr_args))
Here the nice thing is you can do partial application as the object get's passed along. 这里的好处是你可以在传递对象时进行部分应用。 If you
partial
using named variables then you eliminate the ordering issue of the arguments. 如果您
partial
使用命名变量,那么您将消除参数的排序问题。
from functools import partial
def pr(text):
print(text)
def frun(func):
func()
frun(partial(pr, *pr_args))
You can using lambda
function to pass pr
function along with parameters to frun
function, without changing your code. 您可以使用
lambda
函数将pr
函数和参数传递给frun
函数,而无需更改代码。
def pr(text):
print(text)
# just another example to show how to pass more than one argument.
def pr2(text, note):
print(text, note)
def frun(func):
func()
frun(lambda: pr('Hello'))
frun(lambda: pr2('Hello', 'World'))
Hello
Hello World
Another way that may be useful for you is to return func
in frun
as callback
function, and then call it with parameters: 另一种可能对你有用的方法是将
frun
func
作为callback
函数返回,然后使用参数调用它:
def pr(text):
print(text)
# just another example to show how to pass more than one argument.
def pr2(text, note):
print(text, note)
def frun(func):
# do some stuff
return func
frun(pr)('Hello')
frun(pr2)('Hello', 'World')
Hello
Hello World
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.