cs258 ยป

Displaying the Page Line Number As An Aid For Debugging

Displaying the page line number during Python's code execution is one of the very useful techniques in tracking down the execution of the program which could aid in a more effective and efficient debugging of a program. To demonstrate it:

import inspect

def getLineNumber():
    return inspect.currentframe().f_back.f_lineno

print "This",getLineNumber()
print "is just",getLineNumber()
pass
print "a test",getLineNumber()

The output will be:

This 6
is just 7
a test 9

In case you want to quickly disable the printing of the page line number on the console output, just modify the function getLineNumber:

import inspect

def getLineNumber():
    #return inspect.currentframe().f_back.f_lineno
    return ''

print "This",getLineNumber()
print "is just",getLineNumber()
pass
print "a test",getLineNumber()

The output will be:

This
is just
a test