Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trouble Understanding O'Reilly Example

Reply
Thread Tools

Trouble Understanding O'Reilly Example

 
 
slyraymond
Guest
Posts: n/a
 
      04-26-2004
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?
 
Reply With Quote
 
 
 
 
Elaine Jackson
Guest
Posts: n/a
 
      04-26-2004
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" <> wrote in message
news:...
| 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?


 
Reply With Quote
 
 
 
 
Isaac To
Guest
Posts: n/a
 
      04-26-2004
>>>>> "slyraymond" == slyraymond <> 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.
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      04-26-2004
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
 
Reply With Quote
 
slyraymond
Guest
Posts: n/a
 
      04-26-2004
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


 
Reply With Quote
 
Mark Lutz
Guest
Posts: n/a
 
      04-26-2004
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 <> wrote in message news:<>...
> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help understanding an Object Oriented Program example goldtech Python 3 10-29-2012 10:21 AM
std::list of class pointers, understanding problem (with minimal example) Frank Steinmetzger C++ 4 07-29-2010 04:12 PM
std::list of class pointers, understanding problem (with minimal example) Frank Steinmetzger C++ 0 07-28-2010 10:54 AM
Understanding exception handler example in SDK ASP General 1 12-12-2004 04:11 PM
Trouble understanding StateServer!! Ric Pullen ASP .Net 1 07-23-2004 08:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57