File: protocol-typing.rst

package info (click to toggle)
python-sybil 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: python: 4,510; makefile: 90
file content (30 lines) | stat: -rw-r--r-- 534 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
.. code-block:: python

    from typing import Protocol, runtime_checkable

    @runtime_checkable
    class NamedValue(Protocol):
        """Interface for an object that has a name and a value."""

        value: float
        name: str

some text

.. code-block:: python

    class NamedValueClass1:
        def __init__(self, name: str, value: float):
            self.name = name
            self.value = value


    v = NamedValueClass1("foo", 1.0)


::

    >>> isinstance(v, NamedValue)
    True

This above should be ``True``