get line number

Bengt Richter bokr at oz.net
Tue Aug 12 12:15:21 EDT 2003


On 12 Aug 2003 12:34:58 GMT, Richard Mertens <richard.mertens at wolfi.org> wrote:

>How do get the actual line number from the actual running sourcecode.
>I need it for debugging purpose
>

 >>> import inspect
 >>> def linehere():
 ...     return 'line %s in %r'%tuple(inspect.getframeinfo(inspect.currentframe().f_back)[1:3])
 ...
 >>> def foo():
 ...     x=1
 ...     y=2
 ...     here = linehere()
 ...     z = 4
 ...     return linehere(), here
 ...
 >>> foo()
 ("line 6 in 'foo'", "line 4 in 'foo'")

The inspect module also has much more that might be of help.

Regards,
Bengt Richter




More information about the Python-list mailing list