Added links. Renumber
This commit is contained in:
@@ -87,7 +87,7 @@ These are not commonly used except when writing library functions.
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) A simple example of variable arguments
|
||||
### Exercise 7.1: A simple example of variable arguments
|
||||
|
||||
Try defining the following function:
|
||||
|
||||
@@ -106,7 +106,7 @@ Try defining the following function:
|
||||
|
||||
Notice how the parameter `*more` collects all of the extra arguments.
|
||||
|
||||
### (b) Passing tuple and dicts as arguments
|
||||
### Exercise 7.2: Passing tuple and dicts as arguments
|
||||
|
||||
Suppose you read some data from a file and obtained a tuple such as
|
||||
this:
|
||||
@@ -146,7 +146,7 @@ Stock('GOOG', 100, 490.1)
|
||||
>>>
|
||||
```
|
||||
|
||||
### (c) Creating a list of instances
|
||||
### Exercise 7.3: Creating a list of instances
|
||||
|
||||
In your `report.py` program, you created a list of instances
|
||||
using code like this:
|
||||
@@ -169,7 +169,7 @@ def read_portfolio(filename):
|
||||
|
||||
You can simplify that code using `Stock(**d)` instead. Make that change.
|
||||
|
||||
### (d) Argument pass-through
|
||||
### Exercise 7.4: Argument pass-through
|
||||
|
||||
The `fileparse.parse_csv()` function has some options for changing the
|
||||
file delimiter and for error reporting. Maybe you'd like to expose those
|
||||
@@ -211,4 +211,4 @@ Now, try silencing the errors:
|
||||
>>>
|
||||
```
|
||||
|
||||
[Next](02_Anonymous_function)
|
||||
[Contents](../Contents) \| [Previous (6.4 Generator Expressions)](../06_Generators/04_More_generators) \| [Next (7.2 Anonymous Functions)](02_Anonymous_function)
|
||||
|
||||
@@ -108,7 +108,7 @@ Stock('IBM', 100, 70.44)
|
||||
>>>
|
||||
```
|
||||
|
||||
### (a) Sorting on a field
|
||||
### Exercise 7.5: Sorting on a field
|
||||
|
||||
Try the following statements which sort the portfolio data
|
||||
alphabetically by stock name.
|
||||
@@ -129,7 +129,7 @@ In this part, the `stock_name()` function extracts the name of a stock from
|
||||
a single entry in the `portfolio` list. `sort()` uses the result of
|
||||
this function to do the comparison.
|
||||
|
||||
### (b) Sorting on a field with lambda
|
||||
### Exercise 7.6: Sorting on a field with lambda
|
||||
|
||||
Try sorting the portfolio according the number of shares using a
|
||||
`lambda` expression:
|
||||
@@ -158,4 +158,4 @@ Note: `lambda` is a useful shortcut because it allows you to
|
||||
define a special processing function directly in the call to `sort()` as
|
||||
opposed to having to define a separate function first (as in part a).
|
||||
|
||||
[Next](03_Returning_functions)
|
||||
[Contents](../Contents) \| [Previous (7.1 Variable Arguments)](01_Variable_arguments) \| [Next (7.3 Returning Functions)](03_Returning_function)
|
||||
|
||||
@@ -119,7 +119,7 @@ You can write functions that make code.
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Using Closures to Avoid Repetition
|
||||
### Exercise 7.7: Using Closures to Avoid Repetition
|
||||
|
||||
One of the more powerful features of closures is their use in
|
||||
generating repetitive code. If you refer back to exercise 5.2
|
||||
@@ -195,7 +195,7 @@ Try creating an instance and verifying that type-checking works.
|
||||
>>>
|
||||
```
|
||||
|
||||
### (b) Simplifying Function Calls
|
||||
### Exercise 7.8: Simplifying Function Calls
|
||||
|
||||
In the above example, users might find calls such as
|
||||
`typedproperty('shares', int)` a bit verbose to type--especially if
|
||||
@@ -226,9 +226,9 @@ Ah, that's a bit better. The main takeaway here is that closures and `lambda`
|
||||
can often be used to simplify code and eliminate annoying repetition. This
|
||||
is often good.
|
||||
|
||||
### (c) Putting it into practice
|
||||
### Exercise 7.9: Putting it into practice
|
||||
|
||||
Rewrite the `Stock` class in the file `stock.py` so that it uses typed properties
|
||||
as shown.
|
||||
|
||||
[Next](04_Function_decorators)
|
||||
[Contents](../Contents) \| [Previous (7.2 Anonymous Functions)](02_Anonymous_function) \| [Next (7.4 Decorators)](04_Function_decorators)
|
||||
|
||||
@@ -103,7 +103,7 @@ However, the previous example is a good illustration of how their use tends to a
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) A decorator for timing
|
||||
### Exercise 7.10: A decorator for timing
|
||||
|
||||
If you define a function, its name and module are stored in the
|
||||
`__name__` and `__module__` attributes. For example:
|
||||
@@ -149,4 +149,4 @@ Discussion: This `@timethis` decorator can be placed in front of any
|
||||
function definition. Thus, you might use it as a diagnostic tool for
|
||||
performance tuning.
|
||||
|
||||
[Next](05_Decorated_methods)
|
||||
[Contents](../Contents) \| [Previous (7.3 Returning Functions)](03_Returning_functions) \| [Next (7.5 Decorated Methods)](05_Decorated_methods)
|
||||
|
||||
@@ -121,7 +121,7 @@ Start this exercise by defining a `Date` class. For example:
|
||||
>>>
|
||||
```
|
||||
|
||||
### (a) Class Methods
|
||||
### Exercise 7.11: Class Methods
|
||||
|
||||
A common use of class methods is to provide alternate constructors
|
||||
(epecially since Python doesn't support overloaded methods). Modify
|
||||
@@ -161,7 +161,7 @@ Yow!
|
||||
>>>
|
||||
```
|
||||
|
||||
### (b) Class Methods in Practice
|
||||
### Exercise 7.12: Class Methods in Practice
|
||||
|
||||
In your `report.py` and `portfolio.py` files, the creation of a `Portfolio`
|
||||
object is a bit muddled. For example, the `report.py` program has code like this:
|
||||
@@ -258,3 +258,4 @@ To use this new Portfolio class, you can now write code like this:
|
||||
Make these changes to the `Portfolio` class and modify the `report.py`
|
||||
code to use the class method.
|
||||
|
||||
[Contents](../Contents) \| [Previous (7.4 Decorators)](04_Function_decorators) \| [Next (8 Testing and Debugging)](../08_Testing_debugging/00_Overview)
|
||||
|
||||
Reference in New Issue
Block a user