variable name lookup

Alex Martelli aleax at aleax.it
Thu Nov 7 10:20:23 EST 2002


ffdsfdsfds wrote:

> Hi,
> I would like to know which method of the dictionary object
> is used for the lookup of variables at execution of the bytecode.\

You can check in ceval.c, and you'll find out that strictly
speaking none is...:

case LOAD_GLOBAL:
        w = GETITEM(names, oparg);
        if (PyString_CheckExact(w)) {
                /* Inline the PyDict_GetItem() calls.
                   WARNING: this is an extreme speed hack.
                   Do not try this at home. */
                long hash = ((PyStringObject *)w)->ob_shash;
  [ etc etc, snipped; lines 1714 ff in ceval.c for Python 2.2.2 ]

i.e., because of the necessary "extreme speed hack", PyDict_GetItem
isn't exactly "used" -- rather, it's basically duplicated inline
right in the "case LOAD_GLOBAL:" leg of the switch (when the
variable's name is of type string exactly).

I'm not sure why you need this information, but if it's in order
to be able to use a subclass of dict and override its methods to
influence variable lookup, forget it -- none of the various var
access paths in ceval.c uses namespace dictionaries at all
polymorphically (for local variables in particular, this is
even reflected in the bytecode, e.g. LOAD_FAST rather than
LOAD_GLOBAL or LOAD_NAME...).  Speed, apparently, is just too
important here for this fundamental task to be handled in any
other way except full-speed-ahead.


Alex




More information about the Python-list mailing list