How to inject variable into scope with a decorator?

You can't. Scoped names (closures) are determined at compile time, you cannot add more at runtime.

The best you can hope to achieve is to add global names, using the function's own global namespace:

def decorator_factory(value):
    def msg_decorator(f):
        def inner_dec(*args, **kwargs):
            g = f.__globals__  # use f.func_globals for py < 2.6
            sentinel = object()

            oldvalue = g.get('var', sentinel)
            g['var'] = value

            try:
                res = f(*args, **kwargs)
            finally:
                if oldvalue is sentinel:
                    del g['var']
                else:
                    g['var'] = oldvalue

            return res
        return inner_dec
    return msg_decorator

f.__globals__ is the global namespace for the wrapped function, so this works even if the decorator lives in a different module. If var was defined as a global already, it is replaced with the new value, and after calling the function, the globals are restored.

This works because any name in a function that is not assigned to, and is not found in a surrounding scope, is marked as a global instead.

Demo:

>>> c = 'Message'
>>> @decorator_factory(c)
... def msg_printer():
...     print var
... 
>>> msg_printer()
Message
>>> 'var' in globals()
False

But instead of decorating, I could just as well have defined var in the global scope directly.

Note that altering the globals is not thread safe, and any transient calls to other functions in the same module will also still see this same global.


You can't. Python has lexical scoping. That means the meaning of an identifier is determined solely based on the scopes that physically surround it when you look at the source code.


Here's a way of injecting multiple variables into a function's scope in a manner somewhat similar to what @Martijn Pieters does in his answer. I'm posting it primarily because it's a more general solution and would not need to be applied multiple times to do it — as would be required by his (and many of the other) answers.

It should be noted that a closure is formed between the decorated function and the namespace dictionary, so changing its contents — e.g. namespace['a'] = 42will affect subsequent calls to the function.

from functools import wraps

def inject_variables(context):
    """ Decorator factory. """

    def variable_injector(func):
        """ Decorator. """
        @wraps(func)
        def decorator(*args, **kwargs):
            func_globals = func.__globals__

            # Save copy of any global values that will be replaced.
            saved_values = {key: func_globals[key] for key in context
                                                        if key in func_globals}
            func_globals.update(context)
            try:
                result = func(*args, **kwargs)
            finally:
                func_globals.update(saved_values)  # Restore replaced globals.

            return result

        return decorator

    return variable_injector


if __name__ == '__main__':
    namespace = dict(a=5, b=3)

    @inject_variables(namespace)
    def test():
        print('a:', a)
        print('b:', b)

    test()

There is a clean way to do what you want without using global variable. If you want to be stateless and threads safe, you don't really have the choice.

Use the "kwargs" variable:

c = 'Message'

def decorator_factory(value):
    def msg_decorator(f):
    def inner_dec(*args, **kwargs):
        kwargs["var"] = value
        res = f(*args, **kwargs)
        return res
    return inner_dec
return msg_decorator

@decorator_factory(c)
def msg_printer(*args, **kwargs):
    print kwargs["var"]

msg_printer()