內建常數¶
有一小部分的常數存在於內建命名空間中。他們是:
- 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 aSyntaxError
.None
is the sole instance of theNoneType
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 thetypes.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 returnNotImplemented
, the interpreter will raise an appropriate exception. Incorrectly returningNotImplemented
will result in a misleading error message or theNotImplemented
value being returned to Python code.請參見 實作算術操作 以找到更多範例。
備註
NotImplementedError
andNotImplemented
are not interchangeable, even though they have similar names and purposes. SeeNotImplementedError
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 aDeprecationWarning
. It will raise aTypeError
in a future version of Python.
- Ellipsis¶
與刪節號 "
...
" 字面相同。為一特殊值,大多用於結合使用者定義資料型別的延伸切片語法 (extended slicing syntax)。Ellipsis
是型別types.EllipsisType
的唯一實例。
備註
None
,False
,True
,以及 __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)。
- license¶
當印出此物件時,會印出訊息 "Type license() to see the full license text" 。當被呼叫時,則會以分頁形式印出完整的許可證文字(一次一整個畫面)。