[Python-Dev] Why co_names? Wouldn't be simpler to just use co_consts?

Nick Coghlan ncoghlan at gmail.com
Wed Jan 28 13:40:13 CET 2015


On 28 January 2015 at 21:21, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Andrea Griffini wrote:
>>
>> Sorry if the question is naive, but why is co_names needed? Wouldn't be
>> simpler to just use co_consts?
>
> One reason might be that keeping them separate means
> you can have up to 256 names and 256 consts using
> 1-byte opcode arguments. Otherwise, you'd be limited
> to a total of 256 of both.

They're logically distinct things accessed by different opcodes for
very different purposes. While you theoretically *could* use one array
to hold both, it would make the eval code harder to read, and various
introspection tasks (like "tell me all the names referenced from this
code object") significantly more difficult.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list