Trouble Understanding O'Reilly Example

S

slyraymond

On page 214 of _Learning Python_, the following function is described as one
that will return the smallest item in a group of arguments:

def min1(*args):
res = args[0]
for arg in args[1:]:
if arg < args:
res = arg
return res

However, when the function called with...

print min1(3,4,1,2)

....it returns:

2

Why?
 
E

Elaine Jackson

This is one of those "V8 bugs" that make you smack your forehead: line 4 of the
function definition should read " if arg < res: ". Fix that, and everything
works as expected.

| On page 214 of _Learning Python_, the following function is described as one
| that will return the smallest item in a group of arguments:
|
| def min1(*args):
| res = args[0]
| for arg in args[1:]:
| if arg < args:
| res = arg
| return res
|
| However, when the function called with...
|
| print min1(3,4,1,2)
|
| ...it returns:
|
| 2
|
| Why?
 
I

Isaac To

slyraymond> On page 214 of _Learning Python_, the following function is
slyraymond> described as one that will return the smallest item in a
slyraymond> group of arguments:

slyraymond> def min1(*args): res = args[0] for arg in args[1:]: if arg <
slyraymond> args: res = arg return res

Should be, if arg < res.

Regards,
Isaac.
 
C

Chris

slyraymond said:
def min1(*args):
res = args[0]
for arg in args[1:]:
if arg < args:
res = arg
return res
Apologizing for whatever damage knode did to the formatting, try:

if arg < res:

instead of

if arg < args

For debugging purposes, add the line

print args

just before the return statement to see exactly what is going on.

Chris
 
S

slyraymond

So the book contains a typo.

Hmm. What's amusing about this particular typo is that on the very next
page the authors give the result of the function call, and the erroneous
result is consistent with the code:

(from p. 215):
"C:\Python22>python mins.py
2"

....a double typo!
slyraymond said:
def min1(*args):
res = args[0]
for arg in args[1:]:
if arg < args:
res = arg
return res
Apologizing for whatever damage knode did to the formatting, try:

if arg < res:

instead of

if arg < args

For debugging purposes, add the line

print args

just before the return statement to see exactly what is going on.

Chris
 
M

Mark Lutz

Congratulations -- you've found what is probably
the worst typo in the first printing of the 2nd
Edition of this book. As others have pointed
out, it should say arg < res, not arg < args.

For future reference, O'Reilly maintains the full
list of errata for the book, including this one,
here:

http://www.oreilly.com/catalog/lpython2/errata/

Typos happen, of course, and this edition has a
relatively low number of them. But this one is
made all the more maddening by the fact that I've
coded this example correctly at least one hundred
times during classes. Despite this, testing, and
a formal technical review process, typos always
manage to sneak in. Alas, writing computer books
is no place for a perfectionist to be.

--Mark Lutz (http://www.rmi.net/~lutz)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top