Skip to content

Commit 3260f37

Browse files
BruceEckelclaude
andcommitted
Update prose around the listings that now print exception messages
ch05 and Solutions 05/22 quoted the full message as if the listing hid it; they now say the listing trims it. Solutions 20 exercise 2 caught `Exception` and relied on the printed type name to show FrozenInstanceError; it now catches FrozenInstanceError by name so the prose's reference still points at something in the listing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UwVtUw69CHsGnBRSZfQnyM
1 parent aacc0ad commit 3260f37

5 files changed

Lines changed: 9 additions & 7 deletions

File tree

Chapters/05_Functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ Only the named form reaches `total`.
429429

430430
Calling `divide(a=10, b=2)` is an error,
431431
because `a` and `b` are positional-only.
432-
The full message reports `got some positional-only arguments passed as keyword arguments: 'a, b'`.
432+
The full message ends by naming the offenders, `'a, b'`.
433+
The listing trims that tail to fit.
433434
Calling `make_user("Sue", True)` is an error, because `admin` is keyword-only.
434435
The type checker catches both mistakes without running the code,
435436
so each line carries a `# type: ignore` saying the misuse is deliberate.

Solutions/05_Functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ except UnboundLocalError as e:
216216
```
217217

218218
Both calls raise `UnboundLocalError: cannot access local variable
219-
'count' where it is not associated with a value`. Without `global`,
219+
'count' where it is not associated with a value`,
220+
which the listing trims after the variable name. Without `global`,
220221
the assignment in `count += 1` makes `count` local to
221222
`writes_global()`, so the read half of `+=` looks for a local that
222223
has no value yet. `rebinds()` fails for the same reason even though

Solutions/20_Rethinking_Objects.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ repeated. This is the tedium that motivates freezing the data instead.
6363

6464
```python
6565
# exercise_2.py
66-
from dataclasses import dataclass
66+
from dataclasses import FrozenInstanceError, dataclass
6767

6868
@dataclass(frozen=True)
6969
class Immutable:
@@ -75,7 +75,7 @@ print(data)
7575
#: Immutable(numbers=[1, 2, 999])
7676
try:
7777
data.numbers = [3] # type: ignore
78-
except Exception as e:
78+
except FrozenInstanceError as e:
7979
print(e)
8080
#: cannot assign to field 'numbers'
8181
try:

Solutions/22_Data_Transfer_Objects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ already refers to, so `append()` reaches straight through the record
8080
and edits its contents. Neither `ty` nor Python reports anything,
8181
because no assignment to a field ever happens.
8282

83-
Using the record as a `dict` key raises a `TypeError`, whose message
83+
Using the record as a `dict` key raises a `TypeError`, whose full message
8484
names the cause: `cannot use 'Recipe' as a dict key (unhashable type:
8585
'list')`. Hashing a tuple hashes each element, so a `Recipe` is
8686
hashable only when every field is. The `list` has no hash, so the

SolutionsCode/20_Rethinking_Objects/exercise_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# exercise_2.py
2-
from dataclasses import dataclass
2+
from dataclasses import FrozenInstanceError, dataclass
33

44
@dataclass(frozen=True)
55
class Immutable:
@@ -11,7 +11,7 @@ class Immutable:
1111
#: Immutable(numbers=[1, 2, 999])
1212
try:
1313
data.numbers = [3] # type: ignore
14-
except Exception as e:
14+
except FrozenInstanceError as e:
1515
print(e)
1616
#: cannot assign to field 'numbers'
1717
try:

0 commit comments

Comments
 (0)