“дженерики Python” Ответ

Шаблон Python Generics

from typing import TypeVar, Generic

T = TypeVar('T')

class Stack(Generic[T]):
    def __init__(self) -> None:
        # Create an empty list with items of type T
        self.items: List[T] = []

    def push(self, item: T) -> None:
        self.items.append(item)

    def pop(self) -> T:
        return self.items.pop()

    def empty(self) -> bool:
        return not self.items
firststef

дженерики Python

# Generics can be parameterized by using a factory available in typing called TypeVar.
from collections.abc import Sequence
from typing import TypeVar

T = TypeVar('T')      # Declare type variable

def first(l: Sequence[T]) -> T:   # Generic function
    return l[0]
Danila

Ответы похожие на “дженерики Python”

Вопросы похожие на “дженерики Python”

Больше похожих ответов на “дженерики Python” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования