builtinsBuilt-in objects内置对象


This module provides direct access to all ‘built-in’ identifiers of Python; for example, builtins.open is the full name for the built-in function open(). 该模块提供对Python所有“内置”标识符的直接访问;例如,builtins.open是内置函数open()的全名。See Built-in Functions and Built-in Constants for documentation.有关文档,请参阅内置函数内置常量

This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed. 大多数应用程序通常不会显式访问此模块,但在提供与内置值同名的对象的模块中非常有用,但在这些模块中也需要内置该名称。For example, in a module that wants to implement an open() function that wraps the built-in open(), this module can be used directly:例如,在一个想要实现包装内置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 upper-case.'''

def __init__(self, f):
self._f = f

def read(self, count=-1):
return self._f.read(count).upper()

# ...

As an implementation detail, most modules have the name __builtins__ made available as part of their globals. 作为实现细节,大多数模块的名称__builtins__作为其全局变量的一部分提供。The value of __builtins__ is normally either this module or the value of this module’s __dict__ attribute. __builtins__的值通常是此模块或此模块的__dict__属性的值。Since this is an implementation detail, it may not be used by alternate implementations of Python.因为这是一个实现细节,所以Python的其他实现可能不会使用它。