我需要从被调用者那里获取调用者信息(什么文件/什么行).我了解到我可以使用inpect模块来达到目的,但不是如何.
如何通过检查获得这些信息?或者有没有其他方法来获取信息?
import inspect
print __file__
c=inspect.currentframe()
print c.f_lineno
def hello():
print inspect.stack
?? what file called me in what line?
hello()
Run Code Online (Sandbox Code Playgroud) 如何在我的代码中嵌入一个IPython shell并让它自动显示调用它的行号和函数?
我目前有以下设置在我的代码中嵌入IPython shell:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
from IPython.config.loader import Config
# Configure the prompt so that I know I am in a nested (embedded) shell
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '
# Messages displayed when I drop into and exit the shell.
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you …Run Code Online (Sandbox Code Playgroud)