How can I get the line number ?

K

kk

Hello

I am writing some Python code that runs in another application(has
wrapper functions). Due to lack of debugging I am printing out alot of
outputs and manual messages. I want to be able to create a function
that would let me print the current line number that is called from.
This is not for debugging exceptions it is rather to simplify my debug
messages, at least I can trace my debug messages.

thanks
 
M

Maxim Khitrov

Hello

I am writing some Python code that runs in another application(has
wrapper functions). Due to lack of debugging I am printing out alot of
outputs and manual messages. I want to be able to create a function
that would let me print the current line number that is called from.
This is not for debugging exceptions it is rather to simplify my debug
messages, at least I can trace my debug messages.

thanks

Modify the following as needed:

from inspect import currentframe, getframeinfo

def caller_info(depth=0):
"""
Get file, line number, and name of the calling function.
"""
if depth < 0:
raise ValueError('invalid stack depth')

caller = frame = currentframe()
try:
for i in xrange(-1, depth):
caller = caller.f_back
if caller is None:
return (None, None, None)

return getframeinfo(caller, 0)[:3]
finally:
del caller, frame

- Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top