Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{__builtin__} --- |
Fred Drake | 0a6864e | 2004-12-23 16:50:36 +0000 | [diff] [blame^] | 2 | Built-in objects} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | deb7e09 | 1999-04-23 17:11:53 +0000 | [diff] [blame] | 4 | \declaremodule[builtin]{builtin}{__builtin__} |
Fred Drake | 0a6864e | 2004-12-23 16:50:36 +0000 | [diff] [blame^] | 5 | \modulesynopsis{The module that provides the built-in namespace.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 8 | This module provides direct access to all `built-in' identifiers of |
Fred Drake | 0a6864e | 2004-12-23 16:50:36 +0000 | [diff] [blame^] | 9 | Python; for example, \code{__builtin__.open} is the full name for the |
| 10 | built-in function \function{open()}. See chapter~\ref{builtin}, |
| 11 | ``Built-in Objects.'' |
| 12 | |
| 13 | This module is not normally accessed explicitly by most applications, |
| 14 | but can be useful in modules that provide objects with the same name |
| 15 | as a built-in value, but in which the built-in of that name is also |
| 16 | needed. For example, in a module that wants to implement an |
| 17 | \function{open()} function that wraps the built-in \function{open()}, |
| 18 | this module can be used directly: |
| 19 | |
| 20 | \begin{verbatim} |
| 21 | import __builtin__ |
| 22 | |
| 23 | def open(path): |
| 24 | f = __builtin__.open(path, 'r') |
| 25 | return UpperCaser(f) |
| 26 | |
| 27 | class UpperCaser: |
| 28 | '''Wrapper around a file that converts output to upper-case.''' |
| 29 | |
| 30 | def __init__(self, f): |
| 31 | self._f = f |
| 32 | |
| 33 | def read(self, count=-1): |
| 34 | return self._f.read(count).upper() |
| 35 | |
| 36 | # ... |
| 37 | \end{verbatim} |
| 38 | |
| 39 | As an implementation detail, most modules have the name |
| 40 | \code{__builtins__} (note the \character{s}) made available as part of |
| 41 | their globals. The value of \code{__builtins__} is normally either |
| 42 | this module or the value of this modules's \member{__dict__} |
| 43 | attribute. Since this is an implementation detail, it may not be used |
| 44 | by alternate implementations of Python. |