【Python】—— 获取函数内部变量名称

原文出处:

https://blog.csdn.net/maixiaochai/article/details/88693507

 

关键语句:

func_vars = func.__code__.co_varnames

使用举例:

def my_func():
    lis = []
    dic = dict()
    res = lis + ['get', 'func', 'vars']
    words = "Life is short, You need Python!"
    print(words)
 
 
def get_func_varnames(func):
    func_vars = func.__code__.co_varnames
    print(func_vars)
 
 
if __name__ == "__main__":
    get_func_varnames(my_func)

结果:

('lis', 'dic', 'res', 'words')

 

posted @ 2019-10-25 13:42  wangju003  阅读(1486)  评论(0编辑  收藏  举报