3. 内置常量

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

False

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

True

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

None

The sole value of the type 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__() , 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. Its truth value is true.

注意

When a binary (or in-place) method returns NotImplemented the interpreter will try the reflected operation on the other type (or some other fallback, depending on the operator). If all attempts return NotImplemented , the interpreter will raise an appropriate exception. Incorrectly returning NotImplemented will result in a misleading error message or the NotImplemented value being returned to Python code.

实现算术运算 范例。

注意

NotImplementedError and NotImplemented are not interchangeable, even though they have similar names and purposes. See NotImplementedError for details on when to use it.

Ellipsis

The same as the ellipsis literal “ ... ”. 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 模块

site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.

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 called, raise SystemExit with the specified exit code.

credits

Objects that when printed or called, print the text of copyright or credits, respectively.

license

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

内容表

上一话题

2. 内置函数

下一话题

4. 内置类型

本页