| 1 |
Built-in types |
| 2 |
============== |
| 3 |
|
| 4 |
This chapter introduces some commonly used built-in types. We will |
| 5 |
cover many other kinds of types later. |
| 6 |
|
| 7 |
Simple types |
| 8 |
............ |
| 9 |
|
| 10 |
Here are examples of some common built-in types: |
| 11 |
|
| 12 |
====================== =============================== |
| 13 |
Type Description |
| 14 |
====================== =============================== |
| 15 |
``int`` integer |
| 16 |
``float`` floating point number |
| 17 |
``bool`` boolean value (subclass of ``int``) |
| 18 |
``str`` text, sequence of unicode codepoints |
| 19 |
``bytes`` 8-bit string, sequence of byte values |
| 20 |
``object`` an arbitrary object (``object`` is the common base class) |
| 21 |
====================== =============================== |
| 22 |
|
| 23 |
All built-in classes can be used as types. |
| 24 |
|
| 25 |
Any type |
| 26 |
........ |
| 27 |
|
| 28 |
If you can't find a good type for some value, you can always fall back |
| 29 |
to ``Any``: |
| 30 |
|
| 31 |
====================== =============================== |
| 32 |
Type Description |
| 33 |
====================== =============================== |
| 34 |
``Any`` dynamically typed value with an arbitrary type |
| 35 |
====================== =============================== |
| 36 |
|
| 37 |
The type ``Any`` is defined in the :py:mod:`typing` module. |
| 38 |
See :ref:`dynamic-typing` for more details. |
| 39 |
|
| 40 |
Generic types |
| 41 |
............. |
| 42 |
|
| 43 |
Built-in collection type objects support indexing: |
| 44 |
|
| 45 |
====================== =============================== |
| 46 |
Type Description |
| 47 |
====================== =============================== |
| 48 |
``list[str]`` list of ``str`` objects |
| 49 |
``tuple[int, int]`` tuple of two ``int`` objects (``tuple[()]`` is the empty tuple) |
| 50 |
``tuple[int, ...]`` tuple of an arbitrary number of ``int`` objects |
| 51 |
``dict[str, int]`` dictionary from ``str`` keys to ``int`` values |
| 52 |
``Iterable[int]`` iterable object containing ints |
| 53 |
``Sequence[bool]`` sequence of booleans (read-only) |
| 54 |
``Mapping[str, int]`` mapping from ``str`` keys to ``int`` values (read-only) |
| 55 |
``type[C]`` type object of ``C`` (``C`` is a class/type variable/union of types) |
| 56 |
====================== =============================== |
| 57 |
|
| 58 |
The type ``dict`` is a *generic* class, signified by type arguments within |
| 59 |
``[...]``. For example, ``dict[int, str]`` is a dictionary from integers to |
| 60 |
strings and ``dict[Any, Any]`` is a dictionary of dynamically typed |
| 61 |
(arbitrary) values and keys. ``list`` is another generic class. |
| 62 |
|
| 63 |
``Iterable``, ``Sequence``, and ``Mapping`` are generic types that correspond to |
| 64 |
Python protocols. For example, a ``str`` object or a ``list[str]`` object is |
| 65 |
valid when ``Iterable[str]`` or ``Sequence[str]`` is expected. |
| 66 |
You can import them from :py:mod:`collections.abc` instead of importing from |
| 67 |
:py:mod:`typing`. |
| 68 |
|
| 69 |
See :ref:`generic-builtins` for more details. |
| 70 |
|
| 71 |
These legacy types defined in :py:mod:`typing` are also supported: |
| 72 |
|
| 73 |
====================== =============================== |
| 74 |
Type Description |
| 75 |
====================== =============================== |
| 76 |
``List[str]`` list of ``str`` objects |
| 77 |
``Tuple[int, int]`` tuple of two ``int`` objects (``Tuple[()]`` is the empty tuple) |
| 78 |
``Tuple[int, ...]`` tuple of an arbitrary number of ``int`` objects |
| 79 |
``Dict[str, int]`` dictionary from ``str`` keys to ``int`` values |
| 80 |
``Type[C]`` type object of ``C`` (``C`` is a class/type variable/union of types) |
| 81 |
====================== =============================== |