What is co_names?

Viewed 8283

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.

Edit 06/06/2022

Pull request was merged 09/24/2021.

1 Answers
Related