M
M
mkone1122021-12-22 05:10:31
Python
mkone112, 2021-12-22 05:10:31

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

1 answer(s)
V
Vladimir Kuts, 2021-12-22
@mkone112

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 question

Ask a Question

731 491 924 answers to any question