| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1 | :mod:`builtins` --- Built-in objects | 
 | 2 | ==================================== | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 |  | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 4 | .. module:: builtins | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 |    :synopsis: The module that provides the built-in namespace. | 
 | 6 |  | 
 | 7 |  | 
 | 8 | This module provides direct access to all 'built-in' identifiers of Python; for | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 9 | example, ``builtins.open`` is the full name for the built-in function | 
| Éric Araujo | 96deb75 | 2011-06-08 04:53:20 +0200 | [diff] [blame] | 10 | :func:`open`.  See :ref:`built-in-funcs` and :ref:`built-in-consts` for | 
 | 11 | documentation. | 
 | 12 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 |  | 
 | 14 | This module is not normally accessed explicitly by most applications, but can be | 
 | 15 | useful in modules that provide objects with the same name as a built-in value, | 
 | 16 | but in which the built-in of that name is also needed.  For example, in a module | 
 | 17 | that wants to implement an :func:`open` function that wraps the built-in | 
 | 18 | :func:`open`, this module can be used directly:: | 
 | 19 |  | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 20 |    import builtins | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 |  | 
 | 22 |    def open(path): | 
| Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 23 |        f = builtins.open(path, 'r') | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 |        return UpperCaser(f) | 
 | 25 |  | 
 | 26 |    class UpperCaser: | 
 | 27 |        '''Wrapper around a file that converts output to upper-case.''' | 
 | 28 |  | 
 | 29 |        def __init__(self, f): | 
 | 30 |            self._f = f | 
 | 31 |  | 
 | 32 |        def read(self, count=-1): | 
 | 33 |            return self._f.read(count).upper() | 
 | 34 |  | 
 | 35 |        # ... | 
 | 36 |  | 
| Georg Brandl | 62f52c4 | 2010-11-26 12:08:19 +0000 | [diff] [blame] | 37 | As an implementation detail, most modules have the name ``__builtins__`` made | 
 | 38 | available as part of their globals.  The value of ``__builtins__`` is normally | 
| Florent Xicluna | 45c6c3e | 2011-11-11 19:55:21 +0100 | [diff] [blame] | 39 | either this module or the value of this module's :attr:`__dict__` attribute. | 
| Georg Brandl | 62f52c4 | 2010-11-26 12:08:19 +0000 | [diff] [blame] | 40 | Since this is an implementation detail, it may not be used by alternate | 
 | 41 | implementations of Python. |