basic questions on cmp, < and sort

  • Thread starter =?ISO-8859-1?Q?Sch=FCle_Daniel?=
  • Start date
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

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
 
B

Ben Finney

Schüle Daniel said:
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?
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Schüle Daniel said:
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:

py> cmp("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
 
H

Hendrik van Rooyen

To: <[email protected]>
Sent: Thursday, October 26, 2006 4:44 AM
Subject: Re: basic questions on cmp, < and sort


Schüle Daniel said:
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
 
F

Fredrik Lundh

Schüle Daniel said:
first question

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

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

http://docs.python.org/lib/comparisons.html

"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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top