Posts: 68
Threads: 26
Joined: Feb 2020
Hi
I am looking for the code to return the line number of the call, so I could code
print(line_number())
Arbiel
using Ubuntu 18.04.4 LTS, Python 3.8
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
Posts: 4,598
Threads: 71
Joined: Jan 2018
Use my printat() function
def printat(*args, **kwargs):
"""Print function with additional line number and filename information.
Adds a string such as "at line 31 in foo.py" to the printed output,
to indicate the position where the printat() function was called.
All the calls to print() in a program can be changed
to provide additional information by adding
print = printat
at the top of the program.
"""
import os, sys
level = kwargs.pop('level', 0)
frame = sys._getframe(level+1)
try:
lineno, code = frame.f_lineno, frame.f_code
args += (f'at line {lineno} in {os.path.basename(code.co_filename)}',)
finally:
del frame
print(*args, **kwargs)
« We can solve any problem by introducing an extra level of indirection »
Posts: 68
Threads: 26
Joined: Feb 2020
Hi Gribouillis
Thank you
It works fine.
Arbiel
using Ubuntu 18.04.4 LTS, Python 3.8
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)