我想提取函数中使用的所有常量。在Python 2.7中,我可以遍历函数的co_constsin__code__并提取所有字符串。例如, Python 2.7
>>> def func():
...: return 'foo' if True else ('bar',)
>>> print(func.__code__.co_consts)
(None, 'foo', 'bar', ('bar',)) # no need to look into the tuple as 'bar' is available outside it as well.
这会给我'foo'和'bar'预期的那样。但是,在 中Python 3.7,代码对象'bar'只包含在元组内部。
蟒蛇 3.7
>>> print(func.__code__.co_consts)
(None, True, 'foo', ('bar',))
这意味着我还需要查看 co_consts 中的元组。我的困惑是内部是否可以有更多级别的嵌套co_consts?
哈士奇WWW
呼啦一阵风
相关分类