📜  python 模板泛型 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:03.197000             🧑  作者: Mango

代码示例1
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