如何在python中使用回调函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17081243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a callback function in python?
提问by Alojz Janez
I wonder how to correctly use python 2.7 callback functions.
我想知道如何正确使用 python 2.7 回调函数。
I have some callback functions from Cherrypy auth examples in my code.
我的代码中有一些来自 Cherrypy auth 示例的回调函数。
(These callbacks return a function that can evaluate to True or False, depending on the logged in user being in a group or not.)
(这些回调返回一个函数,该函数可以评估为 True 或 False,具体取决于登录用户是否在组中。)
I wonder if a callback is executed or not if I write a piece of code like this:
我想知道如果我写了一段这样的代码,是否会执行回调:
Given the definition from the library is:
鉴于图书馆的定义是:
def member_of(groupname):
def check():
if groupname == 'admin':
if cherrypy.request.login == 'joe':
return True
if cherrypy.request.login == 'toni':
return True
return False
return False
# .... (other groups checked in the same way)
return check # returns a callback function from my understanding?
How can I apply and execute the callback in my code?
如何在我的代码中应用和执行回调?
If I put it like this:
如果我这样说:
if member_of('admin'):
do_something()
else:
do_something_else()
Will this execute the calllback and check for the admin group? Or will it find out if the value of "member_of" is a function definition and a function definition is probably always a "True" value (or maybe a False value) but both are wrong, because it needs to be executed
这会执行回调并检查管理员组吗?或者它会找出“member_of”的值是否是一个函数定义,一个函数定义可能总是一个“真”值(或者可能是一个假值)但两者都是错误的,因为它需要被执行
Can you enlighten me on this? How can I make sure a callback is executed? An how can I pass it around as it is?
你能启发我吗?如何确保执行回调?我怎样才能按原样传递它?
采纳答案by mit
In python, like in many other languages, a variable can also contain a function and you can pass them around like other variables that contain e.g. numbers or strings.
在 python 中,就像在许多其他语言中一样,一个变量也可以包含一个函数,你可以像其他包含数字或字符串的变量一样传递它们。
CherryPy's member_offunction itself does return a function in your example.
CherryPy 的member_of函数本身确实在您的示例中返回了一个函数。
I am explaining it in simple steps:
我用简单的步骤来解释它:
If you write member_of()it returns the result of the function member_of() which is the function with the name checkin this case.
如果你写member_of()它返回函数 member_of() 的结果,check在这种情况下它是具有名称的函数。
cb_function = member_of('admin')
At this point the variable cb_functionholds the result of calling the function member_of, and in the last line member_ofreturns check, which was defined within the function member_ofas another function!
此时变量cb_function保存了调用函数的结果,member_of最后一行member_of返回check,在函数中定义member_of为另一个函数!
You have to call the first result again, because you can and you have to treat it in almost the same way as a local function, that you defined in the current context, to get the final result, by doing something like:
您必须再次调用第一个结果,因为您可以并且必须以与在当前上下文中定义的本地函数几乎相同的方式对待它,以通过执行以下操作来获得最终结果:
my_result = cb_function()
And then you would continue and use the result. For example you could check its boolean value:
然后你会继续并使用结果。例如,您可以检查其布尔值:
if my_result:
# do something
...
The 3 steps from above together can be written shorter:
上面的 3 个步骤可以写得更短:
cb_function = member_of('admin')
if cb_function():
# do something
...
Or even shorter:
或者更短:
if member_of('admin')():
# do something
...
At first it may appear a little strange in python to have the double ()(), but if you think about it for a while it makes sense.
起初在 python 中使用 double 可能看起来有点奇怪()(),但如果你仔细考虑一下,它是有道理的。
回答by specialscope
If you execute it, it is plain simple.
如果你执行它,它很简单。
member_of()will return method object check.
you have to execute to get result by doing something like if member_of('admin')():
or,
member_of()将返回方法对象检查。你必须通过执行类似 if 的操作来获得结果member_of('admin')():或者,
k=member_of('admin')
if k():
To do your task.
完成你的任务。

