blob: f17e1a37875168fa9b4833cf52c5d49038b9e760 [file] [log] [blame]
Éric Araujo96deb752011-06-08 04:53:20 +02001.. _built-in-consts:
2
Georg Brandl116aa622007-08-15 14:28:22 +00003Built-in Constants
4==================
5
6A small number of constants live in the built-in namespace. They are:
7
Georg Brandl116aa622007-08-15 14:28:22 +00008.. data:: False
9
Georg Brandl48310cd2009-01-03 21:18:54 +000010 The false value of the :class:`bool` type. Assignments to ``False``
Christian Heimes5b5e81c2007-12-31 16:14:33 +000011 are illegal and raise a :exc:`SyntaxError`.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013
14.. data:: True
15
Georg Brandl48310cd2009-01-03 21:18:54 +000016 The true value of the :class:`bool` type. Assignments to ``True``
Christian Heimes5b5e81c2007-12-31 16:14:33 +000017 are illegal and raise a :exc:`SyntaxError`.
Georg Brandl116aa622007-08-15 14:28:22 +000018
Georg Brandl116aa622007-08-15 14:28:22 +000019
20.. data:: None
21
R David Murraye60e12b2012-07-22 20:43:13 -040022 The sole value of the type ``NoneType``. ``None`` is frequently used to
Georg Brandl116aa622007-08-15 14:28:22 +000023 represent the absence of a value, as when default arguments are not passed to a
Christian Heimes5b5e81c2007-12-31 16:14:33 +000024 function. Assignments to ``None`` are illegal and raise a :exc:`SyntaxError`.
Georg Brandl116aa622007-08-15 14:28:22 +000025
26
27.. data:: NotImplemented
28
Ethan Furmanb0049432014-11-26 21:15:35 -080029 Special value which should be returned by the binary special methods
30 (e.g. :meth:`__eq__`, :meth:`__lt__`, :meth:`__add__`, :meth:`__rsub__`,
31 etc.) to indicate that the operation is not implemented with respect to
32 the other type; may be returned by the in-place binary special methods
33 (e.g. :meth:`__imul__`, :meth:`__iand__`, etc.) for the same purpose.
MojoVampire469325c2020-03-03 18:50:17 +000034 It should not be evaluated in a boolean context.
Ethan Furmanb0049432014-11-26 21:15:35 -080035
Ethan Furman20bd9f02016-08-05 15:10:16 -070036 .. note::
Ethan Furmanb0049432014-11-26 21:15:35 -080037
Ethan Furman20bd9f02016-08-05 15:10:16 -070038 When a binary (or in-place) method returns ``NotImplemented`` the
39 interpreter will try the reflected operation on the other type (or some
40 other fallback, depending on the operator). If all attempts return
41 ``NotImplemented``, the interpreter will raise an appropriate exception.
42 Incorrectly returning ``NotImplemented`` will result in a misleading
43 error message or the ``NotImplemented`` value being returned to Python code.
Ethan Furmanb0049432014-11-26 21:15:35 -080044
Ethan Furman20bd9f02016-08-05 15:10:16 -070045 See :ref:`implementing-the-arithmetic-operations` for examples.
46
47 .. note::
48
zertrin05f53732017-03-20 20:24:39 +080049 ``NotImplementedError`` and ``NotImplemented`` are not interchangeable,
Ethan Furman20bd9f02016-08-05 15:10:16 -070050 even though they have similar names and purposes.
51 See :exc:`NotImplementedError` for details on when to use it.
Georg Brandl116aa622007-08-15 14:28:22 +000052
MojoVampire469325c2020-03-03 18:50:17 +000053 .. versionchanged:: 3.9
54 Evaluating ``NotImplemented`` in a boolean context is deprecated. While
55 it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
56 It will raise a :exc:`TypeError` in a future version of Python.
57
Georg Brandl116aa622007-08-15 14:28:22 +000058
Serhiy Storchakaddb961d2018-10-26 09:00:49 +030059.. index:: single: ...; ellipsis literal
Georg Brandl116aa622007-08-15 14:28:22 +000060.. data:: Ellipsis
61
Serhiy Storchakaddb961d2018-10-26 09:00:49 +030062 The same as the ellipsis literal "``...``". Special value used mostly in conjunction
Xtreaka8d5e2f2018-10-08 20:44:16 +053063 with extended slicing syntax for user-defined container data types.
Georg Brandl96593ed2007-09-07 14:15:41 +000064
65
66.. data:: __debug__
67
Christian Heimes5b5e81c2007-12-31 16:14:33 +000068 This constant is true if Python was not started with an :option:`-O` option.
Christian Heimes5b5e81c2007-12-31 16:14:33 +000069 See also the :keyword:`assert` statement.
Christian Heimes9bd667a2008-01-20 15:14:11 +000070
Georg Brandl0c7ade22010-08-02 19:39:17 +000071
Georg Brandlc589a702010-08-02 19:36:36 +000072.. note::
73
74 The names :data:`None`, :data:`False`, :data:`True` and :data:`__debug__`
75 cannot be reassigned (assignments to them, even as an attribute name, raise
76 :exc:`SyntaxError`), so they can be considered "true" constants.
77
Christian Heimes9bd667a2008-01-20 15:14:11 +000078
79Constants added by the :mod:`site` module
80-----------------------------------------
81
82The :mod:`site` module (which is imported automatically during startup, except
83if the :option:`-S` command-line option is given) adds several constants to the
84built-in namespace. They are useful for the interactive interpreter shell and
85should not be used in programs.
86
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000087.. data:: quit(code=None)
88 exit(code=None)
Christian Heimes9bd667a2008-01-20 15:14:11 +000089
90 Objects that when printed, print a message like "Use quit() or Ctrl-D
91 (i.e. EOF) to exit", and when called, raise :exc:`SystemExit` with the
Benjamin Peterson8719ad52009-09-11 22:24:02 +000092 specified exit code.
Christian Heimes9bd667a2008-01-20 15:14:11 +000093
94.. data:: copyright
Christian Heimes9bd667a2008-01-20 15:14:11 +000095 credits
96
Gerrit Holl243d6d72018-02-17 03:48:57 +000097 Objects that when printed or called, print the text of copyright or
98 credits, respectively.
99
100.. data:: license
101
102 Object that when printed, prints the message "Type license() to see the
103 full license text", and when called, displays the full license text in a
Christian Heimes9bd667a2008-01-20 15:14:11 +0000104 pager-like fashion (one screen at a time).