“Python generyczny” Kod odpowiedzi

Szablon 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 generyczny

# 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

Odpowiedzi podobne do “Python generyczny”

Pytania podobne do “Python generyczny”

Więcej pokrewnych odpowiedzi na “Python generyczny” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu