469,589 Members | 2,177 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,589 developers. It's quick & easy.

Trouble Understanding O'Reilly Example

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?
Jul 18 '05 #1
5 1354
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.

"slyraymond" <sl*********@charter.net> wrote in message
news:10*************@corp.supernews.com...
| 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?
Jul 18 '05 #2
slyraymond wrote:
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
Jul 18 '05 #3
>>>>> "slyraymond" == slyraymond <sl*********@charter.net> writes:

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.
Jul 18 '05 #4
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!

Chris wrote:
slyraymond wrote:
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


Jul 18 '05 #5
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)
slyraymond <sl*********@charter.net> wrote in message news:<10*************@corp.supernews.com>...
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?

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andreas Paasch | last post: by
14 posts views Thread by BlackHawke | last post: by
4 posts views Thread by the.theorist | last post: by
7 posts views Thread by Richard Phillips | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.