This book covers Python 3.0 and 3.1 and should work with all Python 3.x versions. After reading the book we recommend reading the "What's New in Python" documents for 3.2, 3.3, etc., to learn about new features and improvements that have been made since the book was published.
Second Edition: Errata for Second Printing
Second Edition: Errata for All Printings
remove()
which removes an item at a given index position." with
"and remove()
which removes the given item." Later references
to the list.remove()
method are correct.
Daniel Wójcikequal_float()
function shown on
this page is simple and standard, but it doesn't work well when the
numbers compared have very different magnitudes. Here's an improved
version:
def equal_float(a, b):
return abs(a - b) <= (sys.float_info.epsilon * min(abs(a), abs(b)))
The examples' Util
module has an equal_float()
function that takes an optional third argument—the number of
decimal places to compare to.
William Joness.isdigit()
description to "a Unicode digit".
Ross Burnettgreens
example), I say: "Here, using mapping unpacking
(**
) has exactly the same effect as
writing .format(green=greens.green, olive=greens.olive,
lime=greens.lime)
". That would be true if greens
was an
object with those attributes, but in fact it is simply a dict
with those keys, so the correct code is:
.format(green=greens["green"], olive=greens["olive"],
lime=greens["lime"])
.
Clemens Kaposifrom
.. import Png
(which means import the Png
module from the
module two directories—because there are two dots—above this
one, i.e., from the Graphics
module). This correction has been
applied to the downloadable archives.
Authoroptparse
module discussed here is
deprecated in Python 3.2; it has been superceded by the
argparse
module. The book's examples continue to use
optparse
so that they work with all Python 3.x
versions. Stick with optparse
if you need to maintain backward
compatibility with Python 3.1; otherwise switch to
argparse
which is better (and easy to switch to).
Jim JohnstonSortedList.py
module (and various
alternative versions such as SortedListAbc.py
), have a subtle
bug that causes incorrect behavior if the key function folds case (e.g.,
lambda x: x.lower()
) and there are multiple values which
have the same keys (e.g., "abc"
and "ABC"
). The
algorithms used in the methods shown in the book are correct, but many
of the methods must be changed to account for this use case. The
necessary corrections have been applied to the downloadable
archives.
Kahn Fusion__set__()
method), and returns the
descriptor itself, since it is the descriptor through which the instance
data is accessed."
setter()
method (return
self.__setter
) with return self
.
Luca Boassogrepword-t.py
and
grepword-m.py
examples.
In modern versions of Python these examples can produce
inconsistent results on different runs. This problem can be solved for
both programs by using a result queue instead of printing as we go. This
requires the following changes:
main()
method by adding one of these lines after creating the
work_queue
:result_queue = queue.Queue() # grepword-t.py
result_queue = multiprocessing.SimpleQueue() # grepword-m.py
Worker
class'
__init__()
method's signature to: def __init__(self, work_queue, result_queue, word,
number):
result_queue
in the instance by adding this
line in the __init__()
method's body: self.result_queue = result_queue
Worker
class'
run()
method, replacing the print()
statement
with: self.result_queue.put(f'{self.number}{filename}')
main()
method after
the work_queue.join()
call:
while not result_queue.empty():
print(result_queue.get())
AUTOINCREMENT
. For some
versions of SQLite this keyword is a syntax error. If this is the case
for your version of SQLite simply delete the keyword—you will
still get autoincrementing since for versions of SQLite that don't
accept AUTOINCREMENT
, the use of INTEGER PRIMARY KEY
implies autoincrement.
Florian Rämischpyparsing
for both
Python 2 and Python 3 (before it was pyparsing_py3
for Python 3). The other changes are bug fixes, enhancements, and
additional examples, so apart from the import, everything in the book
remains valid.
AuthorSecond Edition: Errata for the First Printing only (additional to the errata above—these are all fixed in the Second and subsequent printings)
print_unicode_uni.py
and quadratic_uni.py
)
are provided to work around this. Now Glenn Linderman has found a
solution (that I've tested successfully with Python 3.1); see
Python Bug
1602/message #94445. If the
solution is followed, Windows users can use the same
print_unicode.py
and quadratic.py
programs as Linux
and Mac OS X users and see the same characters output. (Be
aware though, that this may cause problems executing other programs in
the console, so for anything else it is probably best to use a separate
console.)
Authorprint_unicode()
function has a
subtle bug. Replace the line:
end = sys.maxunicode
with the line:
end = min(0xD800, sys.maxunicode) # Stop at surrogate pairs
This is because Python can't handle surrogate pairs and some of them
start at code point U+D800. This correction has been applied to the downloadable archives.
Hugo Gagnon__repr__()
method I say that it is necessary to call
super().__repr__()
to access the base class's
__repr__()
method without causing infinite recursion. That
reason is true in general, but not in this particular case. Nonetheless,
the code shown is correct. What I should have said is this:
str.format()
's second argument we cannot just pass
self
. There are two reasons for this. First, if we use
self
, Python will format it using the __str__()
method
if that is present in this class or (as in this case) in one of its base
classes, whereas we want the representational format rather than the
string format. And second, if there is no __str__()
implementation, Python will fall back to using __repr__()
, thus
leading to infinite recursion. By calling the base class's
__repr__()
method we ensure that we get the representational
format and at the same time avoid depending on whether or not a
__str__()
method happens to be implemented."
Luca BoassoYour Privacy • Copyright © 2006 Mark Summerfield. All Rights Reserved. Patent Free