Links and renumbering

This commit is contained in:
David Beazley
2020-05-26 15:30:14 -05:00
parent 9c032f7a23
commit 45c7ec1f4a
10 changed files with 49 additions and 42 deletions

View File

@@ -362,10 +362,10 @@ Frameworks / libraries sometimes use it for advanced features involving composit
## Exercises
In Exercise 4.1, you defined a class `Stock` that represented a holding of stock.
In Section 4, you defined a class `Stock` that represented a holding of stock.
In this exercise, we will use that class.
### (a) Representation of Instances
### Exercise 5.1: Representation of Instances
At the interactive shell, inspect the underlying dictionaries of the two instances you created:
@@ -380,7 +380,7 @@ At the interactive shell, inspect the underlying dictionaries of the two instanc
>>>
```
### (b) Modification of Instance Data
### Exercise 5.2: Modification of Instance Data
Try setting a new attribute on one of the above instances:
@@ -412,7 +412,7 @@ top of a dictionary. *Note: it should be emphasized that direct
manipulation of the dictionary is uncommon—you should always write
your code to use the (.) syntax.*
### (c) The role of classes
### Exercise 5.3: The role of classes
The definitions that make up a class definition are shared by all instances of that class.
Notice, that all instances have a link back to their associated class:
@@ -517,7 +517,7 @@ It is shared by all of the instances that get created.
>>>
```
### (d) Bound Methods
### Exercise 5.4: Bound Methods
A subtle feature of Python is that invoking a method actually involves
two steps and something known as a bound method.
@@ -568,7 +568,7 @@ For example, calling `s(25)` actually does this:
>>>
```
### (e) Inheritance
### Exercise 5.5: Inheritance
Make a new class that inherits from `Stock`.
@@ -617,4 +617,5 @@ Heres how the `cost()` method of instance `n` above would be found:
>>>
```
[Next](02_Classes_encapsulation)
[Contents](../Contents) \| [Previous (4.4 Exceptions)](../04_Classes_objects/04_Defining_exceptions) \| [Next (5.2 Encapsulation)](02_Classes_encapsulation)

View File

@@ -252,7 +252,7 @@ day-to-day coding.
## Exercises
### (a) Simple properties
### Exercise 5.6: Simple properties
Properties are a useful way to add "computed attributes" to an object.
In Exercise 4.1, you created an object `Stock`. Notice that on your
@@ -289,7 +289,7 @@ Try calling `s.cost()` as a function and observe that it doesnt work now that
>>>
```
### (b) Properties and Setters
### Exercise 5.7: Properties and Setters
Modify the `shares` attribute so that the value is stored in a private
attribute and that a pair of property functions are used to ensure
@@ -306,7 +306,7 @@ TypeError: expected an integer
>>>
```
### (c) Adding slots
### Exercise 5.8: Adding slots
Modify the `Stock` class so that it has a `__slots__` attribute.
Then, verify that new attributes cant be added:
@@ -333,3 +333,5 @@ What happens if you try to inspect the underlying dictionary of `s` above?
It should be noted that `__slots__` is most commonly used as an
optimization on classes that serve as data structures. Using slots
will make such programs use far-less memory and run a bit faster.
[Contents](../Contents) \| [Previous (5.1 Dictionaries Revisited)](01_Dicts_revisited) \| [Next (6 Generators)](../06_Generators/00_Overview)