内置常量¶
有少数的常量存在于内置命名空间中。 它们是:
- 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
Noneare illegal and raise aSyntaxError.Noneis the sole instance of theNoneTypetype.
- 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.NotImplementedis the sole instance of thetypes.NotImplementedTypetype.备注
当一个双目(或原地)方法返回
NotImplemented时解释器将尝试对另一种类型(或其他回退操作,具体取决于所用的运算符)的反射操作。 如果所有尝试都返回NotImplemented,解释器将引发适当的异常。 错误地返回NotImplemented将导致误导性的错误消息或NotImplemented值被返回给 Python 代码。参见 实现算术运算 了解相关示例。
小心
NotImplemented和NotImplementedError不能互相替代。 此常量应当仅以上文所描述的方式使用;请参阅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 toEllipsisis possible, but assignment to...raises aSyntaxError.Ellipsisis the sole instance of thetypes.EllipsisTypetype.
备注
名称 None,False,True 和 __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())。
- license¶
当打印此对象时,会打印出一条消息“Type license() to see the full license text”,当调用此对象时,将以分页形式显示完整的许可证文本(每次显示一屏)。