3. 内置常量

内置名称空间有存活少量常量。它们是:

False

False 值的 bool 类型。赋值 False 是非法的且会引发 SyntaxError .

True

True 值的 bool 类型。赋值 True 是非法的且会引发 SyntaxError .

None

唯一值对于类型 NoneType . None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None 是非法的且会引发 SyntaxError .

NotImplemented

Special value which should be returned by the binary special methods (e.g. __eq__() , __lt__() , __add__() , __rsub__() ,等) 指示操作不是为其它类型方面而实现的;可以返回通过原位二进制特殊方法 (如 __imul__() , __iand__() , etc.) for the same purpose. Its truth value is true.

注意

当 NotImplemented is returned, the interpreter will then try the reflected operation on the other type, or some other fallback, depending on the operator. If all attempted operations return NotImplemented , the interpreter will raise an appropriate exception.

见 实现算术运算 了解更多细节。

Ellipsis

如同 ... . Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.

__debug__

此常量为 True 若 Python 未启动采用 -O 选项。另请参阅 assert 语句。

注意

名称 None , False , True and __debug__ 无法重新赋值 (赋值它们,即使作为属性名称,引发 SyntaxError ),所以它们可以被认为是真常量。

3.1. 常量的添加通过 site 模块

The site 模块 (在启动期间被自动导入,除了若 -S 命令行选项有给定) 将几个常量添加到内置名称空间。它们很有用对于交互解释器 Shell,且不应用于程序中。

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

对象当打印时,打印消息像 "Use quit() or Ctrl-D (i.e. EOF) to exit",和当调用时,引发 SystemExit 采用指定退出代码。

license
credits

Objects that when printed, print a message like “Type license() to see the full license text”, and when called, display the corresponding text in a pager-like fashion (one screen at a time).