faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 13 people (77%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Getting the current line number

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 267, Adrian Eyre


"""
Packages: miscellaneous.introspection
"""
"""
> Is there an equivalent of the C __LINE__ macro in Python to get the
> currentline number?
"""
import sys
def what_line_am_i_on():
    try:
        raise "Hack"
    except:
        return sys.exc_info()[2].tb_frame.f_back.f_lineno
print "This is line " + str(what_line_am_i_on())