python解释器无效_Python解释器错误

博客讨论了在Python 2.3和2.4版本中,当对象具有自定义的`__cmp__`方法时,列表对象的成员测试可能出现问题。代码示例显示,由于`__cmp__`方法基于常量值进行排序,导致所有OBJ实例被视为相等,从而在进行成员测试时产生错误。解决方案是修改`__cmp__`方法以正确比较对象标识符。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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

解决方案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 wrote: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?

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

Running the above snippet will trigger the assert. The culprit seems to

be the __cmp__ method which sorts on a key with constant value.

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/

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值