I would like to disable my decorator when I execute a function from another script
Following the documentation from the wrapt module, here is my simple decorator and function:
# script_1.py
import wrapt
def _enabled():
return True
@wrapt.decorator(enabled=_enabled)
def my_decorator(wrapped, instance, args, kwargs):
print('decorator enabled')
return wrapped(*args, **kwargs)
@my_decorator
def my_function():
print('function enabled')
pass
If I then call my_function() I will get this result:
>>> my_function()
decorator enabled
function enabled
>>>
However, I would like to toggle the disable/enable functionality from another script, as such:
# script_2.py
from main import my_function
# somehow pass 'False' to the decorator and therefore suppress the the print.
my_function()
In order to get this result:
>>> my_function()
function enabled
>>>
Your
_enabledfunction needs to access some variable whose value you can change, rather than hard-coding its return value.Now you can modify the value of
script1._foobefore callingmy_function.