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

来自菜鸟教程
Python/docs/3.9/library/constants
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:内置常量 — Python 文档}}
 
<div id="built-in-constants" class="section">
 
<div id="built-in-constants" class="section">
  
 
<span id="built-in-consts"></span>
 
<span id="built-in-consts"></span>
= Built-in Constants =
+
= 内置常量 =
  
A small number of constants live in the built-in namespace. They are:
+
少量常量存在于内置命名空间中。 他们是:
  
; <code>False</code>
+
; <span class="sig-name descname"><span class="pre">False</span></span>
: The false value of the [[../functions#bool|<code>bool</code>]] type. Assignments to <code>False</code> are illegal and raise a [[../exceptions#SyntaxError|<code>SyntaxError</code>]].
+
: [[../functions#bool|bool]] 类型的假值。 对 <code>False</code> 的赋值是非法的,并引发 [[../exceptions#SyntaxError|SyntaxError]]
  
; <code>True</code>
+
; <span class="sig-name descname"><span class="pre">True</span></span>
: The true value of the [[../functions#bool|<code>bool</code>]] type. Assignments to <code>True</code> are illegal and raise a [[../exceptions#SyntaxError|<code>SyntaxError</code>]].
+
: [[../functions#bool|bool]] 类型的真实值。 对 <code>True</code> 的赋值是非法的,并引发 [[../exceptions#SyntaxError|SyntaxError]]
  
; <code>None</code>
+
; <span class="sig-name descname"><span class="pre">None</span></span>
: The sole value of the type <code>NoneType</code>. <code>None</code> is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to <code>None</code> are illegal and raise a [[../exceptions#SyntaxError|<code>SyntaxError</code>]].
+
: <code>NoneType</code> 类型的唯一值。 <code>None</code> 经常用于表示没有值,因为默认参数没有传递给函数。 对 <code>None</code> 的赋值是非法的,并引发 [[../exceptions#SyntaxError|SyntaxError]]
  
 
<dl>
 
<dl>
<dt><code>NotImplemented</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">NotImplemented</span></span></dt>
<dd><p>Special value which should be returned by the binary special methods
+
<dd><p>应由二进制特殊方法返回的特殊值(例如 <code>__eq__()</code><code>__lt__()</code><code>__add__()</code><code>__rsub__()</code>等)表示该操作未针对其他类型实现; 可以由就地二进制特殊方法返回(例如 <code>__imul__()</code><code>__iand__()</code> 等)用于相同目的。 不应在布尔上下文中对其进行评估。</p>
(e.g. <code>__eq__()</code>, <code>__lt__()</code>, <code>__add__()</code>, <code>__rsub__()</code>,
 
etc.) to indicate that the operation is not implemented with respect to
 
the other type; may be returned by the in-place binary special methods
 
(e.g. <code>__imul__()</code>, <code>__iand__()</code>, etc.) for the same purpose.
 
It should not be evaluated in a boolean context.</p>
 
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>When a binary (or in-place) method returns <code>NotImplemented</code> the
+
<p>当二进制(或就地)方法返回 <code>NotImplemented</code> 时,解释器将尝试对其他类型的反射操作(或其他一些回退,取决于运算符)。 如果所有尝试都返回 <code>NotImplemented</code>,解释器将引发适当的异常。 错误地返回 <code>NotImplemented</code> 将导致误导性错误消息或 <code>NotImplemented</code> 值返回给 Python 代码。</p>
interpreter will try the reflected operation on the other type (or some
+
<p>有关示例,请参阅 [[../numbers#implementing-the-arithmetic-operations|实现算术运算]] </p>
other fallback, depending on the operator). If all attempts return
 
<code>NotImplemented</code>, the interpreter will raise an appropriate exception.
 
Incorrectly returning <code>NotImplemented</code> will result in a misleading
 
error message or the <code>NotImplemented</code> value being returned to Python code.</p>
 
<p>See [[../numbers#implementing-the-arithmetic-operations|<span class="std std-ref">Implementing the arithmetic operations</span>]] for examples.</p>
 
  
 
</div>
 
</div>
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p><code>NotImplementedError</code> and <code>NotImplemented</code> are not interchangeable,
+
<p><code>NotImplementedError</code> <code>NotImplemented</code> 不可互换,即使它们具有相似的名称和用途。 有关何时使用它的详细信息,请参阅 [[../exceptions#NotImplementedError|NotImplementedError]]</p>
even though they have similar names and purposes.
 
See [[../exceptions#NotImplementedError|<code>NotImplementedError</code>]] for details on when to use it.</p>
 
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.9 版更改: </span>Evaluating <code>NotImplemented</code> in a boolean context is deprecated. While
+
<p><span class="versionmodified changed"> 3.9 版更改:</span> 在布尔上下文中评估 <code>NotImplemented</code> 已弃用。 虽然它当前评估为 true,但它会发出 [[../exceptions#DeprecationWarning|DeprecationWarning]]。 它会在 Python 的未来版本中引发 [[../exceptions#TypeError|TypeError]]</p>
it currently evaluates as true, it will emit a [[../exceptions#DeprecationWarning|<code>DeprecationWarning</code>]].
 
It will raise a [[../exceptions#TypeError|<code>TypeError</code>]] in a future version of Python.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<span id="index-0" class="target"></span>
 
<span id="index-0" class="target"></span>
; <code>Ellipsis</code>
+
; <span class="sig-name descname"><span class="pre">Ellipsis</span></span>
: The same as the ellipsis literal &quot;<code>...</code>&quot;. Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.
+
: 与省略号文字“<code>...</code>”相同。 特殊值主要与用户定义的容器数据类型的扩展切片语法结合使用。
  
; <span id="__debug__"></span><code>__debug__</code>
+
; <span id="__debug__"></span><span class="sig-name descname"><span class="pre">__debug__</span></span>
: This constant is true if Python was not started with an [[../../using/cmdline#cmdoption-O|<code>-O</code>]] option. See also the [[../../reference/simple_stmts#assert|<code>assert</code>]] statement.
+
: 如果 Python 没有使用 [[../../using/cmdline#cmdoption-O|-O]] 选项启动,则此常量为真。 另请参阅 [[../../reference/simple_stmts#assert|assert]] 语句。
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
The names [[#None|<code>None</code>]], [[#False|<code>False</code>]], [[#True|<code>True</code>]] and [[#debug__|<code>__debug__</code>]]
+
名称 [[#None|None]][[#False|False]][[#True|True]] [[#debug__|__debug__]] 不能重新分配(分配给它们,即使作为属性名称,也会引发 [X154X ]SyntaxError),所以它们可以被认为是“真正的”常量。
cannot be reassigned (assignments to them, even as an attribute name, raise
 
[[../exceptions#SyntaxError|<code>SyntaxError</code>]]), so they can be considered &quot;true&quot; constants.
 
  
  
第70行: 第55行:
 
<div id="constants-added-by-the-site-module" class="section">
 
<div id="constants-added-by-the-site-module" class="section">
  
== Constants added by the [[../site#module-site|<code>site</code>]] module ==
+
== site 模块添加的常量 ==
  
The [[../site#module-site|<code>site</code>]] module (which is imported automatically during startup, except
+
[[../site#module-site|site]] 模块(在启动期间自动导入,除非给出了 [[../../using/cmdline#cmdoption-S|-S]] 命令行选项)向内置命名空间添加了几个常量。 它们对于交互式解释器 shell 很有用,不应在程序中使用。
if the [[../../using/cmdline#cmdoption-S|<code>-S</code>]] command-line option is given) adds several constants to the
 
built-in namespace. They are useful for the interactive interpreter shell and
 
should not be used in programs.
 
  
; <code>quit</code><span class="sig-paren">(</span>''<span class="n">code</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span><br />
+
; <span class="sig-name descname"><span class="pre">quit</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span><br />
<code>exit</code><span class="sig-paren">(</span>''<span class="n">code</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
<span class="sig-name descname"><span class="pre">exit</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span>
: Objects that when printed, print a message like &quot;Use quit() or Ctrl-D (i.e. EOF) to exit&quot;, and when called, raise [[../exceptions#SystemExit|<code>SystemExit</code>]] with the specified exit code.
+
: 对象在打印时,会打印出类似“Use quit() Ctrl-D(即 EOF) 退出”,并在调用时使用指定的退出代码引发 [[../exceptions#SystemExit|SystemExit]]
  
; <code>copyright</code><br />
+
; <span class="sig-name descname"><span class="pre">copyright</span></span><br />
<code>credits</code>
+
<span class="sig-name descname"><span class="pre">credits</span></span>
: Objects that when printed or called, print the text of copyright or credits, respectively.
+
: 打印或调用时分别打印版权或署名文本的对象。
  
; <code>license</code>
+
; <span class="sig-name descname"><span class="pre">license</span></span>
: Object that when printed, prints the message &quot;Type license() to see the full license text&quot;, and when called, displays the full license text in a pager-like fashion (one screen at a time).
+
: 打印时打印消息“Type license() 以查看完整许可证文本”的对象,并在调用时以类似寻呼机的方式(一次一个屏幕)显示完整的许可证文本。
  
  
第92行: 第74行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Python 3.9 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Python 3.9 文档]]

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

内置常量

少量常量存在于内置命名空间中。 他们是:

False
bool 类型的假值。 对 False 的赋值是非法的,并引发 SyntaxError
True
bool 类型的真实值。 对 True 的赋值是非法的,并引发 SyntaxError
None
NoneType 类型的唯一值。 None 经常用于表示没有值,因为默认参数没有传递给函数。 对 None 的赋值是非法的,并引发 SyntaxError
NotImplemented

应由二进制特殊方法返回的特殊值(例如 __eq__()__lt__()__add__()__rsub__()等)表示该操作未针对其他类型实现; 可以由就地二进制特殊方法返回(例如 __imul__()__iand__() 等)用于相同目的。 不应在布尔上下文中对其进行评估。

笔记

当二进制(或就地)方法返回 NotImplemented 时,解释器将尝试对其他类型的反射操作(或其他一些回退,取决于运算符)。 如果所有尝试都返回 NotImplemented,解释器将引发适当的异常。 错误地返回 NotImplemented 将导致误导性错误消息或 NotImplemented 值返回给 Python 代码。

有关示例,请参阅 实现算术运算

笔记

NotImplementedErrorNotImplemented 不可互换,即使它们具有相似的名称和用途。 有关何时使用它的详细信息,请参阅 NotImplementedError

3.9 版更改: 在布尔上下文中评估 NotImplemented 已弃用。 虽然它当前评估为 true,但它会发出 DeprecationWarning。 它会在 Python 的未来版本中引发 TypeError

Ellipsis
与省略号文字“...”相同。 特殊值主要与用户定义的容器数据类型的扩展切片语法结合使用。
__debug__
如果 Python 没有使用 -O 选项启动,则此常量为真。 另请参阅 assert 语句。

笔记

名称 NoneFalseTrue__debug__ 不能重新分配(分配给它们,即使作为属性名称,也会引发 [X154X ]SyntaxError),所以它们可以被认为是“真正的”常量。


site 模块添加的常量

site 模块(在启动期间自动导入,除非给出了 -S 命令行选项)向内置命名空间添加了几个常量。 它们对于交互式解释器 shell 很有用,不应在程序中使用。

quit(code=None)

exit(code=None)

对象在打印时,会打印出类似“Use quit() 或 Ctrl-D(即 EOF) 退出”,并在调用时使用指定的退出代码引发 SystemExit
copyright

credits

打印或调用时分别打印版权或署名文本的对象。
license
打印时打印消息“Type license() 以查看完整许可证文本”的对象,并在调用时以类似寻呼机的方式(一次一个屏幕)显示完整的许可证文本。