Created
August 12, 2020 18:47
-
-
Save raeq/32beae8906575da167102c0a4b8f4e10 to your computer and use it in GitHub Desktop.
A simple auto append list.
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 import UserList | |
class AutoAppendList(UserList): | |
""" | |
AutoAppendList. Will append an item if you are off by one index assignment. | |
>>> aal: AutoAppendList = AutoAppendList() | |
>>> for i in range(10): | |
... aal[i] = i | |
>>> print(aal) | |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
""" | |
def __setitem__(self, index, item): | |
"""__setitem__. | |
Args: | |
index: | |
item: | |
""" | |
if index == len(self.data): | |
self.data.append(item) | |
else: | |
self.data[i] = item | |
aal: AutoAppendList = AutoAppendList() | |
for iteration, char in enumerate("hello"): | |
aal[iteration] = ord(char) | |
assert aal == [104, 101, 108, 108, 111] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment