473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1470
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andreas Paasch | last post by:
I'm having trouble with the implode function and need some help here. I've been reading at php.net but that didn't solve my problem. I'm having the below code: $query = "SELECT top1, top2,...
14
by: BlackHawke | last post by:
My Name is Nick Soutter, I am the owner of a small game company, Aepox Games (We're in the middle of a name change from "Lamar Games"), www.lamargames.net. Our first commercial game,...
4
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
7
by: Richard Phillips | last post by:
Hello, Does anyone else out there have a copy of this? I'm working through my copy and have discovered that it has no chapter 11... Pages 341-388 are just not there! Anyone else able to check...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.