I'd want to say: "it is impossible", which is true, for certain degrees of impossible.
There is no way to make a code macro in Python such that the usual C tricks with preprocessor stringifications and such would require.
However, you still have 2 choices:
if sources are available, you can make a debug
function that would dig the line in source code where it was called using the call frames. However Python bytecode only stores line numbers, not column positions on the line, so you couldn't distinguish 2 said function calls on the same line.
Use the eval
trick - pass in a string that is eval
led in the function in the caller's globals and locals, and then printed out.
The 2nd would be slightly less yuck and would be more portable as well:
import inspect
def pr(expr):
calling_frame = inspect.currentframe().f_back
value = eval(expr, calling_frame.f_globals, calling_frame.f_locals)
print("{} = {}".format(expr, value))
Elsewhere you just need to import this and you can do
a = 5
def func():
b = 42
pr('a, b')
func()
which prints out
a, b = (5, 42)