Python - 将函数传递给另一个函数 [英] Python - Passing a function into another function
本文介绍了Python - 将函数传递给另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用python解决一个难题,根据我解决的难题,我将不得不使用一组特殊的规则。如何将函数传递到Python中的另一个函数?
I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in 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:
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
只是一个伪代码,因此不具体,但我得到的代码进行编译,但我需要知道如何调用函数游戏
以及是否正确定义,因为规则将被切换对于 Rule1(v)
或 Rule2(v)
。
This is just a pseudo code and therefore not specific but I get the code to compile but I need to know how to call the function Game
and whether it's correctly defined since rules will be switched for either Rule1(v)
or Rule2(v)
.
推荐答案
只要像其他任何p一样传递它即可a(x):
返回a(%s)%(x,)$($)
Just pass it in like any other parameter:
def a(x):
return "a(%s)" % (x,)
def b(f,x):
return f(x)
print b(a,10)
这篇关于Python - 将函数传递给另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文