Home | History | Annotate | Download | only in library
      1 :mod:`builtins` --- Built-in objects
      2 ====================================
      3 
      4 .. module:: builtins
      5    :synopsis: The module that provides the built-in namespace.
      6 
      7 --------------
      8 
      9 This module provides direct access to all 'built-in' identifiers of Python; for
     10 example, ``builtins.open`` is the full name for the built-in function
     11 :func:`open`.  See :ref:`built-in-funcs` and :ref:`built-in-consts` for
     12 documentation.
     13 
     14 
     15 This module is not normally accessed explicitly by most applications, but can be
     16 useful in modules that provide objects with the same name as a built-in value,
     17 but in which the built-in of that name is also needed.  For example, in a module
     18 that wants to implement an :func:`open` function that wraps the built-in
     19 :func:`open`, this module can be used directly::
     20 
     21    import builtins
     22 
     23    def open(path):
     24        f = builtins.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 
     38 As an implementation detail, most modules have the name ``__builtins__`` made
     39 available as part of their globals.  The value of ``__builtins__`` is normally
     40 either this module or the value of this module's :attr:`~object.__dict__` attribute.
     41 Since this is an implementation detail, it may not be used by alternate
     42 implementations of Python.
     43