title: Is it possible to determine if a function is called in the body of another function or as argument of a function?
body: <p>Suppose we have a Python function:</p>
  
  <pre><code>def func():
      # if called in the body of another function, do something
      # if called as argument to a function, do something different
      pass
  </code></pre>
  
  <p>func() can be called in the body of another function:</p>
  
  <pre><code>def funcA():
      func()
  </code></pre>
  
  <p>func() can be also called as an argument to a function:</p>
  
  <pre><code>def funcB(arg1):
      pass
  
  def funcC(**kwargs):
      pass
  
  funcB(func())
  funcC(kwarg1=func())
  </code></pre>
  
  <p>Is there a way to distinguish between those two case in the body of func()?</p>
  
  <p>EDIT. Here is my use case. I'd like to use Python as a language for rule based 3D model generation. Each rule is a small Python function. Each subsequent rule refines the model and adds additional details. <a href="https://github.com/vvoovv/prokitektura-examples/blob/master/examples/simple01.py" rel="nofollow">Here</a> is an example rule set. <a href="https://github.com/vvoovv/prokitektura-blender/wiki/Tutorial" rel="nofollow">Here</a> is a tutorial describing how the rule set works. My rule language mimics a rule language called CGA shape grammar. If this StackExchange question can be solved, my rule language could be significantly simplified.</p>
  
  <p>EDIT2. Code patching would also suffice for me. For example all cases when func is called in the body of another function are substituted for something like</p>
  
  <pre><code>call_function_on_the_right()&gt;&gt;func()
  </code></pre>