Links and renumbering
This commit is contained in:
@@ -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 @@ Here’s 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)
|
||||
|
||||
|
||||
@@ -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 doesn’t 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 can’t 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)
|
||||
|
||||
Reference in New Issue
Block a user