[Python-3000] __builtin__ and __builtins__

Guido van Rossum guido at python.org
Mon Mar 12 19:53:57 CET 2007


This is all my fault.

Long ago, there was only __builtin__, and all was well; you had to
import it before using it.

Then I added __builtins__ as an internal hook for restricted execution
mode. The idea was that if __builtins__ was set to something
nonstandard you were automatically in restricted mode (which just
turned off a few instrospection features); all your build-in lookups
were done here.

But why is __builtins__ a module in __main__ and a dict elsewhere?
Because in *interactive* mode, printing vars() would include
__builtins__, which would be rather large. Code that incorrectly
assumes it's always one or the other is incorrect and has always been
incorrect; the wart was present when this feature first appeared.
Since this has never been documented AFAIK, it's probably just been
(incorrectly) reverse-engineered and copied around.

I am all for unifying __builtin__ and __builtins__, exactly as Ping's
point #3. Normally, importing __builtin__ again would not change
anything because it'll replace the object with the same object. If you
use some hack to import something else as __builtin__, yes, it would
be good to trap that but I woudn't mind if it simply didn't work
right, being a useless idea.

--Guido

On 3/11/07, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Ka-Ping Yee wrote:
>
> > A little investigation reveals:
> >
> >     In module __main__:
> >         __builtins__ is a reference to module __builtin__.
> >         __builtin__ only exists if you import it.
> >
> >     In any other module:
> >         __builtins__ is a reference to module __builtin__'s __dict__.
> >         __builtin__ only exists if you import it.
>
> As I understand it, __builtins__ has always been
> regarded as an implementation detail, and if you
> want to get hold of the builtins, importing
> __builtin__ is the correct way to do it.
>
> The difference between __main__ and other modules
> is a bit weird. Phil Hassey posted some code
> recently that seemed to assume __builtins__ was
> always a module, which makes me think it was
> changed at some point to speed things up, but
> __main__ was somehow inadvertently excluded from
> the change.
>
> Changing __main__ to match would seem to be a
> good idea. I don't think it would be such a good
> idea to unify __builtin__ and __builtins__,
> because then importing __builtin__ would clobber
> the existing builtin namespace being used by
> the code -- which may not be the same thing.
>
> There might be merit in renaming __builtins__
> to something less confusable, at the expense of
> breaking existing code which refers to it.
>
> --
> Greg
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


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


More information about the Python-3000 mailing list