[ PYTHON ESSENTIAL REFERENCE - ERRATA]

This page describes all of the typos and corrections for the Python
Essential Reference, 4th Edition. If you find errors not listed here,
please let me know so that I can make future editions better!

Special thanks to the following contributors for their "eagle eyes":

Aron Atkins

Randolph Bentson

Jeff Johnson

Nicholas Johnston

Andrew Montalenti

Art Rothstein

Andrew Savige

Hynek Schlawack

David Sharnoff

Mark Summerfield

Brian O'Neill

Locations are specified by page and paragraph numbers (counting from the top of the page). Errors
are denoted in red. Additions or corrections are denoted in
blue.

Location

Description

pg 7, ¶1

"On UNIX, EOF is Ctrl-D; on Windows, it's Ctrl-Z."

Should be

"On UNIX, EOF is Ctrl-D; on Windows, it's Ctrl-Z (or F6)."

pg 9, Note midpage.

"... If you do this, the normal indentation rules don't apply to the
next line, so you are free to format the continued lines as you wish."

Should be

"... If you do this, the normal indentation rules don't apply to the
next line, so you are free to format the continued lines as you wish.
A backslash is never needed for code enclosed in ( ), [
], or { }."

"The s.sort() method sorts the elements of a list and
optionally accepts a key function and reverse flag, both of which must
be specified as keyword arguments."

Should be

"The s.sort() method sorts the elements of a list (all of
which should be of a uniform type) and optionally accepts a key function
and reverse flag, both of which must be specified as keyword
arguments."

pg 41, table 3.4

Clarification of sort() description.

"Sorts items of s in place.
key is a key function. reverse is
a flag that sorts the list in reverse order. key
and reverse should always be specified as keyword
arguments."

Should be

"Sorts items of s in place.
key is a key function. reverse is
a flag that sorts the list in reverse order. key
and reverse should always be specified as keyword
arguments. The items in s should all be of a
uniform type.

pg 44, table 3.5

Clarification of zfill() description.

"Pads a string with zeros on the left up to the specified width.

Should be

"Pads a string with the '0' digit character on the left up to the specified width.

pg 45, table 3.6

Clarification of items() description.

"Returns a sequence of (key, value) pairs."

Should be

"Returns a sequence of all(key, value) pairs in m."

pg 45, table 3.6

Clarification of keys() description.

"Returns a sequence of key values."

Should be

"Returns a sequence of all key values in m."

pg 49, ¶3

"In the example, we have passed f, a an instance of Foo, ..."

Should be

"In the example, we have passed f, an instance of Foo, ..."

pg 55, code midpage

"is" should be "if".

x = A.__new__(A,args)
is isinstance(x,A): x.__init__(args)

Should be:

x = A.__new__(A,args)
if isinstance(x,A): x.__init__(args)

pg 58, ¶1

"Keep in mind that descriptions are optional and rarely need to be defined."

Should be

"Keep in mind that descriptors are optional and are rarely defined directly."

pg 58, Table 3.17

Clarify the description of __get__().

"Returns an attribute value or raises AttributeError."

Should be

"Returns the value of an attribute on instance. If instance is None, self should be returned."

pg 59, ¶3

Clarification of extended slicing

"For example, the following variations of extended slicing are all supported and might be useful for working with multidimensional data structures such as matrices and arrays:"

Should be

"For example, the following variations of extended slicing are all supported by Python's syntax and are sometimes used to manipulate multidimensional data structures such as
matrices and arrays in third-party extensions such as NumPy:"

pg 60, code at top of page

Extra parens in code comment

x = _iter.next() (#_iter.__next__() in Python 3)

Should be:

x = _iter.next() # _iter.__next__() in Python 3

pg 67, table midpage

In the "Operations on Sequences" section, there is a missing comma after "v2" and the wrong font used on the "s" in this fragment:

v1, v2... , vn = s

Should be (note italics on "s"):

v1, v2, ... , vn = s

pg 68, code at bottom

Wrong result in code example:

...
d = a[0:5:2] # d = [0,2]
...

Should be:

...
d = a[0:5:2] # d = [0,2,4]
...

pg 69, code in bottom half page

Use of a better example that illustrates replacing the entire contents of a list:

...
a[2:] = [0] # a = [1,6,0]
...

Should be replaced with:

...
a[:] = [7,8] # a = [7,8]. id(a) remains the same
...

pg 72, 2nd code example

More consistent use of string formatting:

name = "Elwood"
age = 41
r = "%(name)s is %(age)s years old" % vars()

Should be:

name = "Elwood"
age = 41
r = "%(name)s is %(age)d years old" % vars()

pg 72, ¶3

Missing "*" on kwargs:

"A more advanced form of string formatting is available using the s.format(*args,*kwargs) method on strings."

Should be:

"A more advanced form of string formatting is available using the s.format(*args,**kwargs) method on strings."

"Augmented assignment may or may not perform in-place modification of an object depending on its implementation. There, writing x + y
might modify x in-place or create an entirely new object with the value x + y.

These changes reflect the fact that the __get__() method of a descriptor should return the descriptor itself if the instance parameter is None.
This might occur if someone accesses a descriptor using the class instead of an instance.

pg 129, Code example midpage

Better comment:

u = Upperstr("hello") # value is "HELLO"

Should be:

u = Upperstr("hello") # u = "HELLO"

pg 132, ¶2

"For attribute lookup such as obj.name, the special method obj.__getattrribute__("name") is invoked:

Should be (fix mispelling of "getattribute)":

"For attribute lookup such as obj.name, the special method obj.__getattribute__("name") is invoked:

"... or supplying the metaclass keyword argument in the tuple of base classes (Python 3)."

Should be

"... or supplying the metaclass keyword argument after the tuple of base classes (Python 3)."

pg 139, code example at top

In this code example, add a small amount of space around the underscores in "__metaclass__" to make it
clear that there are two leading/trailing underscores and to match the typography used elsewhere on the same page :

class DocMeta(type):
def __init__(self,name,bases,dict):
for key, value in dict.items():
# Skip special and private methods
if key.startswith("__"): continue
# Skip anything not callable
if not hasattr(value,"__call__"): continue
# Check for a doc-string
if not getattr(value,"__doc__"):
raise TypeError("%s must have a docstring" % key)
type.__init__(self,name,bases,dict)

Should be (change all occurrences of "dict" to "attrs"):

class DocMeta(type):
def __init__(self,name,bases,attrs):
for key, value in attrs.items():
# Skip special and private methods
if key.startswith("__"): continue
# Skip anything not callable
if not hasattr(value,"__call__"): continue
# Check for a doc-string
if not getattr(value,"__doc__"):
raise TypeError("%s must have a docstring" % key)
type.__init__(self,name,bases,attrs)