python泛型
2021-01-07 03:29
标签:items init end style eva self empty none bsp python泛型 标签:items init end style eva self empty none bsp 原文地址:https://www.cnblogs.com/wangbin2188/p/12972854.htmlfrom 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