classification
Title: Strange heapq behavior on Python 3.0 when overriding __le__
Type: Stage:
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: einmaliger, giampaolo.rodola, rhettinger
Priority: normal Keywords:

Created on 2008-09-19 13:47 by giampaolo.rodola, last changed 2008-09-19 18:49 by giampaolo.rodola. This issue is now closed.

Messages (4)
msg73425 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2008-09-19 13:47
import heapq

class foo:
    def __init__(self):
        self.timeout = 0
    def __le__(self, other):
        return self.timeout <= other.timeout

heap = []
heapq.heappush(heap, foo())
heapq.heappush(heap, foo())


This code on Python 2.x works without problems, by using Python3.0-RC1
it raises the following exception:


    heapq.heappush(heap, foo())
TypeError: unorderable types: foo() < foo()


Note that the previous 3.0 beta didn't have such problem.
msg73426 - (view) Author: Sascha Müller (einmaliger) Date: 2008-09-19 14:05
heapq expects a _lt_ method, and the error doesn't occur when the _le_
method is changed to _lt. According to the SVN log, this was changed due
to consistency with lists.sort().
msg73432 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-09-19 17:08
It's supposed to be that way.  In 2.6 we support both to help with 
transition. In 3.0, we've cleaned up and made the APIs consistent. Try to 
get in the habit of defining all six rich comparisons to bulletproof code 
and not rely on undocumented implementation details.
msg73436 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2008-09-19 18:49
Ok, thanks for the hints.
History
Date User Action Args
2008-09-19 18:49:13giampaolo.rodolasetmessages: + msg73436
2008-09-19 17:08:10rhettingersetstatus: open -> closed
assignee: rhettinger
resolution: not a bug
messages: + msg73432
nosy: + rhettinger
2008-09-19 14:16:33einmaligersetnosy: + giampaolo.rodola
2008-09-19 14:05:15einmaligersetnosy: + einmaliger, - giampaolo.rodola
messages: + msg73426
2008-09-19 13:47:30giampaolo.rodolacreate