Answer the question
In order to leave comments, you need to log in
Is there a way in function A to know that its result is being passed as an argument to function B?
def b(*args):
...
def a(*args):
# some magick
if called_from_b:
print('from b')
else:
print('not from b')
a(1)
>> not from b
b(a(1))
>> from b
Answer the question
In order to leave comments, you need to log in
as an option:
import inspect
def b(*args):
a()
def c():
pass
a()
def a(*args):
res = inspect.getouterframes(inspect.currentframe())[1].function
context = inspect.stack()[2].code_context[0]
if res != '<module>' and 'a' in context:
print(f'executed in {res}')
a()
b(a())
c()
# executed in b
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question