Python比较中的不一致 [英] Inconsistency in Python's Comparisons

查看:70
本文介绍了Python比较中的不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不调用双下划线黑魔法的情况下,可以在Python中选择a,b和c,以便:


a< b

b< c

c< a


这可能导致排序功能出现故障。

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(对象):
....传递

.... a = t()
b = u''0''
c =' '1''
a< b
真b< c
真c< a
class t(object): .... pass
.... a = t()
b = u''0''
c = ''1''
a < b True b < c True c < a



True


可能的kludge修复:更改default_3way_compare()以使用basestring

作为两种内置字符串类型的类型名称。


可能的优雅修复:任何其成员可能小于

的类或者大于不同类的成员,通常共享一个超级类,使用超类的名称与不兼容类型的对象进行比较。这与

数字类型的比较相似,因为它们比较类型名称是空的。


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.

推荐答案

Dietrich Epp写道:
Dietrich Epp wrote:
这可能导致排序功能出现故障。
This could cause sorting functions to malfunction.
>>> class t(object):...传递
...>>> a = t()
>>> b = u''0''
>>> c =''1''
>>> a< b真>>> b< c真>>> c< a
>>> class t(object): ... pass
... >>> a = t()
>>> b = u''0''
>>> c = ''1''
>>> a < b True >>> b < c True >>> c < a


是的




你有一个这样的例子,它不涉及
$之间的比较b $ b不同类型?在Python中,除了复数之外,不同类型实例之间的比较

保证一致,但

不一定有用。


如果您正在进行比较,更不用说排序,在实例之间

基本上不可通约的类型(如类的实例和

字符串),我' '我说那是'程序员的错误,虽然不可否认这是一个很奇怪的事情,你发现这样的循环情况。


-

__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE

\ __ /没有学习的自由总是处于危险之中;学习没有

自由总是徒劳无功。 - John F. Kennedy



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


[回复两个回复]


2004年3月16日下午6:06 ,Erik Max Francis写道:
[replying to both replies]

On Mar 16, 2004, at 6:06 PM, Erik Max Francis wrote:
你有一个这样的例子,不涉及不同类型之间的比较吗?在Python中,除了复杂的数字之外,不同类型的实例之间的比较保证是一致的,
但不一定有用。

如果你是在基本不可通约类型的实例(比如类和
字符串的实例)之间进行比较,更不用说排序了,我会说'程序员错误,尽管它确实是<很奇怪,你发现了这样的循环情况。
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.




我想我可以写一个补丁......我想我的抱怨是那么丰富的
与不同类型的比较既不会引发错误,也不会给出b
一致的结果,我期待其中一种或另一种。我会害怕写一个补丁,为类型结构添加一个字段,并且

我会有点羞于写一个kludge补丁。


我不会说比较不同的不同类型必然是一个

错误。我最后一次排序列表,它是异构的 -

它包含不同的文件类,一些包含路径,一些

临时文件,它们没有路径按设计(

对象的子类)。我对它进行了排序,所以输出不会是任意的,

不可读的顺序,程序不依赖于订单

all 。


我想知道这是否有利于性能?即,当比较

不同类型的对象时,如果首先比较''比较类''(数字,

basetring等),则其他比较可能<跳过
跳过。这是一个边缘案例,所以没有人应该注意/没人

会关心/应该避免对更常见的案件造成任何损害。


我已经我可以写的其他软件有很多其他补丁,

所以我想这根本不重要。



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.

Erik Max Francis< ma*@alcyone.com>在消息新闻中写道:< 40 *************** @ alcyone.com> ...
Erik Max Francis <ma*@alcyone.com> wrote in message news:<40***************@alcyone.com>...
Dietrich Epp写道:
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?




#由于显而易见的原因,这不应该算数。但是到底是什么......


class ContrivedExample(object):

def __init __(self,n):

self .__ n = n

def __cmp __(自我,其他):

if self .__ n == other .__ n:

return 0

elif(自我.__ n - 其他.__ n)%3 == 1:

返回-1

否则:

返回1


paper = ContrivedExample(0)

rock = ContrivedExample(1)

scissors = ContrivedExample(2)


打印纸<剪刀

打印剪刀<摇滚

print rock<论文



# 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


这篇关于Python比较中的不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