How to determine file, function and line number?

前端 未结 7 1136
清歌不尽
清歌不尽 2020-11-28 06:14

In C++, I can print debug output like this:

printf(
   \"FILE: %s, FUNC: %s, LINE: %d, LOG: %s\\n\",
   __FILE__,
   __FUNCTION__,
   __LINE__,
   logmessage         


        
7条回答
  •  无人及你
    2020-11-28 06:56

    For example

    import inspect
    frame = inspect.currentframe()
    # __FILE__
    fileName  =  frame.f_code.co_filename
    # __LINE__
    fileNo = frame.f_lineno
    

    There's more here http://docs.python.org/library/inspect.html

提交回复
热议问题