如何使用Python中的回调函数? [英] How to use a callback function in python?

查看:625
本文介绍了如何使用Python中的回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何正确使用Python 2.7回调函数。

I wonder how to correctly use python 2.7 callback functions.

我有权威性的CherryPy例子一些回调函数在我的code。

I have some callback functions from Cherrypy auth examples in my code.

(这些回调返回一个取值为真或假的函数,根据登录的用户组或不存在。)

(These callbacks return a function that can evaluate to True or False, depending on the logged in user being in a group or not.)

我不知道是否被执行或不回调,如果我写了一张code像这样的:

I wonder if a callback is executed or not if I write a piece of code like this:

由于从库中的定义是:

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?

如何申请,在我的code执行回调?

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()

这是否会执行calllback并检查管理组?还是会发现,如果MEMBER_OF的值是一个函数的定义和函数定义大概是总是一个真值(或者以假值),但两者都是错误的,因为它需要执行

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

您可以告诉我这个?我怎样才能确保执行的回调?一个我怎么可以通过它周围,因为它是?

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?

推荐答案

在蟒蛇,就像许多其他语言,变量还可以包含一个功能,你可以通过他们周围像包含如其他变量数字或字符串。

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.

CherryPy的 MEMBER_OF 函数本身做你的榜样返回的功能。

CherryPy's member_of function itself does return a function in your example.

我解释它在简单的步骤:

I am explaining it in simple steps:

如果你写 MEMBER_OF()返回)功能MEMBER_OF(的结果是一个名为检查在这种情况下。

If you write member_of() it returns the result of the function member_of() which is the function with the name check in this case.

cb_function = member_of('admin')

此时变量 cb_function 保存调用函数 MEMBER_OF 的结果,并在最后一行 MEMBER_OF 收益检查,这是在函数中定义的 MEMBER_OF 作为另一个功能!

At this point the variable cb_function holds the result of calling the function member_of, and in the last line member_of returns check, which was defined within the function member_of as another function!

您必须再次拨打第一个结果,因为你可以,你必须在几乎相同的方式把它当作一个局部的功能,那你在目前情况下的定义,得到最终的结果是,通过做类似:

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
  ...   

从上面在一起的3个步骤可以写成短:

The 3 steps from above together can be written shorter:

cb_function = member_of('admin')
  if cb_function():
    # do something
    ...

或者更短:

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中的回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