Trouble Understanding O'Reilly Example

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slyraymond

    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?
  • Elaine Jackson

    #2
    Re: Trouble Understanding O'Reilly Example

    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 " <sly_raymond@ch arter.net> wrote in message
    news:108p21f7as q1cc8@corp.supe rnews.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?


    Comment

    • Chris

      #3
      Re: Trouble Understanding O'Reilly Example

      slyraymond wrote:
      [color=blue]
      > def min1(*args):
      > res = args[0]
      > for arg in args[1:]:
      > if arg < args:
      > res = arg
      > return res[/color]
      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

      Comment

      • Isaac To

        #4
        Re: Trouble Understanding O'Reilly Example

        >>>>> "slyraymond " == slyraymond <sly_raymond@ch arter.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.

        Comment

        • slyraymond

          #5
          Re: Trouble Understanding O'Reilly Example

          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>py thon mins.py
          2"

          ....a double typo!

          Chris wrote:
          [color=blue]
          > slyraymond wrote:
          >[color=green]
          >> def min1(*args):
          >> res = args[0]
          >> for arg in args[1:]:
          >> if arg < args:
          >> res = arg
          >> return res[/color]
          > 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[/color]

          Comment

          • Mark Lutz

            #6
            Re: Trouble Understanding O'Reilly Example

            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:



            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 <sly_raymond@ch arter.net> wrote in message news:<108p21f7a sq1cc8@corp.sup ernews.com>...[color=blue]
            > 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?[/color]

            Comment

            Working...