basic questions on cmp, < and sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schüle Daniel

    #1

    basic questions on cmp, < and sort

    Hello,

    first question

    In [117]: cmp("ABC",['A','B','C'])
    Out[117]: 1

    against what part of the list is the string "ABC" compared?

    second question

    In [119]: class X(object):
    .....: pass
    .....:

    In [120]: X() < X()
    Out[120]: True

    In [121]: X() < X()
    Out[121]: False

    In [122]: X() < X()
    Out[122]: True

    In [123]: X() < X()
    Out[123]: True

    In [124]: X() < X()
    Out[124]: False

    class X does not implement < and cmp
    what is this comparision is based on?

    third question

    sort([[1,2,3],["ABC"],['Z','A'], X(), 4)

    how does python handle heterogenous items in the list
    in this case?

    first I assumed that cmp function used in sort
    is based on len, when the items are sequences, but this is wrong

    Regards, Daniel
  • Ben Finney

    #2
    Re: basic questions on cmp, &lt; and sort

    Schüle Daniel <uval@rz.uni-karlsruhe.dewri tes:
    Hello,
    >
    first question
    >
    In [117]: cmp("ABC",['A','B','C'])
    Out[117]: 1
    >
    against what part of the list is the string "ABC" compared?
    Why "part"? There are two objects; they are compared to each other.

    How this comparison is implemented is a matter handled by the class of
    each object.
    second question
    >
    In [119]: class X(object):
    .....: pass
    .....:
    >
    In [120]: X() < X()
    [... differing results ...]
    >
    class X does not implement < and cmp
    what is this comparision is based on?
    Classes can implement various functions to allow comparisons to work:

    <URL:http://docs.python.org/ref/customization.h tml#l2h-187>

    In the absence of those, the comparison's result is (I believe)
    implementation-dependent -- which means, "don't rely on any particular
    behaviour".
    third question
    >
    sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
    >>sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
    File "<stdin>", line 1
    sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
    ^
    SyntaxError: invalid syntax

    Care to give an actual example?

    --
    \ "I went to the hardware store and bought some used paint. It |
    `\ was in the shape of a house." -- Steven Wright |
    _o__) |
    Ben Finney

    Comment

    • Martin v. Löwis

      #3
      Re: basic questions on cmp, &lt; and sort

      Schüle Daniel schrieb:
      first question
      >
      In [117]: cmp("ABC",['A','B','C'])
      Out[117]: 1
      >
      against what part of the list is the string "ABC" compared?
      The names of the types are compared:

      pycmp("string", "list")
      1
      second question
      >
      In [119]: class X(object):
      .....: pass
      .....:
      >
      In [120]: X() < X()
      Out[120]: True
      >
      what is this comparision is based on?
      The addresses of the objects, in main memory.
      third question
      >
      sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
      >
      how does python handle heterogenous items in the list
      in this case?
      pair-by-pair. It is quite difficult to impose a total
      order on all objects; this will go away in Python 3.

      Regards,
      Martin

      Comment

      • Fredrik Lundh

        #4
        Re: basic questions on cmp, &lt; and sort

        Schüle Daniel wrote:
        first question
        >
        In [117]: cmp("ABC",['A','B','C'])
        Out[117]: 1
        >
        against what part of the list is the string "ABC" compared?


        "Objects of different types, except different numeric types and
        different string types, never compare equal; such objects are
        ordered consistently but arbitrarily (so that sorting a hetero-
        geneous array yields a consistent result)"
        class X does not implement < and cmp
        what is this comparision is based on?
        "objects of the same types that don't support proper comparison
        are ordered by their address"
        third question
        >
        sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
        >
        how does python handle heterogenous items in the list
        in this case?

        "Objects of different types, except different numeric types and
        different string types, never compare equal; such objects are
        ordered consistently but arbitrarily (so that sorting a hetero-
        geneous array yields a consistent result)"
        first I assumed
        no, first you assumed that this wasn't documented.

        </F>

        Comment

        • Hendrik van Rooyen

          #5
          Re: basic questions on cmp, &lt; and sort

          "Ben Finney" <bignose+hate s-spam@benfinney. id.auwrote:
          To: <python-list@python.org >
          Sent: Thursday, October 26, 2006 4:44 AM
          Subject: Re: basic questions on cmp, < and sort


          Schüle Daniel <uval@rz.uni-karlsruhe.dewri tes:
          8<-------------------------------------------
          third question
          >
          sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
          >>sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
          File "<stdin>", line 1
          sort([[1,2,3],["ABC"],['Z','A'], X(), 4)
          ^
          SyntaxError: invalid syntax

          needs a closing ']' to make the list a list.....

          - Hendrik

          Comment

          Working...