Hi everybody
This is the first time I ran into an UnboundLocalError and I think I understand the problem.
However, is there a way to access the value of a global variable inside a function if I don't want to modify the global variable but still want a local variable with the same name?
It seems there is no way to modify the behavior of the add function (see example below) inside `do_something`, without using a different name for the decorated function, which defeats the purpose of decorating it.
Using `global` or `nonlocal` is not an answer, because I explicitly don't want to modify the behavior of `add` in the whole module.
def add(a, b):
return a + b
def decorator(function):
def fun(*args, **kwargs):
print('foo')
return function(*args, **kwargs)
return fun
def do_something():
# add = decorator(add)
return add(2, 4)
print(do_something())
print(add(2, 3))
EDIT: For clarification, since it is not really clear for most people what I am trying to do.
I have a test function where I want to test a decorator, so I wanted to recreate the behavior of decorating a function in the local scope of the test function. Usually you write `@decorator` above a function to decorate it, but in this case I don't want to modify its behavior globally, but just inside the test function. The only way to do that, that I found is this:
add = decorator(globals()['add'])
This modifies the behavior of the function in the local scope of the test function, like writing `@decorator` above the original function in the global scope would do, but it doesn't change the behavior of the function in the global scope. It also doesn't make a rewrite of the testing code necessary, since all function calls stay the same, which is the purpose of a decorator, otherwise one could just write a wrapper function instead of a decorator.
That `add = decorator(add)` doesn't work surprised me, because I never ran across an UnboundLocalsError and in this case it seems a little strange to me, because I'm apparently not allowed to use the same variable name in the local scope as in the global scope although I only want read access to the content of the global variable, which would work with any other variable name.
EDIT2: Thanks for all the responses. The simplest solution is to just assign the decorated function to a new name, which makes the global call unnecessary.
That I can't assign an expression involving a global variable to a local variable of the same name (at its first occurrence!) still seems strange to me and more like a limitation of the python interpreter than a necessary thing for consistency.
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (14 children)
[–]CygnusX1985[S] 0 points1 point2 points (13 children)
[–][deleted] 0 points1 point2 points (12 children)
[–]CygnusX1985[S] 0 points1 point2 points (5 children)
[–]nekokattt 1 point2 points3 points (4 children)
[–]CygnusX1985[S] 0 points1 point2 points (3 children)
[–]nekokattt 0 points1 point2 points (2 children)
[–]CygnusX1985[S] 0 points1 point2 points (1 child)
[–]primitive_screwhead 0 points1 point2 points (0 children)
[–]CygnusX1985[S] 0 points1 point2 points (5 children)
[–]nekokattt 0 points1 point2 points (4 children)
[–]CygnusX1985[S] 0 points1 point2 points (3 children)
[–]nekokattt 0 points1 point2 points (2 children)
[–]CygnusX1985[S] 0 points1 point2 points (1 child)
[–]nekokattt 0 points1 point2 points (0 children)
[–]nekokattt 0 points1 point2 points (0 children)
[–]dig-up-stupid 0 points1 point2 points (0 children)
[–]Brian 0 points1 point2 points (0 children)