[英]How do I make a function return another function
How do I make a function that returns another function, but without an if statement?如何制作返回另一个 function 但没有 if 语句的 function?
For example:例如:
def mainFunction():
if addRequirement(10) == False: return
def addRequirement(r):
if r > 0: return True
else: return False
The above, except without the 'if xyz == False: return' part, just 'addRequirement(10)'以上,除了没有 'if xyz == False: return' 部分,只是 'addRequirement(10)'
Thanks,谢谢,
EDIT 1:编辑1:
I should have mentioned that I'm writing a discord bot, and that the bot's command has a requirement.我应该提到我正在编写一个 discord 机器人,并且机器人的命令有一个要求。 The addRequirement function just cancels the main async function if it isn't met. addRequirement function 只是取消主异步 function 如果不满足。
Not sure I fully understand the requirement here but perhaps:不确定我是否完全理解这里的要求,但也许:
def mainFunction():
return addRequirement(10)
def addRequirement(r):
return r > 0
If you mean that you want to write a command to return a value based on a condition without using the 'if' statement, you can use inline if as follows:如果你的意思是你想编写一个命令来根据条件返回一个值而不使用'if'语句,你可以使用 inline if 如下:
def addRequirement(r):
return something if anyConditions else somethingElse
But, if you mean you just wanna change the main function and the program still works, you can use this:但是,如果你的意思是你只想改变主 function 并且程序仍然有效,你可以使用这个:
def mainFunction():
addRequirement(10)
def addRequirement(r):
return passFunction if r>0 else retFunction
def passFunction():
pass
def retFunction():
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.