473,387 Members | 1,379 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,387 software developers and data experts.

Inconsistency in Python's Comparisons

Without invoking double-underscore black magic, it is possible to
choose a, b, and c in Python such that:

a < b
b < c
c < a

This could cause sorting functions to malfunction.
class t(object): .... pass
.... a = t()
b = u'0'
c = '1'
a < b True b < c True c < a

True

Possible kludge fix: Change default_3way_compare() to use "basestring"
as the typename for both built-in string types.

Possible elegant fix: Any class whose members may either be less than
or greater than members of a different class, which often share a
common superclass, use the name of the superclass for comparisons with
objects of incompatible types. This parallels the comparison of
numeric types because they compare as if their type name were empty.
Jul 18 '05 #1
11 1495
Dietrich Epp wrote:
This could cause sorting functions to malfunction.
>>> class t(object): ... pass
... >>> a = t()
>>> b = u'0'
>>> c = '1'
>>> a < b True >>> b < c True >>> c < a

True


Do you have an example of this that doesn't involve comparisons between
different types? In Python, except for complex numbers, comparisons
between instances of different types is guaranteed to be consistent, but
not necessarily helpful.

If you're doing comparisons, not to mention sorting, between instances
of fundamentally incommensurable types (like instances of a class and
strings), I'd say that's programmer error, although admittedly it is a
little strange that you found a circular situation like that.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Liberty without learning is always in peril; learning without
liberty is always in vain. -- John F. Kennedy
Jul 18 '05 #2
[replying to both replies]

On Mar 16, 2004, at 6:06 PM, Erik Max Francis wrote:
Do you have an example of this that doesn't involve comparisons between
different types? In Python, except for complex numbers, comparisons
between instances of different types is guaranteed to be consistent,
but
not necessarily helpful.

If you're doing comparisons, not to mention sorting, between instances
of fundamentally incommensurable types (like instances of a class and
strings), I'd say that's programmer error, although admittedly it is a
little strange that you found a circular situation like that.


I guess I could write a patch... I suppose my complaint is that rich
comparison with different types neither raises an error nor gives
consistent results, and I expect either one or the other. I'd be
scared to write a patch that added a field to the type structure, and
I'd be a bit ashamed to write a kludge patch.

I wouldn't say comparing different different types is necessarily an
error, though. The last time I sorted a list, it was heterogeneous --
it contained different file classes, some with paths, and some
temporary files which couldn't have paths by design (both subclasses of
object). I sorted it so the output wouldn't be in some arbitrary,
impossible-to-read order, the program didn't depend on the order at
all.

I wonder if this could benefit performance? I.e, when comparing
objects of different types, if the 'comparison class' (number,
basestring, etc.) is compared first, other comparisons might be
skipped. This is an edge case though, so nobody should notice/nobody
will care/any detriment to more common cases should be avoided.

I've got a lot of other patches for other software I could be writing,
so I suppose this isn't really important at all.

Jul 18 '05 #3
Erik Max Francis <ma*@alcyone.com> wrote in message news:<40***************@alcyone.com>...
Dietrich Epp wrote:
This could cause sorting functions to malfunction.
[example of nontransitive "<"]


Do you have an example of this that doesn't involve comparisons between
different types?


# This shouldn't count, for obvious reasons. But what the heck...

class ContrivedExample(object):
def __init__(self, n):
self.__n = n
def __cmp__(self, other):
if self.__n == other.__n:
return 0
elif (self.__n - other.__n) % 3 == 1:
return -1
else:
return 1

paper = ContrivedExample(0)
rock = ContrivedExample(1)
scissors = ContrivedExample(2)

print paper < scissors
print scissors < rock
print rock < paper
Jul 18 '05 #4
Dan Bishop wrote:
# This shouldn't count, for obvious reasons. But what the heck...
He explicitly restricted himself to not invoking "double-underscore
magic." You can certainly make classes which don't constitute a
well-ordering, and you can certainly get bizarre behavior by relying on
the (arbitrary) ordering between types, but the only thing I'd be
worried about is such a circular ordering between types in the same
class.
paper = ContrivedExample(0)
rock = ContrivedExample(1)
scissors = ContrivedExample(2)


