Title: stdlib inspect documentation on code.co_names is incorrect
Type: Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Dutcho, docs@python
Priority: normal Keywords: easy

Created on 2021-10-16 10:45 by Dutcho, last changed 2022-01-14 13:56 by iritkatriel.

Messages (1)
msg404068 - (view) Author: (Dutcho) Date: 2021-10-16 10:45
The standard library documentation on module inspect starts with an overview of types and attributes. This overview (in all Python versions) states:
    code.co_names: tuple of names of local variables
    code.co_varnames: tuple of names of arguments and local variables
That suggests the argument names are set(code.co_varnames) - set(code.co_names), which is incorrect.

I think the attribute description should be:
    code.co_names: tuple of names of used global and built-in variables

>>> def f(x): a = 1; print(f, a)
>>> assert f.__code__.co_varnames == ('x', 'a')  # argument and local, in that order
>>> assert set(f.__code__.co_names) == set(('f', 'print'))  # global and built-in (don't care order), not local
Date User Action Args
2022-01-14 13:56:49iritkatrielsetkeywords: + easy
versions: - Python 3.6, Python 3.7, Python 3.8
2021-10-16 10:45:28Dutchocreate