Éric Araujo | 96deb75 | 2011-06-08 04:53:20 +0200 | [diff] [blame] | 1 | .. _built-in-consts: |
| 2 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | Built-in Constants |
| 4 | ================== |
| 5 | |
| 6 | A small number of constants live in the built-in namespace. They are: |
| 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. data:: False |
| 9 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 10 | The false value of the :class:`bool` type. Assignments to ``False`` |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 11 | are illegal and raise a :exc:`SyntaxError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
| 14 | .. data:: True |
| 15 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 16 | The true value of the :class:`bool` type. Assignments to ``True`` |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 17 | are illegal and raise a :exc:`SyntaxError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
| 20 | .. data:: None |
| 21 | |
Bas van Beek | 0d0e9fe | 2020-09-22 17:55:34 +0200 | [diff] [blame] | 22 | An object frequently used to represent the absence of a value, as when |
| 23 | default arguments are not passed to a function. Assignments to ``None`` |
| 24 | are illegal and raise a :exc:`SyntaxError`. |
| 25 | ``None`` is the sole instance of the :data:`NoneType` type. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | .. data:: NotImplemented |
| 29 | |
Bas van Beek | 0d0e9fe | 2020-09-22 17:55:34 +0200 | [diff] [blame] | 30 | A special value which should be returned by the binary special methods |
Ethan Furman | b004943 | 2014-11-26 21:15:35 -0800 | [diff] [blame] | 31 | (e.g. :meth:`__eq__`, :meth:`__lt__`, :meth:`__add__`, :meth:`__rsub__`, |
| 32 | etc.) to indicate that the operation is not implemented with respect to |
| 33 | the other type; may be returned by the in-place binary special methods |
| 34 | (e.g. :meth:`__imul__`, :meth:`__iand__`, etc.) for the same purpose. |
MojoVampire | 469325c | 2020-03-03 18:50:17 +0000 | [diff] [blame] | 35 | It should not be evaluated in a boolean context. |
Bas van Beek | 0d0e9fe | 2020-09-22 17:55:34 +0200 | [diff] [blame] | 36 | ``NotImplemented`` is the sole instance of the :data:`types.NotImplementedType` type. |
Ethan Furman | b004943 | 2014-11-26 21:15:35 -0800 | [diff] [blame] | 37 | |
Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 38 | .. note:: |
Ethan Furman | b004943 | 2014-11-26 21:15:35 -0800 | [diff] [blame] | 39 | |
Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 40 | When a binary (or in-place) method returns ``NotImplemented`` the |
| 41 | interpreter will try the reflected operation on the other type (or some |
| 42 | other fallback, depending on the operator). If all attempts return |
| 43 | ``NotImplemented``, the interpreter will raise an appropriate exception. |
| 44 | Incorrectly returning ``NotImplemented`` will result in a misleading |
| 45 | error message or the ``NotImplemented`` value being returned to Python code. |
Ethan Furman | b004943 | 2014-11-26 21:15:35 -0800 | [diff] [blame] | 46 | |
Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 47 | See :ref:`implementing-the-arithmetic-operations` for examples. |
| 48 | |
| 49 | .. note:: |
| 50 | |
zertrin | 05f5373 | 2017-03-20 20:24:39 +0800 | [diff] [blame] | 51 | ``NotImplementedError`` and ``NotImplemented`` are not interchangeable, |
Ethan Furman | 20bd9f0 | 2016-08-05 15:10:16 -0700 | [diff] [blame] | 52 | even though they have similar names and purposes. |
| 53 | See :exc:`NotImplementedError` for details on when to use it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
MojoVampire | 469325c | 2020-03-03 18:50:17 +0000 | [diff] [blame] | 55 | .. versionchanged:: 3.9 |
| 56 | Evaluating ``NotImplemented`` in a boolean context is deprecated. While |
| 57 | it currently evaluates as true, it will emit a :exc:`DeprecationWarning`. |
| 58 | It will raise a :exc:`TypeError` in a future version of Python. |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
Serhiy Storchaka | ddb961d | 2018-10-26 09:00:49 +0300 | [diff] [blame] | 61 | .. index:: single: ...; ellipsis literal |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | .. data:: Ellipsis |
| 63 | |
Bas van Beek | 0d0e9fe | 2020-09-22 17:55:34 +0200 | [diff] [blame] | 64 | The same as the ellipsis literal "``...``". Special value used mostly in conjunction |
Xtreak | a8d5e2f | 2018-10-08 20:44:16 +0530 | [diff] [blame] | 65 | with extended slicing syntax for user-defined container data types. |
Bas van Beek | 0d0e9fe | 2020-09-22 17:55:34 +0200 | [diff] [blame] | 66 | ``Ellipsis`` is the sole instance of the :data:`types.EllipsisType` type. |
Georg Brandl | 96593ed | 2007-09-07 14:15:41 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | .. data:: __debug__ |
| 70 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 71 | This constant is true if Python was not started with an :option:`-O` option. |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 72 | See also the :keyword:`assert` statement. |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 0c7ade2 | 2010-08-02 19:39:17 +0000 | [diff] [blame] | 74 | |
Georg Brandl | c589a70 | 2010-08-02 19:36:36 +0000 | [diff] [blame] | 75 | .. note:: |
| 76 | |
| 77 | The names :data:`None`, :data:`False`, :data:`True` and :data:`__debug__` |
| 78 | cannot be reassigned (assignments to them, even as an attribute name, raise |
| 79 | :exc:`SyntaxError`), so they can be considered "true" constants. |
| 80 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 81 | |
| 82 | Constants added by the :mod:`site` module |
| 83 | ----------------------------------------- |
| 84 | |
| 85 | The :mod:`site` module (which is imported automatically during startup, except |
| 86 | if the :option:`-S` command-line option is given) adds several constants to the |
| 87 | built-in namespace. They are useful for the interactive interpreter shell and |
| 88 | should not be used in programs. |
| 89 | |
Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 90 | .. data:: quit(code=None) |
| 91 | exit(code=None) |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 92 | |
| 93 | Objects that when printed, print a message like "Use quit() or Ctrl-D |
| 94 | (i.e. EOF) to exit", and when called, raise :exc:`SystemExit` with the |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 95 | specified exit code. |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 96 | |
| 97 | .. data:: copyright |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 98 | credits |
| 99 | |
Gerrit Holl | 243d6d7 | 2018-02-17 03:48:57 +0000 | [diff] [blame] | 100 | Objects that when printed or called, print the text of copyright or |
| 101 | credits, respectively. |
| 102 | |
| 103 | .. data:: license |
| 104 | |
| 105 | Object that when printed, prints the message "Type license() to see the |
| 106 | full license text", and when called, displays the full license text in a |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 107 | pager-like fashion (one screen at a time). |