Re: basic questions on cmp, and sort

2006-10-26 Thread Fredrik Lundh
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic questions on cmp, and sort

2006-10-26 Thread Hendrik van Rooyen
 Ben Finney [EMAIL PROTECTED] wrote:
To: python-list@python.org
Sent: Thursday, October 26, 2006 4:44 AM
Subject: Re: basic questions on cmp,  and sort


Schüle Daniel [EMAIL PROTECTED] writes:
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

-- 
http://mail.python.org/mailman/listinfo/python-list

basic questions on cmp, and sort

2006-10-25 Thread Schüle 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic questions on cmp, and sort

2006-10-25 Thread Ben Finney
Schüle Daniel [EMAIL PROTECTED] writes:

 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

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: basic questions on cmp, and sort

2006-10-25 Thread Martin v. Löwis
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:

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
-- 
http://mail.python.org/mailman/listinfo/python-list