Message303093
| Author |
lisroach |
| Recipients |
Mikołaj Babiak, lisroach, ncoghlan, r.david.murray, rhettinger |
| Date |
2017年09月27日.04:44:49 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1506487489.96.0.154975027568.issue31145@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
I think it is a good idea to have a simple way to add a value to sort on in general, it could have some interesting use-cases. Also I am with Nick a name change would make the broader scope clearer.
What I am not sure about is making the comparison have to be between two items of the same wrapper type - since right now we are comparing priority to priority attributes.
It makes sense for PriorityQueues, but if we wanted to use this for something more arbitrary like comparing a string with an int priority to an int we end up having to convert both data types to the new wrapper type:
str_cmp = KeyedItem(20, 'cat')
int_cmp = KeyedItem(30, 30)
str_cmp < int_cmp
I don't like having to convert to the new wrapper unless it's relevant, I'd rather do:
str_cmp = KeyedItem(20, 'cat')
str_cmp < 30
It could be instead:
class KeyedItem:
def __init__(self, key, item):
self.key = key
self.item = item
def __eq__(self, other):
if not isinstance(other, KeyedItem):
return self.key == other
return self.key == other.key
def __lt__(self, other):
if not isinstance(other, KeyedItem):
return self.key < other
return self.key < other.key
... |
|