I have a function that has a return value. I'd like the function to behave differently when the return value of the function is assigned to something.
So something like this:
def func():
if return_value_assigned():
print("yes")
else:
print("no")
return 0
# this should print "no"
func()
# this should print "yes"
a = func()
I've tried to use this method:
frame_stack = inspect.getouterframes(inspect.currentframe())
code_context = frame_stack[2].code_context[0]
is_return_value_assigned = re.match(r"[\w+ ,]*=", code_context) is not None
However, apparently, code_context only stores the last line of the function call. So for example, this wouldn't work:
a = func(*args,
**kwargs)
Is there a better way of doing this? Is it possible to get the code context of the entire function call instead of just the last line?