Type aliases
Type aliases are defined by simple variable assignments:
Url = str def retry(url: Url, retry_count: int) -> None: ...
Note that we recommend capitalizing alias names, since they represent user-defined types, which (like user-defined classes) are typically spelled that way.
Type aliases may be as complex as type hints in annotations – anything that is acceptable as a type hint is acceptable in a type alias:
from typing import TypeVar, Iterable, Tuple T = TypeVar('T', int, float, complex) Vector = Iterable[Tuple[T, T]] def inproduct(v: Vector[T]) -> T: return sum(x*y for x, y in v) def dilate(v: Vector[T], scale: T) -> Vector[T]: return ((x * scale, y * scale) for x, y in v) vec = [] # type: Vector[float]
This is equivalent to:
from typing import TypeVar, Iterable, Tuple T = TypeVar('T', int, float, complex) def inproduct(v: Iterable[Tuple[T, T]]) -> T: return sum(x*y for x, y in v) def dilate(v: Iterable[Tuple[T, T]], scale: T) -> Iterable[Tuple[T, T]]: return ((x * scale, y * scale) for x, y in v) vec = [] # type: Iterable[Tuple[float, float]]
ref: https://peps.python.org/pep-0484/#type-aliases
标签:scale,Tuple,Python,float,alias,Vector,type,Iterable From: https://www.cnblogs.com/sakuraki/p/17548352.html