blob: 217b161380327bde77d447db4948e58a97572027 [file] [log] [blame]
Georg Brandl1a3284e2007-12-02 09:40:06 +00001:mod:`builtins` --- Built-in objects
2====================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl1a3284e2007-12-02 09:40:06 +00004.. module:: builtins
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: The module that provides the built-in namespace.
6
7
8This module provides direct access to all 'built-in' identifiers of Python; for
Georg Brandl1a3284e2007-12-02 09:40:06 +00009example, ``builtins.open`` is the full name for the built-in function
Georg Brandl116aa622007-08-15 14:28:22 +000010:func:`open`. See chapter :ref:`builtin`.
11
12This module is not normally accessed explicitly by most applications, but can be
13useful in modules that provide objects with the same name as a built-in value,
14but in which the built-in of that name is also needed. For example, in a module
15that wants to implement an :func:`open` function that wraps the built-in
16:func:`open`, this module can be used directly::
17
Georg Brandl1a3284e2007-12-02 09:40:06 +000018 import builtins
Georg Brandl116aa622007-08-15 14:28:22 +000019
20 def open(path):
Georg Brandl1a3284e2007-12-02 09:40:06 +000021 f = builtins.open(path, 'r')
Georg Brandl116aa622007-08-15 14:28:22 +000022 return UpperCaser(f)
23
24 class UpperCaser:
25 '''Wrapper around a file that converts output to upper-case.'''
26
27 def __init__(self, f):
28 self._f = f
29
30 def read(self, count=-1):
31 return self._f.read(count).upper()
32
33 # ...
34
35As an implementation detail, most modules have the name ``__builtins__`` (note
36the ``'s'``) made available as part of their globals. The value of
37``__builtins__`` is normally either this module or the value of this modules's
38:attr:`__dict__` attribute. Since this is an implementation detail, it may not
39be used by alternate implementations of Python.
40