内置常量

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

False

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

True

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

None

通常被用来代表空值的对象,例如未向某个函数传入默认参数时。 向 None 赋值是非法的并会引发 SyntaxErrorNoneNoneType 类型的唯一实例。

NotImplemented

一个应当由双目运算特殊方法(如 __eq__(), __lt__(), __add__(), __rsub__() 等)返回的特殊值,用来表明该运算没有针对其他类型的实现;也可由原地双目运算特殊方法(如 __imul__(), __iand__() 等)出于同样的目的而返回。 它不应在布尔上下文中被求值。 NotImplementedtypes.NotImplementedType 类型的唯一实例。

备注

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

参见 实现算术运算 为例。

小心

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

在 3.9 版本发生变更: 在布尔上下文件中对 NotImplemented 求值的操作已被弃用。 虽然它目前会被求解为真值,但将同时发出 DeprecationWarning。 它将在未来的 Python 版本中引发 TypeError

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)

Objects that when printed, print a message like "Use quit() or Ctrl-D (i.e. EOF) to exit", and when accessed directly in the interactive interpreter or called as functions, raise SystemExit with the specified exit code.

help

Object that when printed, prints the message "Type help() for interactive help, or help(object) for help about object.", and when accessed directly in the interactive interpreter, invokes the built-in help system (see help()).

credits

打印或调用的对象分别打印版权或作者的文本。

license

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