Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a callback function in python?

I wonder how to correctly use python 2.7 callback functions.

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

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

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?

like image 232
Alojz Janez Avatar asked Jun 13 '13 07:06

Alojz Janez


People also ask

How do I call a callback function in Python?

In the Parallel Python Module, the submit function is known as the callback function. The callback function acts as an argument for any other function. The other function in which the callback function is an argument calls the callback function in its function definition.

How do you execute a callback function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

How do you get data from a callback function in Python?

The easiest way I can think of to do this in your code is to just store the input as a variable in the CallBackStuff object. Then after you've called the call-back function, you can just access the input data from the CallBackStuff instance.

When would you use a callback function?

Callbacks make sure that a function is not going to run before a task is completed but will run right after the task has completed. It helps us develop asynchronous JavaScript code and keeps us safe from problems and errors.


2 Answers

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's member_of function itself does return a function in your example.

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 check in this case.

cb_function = member_of('admin')

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

The 3 steps from above together can be written shorter:

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.

like image 141
mit Avatar answered Sep 20 '22 13:09

mit


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,

k=member_of('admin')
if k():

To do your task.

like image 30
specialscope Avatar answered Sep 17 '22 13:09

specialscope