calling_function

privex.helpers.black_magic.calling_function(skip=2) → Optional[str][source]

Returns the name of the function which called your function/method.

Example:

>>> def x(skip=2): return calling_function(skip=2)
>>>
>>> def y(skip=2): return x(skip)
>>>
>>> def z(skip=2): return y(skip)
>>>
>>> print(y())   # The call to x() returns that 'y' is the function which called it.
y
>>> print(z())   # The call to z() calls y() -> x() - still returning that 'y' is the caller of x()
y
>>> # If we adjust skip to 3 instead of 2, we can see that z() is the function that called y() which called x()
>>> print(z(3))
z
Parameters

skip (int) – Skip this many frames. 0 = calling_function() 1 = function which called calling_function() 2 = function which called the function that called calling_function() (default) and so on…

Return str|None function_name

Either a string containing the function name, or None if you’ve skipped too many frames.