"""演示 PEP 695 的 class/def/type 泛型语法,并与旧式 TypeVar 对照。""" from __future__ import annotations from collections.abc import Sequence from typing import Generic, TypeVar T = TypeVar("T") class LegacyBox(Generic[T]): def __init__(self, value: T) -> None: self.value = value class Box[T]: def __init__(self, value: T) -> None: self.value = value def get(self) -> T: return self.value type Pair[T] = tuple[T, T] def first[T](items: Sequence[T]) -> T: return items[0] def main() -> None: print(f"legacy: {LegacyBox[int]!r}") print(f"pep695: {Box[int]!r}") print(f"pair: {Pair[int]!r}") print(f"pair type: {type(Pair[int]).__name__}") print(f"first: {first([1, 2])}") pair: Pair[int] = (1, 2) print(f"pair value: {pair}") if __name__ == "__main__": main()