4. 内置常量

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

False

False 值的 bool 类型。

2.3 版新增。

True

True 值的 bool 类型。

2.3 版新增。

None

The sole value of types.NoneType . None is frequently used to represent the absence of a value, as when default arguments are not passed to a function.

2.4 版改变: 赋值 None 是非法的且会引发 SyntaxError .

NotImplemented

Special value which can be returned by the “rich comparison” special methods ( __eq__() , __lt__() , and friends), to indicate that the comparison is not implemented with respect to the other type.

Ellipsis

Special value used in conjunction with extended slicing syntax.

__debug__

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

注意

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

2.7 版改变: 赋值 __debug__ as an attribute became illegal.

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

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

quit ( [ code=None ] )
exit ( [ code=None ] )

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

credits

对象当打印或调用时,分别打印版权或信用文本。

license

对象当打印时,打印消息 "Type license() to see the full license text",和当调用时,以像分页程序的方式 (每次一屏) 显示完整许可文本。

内容表

上一话题

2. 内置函数

下一话题

5. 内置类型

本页