-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC_LSP2.py
More file actions
35 lines (27 loc) · 1.01 KB
/
C_LSP2.py
File metadata and controls
35 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from abc import ABC, abstractmethod
from typing import Generic, Iterable, Iterator, Sized, TypeVar
_TItem = TypeVar("_TItem")
# Represents a set - items are unique, and when iterated return by order of insertion
class ISet(ABC, Iterable[_TItem], Sized, Generic[_TItem]):
@abstractmethod
def add(self, item: _TItem) -> None:
...
@abstractmethod
def __len__(self) -> int:
...
@abstractmethod
def __iter__(self) -> Iterator[_TItem]:
...
# Problem: Adding an item isn't idempotent,
# in contrast to set semantics.
# In particular, after adding the same item twice
# the len() will be 2 instead of 1.
class BadSet(ISet[_TItem], Generic[_TItem]):
def __init__(self) -> None:
self.__items_list = list[_TItem]()
def add(self, item: _TItem) -> None:
self.__items_list.append(item)
def __len__(self) -> int:
return len(self.__items_list)
def __iter__(self) -> Iterator[_TItem]:
return iter(self.__items_list)