简体   繁体   English

python 检查模块-python 的问题

[英]problem with python inspect module-python

how to write a program to "identify the line number of the function call?"如何编写程序“识别 function 调用的行号?”

python inspect module provides options to locate the line number but, python 检查模块提供了定位行号的选项,但是,

def di():
  return inspect.currentframe().f_back.f_lineno 

this python code prints the current line number.此 python 代码打印当前行号。

Why not use inspect.stack() ?为什么不使用inspect.stack()

def get_caller_lineno():
    frame, _, lineno, _, _, _ = inspect.stack()[1]
    return lineno

I published a wrapper for inspect with simple stackframe addressing covering the stack frame by a single parameter spos :我发布了一个用于检查的包装器,它使用简单的堆栈帧寻址,通过单个参数spos覆盖堆栈帧:

Eg pysourceinfo.PySourceInfo.getCallerLinenumber(spos=1)例如pysourceinfo.PySourceInfo.getCallerLinenumber(spos=1)

where spos=0 is the lib-function, spos=1 is the caller, spos=2 the caller-of-the-caller, etc.其中spos=0是 lib 函数, spos=1是调用者, spos=2是调用者的调用者,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM