python

获取函数语法信息

2020-06-13  本文已影响0人  wit92
位置参数个数      函数名.__code__.co_argcount
关键字参数个数    函数名.__code__.co_kwonlyargcount
函数中局部参数(位置参数+关键字+可变参数(*d) + 本地变量)     函数名.__code__.co_nlocals
函数内部的常量    函数名.__code__.co_consts
函数所属的文件   函数名.__code__.co_filename
函数所属的文件          函数名.__code__.co_firstlineno
def test(a, b, c, *d, e, f):
    localvar = 1
    x = '123'

print('位置参数个数:', test.__code__.co_argcount)
print('关键字参数个数:', test.__code__.co_kwonlyargcount)
print('函数中局部参数(位置参数+关键字+可变参数(*d) + 本地变量):', test.__code__.co_nlocals)
print('函数内部的常量:', test.__code__.co_consts)
print('函数所属的文件:', test.__code__.co_filename)
print('函数名:', test.__code__.co_name)
print('函数名:',test.__name__)
print('函数在第几行:', test.__code__.co_firstlineno)

控制台打印结果:

位置参数个数: 3
关键字参数个数: 2
函数中局部参数(位置参数+关键字+可变参数(*d) + 本地变量): 8
函数内部的常量: (None, 1, '123')
函数所属的文件: /home/l/PycharmProjects/test_modules/abstest.py
函数名: test
函数在第几行: 61
上一篇 下一篇

猜你喜欢

热点阅读