<Python>获取模块,文件名,行号,函数名

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

猜你喜欢

转载自blog.csdn.net/meta_cpp/article/details/80506588
今日推荐