58

[Disclaimer: there may be more pythonic ways of doing what I want to do, but I want to know how python's scoping works here]

I'm trying to find a way to make a decorator that does something like injecting a name into the scope of another function (such that the name does not leak outside the decorator's scope). For example, if I have a function that says to print a variable named var that has not been defined, I would like to define it within a decorator where it is called. Here is an example that breaks:

c = 'Message'

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

@decorator_factory(c)
def msg_printer():
    print var

msg_printer()

I would like it to print "Message", but it gives:

NameError: global name 'var' is not defined

The traceback even points to wher var is defined:

<ipython-input-25-34b84bee70dc> in inner_dec(*args, **kwargs)
      8         def inner_dec(*args, **kwargs):
      9             var = value
---> 10             res = f(*args, **kwargs)
     11             return res
     12         return inner_dec

So I don't understand why it can't find var.

Is there any way to do something like this?

martineau
  • 99,260
  • 22
  • 139
  • 249
beardc
  • 16,995
  • 16
  • 66
  • 88

11 Answers11

60

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.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • So if I do `def msg_printer(): print var` and try calling `msg_printer`, I get the same name error, but if I then define `var='Hi'` and call it, it prints it off fine. In this example, is `var` not being defined at runtime, after `msg_printer` has been compiled? – beardc Jul 25 '13 at 15:34
  • Because `var` is not defined in the function or in the parent scopes, `var` is marked, at compile time, as a global name instead. But if there was a parent scope, then at compile time, `var` would be marked as a scoped name instead, at which point the decorator trick would not work anymore either. – Martijn Pieters Jul 25 '13 at 15:35
  • Context of globals may be overwritten when you have several concurrent requests. I thought that impossible, but it is - i used this solution for custom user auth(my fail), and after some time see context change if request wasn't process quick. Pay attention, now i'm looking for new solution. – Artyom Lisovskij Nov 20 '19 at 21:33
  • @ArtyomLisovskij that’s why my answer includes the warning at the end: *Note that altering the globals is not thread safe* – Martijn Pieters Nov 21 '19 at 10:54
7

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()
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
M07
  • 825
  • 1
  • 10
  • 19
  • How is this conceptually different from passing a positional argument? – Mad Physicist Feb 01 '18 at 10:30
  • 3
    It is not so different, but as it is written for positional argument you should know the position of your argument. As it is a decorator, you do not know it. **kwargs** is the safest way in point of view because you control the name of your argument. – M07 Feb 02 '18 at 15:41
  • 1
    That's certainly a good enough explanation for me to remove my downvote. Not sure that I can upvote this in context of the accepted answer though. – Mad Physicist Feb 02 '18 at 15:43
6

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.

newacct
  • 110,405
  • 27
  • 152
  • 217
6

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.

from functools import wraps

def inject_variables(context):
    """ Decorator factory. """
    def variable_injector(func):
        @wraps(func)
        def decorator(*args, **kwargs):
            try:
                func_globals = func.__globals__  # Python 2.6+
            except AttributeError:
                func_globals = func.func_globals  # Earlier versions.

            saved_values = func_globals.copy()  # Shallow copy of dict.
            func_globals.update(context)

            try:
                result = func(*args, **kwargs)
            finally:
                func_globals = saved_values  # Undo changes.

            return result

        return decorator

    return variable_injector

if __name__ == '__main__':
    namespace = {'a': 5, 'b': 3}

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

    test()
martineau
  • 99,260
  • 22
  • 139
  • 249
3

Update __globals__ works for me.

def f():
    print(a)


def with_context(**kw):
    def deco(fn):
        g = fn.__globals__
        g.update(kw)
        return fn

    return deco


with_context(a=3)(f)() # 3
weaming
  • 3,467
  • 1
  • 14
  • 11
2

Python is lexically scoped, so I'm afraid there is no clean way to do what you want without some potentially nasty side effects. I recommend just passing var into the function via the decorator.

c = 'Message'

def decorator_factory(value):
    def msg_decorator(f):
        def inner_dec(*args, **kwargs):
            res = f(value, *args, **kwargs)
            return res
        inner_dec.__name__ = f.__name__
        inner_dec.__doc__ = f.__doc__
        return inner_dec
    return msg_decorator

@decorator_factory(c)
def msg_printer(var):
    print var

msg_printer()  # prints 'Message'
Alexander Otavka
  • 796
  • 1
  • 6
  • 9
  • 1
    What is the `msg_decorator.__name__ = f.__name__` and `msg_decorator.__doc__ = f.__doc__` for? Is it necessary? – gogo_gorilla Dec 09 '16 at 09:27
  • 3
    @stackoverflowwww Every function in python has a name (unless it was made with lambda), and many have docstrings. Both are important for generating documentation, so we copy them to the wrapper function. I made an error in my answer, they should actually be copied over onto `inner_dec`. – Alexander Otavka Dec 15 '16 at 06:59
  • 3
    See also `functools.wraps` for doing this sort of thing – killthrush Jan 28 '17 at 16:06
  • See obvious from the other answers that there **are** "clean" ways of doing it... – martineau May 25 '18 at 20:37
