Is it possible to determine whether a function is called in the body of another function or as an argument to a function?

Suppose we have a Python function:

def func():
    # if called in the body of another function, do something
    # if called as argument to a function, do something different
    pass

func () can be called in the body of another function:

def funcA():
    func()

func () can also be called as an argument to a function:

def funcB(arg1):
    pass

def funcC(**kwargs):
    pass

funcB(func())
funcC(kwarg1=func())

Is there a way to distinguish between these two cases in the body of func ()?

. . Python . Python. . . - , , . , CGA. StackExchange , .

EDIT2. . , , func , -

call_function_on_the_right()>>func()
+4
1

, . , , . funct() onplevel funca(), , func() , , , . , , , , .

? .

? , , , , inspect.stack() .

, , , , . .

  • .
  • , func(func())? !

, . f_code, - f_lasti, . , , . - ( dis) , , , .

100%, , , . , , " ", - . , , "" , .

, , .:)

+1

All Articles