[Python-3000] __builtin__ and __builtins__

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Mar 12 05:37:20 CET 2007


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


More information about the Python-3000 mailing list