Where is the __builtin__ module in Python3? Why was it renamed?

I was interested to learn about the __builtin__ module and how it was used, but I can not find it in Python3! Why was this moved?

Python 2.7

 >>> import __builtin__ >>> 

Python 3.2

 >>> import __builtin__ Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named __builtin__ >>> 
+39
python
Jan 28 '12 at 19:07
source share
1 answer

The __builtin__ module has been renamed builtins in Python3.

This change addresses two sources of confusion for the average Python developer.

  • Is it '__builtins__' or '__builtin__' in the global namespace? Darn!
  • __builtin__ a special method name or module? I can not tell.

This confusion is mainly due to a violation of pep8 . In addition, the lack of pluralization on the module also interferes with communication. Both of these are largely illustrated by Guido lengths, which should explain the following from http://mail.python.org/pipermail/python-ideas/2009-March/003821.html :

[CPython] looks at globals containing a special magic notation __builtins__ (with the character 's'), which is a dict where built-in functions are viewed. When this dict is the same object as the default inline dict (which is __builtin__.__dict__ , where __builtin__ - without 's' is the module that defines the built-in functions), it gives supervisor privileges; ...

For example,

python2.7

 >>> import __builtin__ >>> vars(globals()['__builtins__']) is vars(__builtin__) True >>> 

Python3.2

 >>> import builtins >>> vars(globals()['__builtins__']) is vars(builtins) True >>> 

Related Resources :

Other name changes - http://docs.pythonsprints.com/python3_porting/py-porting.html#name-changes

For a brief explanation of how __builtins__ used for name resolution - __ built-in module in Python

+55
Jan 28 '12 at 19:09
source share



All Articles