Created
August 12, 2020 18:45
-
-
Save raeq/628c143cb2e94dbabb3f242aca20a229 to your computer and use it in GitHub Desktop.
A frozen list implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections.abc import Iterable | |
from collections import UserList | |
def immutable_decorator(f): | |
def wrapper(self, *args, **kwargs): | |
raise TypeError("Object is frozen") | |
return wrapper | |
class FrozenList(UserList): # pylint: disable=too-many-ancestors | |
""" | |
A List which is immutable. | |
>>> fl: FrozenList = FrozenList("hello") | |
>>> fl:FrozenList = FrozenList([1, 2, 4]) | |
>>> print(fl[1:2]) | |
[2] | |
>>> print(fl) | |
[1, 2, 4] | |
>>> fl.append(1) | |
Traceback (most recent call last): | |
... | |
TypeError: Object is frozen | |
>>> fl.extend(1) | |
Traceback (most recent call last): | |
... | |
TypeError: Object is frozen | |
""" | |
@immutable_decorator | |
def __setitem__(self, i: int, o) -> None: | |
pass | |
@immutable_decorator | |
def __add__(self, other): | |
pass | |
@immutable_decorator | |
def __iadd__(self, other): | |
pass | |
@immutable_decorator | |
def __mul__(self, n: int): | |
pass | |
@immutable_decorator | |
def __imul__(self, n: int): | |
pass | |
@immutable_decorator | |
def append(self, item) -> None: | |
pass | |
@immutable_decorator | |
def insert(self, i: int, item) -> None: | |
pass | |
@immutable_decorator | |
def pop(self, i: int): | |
pass | |
@immutable_decorator | |
def remove(self, item) -> None: | |
pass | |
@immutable_decorator | |
def clear(self) -> None: | |
pass | |
@immutable_decorator | |
def reverse(self) -> None: | |
pass | |
@immutable_decorator | |
def extend(self, other) -> None: | |
pass | |
l: list = [1, 2, 4] | |
fl: FrozenList = FrozenList(l) | |
assert fl[1:2] == [2] | |
fl: FrozenList = FrozenList("help") | |
assert fl[1::2] == ["e", "p"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment