0

I'm not sure if this is possible, but I'm just wondering if I have code of the form:

def f1()
  return f2()

def f2()
  print(this.calling_function())

What can I put in this.calling_function() to get it to print f1 as the answer?

(In other words, how can I go one level up in the stack trace).

cjm2671
  • 18,348
  • 31
  • 102
  • 161
  • You can go up the stack, but the calling function doesn't know it's name either. – Martijn Pieters Jul 30 '16 at 15:23
  • This is a combination of [Determine function name from within that function (without using traceback)](http://stackoverflow.com/q/5067604) and `sys._getframe(1)`. – Martijn Pieters Jul 30 '16 at 15:27
  • The most detailed dulicate of this topic I found was http://stackoverflow.com/questions/2654113/python-how-to-get-the-callers-method-name-in-the-called-method –  Jul 30 '16 at 15:42
  • 1
    Why do you want to know? Normally, a function should not care who is calling it. – Ian McLaird Jul 30 '16 at 15:45
  • If it's important to you, why not pass it as a parameter, rather than mucking about – Rolf of Saxony Jul 30 '16 at 15:52

0 Answers0