How to inject variable into scope with a decorator?

前端 未结 11 2065
我寻月下人不归
我寻月下人不归 2020-12-04 17:31

[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 dec

相关标签:
11条回答
  • 2020-12-04 17:59

    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
    
    0 讨论(0)
  • 2020-12-04 18:07

    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/

    0 讨论(0)
  • 2020-12-04 18:09

    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()
    
    0 讨论(0)
  • 2020-12-04 18:10

    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.

    0 讨论(0)
  • 2020-12-04 18:18

    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.

    0 讨论(0)
提交回复
热议问题