basic questions on cmp, < and sort

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Oct 26 04:44:10 CEST 2006


Schüle Daniel <uval at rz.uni-karlsruhe.de> writes:

> 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.html#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




More information about the Python-list mailing list