why (1, 2, 3) > [1, 2, 3] is true?

F

fat bold cyclop

I tired to google for comparison of tuple to list but i failed.

Could anyone explain it to me?

Best regards,
fat bold cyclop
 
S

Stefan Behnel

fat bold cyclop, 25.02.2010 14:00:
I tired to google for comparison of tuple to list but i failed.

Could anyone explain it to me?

Both are not equal, so the comparison returns an arbitrary result in Py2.

Note that this was fixed in Py3:

Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: tuple() > list()


Stefan
 
F

fat bold cyclop

Both are not equal, so the comparison returns an arbitrary result in Py2.
Thanks, Stefan. If I understand you correctly the comparison is not
valid.
But I wonder if there is any logic behind this (in 2.x).
Is it possible to predict result of this comparison?

Thanks again,
fbc
 
R

Richard Thomas

Thanks, Stefan. If I understand you correctly the comparison is not
valid.
But I wonder if there is any logic behind this (in 2.x).
Is it possible to predict result of this comparison?

Thanks again,
fbc

I believe in 2.x they are ordered by the names of their types but I
could be wrong.

1 < [] < '' < () < u''
 
I

Iain King

Thanks, Stefan. If I understand you correctly the comparison is not
valid.
But I wonder if there is any logic behind this (in 2.x).
Is it possible to predict result of this comparison?

Thanks again,
fbc

I haven't looked in the source to check (and I'm almost 100% certain
that tuple > list is an implementation detail), but I have not found
any pair of tuple and list in which the list is treated as the
greater. Possibly related: type(tuple()) is > type(list()). Or, to
let the interpreter tell you why (1,2,3) > [1,2,3]:
True

Iain
 
S

Stefan Behnel

fat bold cyclop, 25.02.2010 15:03:
Thanks, Stefan. If I understand you correctly the comparison is not
valid.
But I wonder if there is any logic behind this (in 2.x).
Is it possible to predict result of this comparison?

The result is predictable, it's just arbitrary in that it does not depend
on the values that you are comparing but only on their type.

Stefan
 
T

Terry Reedy

In general, no. The result is arbitrary, with the constraint of being
consistent within a particular run.
I believe in 2.x they are ordered by the names of their types but I
could be wrong.

This is currently true in *CPython* 2.x, but that is an implementation
artifact that has changed and might be different with other implementations.

Terry Jan Reedy
 
R

Robert Kern

why (1, 2, 3)> [1, 2, 3] is true?

It's simple, Everything must have a value!

That is not at all an explanation, much less a true one. Please read the other
posts in this thread; they have explained the situation rather well.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top