Код: Выделить всё
class SetQueue(queue.Queue):
"""Queue which will allow a given object to be put once only.
Objects are considered identical if hash(object) are identical.
"""
def __init__(self, maxsize=0):
"""Initialise queue with maximum number of items.
0 for infinite queue
"""
super().__init__(maxsize)
self.all_items = set()
def _put(self):
if item not in self.all_items:
super()._put(item)
self.all_items.add(item)
Код: Выделить всё
from typing import Generic, Iterable, Set, TypeVar
# Type for mypy generics
T = TypeVar('T')
class SetQueue(queue.Queue):
"""Queue which will allow a given object to be put once only.
Objects are considered identical if hash(object) are identical.
"""
def __init__(self, maxsize: int=0) -> None:
"""Initialise queue with maximum number of items.
0 for infinite queue
"""
super().__init__(maxsize)
self.all_items = set() # type: Set[T]
def _put(self, item: T) -> None:
if item not in self.all_items:
super()._put(item)
self.all_items.add(item)
Я думаю, что мне где-то нужен Generic[T], но каждая моя попытка выдает синтаксическую ошибку. Во всех примерах в документации показано создание подкласса от Generic[T], но не создание подкласса от какого-либо другого объекта.
Кто-нибудь знает, как определить универсальный тип для SetQueue?
Подробнее здесь: https://stackoverflow.com/questions/454 ... c-subclass
Мобильная версия