Lisa Roach <lisaroac...@gmail.com> added the comment:

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
     ...

----------
nosy: +lisroach

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31145>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to