469,630 Members | 1,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,630 developers. It's quick & easy.

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
Oct 26 '06 #1
4 1361
Schüle Daniel <uv**@rz.uni-karlsruhe.dewrites:
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

Oct 26 '06 #2
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
Oct 26 '06 #3
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?
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>

Oct 26 '06 #4
"Ben Finney" <bi****************@benfinney.id.auwrote:
To: <py*********@python.org>
Sent: Thursday, October 26, 2006 4:44 AM
Subject: Re: basic questions on cmp, < and sort
Schüle Daniel <uv**@rz.uni-karlsruhe.dewrites:
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

Oct 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael Foord | last post: by
11 posts views Thread by Les Paul | last post: by
21 posts views Thread by nateastle | last post: by
3 posts views Thread by JJ | last post: by
1 post views Thread by Rik Wasmus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.