Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject variable into scope with a decorator?

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

like image 722
beardc Avatar asked Jul 25 '13 15:07

beardc


People also ask

What is the correct syntax for using a decorator?

Decorators use a special syntax in JavaScript, whereby they are prefixed with an @ symbol and placed immediately before the code being decorated.

How are variables scoped in Python?

What is Variable Scope in Python? In programming languages, variables need to be defined before using them. These variables can only be accessed in the area where they are defined, this is called scope. You can think of this as a block where you can access variables.

Can a decorator be a method?

Decorators can be implemented in a number of different ways. One useful use-case for decorators involves using them with methods defined in a class. Decorating methods in the classes we create can extend the functionality of the defined method.


1 Answers

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.

like image 166
Martijn Pieters Avatar answered Sep 28 '22 05:09

Martijn Pieters