472,368 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 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 1452
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.