Python—将一个函数传递给另一个函数
我正在使用python解决一个难题,根据我要解决的难题,我必须使用一组特殊的规则。在Python中如何将一个函数传递给另一个函数 范例Python—将一个函数传递给另一个函数,python,function,first-class-functions,Python,Function,First Class Functions,我正在使用python解决一个难题,根据我要解决的难题,我必须使用一组特殊的规则。在Python中如何将一个函数传递给另一个函数 范例 def Game(listA, listB, rules): if rules == True: do... else: do... def Rule1(v): if "variable_name1" in v: return False elif "variable_name2" in v:
def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if "variable_name1" in v:
return False
elif "variable_name2" in v:
return False
else:
return True
def Rule2(v):
if "variable_name3" and "variable_name4" in v:
return False
elif "variable_name4" and variable_name1 in v:
return False
else:
return True
这只是一个伪代码,因此并不具体,但我得到了要编译的代码,但我需要知道如何调用函数
Game
,以及它是否正确定义,因为规则将被切换为Rule1(v)
或Rule2(v)
,只需像任何其他参数一样传递它:
def a(x):
return "a(%s)" % (x,)
def b(f,x):
return f(x)
print b(a,10)
只需像任何其他参数一样传入它:
def a(x):
return "a(%s)" % (x,)
def b(f,x):
return f(x)
print b(a,10)
将函数视为程序中的变量,以便轻松地将其传递给其他函数:
def test ():
print "test was invoked"
def invoker(func):
func()
invoker(test) # prints test was invoked
将函数视为程序中的变量,以便轻松地将其传递给其他函数:
def test ():
print "test was invoked"
def invoker(func):
func()
invoker(test) # prints test was invoked
把它传过来,就像这样:
Game(list_a, list_b, Rule1)
然后您的游戏函数可能看起来像这样(仍然是伪代码):
把它传过来,就像这样:
Game(list_a, list_b, Rule1)
然后您的游戏函数可能看起来像这样(仍然是伪代码):
通过删除括号,函数名可以成为变量名(因此可以作为参数传递)。通过添加括号,变量名可以成为函数名 在您的示例中,将变量
规则
等同于一个函数,去掉括号和参数。然后在game()
函数中,使用括号和v
参数调用rules(v)
if puzzle == type1:
rules = Rule1
else:
rules = Rule2
def Game(listA, listB, rules):
if rules( v ) == True:
do...
else:
do...
通过删除括号,函数名可以成为变量名(因此可以作为参数传递)。通过添加括号,变量名可以成为函数名 在您的示例中,将变量
规则
等同于一个函数,去掉括号和参数。然后在game()
函数中,使用括号和v
参数调用rules(v)
if puzzle == type1:
rules = Rule1
else:
rules = Rule2
def Game(listA, listB, rules):
if rules( v ) == True:
do...
else:
do...
用于向函数传递函数和任何参数:
from typing import Callable
def looper(fn: Callable, n:int, *args, **kwargs):
"""
Call a function `n` times
Parameters
----------
fn: Callable
Function to be called.
n: int
Number of times to call `func`.
*args
Positional arguments to be passed to `func`.
**kwargs
Keyword arguments to be passed to `func`.
Example
-------
>>> def foo(a:Union[float, int], b:Union[float, int]):
... '''The function to pass'''
... print(a+b)
>>> looper(foo, 3, 2, b=4)
6
6
6
"""
for i in range(n):
fn(*args, **kwargs)
根据您正在执行的操作,可以定义或使用。将函数和任何参数传递给函数:
from typing import Callable
def looper(fn: Callable, n:int, *args, **kwargs):
"""
Call a function `n` times
Parameters
----------
fn: Callable
Function to be called.
n: int
Number of times to call `func`.
*args
Positional arguments to be passed to `func`.
**kwargs
Keyword arguments to be passed to `func`.
Example
-------
>>> def foo(a:Union[float, int], b:Union[float, int]):
... '''The function to pass'''
... print(a+b)
>>> looper(foo, 3, 2, b=4)
6
6
6
"""
for i in range(n):
fn(*args, **kwargs)
根据您正在执行的操作,定义或使用函数可能是有意义的。函数是python中的一流对象。您可以传递它们,将它们包含在dicts、列表等中。只是不要在函数名后面包含括号。例如,对于名为
myfunction
的函数:myfunction
表示函数本身,myfunction()
意味着调用函数并获取其返回值。如果函数是对象上的一个方法,并使用该对象的属性执行其工作,该怎么办?如果我传递的函数具有不同数量的输入参数,该怎么办?然后我应该使用关键字参数吗?如果这两个函数在单独的python文件中呢?函数是python中的一级对象。您可以传递它们,将它们包含在dicts、列表等中。只是不要在函数名后面包含括号。例如,对于名为myfunction
的函数:myfunction
表示函数本身,myfunction()
意味着调用函数并获取其返回值。如果函数是对象上的一个方法,并使用该对象的属性执行其工作,该怎么办?如果我传递的函数具有不同数量的输入参数,该怎么办?然后我应该使用关键字参数吗?如果这两个函数在单独的python文件中呢?是的。在上面的示例中,invoker
函数在调用函数时需要提供这些参数。Yes。在上面的示例中,invoker
函数在调用函数时需要提供这些参数。