什么是co_names? [英] What is co_names?
问题描述
说明,价格为 co_names
读取:
局部变量名称元组
但是实际上, co_names
是一个全局变量名的元组,而 co_varnames
是局部变量名称(和参数名称)的元组。例如:
a = 1
def f(b):
c = a + b
print(f .__ code __。co_varnames)#打印('b','c')
print(f .__ code __。co_names)#打印('a',)
此外,文档中的 dis
模块很多指令说明暗示 co_names
包含全局变量的名称。例如, LOAD_GLOBAL
description 读取:
将全局名为co_names [namei]加载到堆栈中。
我在这里误解了吗? co_names
是否真的包含局部变量名称?
编辑07/17/2017
如评论/答案中所述,这似乎是文档错误。已在此处提交了问题。
编辑07/22/2017
提出请求,以解决此文档错误,已批准并等待合并。
正如其他人所说的那样,这似乎是文档错误。 关于代码对象的文档显然与 检查
的文档:
co_varnames
是包含局部变量名称的元组(以参数名称开头); [...]co_names
是一个包含字节码使用的名称的元组;
此外,访问代码对象的属性 co_names
和 co_varnames
与以下内容冲突在检查
中表示:
>> def f():
... a = 1
... b = 2
...
> f .__ code __。co_names
()
>>> f .__ code __。co_varnames
('a','b')
此外,评论在 CPython的编译器的源代码中,明确提到 co_varnames
用于局部变量:
PyObject * u_names; / *所有名称* /
PyObject * u_varnames; / *局部变量* /
看不到的原因 co_varnames
是因为上面的代码正在初始化Python用于编译代码的编译器对象的属性。 u_names
和 u_varnames
稍后都传递给 PyCode_New
-CPython代码对象的构造函数:
names = dict_keys_inorder(c-> u-> u_names,0);
varnames = dict_keys_inorder(c-> u-> u_varnames,0);
...
co = PyCode_New(...,names,varnames,...);
和 PyCode_New
分配名称
和 varnames
分别添加到 co_names
和 co_varnames
属性:
Py_INCREF(名称);
co-> co_names =名称;
Py_INCREF(varnames);
co-> co_varnames = varnames;
如果您还没有的话,建议填写 bugs.python.org 上的错误报告,以使Python开发团队知道文档中的这种不一致之处。 / p>
The description for co_names
in the inspect module reads:
tuple of names of local variables
However in practice it appears that co_names
is a tuple of global variable names while co_varnames
is a tuple of local variable names (and argument names). For example:
a = 1
def f(b):
c = a + b
print(f.__code__.co_varnames) # prints ('b', 'c')
print(f.__code__.co_names) # prints ('a',)
Furthermore in the docs for the dis
module many instruction descriptions imply that co_names
contains names of global variables. For example the LOAD_GLOBAL
description reads:
Loads the global named co_names[namei] onto the stack.
Am I misunderstanding something here? Does co_names
really contain "names of local variables"?
Edit 07/17/2017
As mentioned in the comments/answers this appears to be a documentation error. Bug issue filed here.
Edit 07/22/2017
Pull request to fix this documentation error approved and waiting to be merged.
As other's have already said, this seems to be a documentation error. The documentation for code objects clearly contradicts the documentation for inspect
:
co_varnames
is a tuple containing the names of the local variables (starting with the argument names); [...]co_names
is a tuple containing the names used by the bytecode;
Also, accessing the attributes co_names
and co_varnames
of code objects conflicts with what was stated in inspect
:
>>> def f():
... a = 1
... b = 2
...
>>> f.__code__.co_names
()
>>> f.__code__.co_varnames
('a', 'b')
Furthermore, comments in the source code for CPython's compiler explicitly mention that co_varnames
is for local variables:
PyObject *u_names; /* all names */
PyObject *u_varnames; /* local variables */
The reason you don't see co_varnames
is because the above code is initializing attributes for the compiler object that Python uses to compile code. u_names
and u_varnames
are both later passed into PyCode_New
- the constructor for CPython code objects:
names = dict_keys_inorder(c->u->u_names, 0);
varnames = dict_keys_inorder(c->u->u_varnames, 0);
...
co = PyCode_New(..., names, varnames, ... );
And PyCode_New
assigns names
and varnames
to the co_names
and co_varnames
attributes respectively:
Py_INCREF(names);
co->co_names = names;
Py_INCREF(varnames);
co->co_varnames = varnames;
If you already have not, I suggest filling out a bug report at bugs.python.org to let the Python development team know about this inconsistency in the documentation.
这篇关于什么是co_names?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!