Home > Backend Development > Python Tutorial > Some more sugar for python

Some more sugar for python

Barbara Streisand
Release: 2024-11-02 12:44:02
Original
914 people have browsed it

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:
        ...
Copy after login

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]
Copy after login

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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template