Python函数的内省-Introspection

Python函数可以进行内省-Introspection,查看函数内部的细节,方式就是使用函数的__code__属性。

def func(a, b = 2):
    return a + b


>>>dir(func.__code__)    # 只是列出了一部分
['__class__',  '__delatrr__', ...., 'co_code', 'co_filename', 'co_argcount', 'co_varnames',...]

>>>func.__code__.co_varnames   # 参数列表
('a', 'b')

>>>func.__code__.co_argcount     # 参数个数
2

 

posted @ 2018-06-24 14:36  chaoguo1234  阅读(571)  评论(0编辑  收藏  举报