CKTIS / Python
typing.Protocol
Opiše strukturni vmesnik za statično preverjanje.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
class SizedLabel(Protocol): ...Opiše strukturni vmesnik za statično preverjanje.
Razred z ustreznimi člani ga izpolni brez izrecnega dedovanja.
Primer pokliče zahtevano metodo, vendar isinstance s protokolom brez runtime_checkable sproži TypeError.
Primeri
from typing import Protocol
class Labelled(Protocol):
def label(self) -> str: ...
class Item:
def label(self) -> str:
return "item"
def show(x: Labelled) -> str:
return x.label()
print(show(Item()))
try:
isinstance(Item(), Labelled)
except TypeError:
print("static protocol")
Rezultat
item static protocol
Primer pokliče zahtevano metodo, vendar isinstance s protokolom brez runtime_checkable sproži TypeError.