In short, no.
What is happening here is func_a()
is declaring and assigning lr
as a local variable.
You can fix this by adding the global
keyword:
lr = 0
def config(func):
def new_function(*args,**kwargs):
func(*args,**kwargs)
return new_function
@config
def func_a():
global lr
lr = 0.1
func_a()
print(lr)
But using the global
keyword is annoying and can cause bugs if you forget it, so you might be better off returning:
lr = 0
def config(func):
def new_function(*args,**kwargs):
return func(*args,**kwargs)
return new_function
@config
def func_a():
return 0.1
lr = func_a()
print(lr)
Edit
You could put it in a class somehow like this:
# Make an empty class:
class Foo: pass
# Or if you want to initalize the variables:
class Foo:
def __init__(self):
lr = 0
f = Foo()
# The decorator doesn't make much of a difference
def func_a():
f.lr = 0.1
func_a()
print(f.lr)
Now, if you want to keep absolutely all the local variables that func_a
defines, you could do this:
def func_a():
lr = 0.1
f.__dict__.update(locals())