By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 1,993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

Type of __builtins__ changes from module import to execution?

P: n/a
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

Jun 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Adam Hupp <ad**@hupp.orgwrote:
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>

Ciao
Marc
Jun 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.