Type of __builtins__ changes from module import to execution?

Marc Christiansen usenet at solar-empire.de
Fri Jun 22 13:43:37 CEST 2007

Adam Hupp <adam at hupp.org> wrote:
> I've noticed some unexpected behavior with __builtins__ during module
> import.  It seems that during module import __builtins__  is a dict
> but at all other times it is a module.
> For example, if the file testmod.py has these contents:
> print type(__builtins__)
> print "has str attr", hasattr(__builtins__, 'str')
> The output differs depending on how it is run:
> $ python ~/testmod.py
> <type 'module'>
> has str True
> vs.
> $ python -c 'import testmod'
> <type 'dict'>
> has str False
> Anyone know if there a reason for this behavior?    Is it a bug?  I've
> seen this in 2.4 and 3.0.
> -Adam

No, it's not a bug. __builtins__ is an implementation detail. You want
__builtin__. See <http://docs.python.org/lib/module-builtin.html>


More information about the Python-list mailing list