內建常數

有一小部分的常數存在於內建命名空間中。他們是:

False

bool 型別中的 false 值。對於 False 的賦值是不合法的,並且會拋出 SyntaxError

True

bool 型別中的 true 值。對於 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 None are illegal and raise a SyntaxError. None is the sole instance of the NoneType type.

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. NotImplemented is the sole instance of the types.NotImplementedType type.

備註

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.

在 3.9 版的變更: Evaluating NotImplemented in a boolean context is deprecated. While it currently evaluates as true, it will emit a DeprecationWarning. It will raise a TypeError in a future version of Python.

Ellipsis

與刪節號 "..." 字面相同。為一特殊值,大多用於結合使用者定義資料型別的延伸切片語法 (extended slicing syntax)。Ellipsis 是型別 types.EllipsisType 的唯一實例。

__debug__

如果 Python 沒有被以 -O 選項啟動,則此常數為 true。請參見 assert 陳述式。

備註

NoneFalseTrue,以及 __debug__ 都是不能被重新賦值的(任何對它們的賦值,即使是屬性的名稱,也會拋出 SyntaxError)。因此,它們可以被視為”真正的”常數。

site module(模組)所添增的常數

site module(模組)(在啟動期間自動 import ,除非有給予 -S 指令行選項)會添增一些常數到內建命名空間 (built-in namespace) 中。它們在互動式直譯器中是很有幫助的,但不應該在程式 (programs) 中被使用。

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

當印出物件時,會印出一個訊息: "Use quit() or Ctrl-D (i.e. EOF) to exit" 。當被呼叫時,則會拋出 SystemExit 並帶有指定的返回碼(exit code)。

credits

當印出或是呼叫此物件時,分別會印出版權與致謝的文字。

license

當印出此物件時,會印出訊息 "Type license() to see the full license text" 。當被呼叫時,則會以分頁形式印出完整的許可證文字(一次一整個畫面)。