Skip to content

Commit 9fb7d37

Browse files
committed
Create sample code for UserList vs list
1 parent 1b3bdaa commit 9fb7d37

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Custom Python Lists: Inheriting From list vs UserList
2+
3+
This folder provides the code examples for the article [Custom Python Lists: Inheriting From list vs UserList
4+
](https://realpython.com/inherit-python-list/).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class CustomList(list):
2+
def join(self, sep=" "):
3+
return sep.join(str(item) for item in self)
4+
5+
def map(self, action):
6+
return type(self)(action(item) for item in self)
7+
8+
def filter(self, predicate):
9+
return type(self)(item for item in self if predicate(item))
10+
11+
def for_each(self, func):
12+
for item in self:
13+
yield func(item)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import UserList
2+
3+
4+
class CustomList(UserList):
5+
def join(self, sep=" "):
6+
return sep.join(self.data)
7+
8+
def map(self, action):
9+
return type(self)(action(item) for item in self.data)
10+
11+
def filter(self, predicate):
12+
return type(self)(item for item in self if predicate(item))
13+
14+
def for_each(self, func):
15+
for item in self.data:
16+
yield func(item)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class NumberList(list):
2+
def __init__(self, iterable):
3+
super().__init__(self._validate_number(item) for item in iterable)
4+
5+
def __setitem__(self, index, item):
6+
super().__setitem__(index, self._validate_number(item))
7+
8+
def insert(self, index, item):
9+
super().insert(index, self._validate_number(item))
10+
11+
def append(self, item):
12+
super().append(self._validate_number(item))
13+
14+
def extend(self, other):
15+
if isinstance(other, type(self)):
16+
super().extend(other)
17+
else:
18+
super().extend(self._validate_number(item) for item in other)
19+
20+
def _validate_number(self, value):
21+
if isinstance(value, (int, float, complex)):
22+
return value
23+
raise TypeError(f"numeric value expected, got {type(value).__name__}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections import UserList
2+
3+
4+
class NumberList(UserList):
5+
def __init__(self, iterable):
6+
self.data = [self._validate_number(item) for item in iterable]
7+
8+
def __setitem__(self, index, item):
9+
self.data[index] = self._validate_number(item)
10+
11+
def insert(self, index, item):
12+
self.data.insert(index, self._validate_number(item))
13+
14+
def append(self, item):
15+
self.data.append(self._validate_number(item))
16+
17+
def extend(self, other):
18+
if isinstance(other, type(self)):
19+
self.data.extend(other)
20+
else:
21+
self.data.extend(self._validate_number(item) for item in other)
22+
23+
def _validate_number(self, value):
24+
if isinstance(value, (int, float, complex)):
25+
return value
26+
raise TypeError(f"numeric value expected, got {type(value).__name__}")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import timeit
2+
from collections import UserList
3+
4+
5+
class StringList_list(list):
6+
def __init__(self, iterable):
7+
super().__init__(str(item) for item in iterable)
8+
9+
def __setitem__(self, index, item):
10+
super().__setitem__(index, str(item))
11+
12+
def insert(self, index, item):
13+
super().insert(index, str(item))
14+
15+
def append(self, item):
16+
super().append(str(item))
17+
18+
def extend(self, other):
19+
if isinstance(other, type(self)):
20+
super().extend(other)
21+
else:
22+
super().extend(str(item) for item in other)
23+
24+
25+
class StringList_UserList(UserList):
26+
def __init__(self, iterable):
27+
self.data = [str(item) for item in iterable]
28+
29+
def __setitem__(self, index, item):
30+
self.data[index] = str(item)
31+
32+
def insert(self, index, item):
33+
self.data.insert(index, str(item))
34+
35+
def append(self, item):
36+
self.data.append(str(item))
37+
38+
def extend(self, other):
39+
if isinstance(other, type(self)):
40+
self.data.extend(other)
41+
else:
42+
self.data.extend(str(item) for item in other)
43+
44+
45+
init_data = range(10000)
46+
47+
list_initialization = min(
48+
timeit.repeat(
49+
stmt="StringList_list(init_data)",
50+
number=1000,
51+
repeat=5,
52+
globals=globals(),
53+
)
54+
)
55+
56+
user_list_initialization = min(
57+
timeit.repeat(
58+
stmt="StringList_UserList(init_data)",
59+
number=1000,
60+
repeat=5,
61+
globals=globals(),
62+
)
63+
)
64+
65+
print(
66+
f"list is {list_initialization / user_list_initialization:.3f}",
67+
"times slower than UserList",
68+
)
69+
70+
71+
extended_list = StringList_list(init_data)
72+
list_extend = min(
73+
timeit.repeat(
74+
stmt="extended_list.extend(init_data)",
75+
number=5,
76+
repeat=2,
77+
globals=globals(),
78+
)
79+
)
80+
81+
extended_user_list = StringList_UserList(init_data)
82+
user_list_extend = min(
83+
timeit.repeat(
84+
stmt="extended_user_list.extend(init_data)",
85+
number=5,
86+
repeat=2,
87+
globals=globals(),
88+
)
89+
)
90+
91+
print(
92+
f"list is {list_extend / user_list_extend:.3f}",
93+
"times slower than UserList",
94+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import UserList
2+
3+
4+
class StringList(UserList):
5+
def __init__(self, iterable):
6+
super().__init__(str(item) for item in iterable)
7+
8+
def __setitem__(self, index, item):
9+
super().__setitem__(index, str(item))
10+
11+
def insert(self, index, item):
12+
super().insert(index, str(item))
13+
14+
def append(self, item):
15+
super().append(str(item))
16+
17+
def extend(self, other):
18+
if isinstance(other, type(self)):
19+
super().extend(other)
20+
else:
21+
super().extend(str(item) for item in other)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import UserList
2+
3+
4+
class StringList(UserList):
5+
def __init__(self, iterable):
6+
self.data = [str(item) for item in iterable]
7+
8+
def __setitem__(self, index, item):
9+
self.data[index] = str(item)
10+
11+
def insert(self, index, item):
12+
self.data.insert(index, str(item))
13+
14+
def append(self, item):
15+
self.data.append(str(item))
16+
17+
def extend(self, other):
18+
if isinstance(other, type(self)):
19+
self.data.extend(other)
20+
else:
21+
self.data.extend(str(item) for item in other)

0 commit comments

Comments
 (0)