内置常量

有少数的常量存在于内置命名空间中。 它们是:

False

bool 类型的假值。 给 False 赋值是非法的并会引发 SyntaxError

True

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 a SyntaxError. None is the sole instance of the NoneType type.

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 the types.NotImplementedType type.

备注

当一个双目(或原地)方法返回 NotImplemented 时解释器将尝试对另一种类型(或其他回退操作,具体取决于所用的运算符)的反射操作。 如果所有尝试都返回 NotImplemented,解释器将引发适当的异常。 错误地返回 NotImplemented 将导致误导性的错误消息或 NotImplemented 值被返回给 Python 代码。

参见 实现算术运算 了解相关示例。

小心

NotImplementedNotImplementedError 不能互相替代。 此常量应当仅以上文所描述的方式使用;请参阅 NotImplementedError 了解正确使用该异常的相关细节。

在 3.9 版本发生变更: 在布尔运算上下文中对 NotImplemented 求值的做法已被弃用。

在 3.14 版本发生变更: 在布尔运算上下文中对 NotImplemented 求值现在会引发 TypeError。 在之前版本中它会被求值为 True 并将自 Python 3.9 起发出 DeprecationWarning

Ellipsis

The same as the ellipsis literal "...", an object frequently used to indicate that something is omitted. Assignment to Ellipsis is possible, but assignment to ... raises a SyntaxError. Ellipsis is the sole instance of the types.EllipsisType type.

__debug__

如果 Python 没有以 -O 选项启动,则此常量为真值。 另请参见 assert 语句。

备注

名称 NoneFalseTrue__debug__ 无法被重新赋值 (对它们赋值,即使是作为属性名,也会引发 SyntaxError),所以它们可以被认为是"真正的"常量。

site 模块添加的常量

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

quit(code=None)
exit(code=None)

当对象被打印时,会打印一条消息如 "Use quit() or Ctrl-D (i.e. EOF) to exit",当在交互解释器中直接访问或作为函数调用时,将引发 SystemExit 并附带指定的退出码。

help

该对象在打印时会输出消息 "Type help() for interactive help, or help(object) for help about object.",当在交互式解释器中直接访问该对象时,则会调用内置的帮助系统 (参见 help())。

credits

这些对象在被打印或调用时,将分别打印版权或致谢文本。

license

当打印此对象时,会打印出一条消息“Type license() to see the full license text”,当调用此对象时,将以分页形式显示完整的许可证文本(每次显示一屏)。