相关疑难解决方法(0)

如何使用inspect来从python中获取调用者的信息?

我需要从被调用者那里获取调用者信息(什么文件/什么行).我了解到我可以使用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)

python inspect

72
推荐指数
2
解决办法
3万
查看次数

使用IPython作为有效的调试器

如何在我的代码中嵌入一个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)

python ipython

2
推荐指数
1
解决办法
1034
查看次数

标签 统计

python ×2

inspect ×1

ipython ×1