Links and renumbering
This commit is contained in:
@@ -110,7 +110,7 @@ If you want to operate on an instance, you always have to refer too it explicitl
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Objects as Data Structures
|
||||
### Exercise 4.1: Objects as Data Structures
|
||||
|
||||
In section 2 and 3, we worked with data represented as tuples and dictionaries.
|
||||
For example, a holding of stock could be represented as a tuple like this:
|
||||
@@ -182,7 +182,7 @@ dictionary, just with somewhat different syntax.
|
||||
For example, instead of writing `s['name']` or `s['price']`, you now
|
||||
write `s.name` and `s.price`.
|
||||
|
||||
### (b) Reading Data into a List of Objects
|
||||
### Exercise 4.2: Reading Data into a List of Objects
|
||||
|
||||
In your `stock.py` program, write a function
|
||||
`read_portfolio(filename)` that reads portfolio data from a file into
|
||||
@@ -226,7 +226,7 @@ Try a list comprehension:
|
||||
|
||||
Again, notice the similarity between `Stock` objects and dictionaries. They’re basically the same idea, but the syntax for accessing values differs.
|
||||
|
||||
### (c) Adding some Methods
|
||||
### Exercise 4.3: Adding some Methods
|
||||
|
||||
With classes, you can attach functions to your objects. These are
|
||||
known as methods and are functions that operate on the data stored
|
||||
@@ -250,4 +250,4 @@ work like this:
|
||||
>>>
|
||||
```
|
||||
|
||||
[Next](02_Inheritance)
|
||||
[Contents](../Contents) \| [Previous (3.6 Design discussion)](../03_Program_organization/06_Design_discussion) \| [Next (4.2 Inheritance)](02_Inheritance)
|
||||
|
||||
@@ -220,7 +220,7 @@ We're not going to explore multiple inheritance further in this course.
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Print Portfolio
|
||||
### Exercise 4.4: Print Portfolio
|
||||
|
||||
A major use of inheritance is in writing code that’s meant to be extended or customized in various ways—especially in libraries or frameworks.
|
||||
To illustrate, start by adding the following function to your `stock.py` program:
|
||||
@@ -263,7 +263,7 @@ When you run your `stock.py`, you should get this output:
|
||||
IBM 100 70.44
|
||||
```
|
||||
|
||||
### (b) An Extensibility Problem
|
||||
### Exercise 4.5: An Extensibility Problem
|
||||
|
||||
Suppose that you wanted to modify the `print_portfolio()` function to
|
||||
support a variety of different output formats such as plain-text,
|
||||
@@ -329,7 +329,7 @@ if __name__ == '__main__':
|
||||
When you run this new code, your program will immediately crash with a `NotImplementedError` exception.
|
||||
That’s not too exciting, but continue to the next part.
|
||||
|
||||
### (c) Using Inheritance to Produce Different Output
|
||||
### Exercise 4.6: Using Inheritance to Produce Different Output
|
||||
|
||||
The `TableFormatter` class you defined in part (a) is meant to be extended via inheritance.
|
||||
In fact, that’s the whole idea. To illustrate, define a class `TextTableFormatter` like this:
|
||||
@@ -430,7 +430,7 @@ Using a similar idea, define a class `HTMLTableFormatter` that produces a table
|
||||
|
||||
Test your code by modifying the main program to create a `HTMLTableFormatter` object instead of a `CSVTableFormatter` object.
|
||||
|
||||
### (d) Polymorphism in Action
|
||||
### Exercise 4.7: Polymorphism in Action
|
||||
|
||||
A major feature of object-oriented programming is that you can plug an
|
||||
object into a program and it will work without having to change any of
|
||||
@@ -499,4 +499,4 @@ That said, understanding what happened in this exercise will take you
|
||||
pretty far in terms of using most library modules and knowing
|
||||
what inheritance is good for (extensibility).
|
||||
|
||||
[Next](03_Special_methods)
|
||||
[Contents](../Contents) \| [Previous (4.1 Classes)](01_Class) \| [Next (4.3 Special methods)](03_Special_methods)
|
||||
|
||||
@@ -202,7 +202,7 @@ x = getattr(obj, 'x', None)
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Better output for printing objects
|
||||
### Exercise 4.8: Better output for printing objects
|
||||
|
||||
All Python objects have two string representations. The first
|
||||
representation is created by string conversion via `str()` (which is
|
||||
@@ -248,7 +248,7 @@ See what happens when you read a portfolio of stocks and view the resulting list
|
||||
>>>
|
||||
```
|
||||
|
||||
### (b) An example of using `getattr()`
|
||||
### Exercise 4.9: An example of using `getattr()`
|
||||
|
||||
In Exercise 4.2 you worked with a function `print_portfolio()` that made a table for a stock portfolio.
|
||||
That function was hard-coded to only work with stock data—-how limiting! You can do so much more if you use functions such as `getattr()`.
|
||||
@@ -306,7 +306,7 @@ format. Here’s how it should work:
|
||||
>>>
|
||||
```
|
||||
|
||||
### (c) Exercise Bonus: Column Formatting
|
||||
### Exercise 4.10: Exercise Bonus: Column Formatting
|
||||
|
||||
Modify the `print_table()` function in part (B) so that it also
|
||||
accepts a list of format specifiers for formatting the contents of
|
||||
@@ -329,4 +329,5 @@ each column.
|
||||
>>>
|
||||
```
|
||||
|
||||
[Next](04_Defining_exceptions)
|
||||
[Contents](../Contents) \| [Previous (4.2 Inheritance)](02_Inheritance) \| [Next (4.4 Exceptions)](04_Defining_exceptions)
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ class ProtocolError(NetworkError):
|
||||
|
||||
## Exercises
|
||||
|
||||
### (a) Defining a custom exception
|
||||
### Exercise 4.11: Defining a custom exception
|
||||
|
||||
It is often good practice for libraries to define their own exceptions.
|
||||
|
||||
@@ -47,3 +47,5 @@ Traceback (most recent call last):
|
||||
FormatError: Unknown table format xls
|
||||
>>>
|
||||
```
|
||||
|
||||
[Contents](../Contents) \| [Previous (4.3 Special methods)](03_Special_methods) \| [Next (5 Object Model)](../05_Object_model/00_Overview)
|
||||
|
||||
Reference in New Issue
Block a user