I have a case similar to this -
flag = True
print "Before All things happened, flag is", flag
def decorator(*a):
def real_decorator(function):
def wrapper(*args, **kwargs):
global flag
flag = False
print "just in real decorator before function call i.e. before", function.__name__
print "flag is " , flag
function(*args, **kwargs)
print "In real decorator after function call i.e. after", function.__name__
flag = True
print "flag is ", flag
return wrapper
return real_decorator
@decorator()
def subtota():
print "in subtota"
print "flag is" , flag
@decorator()
def print_args(*args):
print "in print args"
for arg in args:
print arg
print "flag is ", flag
subtota()
print "we do want flag to be false here, after subtota"
print "but, flag is ", flag
print_args("bilbo", "baggins")
print "after All things happended flag is ", flag
And the output is
Before All things happened, flag is True
just in real decorator before function call i.e. before print_args
flag is False
in print args
bilbo
baggins
flag is False
just in real decorator before function call i.e. before subtota
flag is False
in subtota
flag is False
In real decorator after function call i.e. after subtota
flag is True
we do want flag to be false here, after subtota
but, flag is True
In real decorator after function call i.e. after print_args
flag is True
after All things happended flag is True
Here, I do not want to change the value of flag after subtota() or may be we can say that, we want to keep behaviors of each function independent to each other.
How can we achieve this?
PS - Cannot avoid using Module-level global variable flag.
EDIT- desired behavior - Only after the uppermost function is executed, the flag should be false.