How to find number of line that is currently executing?

Chris Rebert clp2 at rebertia.com
Sat Oct 10 05:54:32 CEST 2009


On Fri, Oct 9, 2009 at 8:46 PM, Dr. Phillip M. Feldman
<pfeldman at verizon.net> wrote:
>
> I would like to put a statement on line N of my program that prints the line
> number that is currently executing. This may sound fairly trivial, but I
> don't want to hard code the line number because N will change if lines are
> inserted or deleted above that point. Any advice will be appreciated.

If you're doing this for debugging, it's often more convenient to just
come up with a unique string you can grep for, but anyway:

import traceback
print traceback.extract_stack()[-1][1]

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list