关键语句:
func_vars = func.__code__.co_varnames
使用举例:
# -*- coding: utf-8 -*-
# @File: get_func_names.py
# @Project: src
# @Date: 2019/3/20 16:37
# @Author: Maixiaochai
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')