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

猜你喜欢

转载自www.cnblogs.com/chaoguo1234/p/9220489.html