Home  >  Article  >  Backend Development  >  Some more sugar for python

Some more sugar for python

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 12:44:02799browse

Algo más de azúcar para 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn