|
| 1 | + |
| 2 | +# 3.1 Scripting |
| 3 | + |
| 4 | +> Also, a little known fact is that Python runs a bit faster if you use functions. |
| 5 | +
|
| 6 | +https://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function |
| 7 | + |
| 8 | +The short answer is that it is faster to store local variables than globals. |
| 9 | + |
| 10 | +# 3.6 Design Discussion |
| 11 | + |
| 12 | +In this section we reconsider a design decision made earlier. |
| 13 | + |
| 14 | +### Filenames versus Iterables |
| 15 | + |
| 16 | +Compare these two programs that return the same output. |
| 17 | + |
| 18 | +```python |
| 19 | +# Provide a filename |
| 20 | +def read_data(filename): |
| 21 | + records = [] |
| 22 | + with open(filename) as f: |
| 23 | + for line in f: |
| 24 | + ... |
| 25 | + records.append(r) |
| 26 | + return records |
| 27 | + |
| 28 | +d = read_data('file.csv') |
| 29 | +``` |
| 30 | + |
| 31 | +```python |
| 32 | +# Provide lines |
| 33 | +def read_data(lines): |
| 34 | + records = [] |
| 35 | + for line in lines: |
| 36 | + ... |
| 37 | + records.append(r) |
| 38 | + return records |
| 39 | + |
| 40 | +with open('file.csv') as f: |
| 41 | + d = read_data(f) |
| 42 | +``` |
| 43 | + |
| 44 | +* Which of these functions do you prefer? Why? |
| 45 | +* Which of these functions is more flexible? |
| 46 | + |
| 47 | +### Deep Idea: "Duck Typing" |
| 48 | + |
| 49 | +[Duck Typing](https://en.wikipedia.org/wiki/Duck_typing) is a computer |
| 50 | +programming concept to determine whether an object can be used for a |
| 51 | +particular purpose. It is an application of the [duck |
| 52 | +test](https://en.wikipedia.org/wiki/Duck_test). |
| 53 | + |
| 54 | +> If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. |
| 55 | +
|
| 56 | +In the second version of `read_data()` above, the function expects any |
| 57 | +iterable object. Not just the lines of a file. |
| 58 | + |
| 59 | +```python |
| 60 | +def read_data(lines): |
| 61 | + records = [] |
| 62 | + for line in lines: |
| 63 | + ... |
| 64 | + records.append(r) |
| 65 | + return records |
| 66 | +``` |
| 67 | + |
| 68 | +This means that we can use it with other *lines*. |
| 69 | + |
| 70 | +```python |
| 71 | +# A CSV file |
| 72 | +lines = open('data.csv') |
| 73 | +data = read_data(lines) |
| 74 | + |
| 75 | +# A zipped file |
| 76 | +lines = gzip.open('data.csv.gz','rt') |
| 77 | +data = read_data(lines) |
| 78 | + |
| 79 | +# The Standard Input |
| 80 | +lines = sys.stdin |
| 81 | +data = read_data(lines) |
| 82 | + |
| 83 | +# A list of strings |
| 84 | +lines = ['ACME,50,91.1','IBM,75,123.45', ... ] |
| 85 | +data = read_data(lines) |
| 86 | +``` |
| 87 | + |
| 88 | +There is considerable flexibility with this design. |
| 89 | + |
| 90 | +*Question: Should we embrace or fight this flexibility?* |
| 91 | + |
| 92 | +### Library Design Best Practices |
| 93 | + |
| 94 | +Code libraries are often better served by embracing flexibility. |
| 95 | +Don't restrict your options. With great flexibility comes great power. |
| 96 | + |
| 97 | +# 5.1 Dictionaries Revisited |
| 98 | + |
| 99 | +### The "Mixin" Pattern |
| 100 | + |
| 101 | +The *Mixin* pattern is a class with a fragment of code. |
| 102 | + |
| 103 | +```python |
| 104 | +class Loud: |
| 105 | + def noise(self): |
| 106 | + return super().noise().upper() |
| 107 | +``` |
| 108 | + |
| 109 | +This class is not usable in isolation. |
| 110 | +It mixes with other classes via inheritance. |
| 111 | + |
| 112 | +```python |
| 113 | +class LoudDog(Loud, Dog): |
| 114 | + pass |
| 115 | + |
| 116 | +class LoudBike(Loud, Bike): |
| 117 | + pass |
| 118 | +``` |
| 119 | + |
| 120 | +Miraculously, loudness was now implemented just once and reused |
| 121 | +in two completely unrelated classes. This sort of trick is one |
| 122 | +of the primary uses of multiple inheritance in Python. |
| 123 | + |
| 124 | +### Why `super()` |
| 125 | + |
| 126 | +Always use `super()` when overriding methods. |
| 127 | + |
| 128 | +```python |
| 129 | +class Loud: |
| 130 | + def noise(self): |
| 131 | + return super().noise().upper() |
| 132 | +``` |
| 133 | + |
| 134 | +`super()` delegates to the *next class* on the MRO. |
| 135 | + |
| 136 | +The tricky bit is that you don't know what it is. You especially don't |
| 137 | +know what it is if multiple inheritance is being used. |
| 138 | + |
| 139 | +### Some Cautions |
| 140 | + |
| 141 | +Multiple inheritance is a powerful tool. Remember that with power |
| 142 | +comes responsibility. Frameworks / libraries sometimes use it for |
| 143 | +advanced features involving composition of components. Now, forget |
| 144 | +that you saw that. |
| 145 | + |
| 146 | + |
| 147 | +# 6.1 Interation Protocol |
| 148 | + |
| 149 | +One important observation about this--generally code is considered |
| 150 | +"Pythonic" if it speaks the common vocabulary of how other parts of |
| 151 | +Python normally work. For container objects, supporting iteration, |
| 152 | +indexing, containment, and other kinds of operators is an important |
| 153 | +part of this. |
| 154 | + |
| 155 | + |
| 156 | +# 6.4 More Generators |
| 157 | + |
| 158 | +### Why Generators |
| 159 | + |
| 160 | +* Many problems are much more clearly expressed in terms of iteration. |
| 161 | + * Looping over a collection of items and performing some kind of operation (searching, replacing, modifying, etc.). |
| 162 | + * Processing pipelines can be applied to a wide range of data processing problems. |
| 163 | +* Better memory efficiency. |
| 164 | + * Only produce values when needed. |
| 165 | + * Contrast to constructing giant lists. |
| 166 | + * Can operate on streaming data |
| 167 | +* Generators encourage code reuse |
| 168 | + * Separates the *iteration* from code that uses the iteration |
| 169 | + * You can build a toolbox of interesting iteration functions and *mix-n-match*. |
0 commit comments