You can filter the line number out. Here's a simple class that does that:
There may be more stuff that would like to extract.
import inspect class GetCallStackItem: def __init__(self): self.full_stack = inspect.stack() for stacklevel, item in enumerate(self.full_stack): attributes = self.get_method_attributes(item) print(f'\nstacklevel: {stacklevel}') for attrname, value in attributes: if attrname == 'lineno': print(f'line Number: {value}') def get_method_attributes(self, method): temp = dir(type('whatever', (object,), {})) return [item for item in inspect.getmembers(method) if item[0] not in temp] if __name__ == '__main__': gcsi = GetCallStackItem()Running this on itself returns the following:
Output:stacklevel: 0
line Number: 5
stacklevel: 1
line Number: 19
If you add a print statement immediately after the second for statement, you'll get an exhaustive of all that's available.There may be more stuff that would like to extract.