默认对象比较被认为有害? [英] default object comparison considered harmful?

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

问题描述

大家好,


昨天我们发现了导致问题很长时间的错误原因。
看来如下:


A级(对象):

通过


print min(1.0,A())


即使A()对象不是数字在
性质中也被Python接受。


原因这种行为似乎是对象的比较操作

class。

有没有办法在Python中禁用这种行为(除了派生新的

''类似对象'的课程没有做比较?)

此致,

Albert

Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a new
''object-like'' class that doesn''t do comparisons?)
Sincerely,
Albert

推荐答案

ATHofkampaécrit:
A.T.Hofkamp a écrit :

大家好,


昨天我们发现造成长期问题的bug的原因。
似乎如下:


A类(对象):

传递


print min(1.0,A())


即使A()对象不是数字的,也被Python接受

性质。


这种行为的原因似乎是对象的比较操作

class。


有没有办法在Python中禁用这种行为(除了派生一个新的
Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a new
''object-like'' class that doesn''t do comparisons?)



除了实现自定义__cmp__函数之外?不是AFAIK。 FWIW,你

不一定需要摆弄遗产 - 你也可以

monkeypatch你的班级(nb:对于新式的物品,你不能
基于每个对象的monkeypatch __magic__方法。


另外,IIRC,这种行为在Python 3中发生了变化。

Other than implementing a custom __cmp__ function ? Not AFAIK. FWIW, you
don''t necessarily need to fiddle with inheritance - you can also
monkeypatch your classes (nb : for new-style objects, you cannot
monkeypatch __magic__ methods on a per-object basis).

Also, IIRC, this behaviour has changed in Python 3.


ATHofkamp写道:
A.T.Hofkamp wrote:

昨天我们发现一个导致问题的bug的原因很长

时间。它似乎如下:


A级(对象):

通过


print min(1.0) ,A())


这是Python接受的,即使A()对象在
nature中不是数字。

这种行为的原因似乎是对象的比较操作

class。


有没有办法禁用此行为Python(除了派生一个

新的''类似对象''类,它没有做比较?)
Yesterday we found the cause of a bug that has caused problems for a long
time. It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.
Is there a way to disable this behavior in Python (other than deriving a
new ''object-like'' class that doesn''t do comparisons?)



As Bruno说,这将在3.0中改变:


#3.0

As Bruno says, this will change in 3.0:

# 3.0


>> A类:传递
>>class A: pass



....

....


>> min(1.0,A())
>>min(1.0, A())



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

TypeError:不可订的类型: A()< float()


对于2.5及以上,你可以提供一个关键功能作为解决方法:


#2.5

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: A() < float()

For 2.5 and above you can provide a key function as a workaround:

# 2.5


>>类A(对象):传递
>>class A(object): pass



....

....


>> min(1,A(),key = float)
>>min(1, A(), key=float)



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

TypeError:float()参数必须是字符串或数字


Peter

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a number

Peter


On 16 Mai,10:03," ATHofkamp" < h ... @ se-162.se.wtb.tue.nlwrote:
On 16 Mai, 10:03, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:

Hello all,

昨天我们发现了导致问题很长时间的bug的原因。
看起来如下:


A类(对象):

通过


打印最小值(1.0,A())


即使是一个()对象不是数字在

性质中。


这种行为的原因似乎是对象的比较操作

class。


有没有办法在Python中禁用这种行为(除了派生一个新的

''类似对象'的类不做比较吗?)


真诚的,

Albert
Hello all,

Yesterday we found the cause of a bug that has caused problems for a long time.
It appeared to be the following:

class A(object):
pass

print min(1.0, A())

which is accepted by Python even though the A() object is not numerical in
nature.

The cause of this behavior seems to be the compare operation of the object
class.

Is there a way to disable this behavior in Python (other than deriving a new
''object-like'' class that doesn''t do comparisons?)

Sincerely,
Albert



你确定你不想使用静态类型的语言

只是通过检查源代码来捕获所有类型错误?

Are you sure you don''t want to use a statically typed language that
captures all type errors just by inspecting your source code?


这篇关于默认对象比较被认为有害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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