Built-in Constants¶ 内置常量 ¶
A small number of constants live in the built-in namespace. They are:
一些常量存在于内置命名空间中。它们是:
- False¶ 虚假
The false value of the
bool
type. Assignments toFalse
are illegal and raise aSyntaxError
.
bool
类型的假值。对False
的赋值是非法的,并引发SyntaxError
。
- True¶ 真实的
The true value of the
bool
type. Assignments toTrue
are illegal and raise aSyntaxError
.
bool
类型的真实值。对True
的赋值是非法的,并引发SyntaxError
。
- None¶ 无
An object frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to
None
are illegal and raise aSyntaxError
.None
is the sole instance of theNoneType
type.
一个经常用来表示值的缺失的对象,比如当默认参数没有传递给函数时。对None
的赋值是非法的,并会引发一个SyntaxError
。None
是NoneType
类型的唯一实例。
- NotImplemented¶ 未实现的 ¶
A special value which should be returned by the binary special methods (e.g.
__eq__()
,__lt__()
,__add__()
,__rsub__()
, 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.__imul__()
,__iand__()
, etc.) for the same purpose. It should not be evaluated in a boolean context.NotImplemented
is the sole instance of thetypes.NotImplementedType
type.
特殊值应由二进制特殊方法(例如__eq__()
,__lt__()
,__add__()
,__rsub__()
等)返回,以指示该操作在其他类型上未实现;可以由就地二进制特殊方法(例如__imul__()
,__iand__()
等)出于同样的目的返回。它不应该在布尔环境中进行评估。NotImplemented
是types.NotImplementedType
类型的唯一实例。Note
When a binary (or in-place) method returns
NotImplemented
the interpreter will try the reflected operation on the other type (or some other fallback, depending on the operator). If all attempts returnNotImplemented
, the interpreter will raise an appropriate exception. Incorrectly returningNotImplemented
will result in a misleading error message or theNotImplemented
value being returned to Python code.
注意:当一个二进制(或原地)方法返回NotImplemented
时,解释器将尝试在另一种类型上执行反射操作(或者根据运算符的不同选择其他回退方法)。如果所有尝试都返回NotImplemented
,解释器将引发适当的异常。错误地返回NotImplemented
将导致一个误导性的错误消息或NotImplemented
值被返回给 Python 代码。See Implementing the arithmetic operations for examples.
请参考实现算术运算的示例。Note
NotImplementedError
andNotImplemented
are not interchangeable, even though they have similar names and purposes. SeeNotImplementedError
for details on when to use it.
请注意,NotImplementedError
和NotImplemented
不能互换使用,尽管它们的名称和用途相似。有关何时使用它的详细信息,请参见NotImplementedError
。Changed in version 3.9: Evaluating
NotImplemented
in a boolean context is deprecated. While it currently evaluates as true, it will emit aDeprecationWarning
. It will raise aTypeError
in a future version of Python.
从 3.9 版本开始更改:在布尔上下文中评估NotImplemented
已被弃用。虽然它当前评估为 true,但它将发出DeprecationWarning
。在未来的 Python 版本中,它将引发TypeError
。
- Ellipsis¶ 省略号 ¶
The same as the ellipsis literal “
...
”. Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.Ellipsis
is the sole instance of thetypes.EllipsisType
type.
与省略号字面值“...
”相同。主要与扩展切片语法一起用于用户定义的容器数据类型。Ellipsis
是types.EllipsisType
类型的唯一实例。
- __debug__¶
This constant is true if Python was not started with an
-O
option. See also theassert
statement.
如果 Python 没有使用-O
选项启动,则此常量为真。另请参见assert
语句。
Note
The names None
, False
, True
and __debug__
cannot be reassigned (assignments to them, even as an attribute name, raise
SyntaxError
), so they can be considered “true” constants.
注意,名称
None
、 False
、 True
和 __debug__
不能重新赋值(即使作为属性名称进行赋值也会引发 SyntaxError
),因此它们可以被视为“真正”的常量。
Constants added by the site
module¶
site
模块添加的常量¶
The site
module (which is imported automatically during startup, except
if the -S
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.
site
模块(在启动时自动导入,除非给出 -S
命令行选项)向内置命名空间添加了几个常量。它们对于交互式解释器 shell 很有用,不应在程序中使用。
- quit(code=None)¶ 退出(code=None)¶
- exit(code=None)¶ 退出(代码=无)¶
Objects that when printed, print a message like “Use quit() or Ctrl-D (i.e. EOF) to exit”, and when called, raise
SystemExit
with the specified exit code.
打印时输出如“使用 quit()或 Ctrl-D(即 EOF)退出”,调用时引发指定的退出码。SystemExit
- copyright¶ 版权
- credits¶ 学分
Objects that when printed or called, print the text of copyright or credits, respectively.
打印或调用时,分别打印版权或信用的文本的对象。
- license¶ 许可证
Object that when printed, prints the message “Type license() to see the full license text”, and when called, displays the full license text in a pager-like fashion (one screen at a time).