Python Forum
getting source line number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: getting source line number (/thread-2210.html)



getting source line number - Skaperen - Feb-27-2017

is there a way to get the source file line number in some code?  for example:

from __future__ import print_function

# print a message with source line number

n = get_source_code_file_line_number()
n += 2
print('now printing at line',str(n))
Output:
now printing at line 6
OR

from __future__ import print_function
for n in range(3)
    print('now printing at line',str(get_source_code_file_line_number()))
    print('now printing at line',str(get_source_code_file_line_number()))
Output:
now printing at line 2 now printing at line 3 now printing at line 2 now printing at line 3 now printing at line 2 now printing at line 3
it's ok if it is based on starting at 1 instead of 0.


RE: getting source line number - Larz60+ - Feb-27-2017

Many (most?) editors can toggle line numbers off and on.


RE: getting source line number - Skaperen - Feb-27-2017

i mean at run time

i found this solution that seems to work:

import inspect
print( 'line', inspect.getframeinfo(inspect.currentframe()).lineno, 'of file', inspect.getframeinfo(inspect.currentframe()).filename )



RE: getting source line number - snippsat - Feb-27-2017

Yes can use inspect for this.
Can make a function,then insert that function call will print line number.
import inspect

def line_numb():
   '''Returns the current line number in our program'''
   return inspect.currentframe().f_back.f_lineno

# print a message with source line number
n = 2 ; print('variable {} is at line {}'.format(n, line_numb()))
print('now printing at line {}'.format(line_numb()))
Output:
variable 2 is at line 8 now printing at line 9



RE: getting source line number - Skaperen - Feb-27-2017

(Feb-27-2017, 03:29 AM)snippsat Wrote: Yes can use inspect for this.
Can make a function,then insert that function call will print line number.
import inspect

def line_numb():
   '''Returns the current line number in our program'''
   return inspect.currentframe().f_back.f_lineno

# print a message with source line number
n = 2 ; print('variable {} is at line {}'.format(n, line_numb()))
print('now printing at line {}'.format(line_numb()))
Output:
variable 2 is at line 8 now printing at line 9

this also gives me the solution to another long time problem: a function to display the name and value of a variable, given only one argument ... a string of the name (so a coding error by the user of this function cannot result in a mismatch) Dance

from inspect import currentframe
from os import environ
def v(n):
    if 'nodebug' in environ:
        return
    x=currentframe().f_back.f_locals
    if n not in x:
        return print(n,'not assigned')
    return print(n,'=',repr(x[n]))
(*) code not tested

edit: correction applied to code: s/[0]//