Download Python/train/3.8/protocol/modern_09.py from SwashBuckler001/idiombench: direct link, hf CLI and curl.
- Browser
- Download file 691 Bytes
-
https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/train/3.8/protocol/modern_09.py
- Command line
-
hf download hf://datasets/SwashBuckler001/idiombench/Python/train/3.8/protocol/modern_09.py
-
curl -L -o modern_09.py https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/train/3.8/protocol/modern_09.py
691 Bytes
| T = TypeVar('T') | |
| T_co = TypeVar('T_co', covariant=True) | |
| T_contra = TypeVar('T_contra', contravariant=True) | |
| class Box(Protocol[T_co]): | |
| def content(self) -> T_co: | |
| ... | |
| box: Box[float] | |
| second_box: Box[int] | |
| box = second_box # This is OK due to the covariance of 'Box'. | |
| class Sender(Protocol[T_contra]): | |
| def send(self, data: T_contra) -> int: | |
| ... | |
| sender: Sender[float] | |
| new_sender: Sender[int] | |
| new_sender = sender # OK, 'Sender' is contravariant. | |
| class Proto(Protocol[T]): | |
| attr: T # this class is invariant, since it has a mutable attribute | |
| var: Proto[float] | |
| another_var: Proto[int] | |
| var = another_var # Error! 'Proto[float]' is incompatible with 'Proto[int]'. | |