Trouble Understanding O'Reilly Example

Discussion in 'Python' started by slyraymond, Apr 26, 2004.

  1. slyraymond

    slyraymond Guest

    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?
     
    slyraymond, Apr 26, 2004
    #1
    1. Advertising

  2. 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?
     
    Elaine Jackson, Apr 26, 2004
    #2
    1. Advertising

  3. slyraymond

    Isaac To Guest

    >>>>> "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.
     
    Isaac To, Apr 26, 2004
    #3
  4. slyraymond

    Chris Guest

    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
     
    Chris, Apr 26, 2004
    #4
  5. slyraymond

    slyraymond Guest

    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
     
    slyraymond, Apr 26, 2004
    #5
  6. slyraymond

    Mark Lutz Guest

    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?
     
    Mark Lutz, Apr 26, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse Liberty
    Replies:
    2
    Views:
    319
  2. sauzer
    Replies:
    1
    Views:
    970
    Teemu Keiski
    Jun 17, 2005
  3. sal
    Replies:
    0
    Views:
    382
  4. Moritz Beller

    O'Reilly book: Mastering algorithms with C

    Moritz Beller, Jun 4, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,421
    CBFalconer
    Jun 7, 2004
  5. Ron Stephens

    Learning Python, 2'nd Edition, O'Reilly

    Ron Stephens, Sep 11, 2003, in forum: Python
    Replies:
    3
    Views:
    304
    Ron Stephens
    Sep 12, 2003
Loading...

Share This Page