我在玩交互式shell,测试新东西等等。我发现一个函数有三个标志
def bar(x,y):
pass
>>> dis.show_code(bar)
Name: bar
Filename: test.py
Argument count: 2
Positional-only arguments: 0
Kw-only arguments: 0
Number of locals: 2
Stack size: 1
Flags: OPTIMIZED, NEWLOCALS, NOFREE
Constants:
0: None
Variable names:
0: x
1: y
字符串也有1个标志
string = "s"
>>> dis.show_code(string)
Filename: <disassembly>
Flags: NOFREE
问题是我们为什么拥有它们,或者我们为什么需要它们
报告说:
在Python源代码中,您可以在^{} 中找到更广泛的标志列表:
这些
FLAGS
表示code
对象的不同组件。当另一个答案指向正确的答案时,我只是给这个组合增加了一点乐趣您可以尝试从
dis
导入FLAGS
的子集然后您可以编写一个简单的函数,并查看
FLAGS
显示的内容:现在,您可以开始增加复杂性并了解其变化:
这里添加
*args
、**kwargs
和yield
分别添加了VARARGS, VARKEYWORDS, GENERATOR
标志。 现在,我想了解更多信息:在这里,添加
@coroutine
decorator会给出NESTED
,async_def + yield
会给出ITERABLE_COROUTINE
,而freevar的存在,即函数bar
,会移除NOFREE
标志相关问题 更多 >
编程相关推荐