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
这只是一个伪代码,因此不是特定的,但我得到了要编译的代码,但我需要知道如何调用函数 Game
以及它是否正确定义,因为规则将切换为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)
.
推荐答案
就像传入任何其他参数一样:
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屋!
查看全文