I do like your example, however :-).

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ You and I / We've seen it all / Chasing our hearts' desire
-- The Russian and Florence, _Chess_
Jul 18 '05 #5
Dietrich Epp wrote:
I wouldn't say comparing different different types is necessarily an
error, though.


It's guaranteed to be consistent, and sort is guaranteed not to go nuts
(as someone else pointed out). The ordering is defined to be
consistent, although it does not define a well-ordering, and furthermore
need not be the same between different implementations (or even
different implementations). It may not be an error, but there's no
reason for it to be a total ordering.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ You and I / We've seen it all / Chasing our hearts' desire
-- The Russian and Florence, _Chess_
Jul 18 '05 #6
"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:40***************@alcyone.com...
Do you have an example of this that doesn't involve comparisons between
different types?


Do long and float count as different types?
Jul 18 '05 #7
Andrew Koenig wrote:
"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:40***************@alcyone.com...

Do you have an example of this that doesn't involve comparisons between
different types?

Do long and float count as different types?


Was this a trick question?
float <type 'float'> long <type 'long'> issubclass(long, long) True issubclass(long, float) False issubclass(float, long)

False

Python says the answer to your question is "yes"...

-Peter
Jul 18 '05 #8
"Peter Hansen" <pe***@engcorp.com> wrote in message
news:Ze********************@powergate.ca...
Andrew Koenig wrote:

"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:40***************@alcyone.com...
Do you have an example of this that doesn't involve comparisons between
different types?
Do long and float count as different types?


Was this a trick question?


Not particularly. Although long and float are obviously different types, I
suspect that most people who say that they do not expect comparisons between
different types to work, they do expect comparisons to work consistently
between integers and floating-point values.
Jul 18 '05 #9
[replying to both replies]

On Mar 16, 2004, at 6:06 PM, Erik Max Francis wrote:
Do you have an example of this that doesn't involve comparisons between
different types? In Python, except for complex numbers, comparisons
between instances of different types is guaranteed to be consistent,
but
not necessarily helpful.

If you're doing comparisons, not to mention sorting, between instances
of fundamentally incommensurable types (like instances of a class and
strings), I'd say that's programmer error, although admittedly it is a
little strange that you found a circular situation like that.


I guess I could write a patch... I suppose my complaint is that rich
comparison with different types neither raises an error nor gives
consistent results, and I expect either one or the other. I'd be
scared to write a patch that added a field to the type structure, and
I'd be a bit ashamed to write a kludge patch.

I wouldn't say comparing different different types is necessarily an
error, though. The last time I sorted a list, it was heterogeneous --
it contained different file classes, some with paths, and some
temporary files which couldn't have paths by design (both subclasses of
object). I sorted it so the output wouldn't be in some arbitrary,
impossible-to-read order, the program didn't depend on the order at
all.

I wonder if this could benefit performance? I.e, when comparing
objects of different types, if the 'comparison class' (number,
basestring, etc.) is compared first, other comparisons might be
skipped. This is an edge case though, so nobody should notice/nobody
will care/any detriment to more common cases should be avoided.

I've got a lot of other patches for other software I could be writing,
so I suppose this isn't really important at all.

Jul 18 '05 #10
> Not particularly. Although long and float are obviously different types, I
suspect that most people who say that they do not expect comparisons between
different types to work, they do expect comparisons to work consistently
between integers and floating-point values.


That is only because floats, integers and longs all represent numbers.
It makes sense to most people to compare numbers.

- Josiah
Jul 18 '05 #11
Dietrich Epp wrote:
I guess I could write a patch... I suppose my complaint is that rich
comparison with different types neither raises an error nor gives
consistent results, and I expect either one or the other. I'd be
scared to write a patch that added a field to the type structure, and
I'd be a bit ashamed to write a kludge patch.


The question here is what you think the goal of writing a patch would
be. You're trying to modify behavior that is as-designed.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Come not between the dragon and his wrath.
-- King Lear (Act I, Scene I)
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rim | last post by:
Hi, With the great unification of types and classes, what will happen to the following identity inconsistency? >>> class myint(int): pass .... >>> a=int(1); b=int(1) >>> a is b 1
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
40
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
17
by: Johannes Bauer | last post by:
Hello group, I'm porting some code of mine to Python 3. One class has the __cmp__ operator overloaded, but comparison doesn't seem to work anymore with that: Traceback (most recent call last):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.