Search code examples
pythondecoratorpython-decorators

Does python allow me to pass dynamic variables to a decorator at runtime?


I am attempting to integrate a very old system and a newer system at work. The best I can do is to utilize an RSS firehouse type feed the system utilizes. The goal is to use this RSS feed to make the other system perform certain actions when certain people do things.

My idea is to wrap a decorator around certain functions to check if the user (a user ID provided in the RSS feed) has permissions in the new system.

My current solution has a lot of functions that look like this, which are called based on an action field in the feed:

actions_dict = {
    ...
    'action1': function1
}

actions_dict[RSSFEED['action_taken']](RSSFEED['user_id'])

def function1(user_id):
    if has_permissions(user_id):
         # Do this function

I want to create a has_permissions decorator that takes the user_id so that I can remove this redundant has_permissions check in each of my functions.

@has_permissions(user_id)
def function1():
    # Do this function

Unfortunately, I am not sure how to write such a decorator. All the tutorials I see have the @has_permissions() line with a hardcoded value, but in my case it needs to be passed at runtime and will be different each time the function is called.

How can I achieve this functionality?


Solution

  • In your question, you've named both, the check of the user_id, as well as the wanted decorator has_permissions, so I'm going with an example where names are more clear: Let's make a decorator that calls the underlying (decorated) function when the color (a string) is 'green'.

    Python decorators are function factories

    The decorator itself (if_green in my example below) is a function. It takes a function to be decorated as argument (named function in my example) and returns a function (run_function_if_green in the example). Usually, the returned function calls the passed function at some point, thereby "decorating" it with other actions it might run before or after it, or both.

    Of course, it might only conditionally run it, as you seem to need:

    def if_green(function):
        def run_function_if_green(color, *args, **kwargs):
            if color == 'green':
                return function(*args, **kwargs)
        return run_function_if_green
    
    
    @if_green
    def print_if_green():
        print('what a nice color!')
    
    
    print_if_green('red')  # nothing happens
    print_if_green('green')  # => what a nice color!
    

    What happens when you decorate a function with the decorator (as I did with print_if_green, here), is that the decorator (the function factory, if_green in my example) gets called with the original function (print_if_green as you see it in the code above). As is its nature, it returns a different function. Python then replaces the original function with the one returned by the decorator.

    So in the subsequent calls, it's the returned function (run_function_if_green with the original print_if_green as function) that gets called as print_if_green and which conditionally calls further to that original print_if_green.

    Functions factories can produce functions that take arguments

    The call to the decorator (if_green) only happens once for each decorated function, not every time the decorated functions are called. But as the function returned by the decorator that one time permanently replaces the original function, it gets called instead of the original function every time that original function is invoked. And it can take arguments, if we allow it.

    I've given it an argument color, which it uses itself to decide whether to call the decorated function. Further, I've given it the idiomatic vararg arguments, which it uses to call the wrapped function (if it calls it), so that I'm allowed to decorate functions taking an arbitrary number of positional and keyword arguments:

    @if_green                     
    def exclaim_if_green(exclamation):
        print(exclamation, 'that IS a nice color!')
    
    exclaim_if_green('red', 'Yay')  # again, nothing
    exclaim_if_green('green', 'Wow')  # => Wow that IS a nice color!
    

    The result of decorating a function with if_green is that a new first argument gets prepended to its signature, which will be invisible to the original function (as run_function_if_green doesn't forward it). As you are free in how you implement the function returned by the decorator, it could also call the original function with less, more or different arguments, do any required transformation on them before passing them to the original function or do other crazy stuff.

    Concepts, concepts, concepts

    Understanding decorators requires knowledge and understanding of various other concepts of the Python language. (Most of which aren't specific to Python, but one might still not be aware of them.)

    For brevity's sake (this answer is long enough as it is), I've skipped or glossed over most of them. For a more comprehensive speedrun through (I think) all relevant ones, consult e.g. Understanding Python Decorators in 12 Easy Steps!.