473,786 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1534
Schüle Daniel <uv**@rz.uni-karlsruhe.dewri tes:
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.h tml#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*********@py thon.org>
Sent: Thursday, October 26, 2006 4:44 AM
Subject: Re: basic questions on cmp, < and sort
Schüle Daniel <uv**@rz.uni-karlsruhe.dewri tes:
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
9290
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. # No warranty express or implied for the accuracy, fitness to purpose
11
13725
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, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
8
5266
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 developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
21
1568
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 basic outline of the program done which is: def Xref(filename): try: fp = open(filename, "r") lines = fp.readlines() fp.close()
3
2625
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? public bool Equivalent(ArrayList x, ArrayList y) { if (x is IComparable) { x.Sort(); x.Sort(); }
3
1526
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 have a thread whose job is to send several emails (i.e. a long task that mustn't be run by more that one user at a time). It all works fine, but I am concerned about the lifetime of a thread in the case of smtp errors, and how it will work with...
28
3598
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 business basic. I need to re-write it, bring it kicking and screaming to run on Windows XP. This is for a video rental place, tracks movie and game rentals, customers, employee transactions, reservations, does reports,..... and on. I know some of...
1
204
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 like concatenation at all.
0
2943
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 http://interviewdoor.com/technical/C-Interview-Questions.htm C# Interview Questions http://interviewdoor.com/technical/C-sharp-Interview-Questions.htm C++ Interview Questions http://interviewdoor.com/technical/C++-Interview-Questions.htm
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.