[Python-Dev] Using defaultdict as globals/locals for eval()

Guido van Rossum guido@python.org
Mon, 28 Oct 2002 09:14:53 -0500


> > I created a derived class of the standard `dict' that fills in a
> > default value when a key is not found. This is exactly the same as
> > Guido describes in his "descintro" paper. I tried to use this
> > dictionary as the "globals" parameter with eval(). As Guido
> > already describes in his paper, this doesn't work.
> 
> I've wanted this for years and think it would be an important
> improvement.  Unfortunately, it appears to be a problem without 
> a simple solution.

The solution appears simple (use PyObject_GetItem etc. instead of
PyDict_GetItem) but would cause serious performance hits: the dict API
and its implementatio are highly optimized for this; PyObject_GetItem
would add several levels of function calls, *plus* more reference
count handling and exception handling.  Oren Tirosh's global/builtin
lookup speedup would be impossible.  (Oren, how's that coming?)

In addition, I'm concerned that this would become a popular tool to
hack Python's semantics in all sorts of ways.  While hacking the
semantics is essential for some situations, in most cases this causes
more problems than it solves because you can't use standard tools like
PyChecker when you change something as fundamental as how variable
references are resolved.

--Guido van Rossum (home page: http://www.python.org/~guido/)