|
| 1 | +import collections.abc |
| 2 | +import typing |
| 3 | + |
| 4 | + |
| 5 | +def list_example(): |
| 6 | + l = [1, 2, 3, 4] |
| 7 | + |
| 8 | + for x in reversed(l): |
| 9 | + print(x) |
| 10 | + |
| 11 | + |
| 12 | +class MyContainer: |
| 13 | + def __init__(self, l): |
| 14 | + self.l = l |
| 15 | + |
| 16 | + def __getitem__(self, item): |
| 17 | + return self.l[item] |
| 18 | + |
| 19 | + def __len__(self): |
| 20 | + return len(self.l) |
| 21 | + |
| 22 | + def __iter__(self): |
| 23 | + return iter(self.l) |
| 24 | + |
| 25 | + |
| 26 | +def custom_container_example(): |
| 27 | + l = [1, 2, 3, 4] |
| 28 | + container = MyContainer(l) |
| 29 | + print(f"{list(container)=}") |
| 30 | + print(reversed(container)) |
| 31 | + # print(reversed(reversed(l))) # ERROR |
| 32 | + |
| 33 | + it = reversed(l) |
| 34 | + print(list(it)) |
| 35 | + print(list(it)) |
| 36 | + |
| 37 | + |
| 38 | +class reversed: |
| 39 | + def __init__(self, seq): |
| 40 | + self.seq = seq |
| 41 | + self.n = len(seq) - 1 |
| 42 | + if not hasattr(seq, '__getitem__'): |
| 43 | + raise TypeError("not reversible") |
| 44 | + |
| 45 | + def __iter__(self): |
| 46 | + return self |
| 47 | + |
| 48 | + def __next__(self): # called by next() |
| 49 | + if self.n == -1: |
| 50 | + raise StopIteration |
| 51 | + n = self.n |
| 52 | + self.n = n - 1 |
| 53 | + return self.seq[n] |
| 54 | + |
| 55 | + |
| 56 | +def reversed(seq): |
| 57 | + n = len(seq) - 1 |
| 58 | + if not hasattr(seq, '__getitem__'): |
| 59 | + raise TypeError("not reversible") |
| 60 | + while n != -1: |
| 61 | + yield seq[n] |
| 62 | + n -= 1 |
| 63 | + |
| 64 | + |
| 65 | +def copy_or_no_copy(): |
| 66 | + l = [("a", 1), ("b", 2), ("c", 3)] |
| 67 | + d = dict(l) |
| 68 | + |
| 69 | + makes_a_copy = [ |
| 70 | + dict(l), |
| 71 | + frozenset(l), |
| 72 | + list(l), |
| 73 | + set(l), |
| 74 | + sorted(l), |
| 75 | + tuple(l), |
| 76 | + l[::-1], # or any slice |
| 77 | + [x for x in l], |
| 78 | + ] |
| 79 | + for x in makes_a_copy: |
| 80 | + print(x) |
| 81 | + |
| 82 | + doesnt_make_a_copy = [ |
| 83 | + enumerate(l), |
| 84 | + filter(None, l), |
| 85 | + iter(l), |
| 86 | + map(lambda x: x, l), |
| 87 | + reversed(l), |
| 88 | + zip(l, l), |
| 89 | + d.keys(), |
| 90 | + d.values(), |
| 91 | + d.items(), |
| 92 | + (x for x in l), |
| 93 | + ] |
| 94 | + for x in doesnt_make_a_copy: |
| 95 | + print(x) |
| 96 | + |
| 97 | + |
| 98 | +def numpy_example(): |
| 99 | + import numpy as np |
| 100 | + |
| 101 | + arr = np.array([1, 2, 3]) |
| 102 | + rev = arr[::-1] |
| 103 | + |
| 104 | + print(arr) |
| 105 | + print(rev) |
| 106 | + |
| 107 | + print(arr.data) |
| 108 | + print(rev.data) |
| 109 | + |
| 110 | + print(arr.strides) |
| 111 | + print(rev.strides) |
| 112 | + |
| 113 | + |
| 114 | +def sometimes_you_want_a_copy_example(): |
| 115 | + d = {"a": 1, "b": 2, "c": 3} |
| 116 | + |
| 117 | + for char, val in list(d.items()): |
| 118 | + d[char.upper()] = val |
| 119 | + |
| 120 | + print(d) |
| 121 | + |
| 122 | + |
| 123 | +def iterable_vs_Iterable(): |
| 124 | + container = MyContainer([1, 2, 3]) |
| 125 | + print(isinstance(container, typing.Iterable)) |
| 126 | + print(isinstance(container, collections.abc.Iterable)) |
| 127 | + |
| 128 | + print(iter(container)) |
| 129 | + # print(container.__iter__) |
| 130 | + |
| 131 | + |
| 132 | +def main(): |
| 133 | + list_example() |
| 134 | + custom_container_example() |
| 135 | + copy_or_no_copy() |
| 136 | + numpy_example() |
| 137 | + sometimes_you_want_a_copy_example() |
| 138 | + iterable_vs_Iterable() |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + main() |
0 commit comments