blob: 2cca1d05dfbe6a67856fb97e6aad8354ac99ab0b [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
Éric Araujo96deb752011-06-08 04:53:20 +020010:func:`open`. See :ref:`built-in-funcs` and :ref:`built-in-consts` for
11documentation.
12
Georg Brandl116aa622007-08-15 14:28:22 +000013
14This module is not normally accessed explicitly by most applications, but can be
15useful in modules that provide objects with the same name as a built-in value,
16but in which the built-in of that name is also needed. For example, in a module
17that wants to implement an :func:`open` function that wraps the built-in
18:func:`open`, this module can be used directly::
19
Georg Brandl1a3284e2007-12-02 09:40:06 +000020 import builtins
Georg Brandl116aa622007-08-15 14:28:22 +000021
22 def open(path):
Georg Brandl1a3284e2007-12-02 09:40:06 +000023 f = builtins.open(path, 'r')
Georg Brandl116aa622007-08-15 14:28:22 +000024 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 Brandl62f52c42010-11-26 12:08:19 +000037As an implementation detail, most modules have the name ``__builtins__`` made
38available as part of their globals. The value of ``__builtins__`` is normally
Florent Xicluna45c6c3e2011-11-11 19:55:21 +010039either this module or the value of this module's :attr:`__dict__` attribute.
Georg Brandl62f52c42010-11-26 12:08:19 +000040Since this is an implementation detail, it may not be used by alternate
41implementations of Python.