11# Updating the code from Python 2 to Python 3
22
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+
39## ` print `
410
511The first and most obvious difference is that in Python 3 ` print ` takes parentheses.
@@ -15,12 +21,12 @@ had to be replaced with
1521print("stuff", 1)
1622```
1723
18- This is mostly tedious.
24+ This was mostly just tedious. I should have used 2to3 .
1925
2026## tuple unpacking
2127
2228<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
2430
2531```
2632key=lambda (a, b): b
@@ -50,7 +56,7 @@ doesn't work, and needs to be replaced with
5056list(filter(is_even, my_list))[0]
5157```
5258
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.)
5460
5561In the most subtle case this bit me at (in essence):
5662
@@ -68,7 +74,7 @@ data = list(map(clean, data))
6874```
6975
7076Similarly, 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.
7278
7379## binary mode for CSVs
7480
0 commit comments