blob: 8fb1fef6da2954a37ed5794774602575c648c8ef [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007--------------
Georg Brandl116aa622007-08-15 14:28:22 +00008
9This module provides direct access to all 'built-in' identifiers of Python; for
Georg Brandl1a3284e2007-12-02 09:40:06 +000010example, ``builtins.open`` is the full name for the built-in function
Éric Araujo96deb752011-06-08 04:53:20 +020011:func:`open`. See :ref:`built-in-funcs` and :ref:`built-in-consts` for
12documentation.
13
Georg Brandl116aa622007-08-15 14:28:22 +000014
15This module is not normally accessed explicitly by most applications, but can be
16useful in modules that provide objects with the same name as a built-in value,
17but in which the built-in of that name is also needed. For example, in a module
18that wants to implement an :func:`open` function that wraps the built-in
19:func:`open`, this module can be used directly::
20
Georg Brandl1a3284e2007-12-02 09:40:06 +000021 import builtins
Georg Brandl116aa622007-08-15 14:28:22 +000022
23 def open(path):
Georg Brandl1a3284e2007-12-02 09:40:06 +000024 f = builtins.open(path, 'r')
Georg Brandl116aa622007-08-15 14:28:22 +000025 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
Georg Brandl62f52c42010-11-26 12:08:19 +000038As an implementation detail, most modules have the name ``__builtins__`` made
39available as part of their globals. The value of ``__builtins__`` is normally
Martin Panterbae5d812016-06-18 03:57:31 +000040either this module or the value of this module's :attr:`~object.__dict__` attribute.
Georg Brandl62f52c42010-11-26 12:08:19 +000041Since this is an implementation detail, it may not be used by alternate
42implementations of Python.