ID EN
Typing

Protocol

Python 3.11 🇮🇩 Bahasa Indonesia

Kelas dasar untuk kelas protokol.

Syntax

PYTHON
class typing.Protocol(Generic)

Contoh

Example 1
PYTHON
class Proto(Protocol):
    def meth(self) -> int:
        ...
Example 2
PYTHON
class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())   Passes static type check
Example 3
PYTHON
T = TypeVar("T")

class GenProto(Protocol[T]):
    def meth(self) -> T:
        ...

See Also

runtime_checkable()