如何比较工作? [英] How does compare work?

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

问题描述




如果两个对象不具有可比性(我认为),那么Python会做什么

如果我理解了 Python中的Nutschell正确它应该提出一个

例外,但它不适合我。


这里有两个例子


如果2> ''1'':打印更大

else:print" less_or_equal"


打印less_or_equal





C级:

def __init __(个体经营):传递


X = C()

如果2> X:打印更大


打印更大


在这两种情况下,我都没有例外。

但我想因为这些导致难以发现的错误。


感谢您对此有所了解。


PS这是Python 2.4(CVS)


-

Helmut Jarausch


Lehrstuhl fuer Numerische Mathematik
RWTH - 亚琛大学

D 52056德国亚琛

Hi,

what does Python do if two objects aren''t comparable (to my opinion)
If I''ve understood "Python in a Nutschell" correctly it should raise an
exception but it doesn''t do for me.

Here are two examples

if 2 > ''1'' : print "greater"
else : print "less_or_equal"

prints less_or_equal

and

class C:
def __init__(self): pass

X=C()
if 2 > X : print "greater"

prints greater

In both cases I don''t get an exception.
But I''d like to since these lead to hard-to-find bugs.

Thanks for shedding some light on this.

P.S. This is Python 2.4 (CVS)

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

推荐答案



Helmut Jarausch < JA ****** @ skynet.be>在消息中写道

news:40 ********************** @ news.skynet.be ...

"Helmut Jarausch" <ja******@skynet.be> wrote in message
news:40**********************@news.skynet.be...
如果两个对象不具有可比性(对我的意见),Python会做什么


对不起,Python做它的功能,这通常是比较任何两个

对象,除非明确禁用复数或用户编写的用户类的方法。

如果我已经理解了Python in a Nutschell"正确它应该引发一个
异常,但它不适合我。
what does Python do if two objects aren''t comparable (to my opinion)
Sorry, Python does what it does, which is generally to compare any two
objects unless explicitly disabled as with complex numbers or in
user-written method of user class.
If I''ve understood "Python in a Nutschell" correctly it should raise an
exception but it doesn''t do for me.




也许你可以引用误导你的行,有人可以

解释,或建议改变作者。


Terry J. Reedy




Perhaps you could quote the line that mislead you, and someone could
explain, or suggest a change to the author.

Terry J. Reedy



2004年1月26日星期一下午10:28:31 +0100,Helmut Jarausch写道:
On Mon, Jan 26, 2004 at 10:28:31PM +0100, Helmut Jarausch wrote:


如果有两个Python会做什么对象是不可比的(我认为)
如果我已经理解在Nutschell中使用Python正确它应该引发一个
异常,但它不适合我。

来自Python 2.3.3参考手册中的第5.9节;
Hi,

what does Python do if two objects aren''t comparable (to my opinion)
If I''ve understood "Python in a Nutschell" correctly it should raise an
exception but it doesn''t do for me.
From section 5.9 in Python 2.3.3 Reference manual;



"""

运算符<,>,==,> =,< =和!=比较两个值

对象。对象不必具有相同的类型。如果两者都是数字,

它们将被转换为通用类型。否则,

不同类型的对象总是比较不相等,并且一致但任意地订购




(这个不寻常的定义比较用于简化

操作的定义,如排序和in和not in运算符。

将来,对象的比较规则为

不同类型可能会改变。)

"""


-Inyeol Lee


"""
The operators <, >, ==, >=, <=, and != compare the values of two
objects. The objects need not have the same type. If both are numbers,
they are converted to a common type. Otherwise,
objects of different types always compare unequal, and are ordered
consistently but arbitrarily.

(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the in and not in operators.
In the future, the comparison rules for objects of
different types are likely to change.)
"""

-Inyeol Lee


Inyeol Lee写道:
Inyeol Lee wrote:
(这个不寻常的比较定义用于简化操作的定义,如排序和in和not in运算符。<将来,不同类型对象的比较规则可能会发生变化。)
"""
(This unusual definition of comparison was used to simplify the
definition of operations like sorting and the in and not in operators.
In the future, the comparison rules for objects of
different types are likely to change.)
"""




当比较混合类型变得非法时,这是否意味着排序混合类型的b / b $ b序列也变得非法?或者将是一个

特殊情况?


你的,

格里特。


-

136.如果有人离开他的房子,逃跑,然后他的妻子去另一所房子,如果他回来,并希望采取他的妻子回来了:

因为他逃离了家并逃跑了,这次失控的妻子将不会再回到丈夫那里。

- 公元前1780年,汉谟拉比,法典法则

-

PrePEP:内置路径类型
http://people.nl.linux.org/~gerrit/c.../pep- xxxx.html

阿斯伯格综合症 - 一种个人方法:
http://people.nl.linux.org/~gerrit/english/



When comparing mixed types becomes illegal, does that mean sorting a
sequence of mixed types becomes illegal as well? Or will sort be a
special case?

yours,
Gerrit.

--
136. If any one leave his house, run away, and then his wife go to
another house, if then he return, and wishes to take his wife back:
because he fled from his home and ran away, the wife of this runaway shall
not return to her husband.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger''s Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/


这篇关于如何比较工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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