Passing python functions and storing via message handlers in Perspective

Generally speaking functions are first class in python and yes you can pass them. It’s what allows you to make decorators in general. You can do stuff like

def timer_function(func, args, kwargs):
    start_time = time.time()
    result = func(*args, **kwargs)
    end_time = time.time()
    print end_time - start_time

and then call this on any generic function to time it.

You can store functions in a list and then evaluate them in a certain order and store a list of results

my_funcs = [someModule.foo, module.bar, foo.bar]
results = [func() for func in my_funcs]

You can assign them to a variable (though use case wise I don’t have anything for this)

x = someModule.foo
x()

You most likely don’t want to just be able to send back any function via message handler to be run on the gateway as if someone is able to spoof a client and knows just a little ignition or say some of your functions that relate to deleting/updating your database, then you could be in a world of hurt.

Heres a post I made about it a while back, but my method was passing a string of the function name and then importing it with importlib. However to make sure I was safe I would need to white list what functions would be acceptable for a client to call and I was able to do that with string comparisons. If you’re passing a function object back directly I am not sure how you would have a whitelist.