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