1. 程式人生 > >獲取模組,檔名,行號,函式名

獲取模組,檔名,行號,函式名

import sys  
import inspect  
import os  
  
  
def get_current_function_name():
    return inspect.stack()[1][3]

def get_attrs():  
    print('Module:', __name__)  
    print('File Path: ', __file__)  
    print('File Name: ', os.path.basename(__file__))  
    print('Line No.: ', sys._getframe().f_lineno)  
    print('Func: ', sys._getframe().f_code.co_name)
    print('Func: ', get_current_function_name())  
  
  
get_attrs()  

Module: __main__
File Path:  C:/Users/current/PycharmProjects/untitled/builtin-module-test.py
File Name:  builtin-module-test.py
Line No.:  63
Func:  get_attrs
Func:  get_attrs