[Python-Dev] Fast access to __builtins__
Guido van Rossum
Thu, 27 Mar 2003 22:23:22 -0500
Hi Raymond. Too bad you couldn't make it to the conference! We're
all having a great time on and off the GWU premises. I used your
"more zen" on a slide in my keynote.
> From past rumblings, I gather that Python is moving
> towards preventing __builtins__ from being shadowed.
You must be misunderstanding.
The only thing I want to forbid is to stick a name in *another*
module's globals that would shadow a builtin. E.g. suppose module A
and module B contains:
A.len = lambda a: len(a) or 1 # evil len()
The assignment to A.len would be forbidden.
if random.random() >= 0.5:
len = 42
will always be allowed and mean what it currently means.
The difference is that in the first module, analysis of module A does
not reveal that len is shadowed; OTOH in the second example, analyzing
just the module's code shows that len may be a global built-in. This
is important because a programmer shouldn't have to know the names of
built-in objects she doesn't use (also important because in a future
version of the language, a name you've picked for a global may become
The idea of forbidding module B in the first example is that the
optimizer is allowed to replace len(a) with a bytecode that calls
PyOject_Size() rather than looking up "len" in globals and builtins.
The optimizer should only be allowed to make this assumption if
careful analysis of an entire module doesn't reveal any possibility
that "len" can be shadowed. But it cannot be required to look at all
other modules (since those other modules may not even have been
Hope this helps.
BTW this idea is quite old; I've described it a few years ago under a
subject something like "low-hanging fruit".
--Guido van Rossum (home page: http://www.python.org/~guido/)