Python equivalent of __LINE__ macro?

Edward C. Jones edcjones at
Mon Jan 7 17:33:25 EST 2002

David Brady wrote:

> Is there an easy way to do this?  The handiest thing
> would be to have a variable like __LINE__ hanging
> around, but in a pinch I suppose we could just use the
> traceback and trap all exceptions....  Though I wonder
> if there isn't an easier way.

If you just want to know the line number, try:

def line():
     import inspect
     return inspect.stack()[depth][2]

For details see section 3.11.2 of the Python Library Reference.

Good luck,
Ed Jones

More information about the Python-list mailing list