Safe to import __builtin__ ?

Ben Finney ben+python at benfinney.id.au
Tue Jun 2 22:16:54 EDT 2009


mrstevegross <mrstevegross at gmail.com> writes:

> Is it generally safe to explicitly import __builtin__ in python? That
> is, my code reads like this:
> 
> === foo.py ===
> import __builtin__
> ...
> print __builtin__.type('a')
> === EOF ===
> 
> It seems like it should be a safe import, but I just want to make
> sure.

Yes, it's safe (and this is what the ‘__builtin__’ module is intended
for: <URL:http://docs.python.org/library/__builtin__>).

Be careful, though: there's a separate name, ‘__builtins__’, that is
*not* meant to be imported. It's also implementation-specific, so
shouldn't be relied upon. My advice: don't use ‘__builtins__’ at all,
but be aware that it exists so you spell ‘__builtin__’ correctly.

-- 
 \     “This world in arms is not spending money alone. It is spending |
  `\      the sweat of its laborers, the genius of its scientists, the |
_o__)           hopes of its children.” —Dwight Eisenhower, 1953-04-16 |
Ben Finney



More information about the Python-list mailing list