(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)
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]//
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.