1
1
# Updating the code from Python 2 to Python 3
2
2
3
+ After many requests, here's the code from the book updated from Python 2 to Python 3.
4
+ I have been telling people that there aren't too many changes required, but it turned
5
+ out there were quite a few. Start-to-finish I'd say the porting took me about 4 hours,
6
+ and I'm pretty familiar with the code. Here's a fairly comprehensive list of the issues
7
+ I ran into.
8
+
3
9
## ` print `
4
10
5
11
The first and most obvious difference is that in Python 3 ` print ` takes parentheses.
@@ -15,12 +21,12 @@ had to be replaced with
15
21
print("stuff", 1)
16
22
```
17
23
18
- This is mostly tedious.
24
+ This was mostly just tedious. I should have used 2to3 .
19
25
20
26
## tuple unpacking
21
27
22
28
<a href =" https://www.python.org/dev/peps/pep-3113/ " >PEP-3113</a > eliminates
23
- tuple unpacking in certain places . In particular, that means that code like
29
+ tuple unpacking in function parameters . In particular, that means that code like
24
30
25
31
```
26
32
key=lambda (a, b): b
@@ -50,7 +56,7 @@ doesn't work, and needs to be replaced with
50
56
list(filter(is_even, my_list))[0]
51
57
```
52
58
53
- And likewise with ` zip ` , which in many instances needs to be replaced with ` list(zip(...)) ` .
59
+ And likewise with ` zip ` , which in many instances needs to be replaced with ` list(zip(...)) ` . (In particular, this uglies up my magic unzip trick.)
54
60
55
61
In the most subtle case this bit me at (in essence):
56
62
@@ -68,7 +74,7 @@ data = list(map(clean, data))
68
74
```
69
75
70
76
Similarly, if you have a ` dict ` then its ` .keys() ` is lazy, so you have to wrap
71
- it in ` list ` as well.
77
+ it in ` list ` as well. This is possibly my least favorite change in Python 3.
72
78
73
79
## binary mode for CSVs
74
80
0 commit comments