1+ [ Contents] ( ../Contents ) \| [ Previous (2.7 Object Model)] ( ../02_Working_with_data/07_Objects ) \| [ Next (3.2 More on Functions)] ( 02_More_functions )
2+
13# 3.1 Scripting
24
35In this part we look more closely at the practice of writing Python
@@ -16,7 +18,7 @@ statement3
1618...
1719```
1820
19- We have been writing scripts to this point.
21+ We have mostly been writing scripts to this point.
2022
2123### A Problem
2224
@@ -28,7 +30,7 @@ organized.
2830
2931### Defining Things
3032
31- You must always define things before they get used later on in a program .
33+ Names must always be defined before they get used later.
3234
3335``` python
3436def square (x ):
@@ -41,11 +43,12 @@ z = square(b) # Requires `square` and `b` to be defined
4143```
4244
4345** The order is important.**
44- You almost always put the definitions of variables an functions near the beginning .
46+ You almost always put the definitions of variables and functions near the top .
4547
4648### Defining Functions
4749
4850It is a good idea to put all of the code related to a single * task* all in one place.
51+ Use a function.
4952
5053``` python
5154def read_prices (filename ):
@@ -85,7 +88,7 @@ def foo():
8588 help (math)
8689```
8790
88- There are no * special* statements in Python.
91+ There are no * special* statements in Python (which makes it easy to remember) .
8992
9093### Function Definition
9194
@@ -106,13 +109,14 @@ def foo(x):
106109 bar(x)
107110```
108111
109- Functions must only be defined before they are actually * used* (or called) during program execution.
112+ Functions must only be defined prior to actually being * used* (or called) during program execution.
110113
111114``` python
112115foo(3 ) # foo must be defined already
113116```
114117
115- Stylistically, it is probably more common to see functions defined in a * bottom-up* fashion.
118+ Stylistically, it is probably more common to see functions defined in
119+ a * bottom-up* fashion.
116120
117121### Bottom-up Style
118122
@@ -137,24 +141,26 @@ def spam(x):
137141spam(42 ) # Code that uses the functions appears at the end
138142```
139143
140- Later functions build upon earlier functions.
144+ Later functions build upon earlier functions. Again, this is only
145+ a point of style. The only thing that matters in the above program
146+ is that the call to ` spam(42) ` go last.
141147
142148### Function Design
143149
144150Ideally, functions should be a * black box* .
145151They should only operate on passed inputs and avoid global variables
146- and mysterious side-effects. Main goals: * Modularity* and * Predictability* .
152+ and mysterious side-effects. Your main goals: * Modularity* and * Predictability* .
147153
148154### Doc Strings
149155
150- A good practice is to include documentations in the form of
151- doc-strings . Doc-strings are strings written immediately after the
156+ It's good practice to include documentation in the form of a
157+ doc-string . Doc-strings are strings written immediately after the
152158name of the function. They feed ` help() ` , IDEs and other tools.
153159
154160``` python
155161def read_prices (filename ):
156162 '''
157- Read prices from a CSV file of name,price
163+ Read prices from a CSV file of name,price data
158164 '''
159165 prices = {}
160166 with open (filename) as f:
@@ -164,14 +170,19 @@ def read_prices(filename):
164170 return prices
165171```
166172
173+ A good practice for doc strings is to write a short one sentence
174+ summary of what the function does. If more information is needed,
175+ include a short example of usage along with a more detailed
176+ description of the arguments.
177+
167178### Type Annotations
168179
169- You can also add some optional type annotations to your function definitions.
180+ You can also add optional type hints to function definitions.
170181
171182``` python
172183def read_prices (filename : str ) -> dict :
173184 '''
174- Read prices from a CSV file of name,price
185+ Read prices from a CSV file of name,price data
175186 '''
176187 prices = {}
177188 with open (filename) as f:
@@ -181,13 +192,15 @@ def read_prices(filename: str) -> dict:
181192 return prices
182193```
183194
184- These do nothing. It is purely informational.
185- They may be used by IDEs, code checkers, etc.
195+ The hints do nothing operationally. They are purely informational.
196+ However, they may be used by IDEs, code checkers, and other tools
197+ to do more.
186198
187199## Exercises
188200
189- In section 2, you wrote a program called ` report.py ` that printed out a report showing the performance of a stock portfolio.
190- This program consisted of some functions. For example:
201+ In section 2, you wrote a program called ` report.py ` that printed out
202+ a report showing the performance of a stock portfolio. This program
203+ consisted of some functions. For example:
191204
192205``` python
193206# report.py
@@ -215,8 +228,9 @@ def read_portfolio(filename):
215228...
216229```
217230
218- However, there were also portions of the program that just performed a series of scripted calculations.
219- This code appeared near the end of the program. For example:
231+ However, there were also portions of the program that just performed a
232+ series of scripted calculations. This code appeared near the end of
233+ the program. For example:
220234
221235``` python
222236...
@@ -231,7 +245,8 @@ for row in report:
231245...
232246```
233247
234- In this exercise, we’re going take this program and organize it a little more strongly around the use of functions.
248+ In this exercise, we’re going take this program and organize it a
249+ little more strongly around the use of functions.
235250
236251### Exercise 3.1: Structuring a program as a collection of functions
237252
@@ -242,10 +257,12 @@ functions. Specifically:
242257* Create a function ` print_report(report) ` that prints out the report.
243258* Change the last part of the program so that it is nothing more than a series of function calls and no other computation.
244259
245- ### Exercise 3.2: Creating a function for program execution
260+ ### Exercise 3.2: Creating a top-level function for program execution
246261
247- Take the last part of your program and package it into a single function ` portfolio_report(portfolio_filename, prices_filename) ` .
248- Have the function work so that the following function call creates the report as before:
262+ Take the last part of your program and package it into a single
263+ function ` portfolio_report(portfolio_filename, prices_filename) ` .
264+ Have the function work so that the following function call creates the
265+ report as before:
249266
250267``` python
251268portfolio_report(' Data/portfolio.csv' , ' Data/prices.csv' )
@@ -256,8 +273,9 @@ of function definitions followed by a single function call to
256273` portfolio_report() ` at the very end (which executes all of the steps
257274involved in the program).
258275
259- By turning your program into a single function, it becomes easy to run it on different inputs.
260- For example, try these statements interactively after running your program:
276+ By turning your program into a single function, it becomes easy to run
277+ it on different inputs. For example, try these statements
278+ interactively after running your program:
261279
262280``` python
263281>> > portfolio_report(' Data/portfolio2.csv' , ' Data/prices.csv' )
@@ -272,4 +290,13 @@ For example, try these statements interactively after running your program:
272290>> >
273291```
274292
293+ ### Commentary
294+
295+ Python makes it very easy to write relatively unstructured scripting code
296+ where you just have a file with a sequence of statements in it. In the
297+ big picture, it's almost always better to utilize functions whenever
298+ you can. At some point, that script is going to grow and you'll wish
299+ you had a bit more organization. Also, a little known fact is that Python
300+ runs a bit faster if you use functions.
301+
275302[ Contents] ( ../Contents ) \| [ Previous (2.7 Object Model)] ( ../02_Working_with_data/07_Objects ) \| [ Next (3.2 More on Functions)] ( 02_More_functions )
0 commit comments