Built-in Constants内建常量

A small number of constants live in the built-in namespace. 内置名称空间中存在少量常量。They are:它们是:

False

The false value of the bool type. bool类型的false值。Assignments to False are illegal and raise a SyntaxError.False的赋值是非法的,会引起SyntaxError

True

The true value of the bool type. bool类型的真实值。Assignments to True are illegal and raise a SyntaxError.赋值为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 a SyntaxError. 分配给None是非法的,并且会引起语法错误。None is the sole instance of the NoneType type.NoneType的唯一实例。

NotImplemented

A special value which should be returned by the binary special methods (e.g. __eq__(), __lt__(), __add__(), __rsub__(), etc.) 应通过二进制特殊方法返回的特殊值(例如__eq__()__lt__()__add_()__rsub__()等)。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. 表明未针对其他类型执行该操作;可以通过就地二进制特殊方法(例如__imul__()__iand__()等)返回实机相同的目的。It should not be evaluated in a boolean context. 不应在布尔上下文中对其求值。NotImplemented is the sole instance of the types.NotImplementedType type.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). 当二进制(或就地)方法返回NotImplemented时,解释器将在其他类型上尝试反射操作(或其他某种回退,取决于运算符)。If all attempts return NotImplemented, the interpreter will raise an appropriate exception. 如果所有尝试都返回NotImplemented,解释器将引发相应的异常。Incorrectly returning NotImplemented will result in a misleading error message or the NotImplemented value being returned to Python code.错误地返回NotImplemented将导致错误消息或NotImplemented值返回给Python代码。

See Implementing the arithmetic operations for examples.有关示例,请参阅实现算术运算

Note

NotImplementedError and NotImplemented are not interchangeable, even though they have similar names and purposes. NotImplementedErrorNotImplemented不可互换,即使它们有相似的名称和用途。See NotImplementedError for details on when to use it.有关何时使用它的详细信息,请参阅NotImplementedError

Changed in version 3.9: 在版本3.9中更改:Evaluating NotImplemented in a boolean context is deprecated. 不推荐在布尔上下文中计算NotImplemented。While it currently evaluates as true, it will emit a DeprecationWarning. 虽然它当前的计算结果为true,但它将发出一个DeprecationWarningIt will raise a TypeError in a future version of Python.它将在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 the types.EllipsisType type.Ellipsistypes.EllipsisType类型的唯一实例。

__debug__

This constant is true if Python was not started with an -O option. 如果Python不是以-O选项启动的,则该常量为trueSee also the assert statement.另请参见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.无法重新分配名称NoneFalseTrue__debug__(分配给它们,即使作为属性名称,也会引发语法错误),因此可以将它们视为“True”常量。

Constants added by the site modulesite模块添加的常量

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. site模块(在启动期间自动导入,除非给定了-S命令行选项)向内置名称空间添加了几个常量。They are useful for the interactive interpreter shell and should not be used in programs.它们对于交互式解释器外壳非常有用,不应在程序中使用。

quit(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

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).对象,打印时打印消息“Type license()以查看完整的许可证文本”,调用时以类似寻呼机的方式(一次一个屏幕)显示完整的许可证文本。