print line number and source filename

P

Peng Yu

I want to print filename and line number for debugging purpose. So far
I only find how to print the line number but not how to print
filename.

import inspect
print inspect.currentframe().f_lineno

I found inspect.getsourcefile(), but I have to supply a class name to
it. I have searched online, but I don't find how to print the source
filename. Would you please let me know?

Also, always importing the inspect module and getting the frame and
accessing the lineno from the frame is not very convenient to type. Is
there a shorter way to access the line number (for example, in C++ and
perl, __LINE__ can be used to access line number, which is much more
convenient than the way that I found in python).
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top