組み込み定数¶
組み込み名前空間にはいくつかの定数があります。定数の一覧:
- False¶
bool型の偽値です。Falseへの代入は不正で、SyntaxErrorを送出します。
- True¶
bool型の真値です。Trueへの代入は不正で、SyntaxErrorを送出します。
- None¶
関数にデフォルト引数が渡されなかったときなどに、値の非存在を表すのに頻繁に用いられるオブジェクトです。
Noneへの代入は不正で、SyntaxErrorを送出します。NoneがNoneType型の唯一のインスタンスです。
- NotImplemented¶
特殊な二項演算のメソッド (e.g.
__eq__(),__lt__(),__add__(),__rsub__(), etc.) が、他の型に対して演算が実装されていないことを示すために返す特殊値です。インプレースの特殊な二項演算のメソッド (e.g.__imul__(),__iand__(), etc.) も同じ理由でこの値を返すことがあります。この処理では真偽値コンテキストでの評価はしてはいけません。NotImplementedがtypes.NotImplementedType型の唯一のインスタンスです。注釈
二項演算の (あるいはインプレースの) メソッドが
NotImplementedを返した場合、インタープリタはもう一方の型で定義された対の演算で代用を試みます (あるいは演算によっては他の代替手段も試みます)。試行された演算全てがNotImplementedを返した場合、インタープリタは適切な例外を送出します。NotImplementedを正しく返さないと、誤解を招きかねないエラーメッセージになったり、NotImplementedが Python コードに返されるようなことになります。例として 算術演算の実装 を参照してください。
注意
NotImplementedandNotImplementedErrorare not interchangeable. This constant should only be used as described above; seeNotImplementedErrorfor details on correct usage of the exception.バージョン 3.9 で変更: Evaluating
NotImplementedin a boolean context was deprecated.バージョン 3.14 で変更: Evaluating
NotImplementedin a boolean context now raises aTypeError. It previously evaluated toTrueand emitted aDeprecationWarningsince Python 3.9.
- Ellipsis¶
The same as the ellipsis literal "
...", an object frequently used to indicate that something is omitted. Assignment toEllipsisis possible, but assignment to...raises aSyntaxError.Ellipsisis the sole instance of thetypes.EllipsisTypetype.
注釈
名前 None 、 False 、 True 、 __debug__ は再代入できない (これらに対する代入は、たとえ属性名としてであっても SyntaxError が送出されます) ので、これらは「真の」定数であると考えられます。
site モジュールで追加される定数¶
site モジュール (-S コマンドラインオプションが指定されない限り、スタートアップ時に自動的にインポートされます) は組み込み名前空間にいくつかの定数を追加します。それらは対話的インタープリタシェルで有用ですが、プログラム中では使うべきではありません。
- 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 accessed directly in the interactive interpreter or called as functions, raise
SystemExitwith the specified exit code.
- help
Object that when printed, prints the message "Type help() for interactive help, or help(object) for help about object.", and when accessed directly in the interactive interpreter, invokes the built-in help system (see
help()).
- license¶
表示されたときに "Type license() to see the full license text" というメッセージを表示し、呼び出されたときには完全なライセンスのテキストをページャのような形式で (1画面分づつ) 表示するオブジェクトです。