Home >Backend Development >Python Tutorial >Some more sugar for python
Python 3.12 introduced some syntactic sugar to define generic classes and functions. Let's see how it works:
def max[T](args: Iterable[T]) -> T: ... class list[T]: def __getitem__(self, index: int, /) -> T: ... def append(self, element: T) -> None: ...
This simplification is appreciated and is useful in simple definitions.
It is also now allowed to create aliases with type, although I haven't had a chance to use it yet. The project I'm working on right now uses python 3.10.
type Point = tuple[float, float] type Point[T] = tuple[T, T]
The above is the detailed content of Some more sugar for python. For more information, please follow other related articles on the PHP Chinese website!