blob: f6abe25af9e831d2cb3e9807504e3e76f16aadc1 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{__builtin__} ---
Fred Drake0a6864e2004-12-23 16:50:36 +00002 Built-in objects}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakedeb7e091999-04-23 17:11:53 +00004\declaremodule[builtin]{builtin}{__builtin__}
Fred Drake0a6864e2004-12-23 16:50:36 +00005\modulesynopsis{The module that provides the built-in namespace.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Guido van Rossum470be141995-03-17 16:07:09 +00008This module provides direct access to all `built-in' identifiers of
Fred Drake0a6864e2004-12-23 16:50:36 +00009Python; for example, \code{__builtin__.open} is the full name for the
10built-in function \function{open()}. See chapter~\ref{builtin},
11``Built-in Objects.''
12
13This module is not normally accessed explicitly by most applications,
14but can be useful in modules that provide objects with the same name
15as a built-in value, but in which the built-in of that name is also
16needed. For example, in a module that wants to implement an
17\function{open()} function that wraps the built-in \function{open()},
18this module can be used directly:
19
20\begin{verbatim}
21import __builtin__
22
23def open(path):
24 f = __builtin__.open(path, 'r')
25 return UpperCaser(f)
26
27class 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
39As an implementation detail, most modules have the name
40\code{__builtins__} (note the \character{s}) made available as part of
41their globals. The value of \code{__builtins__} is normally either
42this module or the value of this modules's \member{__dict__}
43attribute. Since this is an implementation detail, it may not be used
44by alternate implementations of Python.