Python from Wise Guy's Viewpoint

Alex Martelli aleax at aleax.it
Sun Oct 19 18:39:41 EDT 2003


Frode Vatvedt Fjeld wrote:

> John Thingstad <john.thingstad at chello.no> writes:
> 
>> [..] Functions are internally delt with using dictionaies.  The

Rather, _names_ are dealt that way (for globals; it's faster for
locals -- then, the compiler can turn the name into an index
into the table of locals' values), whether they're names of functions
or names of other values (Python doesn't separate those namespaces).

>> bytecode compiler gives it a ID and the look up is done using a
>> dictionary.  Removing the function from the dictionary removes the
>> function.  (pythonese for hash-table)
> 
> So to get from the ID to the bytecode, you go through a dictionary?

No; it's up to the implementation, but in CPython the id is the
memory address of the function object, so the bytecode's directly
accessed from there (well, there's a couple of indirectness --
function object to code object to code string -- nothing important).

> And the mapping from name to ID happens perhaps when the caller is
> bytecode-compiled?

No, it's a lookup.  Dict lookup for globals, fast (index in table)
lookup for locals (making locals much faster to access), but a
lookup anyway.  I've already posted about how psyco can optimize
this, being a specializing compiler, when it notices the dynamic
possibilities are not being used in a given case.


Alex






More information about the Python-list mailing list