Python解释器错误 [英] Python interpreter bug

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

问题描述

你好,


我认为我认为这是python解释器中的一个严重错误。


成员资格测试似乎不起作用当这些

对象具有用户定义的__cmp__方法时的对象列表。

它存在于Python 2.3和2.4中。我不知道其他版本。

以下代码说明了这个错误:
来自随机导入选择的


类OBJ:

def __init __(self,identifier):

self.id = identifier

self.allocated = 0

def __cmp__ (自我,其他):

返回cmp(other.allocated,self.allocated)

mylist = [OBJ(i)for i in range(20)]

excluded = [obj for mjist中的obj如果obj.id> choice(范围(20))]

for mjist中的obj:

if obj排除在外:

断言obj.id在[objt.id中为objt排除]

继续


运行上面的代码片段将触发断言。罪魁祸首似乎

是__cmp__方法,它对一个具有恒定价值的键进行排序。

最好的问候

Alain

Hello,

I came accross what i think is a serious bug in the python interpreter.

Membership testing seems not to work for list of objects when these
objects have a user-defined __cmp__ method.
It is present in Python 2.3 and 2.4. I don''t know about other versions.
The following code illustrates the bug:
from random import choice
class OBJ:
def __init__(self,identifier):
self.id=identifier
self.allocated=0
def __cmp__(self,other):
return cmp(other.allocated,self.allocated)
mylist=[OBJ(i) for i in range(20)]
excluded=[obj for obj in mylist if obj.id>choice(range(20))]
for obj in mylist:
if obj in excluded:
assert obj.id in [objt.id for objt in excluded]
continue

Running the above snippet will trigger the assert. The culprit seems to
be the __cmp__ method which sorts on a key with constant value.
Best regards
Alain

推荐答案

为什么会出现错误?你已经做到了这一点,OBJ的每个实例都等于OBJ的每个其他实例。行为符合预期。

Why would it be a bug? You''ve made it so that every instance of OBJ is
equal to every other instance of OBJ. The behaviour is as expected.


al ** ******@yahoo.fr 写道:
你好,

我认为这是我认为是python解释器中的一个严重错误。

当这些对象具有用户定义的__cmp__方法时,成员资格测试似乎不适用于对象列表。
它存在于Python 2.3和2.4中。我不知道其他版本。
下面的代码说明了这个bug:
来自随机导入选择
类OBJ:
def __init __(self,identifier):
self.id = identifier
self.allocated = 0
def __cmp __(self,other):
返回cmp(other.allocated,self.allocated)
mylist = [ OBJ(i)for i in range(20)]
excluded = [obj in mjist in obj.id> choice(range(20))]
for obj in mylist:
如果obj被排除在外:
断言obj.id in [objt.id for objt in excluded]
继续

我假设你只是把继续在那里玩得开心?
Hello,

I came accross what i think is a serious bug in the python interpreter.

Membership testing seems not to work for list of objects when these
objects have a user-defined __cmp__ method.
It is present in Python 2.3 and 2.4. I don''t know about other versions.
The following code illustrates the bug:
from random import choice
class OBJ:
def __init__(self,identifier):
self.id=identifier
self.allocated=0
def __cmp__(self,other):
return cmp(other.allocated,self.allocated)
mylist=[OBJ(i) for i in range(20)]
excluded=[obj for obj in mylist if obj.id>choice(range(20))]
for obj in mylist:
if obj in excluded:
assert obj.id in [objt.id for objt in excluded]
continue
I presume you just put the "continue" in there for fun?
对于mylist中的obj:
.... print obj in excluded

....

True

True

True

True

真实

真实

真实

真实

真实

真实

真实

真实

真实

真实

真实
True

True

True

True

True OBJ(0)== OBJ( 1)
for obj in mylist: .... print obj in excluded
....
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True OBJ(0) == OBJ(1)



True

运行上面的代码片段将触发断言。罪魁祸首似乎是__cmp__方法,它对具有恒定值的键进行排序。


True
Running the above snippet will trigger the assert. The culprit seems to
be the __cmp__ method which sorts on a key with constant value.




确实如此。据我所知,你的对象将全部测试相等。

你的意思是__cmp__方法返回cmp(other.id,self.id)?


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/



Well indeed. As far as I can see your objects will all test equal. Did
you mean the __cmp__ method to return cmp(other.id, self.id)?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


肯定有一个bug。

也许follownig片段更清晰:

类OBJ:

def __init __(自我,标识符):

self.id = identifier

self.allocated = 0

#def __cmp __(self,other):

#return cmp( other.allocated,self.allocated)

mylist = [OBJ(i)for i in range(10)]

excluded = [obj for obj in mylist if obj。 id [2,4,6,8]]

exclusion_list_by_id = [2,4,6,8]

print''排除列表by id ='',exclusion_list_by_id

for mjist中的obj:

print''current obj ='',obj.id,

if obj in excluded:

print''--->这个对象被排除''

断言obj.id in exclusion_list_by_id

继续

打印


如果取消注释这两行,断言将被错误地触发


Alain

There is definitely a bug.
Maybe the follownig snippet is more clear:
class OBJ:
def __init__(self,identifier):
self.id=identifier
self.allocated=0
#def __cmp__(self,other):
# return cmp(other.allocated,self.allocated)
mylist=[OBJ(i) for i in range(10)]
excluded=[obj for obj in mylist if obj.id in [2,4,6,8]]
exclusion_list_by_id=[2,4,6,8]
print ''exclusion list by id='',exclusion_list_by_id
for obj in mylist:
print ''current obj='',obj.id,
if obj in excluded:
print '' ---> THIS OBJECT IS EXCLUDED''
assert obj.id in exclusion_list_by_id
continue
print

If you uncomment the two lines, the assert will be erroneously
triggered.
Alain


这篇关于Python解释器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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