Type of __builtins__ changes from module import to execution?

Adam Hupp adam at hupp.org
Fri Jun 22 05:49:38 CEST 2007

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


$ 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.


More information about the Python-list mailing list