我想知道如何正确使用
Python 2.7回调函数.
我在我的代码中有一些来自Cherrypy auth示例的回调函数.
(这些回调会返回一个可以评估为True或False的函数,具体取决于登录的用户是否在组中.)
我想知道如果我写一个这样的代码,是否执行回调:
给定图书馆的定义是:
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?
如何在我的代码中应用和执行回调?
如果我这样说:
if member_of('admin'):
do_something()
else:
do_something_else()
这将执行calllback并检查管理组?或者它会发现“member_of”的值是否是函数定义,函数定义可能始终是“True”值(或者可能是False值),但都是错误的,因为它需要执行
你能启发我吗我如何确保执行回调?我该如何传递它呢?