1

I'm interested in being able to use code like this to obtain 3 or (3, 7) (the lines containing the current statement), however, f_lineno returns 6 because that is where the actual execution is happening.

I was hoping to find the bounds of the current statement in a way that works in general for multiline statements (not just for this example lol).

import sys

print(  # line 3


    sys._getframe().f_lineno  # line 6
)

(same thing applies for inspect.currentframe because it's the same thing)

I've dug deep into inspect module and the stack frame objects and my current assessment is that this information is not physically available in Python but this was surprising to me and I was wondering if someone either knew some cheeky way around it or the reason that it's like that. (I have a way around my problem using regex and bracket balancing, but obviously it's a bit fragile and not ideal!)

Note: this is for a fun, experimental thing so I hope no one uses this as their villian origin story.

Bea Steers
  • 11
  • 1

0 Answers0