By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,658 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

Trouble Understanding O'Reilly Example

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
>>>>> "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

P: n/a
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

P: n/a
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.