You could write a decorator that does this, but you'll have to add this to every method per hand or do some magic in the module __dict__
(which I don't recommend). This works (I tested it) in python 3.6, if you use the commented version it works in python 2.7. They changed the stack
-signature from 2.X to 3.X.
from inspect import stack
from functools import wraps
def printer(f):
@wraps(f)
def wrapped(*args, **kwargs):
call_info = stack()[1].code_context # In Python 2.7.X, replace with stack()[1][4]
# Use index 1 because 0 is always this wrapper function
for context in call_info:
print(context.strip())
return f(*args, **kwargs)
return wrapped
Use it like this:
@printer
def test():
return 5
x = test()
This will print x = test()
to stdout. If you want this for debugging, that is totally fine, but if this is something your code relies on in production I would not recommend it, because it will kill your performance, see for example here.