builtins — 内置对象


此模块提供对 Python 所有内置标识符的直接访问;例如, builtins.open 是完整名称对于内置函数 open() .

大多数应用程序通常不会明确访问此模块,但在提供同名内置值对象的模块 (其中还需要内置名称) 中会很有用。例如,当模块想要实现 open() 函数以包裹内置 open() ,可以直接使用此模块:

import builtins
def open(path):
    f = builtins.open(path, 'r')
    return UpperCaser(f)
class UpperCaser:
    '''Wrapper around a file that converts output to uppercase.'''
    def __init__(self, f):
        self._f = f
    def read(self, count=-1):
        return self._f.read(count).upper()
    # ...