473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1513
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
21
by: nateastle | last post by:
I have a simple assignment for school but am unsure where to go. The assignment is to read in a text file, split out the words and say which line each word appears in alphabetical order. I have the...
3
by: Samuel R. Neff | last post by:
You can you program against generic interfaces generically? For example, how can I make the following code which works for the non-generic interface also work for the generic counterparts? ...
3
by: JJ | last post by:
I've done a little multi-threading on winform apps some time ago now, so I'm not a complete beginner, but best assume I am for any explanations..: This is an asp.net 2.0 website, using c#: I...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
1
by: Rik Wasmus | last post by:
On Tue, 17 Jun 2008 00:52:16 +0200, Twayne <nobody@devnull.spamcop.net> wrote: Euhm, no you can't. The only thing remotely close is something like: echo 'foo','bar'; .... which is nothing...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.