1

I found an interesting post provides a different solution by creating functions on the fly. Basically:

def wrapper(func):
    cust_globals = func.__globals__.copy()

    # Update cust_globals to your liking

    # Return a new function
    return types.FunctionType(
        func.__code__, cust_globals, func.__name__, func.__defaults__, func.__closure__
    )

See https://hardenedapple.github.io/stories/computers/python_function_override/

Johan de Vries
  • 147
  • 1
  • 8
0

Here is a simple demonstration of using a decorator to add a variable into the scope of a function.

>>> def add_name(name):
...     def inner(func):
...         # Same as defining name within wrapped
...         # function.
...         func.func_globals['name'] = name
...
...         # Simply returns wrapped function reference.
...         return func
... 
...     return inner
...
>>> @add_name("Bobby")
... def say_hello():
...     print "Hello %s!" % name
...
>>> print say_hello()
Hello Bobby!
>>>
tlovely
  • 455
  • 4
  • 12
  • 5
    Note that you are manipulating a shared dictionary here. *Other functions in the same module will see this change too*, and altering the dictionary is not thread-safe. – Martijn Pieters Jul 28 '15 at 10:35
  • @MartijnPieters Is this a problem even if the value modified by the decorator is not modified any more and only read after the decorator has returned? – gogo_gorilla Dec 09 '16 at 09:31
  • 1
    @stackoverflowwww: this modifies the module globals, whenever the function is called. – Martijn Pieters Dec 09 '16 at 09:32
  • @MartijnPieters So why do people not always use the approach suggested above and below by Alexander Otavka and M07, that is, letting the decorator call the decorated functions with arguments and thus pass values to the decorated function? – gogo_gorilla Dec 09 '16 at 09:35
  • 1
    @stackoverflowwww: most people *do* use such techniques. – Martijn Pieters Dec 09 '16 at 09:37
0

Assuming that in python functions are objects, you can do...

#!/usr/bin/python3


class DecorClass(object):
    def __init__(self, arg1, arg2):
        self.a1 = arg1
        self.a2 = arg2

    def __call__(self, function):
        def wrapped(*args):
            print('inside class decorator >>')
            print('class members: {0}, {1}'.format(self.a1, self.a2))
            print('wrapped function: {}'.format(args))
            function(*args, self.a1, self.a2)
        return wrapped


    @DecorClass(1, 2)
    def my_function(f1, f2, *args):
        print('inside decorated function >>')
        print('decorated function arguments: {0}, {1}'.format(f1, f2))
        print('decorator class args: {}'.format(args))


    if __name__ == '__main__':
        my_function(3, 4)

and the result is:

inside class decorator >>
class members: 1, 2
wrapped function: (3, 4)
inside decorated function >>
decorated function arguments: 3, 4
decorator class args: (1, 2)

more explanation here http://python-3-patterns-idioms-test.readthedocs.io/en/latest/PythonDecorators.html

dAn
  • 339
  • 1
  • 3
  • 7
0
def merge(d1, d2):
    d = d1.copy()
    d.update(d2)
    return d

# A decorator to inject variables
def valueDecorator(*_args, **_kargs):
    def wrapper(f):
        def wrapper2(*args, **kargs):
            return f(*args, **kargs)
        wrapper2.__name__ = f.__name__
        wrapper2.__doc__ = f.__doc__
        oldVars = getattr(f, 'Vars', [])
        oldNamedVars = getattr(f, 'NamedVars', {})
        wrapper2.Vars = oldVars + list(_args)
        wrapper2.NamedVars = merge(oldNamedVars, _kargs)
        return wrapper2
    return wrapper

@valueDecorator(12, 13, a=2)
@valueDecorator(10, 11, a=1)
def func():
    print(func.Vars)
    print(func.NamedVars)

Instead of revising the global scope, changing the annotated function itself is more reasonable.

Martin Wang
  • 927
  • 12
  • 17
0

I have catched problem with solution using globals.

Context of globals may be overwritten when you have several concurrent requests. I thought that impossible, but it is - after some time I have catched change of context(globals) if request wasn't quick. Better solution is to pass variable using kwargs:

def is_login(old_fuction):
    def new_function(request, *args, **kwargs):
        secret_token = request.COOKIES.get('secret_token')
        if secret_token:
            items = SomeModel.objects.get(cookie = secret_token)
            if len(items) > 0:
                item = items[0]
                kwargs['current_user'] = item
                return old_fuction(request, *args, **kwargs)
            else:
                return HttpResponse('error')
        return HttpResponse(status=404)
    return new_function

@is_login  
def some_func(request, current_user):
    return HttpResponse(current_user.name)

You'll have to add extra parameter to each decorated function.