“Python/docs/3.9/library/builtins”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/builtins
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:builtins — 内置对象 — Python 文档}}
 
<div id="module-builtins" class="section">
 
<div id="module-builtins" class="section">
  
 
<span id="builtins-built-in-objects"></span>
 
<span id="builtins-built-in-objects"></span>
= [[#module-builtins|<code>builtins</code>]] --- Built-in objects =
+
= builtins — 内置对象 =
  
This module provides direct access to all 'built-in' identifiers of Python; for
 
example, <code>builtins.open</code> is the full name for the built-in function
 
[[../functions#open|<code>open()</code>]]. See [[../functions#built-in-funcs|<span class="std std-ref">Built-in Functions</span>]] and [[../constants#built-in-consts|<span class="std std-ref">Built-in Constants</span>]] 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
+
该模块提供对 Python 的所有“内置”标识符的直接访问; 例如,<code>builtins.open</code> 是内置函数 [[../functions#open|open()]] 的全名。 有关文档,请参阅 [[../functions#built-in-funcs|内置函数]] 和 [[../constants#built-in-consts|内置常量]] 。
that wants to implement an [[../functions#open|<code>open()</code>]] function that wraps the built-in
+
 
[[../functions#open|<code>open()</code>]], this module can be used directly:
+
大多数应用程序通常不会显式访问此模块,但在提供与内置值具有相同名称的对象的模块中很有用,但在这些模块中也需要该名称的内置值。 例如,在一个模块中,想要实现一个封装了内置的 [[../functions#open|open()]] 的 [[../functions#open|open()]] 函数,可以直接使用这个模块:
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第19行: 第16行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import builtins
+
<syntaxhighlight lang="python3">import builtins
  
 
def open(path):
 
def open(path):
第34行: 第31行:
 
         return self._f.read(count).upper()
 
         return self._f.read(count).upper()
  
     # ...</pre>
+
     # ...</syntaxhighlight>
 +
 
 +
</div>
  
 
</div>
 
</div>
 +
作为一个实现细节,大多数模块都有名称 <code>__builtins__</code> 作为其全局变量的一部分。 <code>__builtins__</code> 的值通常是这个模块或者这个模块的 [[../stdtypes#object|__dict__]] 属性的值。 由于这是一个实现细节,它可能不会被 Python 的替代实现使用。
 +
  
 
</div>
 
</div>
As an implementation detail, most modules have the name <code>__builtins__</code> made
+
<div class="clearer">
available as part of their globals. The value of <code>__builtins__</code> is normally
+
 
either this module or the value of this module's [[../stdtypes#object|<code>__dict__</code>]] attribute.
 
Since this is an implementation detail, it may not be used by alternate
 
implementations of Python.
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

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 upper-case.'''

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

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

    # ...

作为一个实现细节,大多数模块都有名称 __builtins__ 作为其全局变量的一部分。 __builtins__ 的值通常是这个模块或者这个模块的 __dict__ 属性的值。 由于这是一个实现细节,它可能不会被 Python 的替代实现使用。