Typeerror: '<' Not Supported Between Instances Of 'state' And 'state' Python 3
Solution 1:
In Python 3 you need to define __lt__
and __eq__
instead of __cmp__
.
See https://docs.python.org/3.1/library/stdtypes.html#comparisons.
Solution 2:
Instead of __cmp__
you need to implement one of the __lt__
, __le__
, __gt__
, or __ge__
methods and use the functools.total_ordering
decorator
functools.total_ordering(cls)
Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:The class must define one of
__lt__()
,__le__()
,__gt__()
, or__ge__()
. In addition, the class should supply an__eq__()
method.
However, a better solution to that would be putting a tuples (priority, state_object)
into the queue, as they suggest in docs for PriorityQueue
The lowest valued entries are retrieved first (the lowest valued entry is the one returned by
sorted(list(entries))[0])
. A typical pattern for entries is a tuple in the form:(priority_number, data)
.
The pitfall of the first approach is that you can modify priority of items that are already in queue and possibly observe unexpected behavior.
In the second approach this is not an issue, since tuples are immutable.
Post a Comment for "Typeerror: '<' Not Supported Between Instances Of 'state' And 'state' Python 3"