MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / Items

Class Items

src/8/implementing_custom_containers/example2.py:3–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import collections
2
3class Items(collections.MutableSequence):
4 def __init__(self, initial=None):
5 self._items = list(initial) if initial is not None else []
6
7 # Required sequence methods
8 def __getitem__(self, index):
9 print('Getting:', index)
10 return self._items[index]
11
12 def __setitem__(self, index, value):
13 print('Setting:', index, value)
14 self._items[index] = value
15
16 def __delitem__(self, index):
17 print('Deleting:', index)
18 del self._items[index]
19
20 def insert(self, index, value):
21 print('Inserting:', index, value)
22 self._items.insert(index, value)
23
24 def __len__(self):
25 print('Len')
26 return len(self._items)
27
28if __name__ == '__main__':
29 a = Items([1, 2, 3])

Callers 1

example2.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected