[PyQt] QTreeWidgetItem is not hashable in Py3

Phil Thompson phil at riverbankcomputing.com
Wed Mar 29 22:22:42 BST 2017


On 29 Mar 2017, at 10:14 pm, Barry Scott <barry at barrys-emacs.org> wrote:
> 
> 
>> On 29 Mar 2017, at 16:41, Phil Thompson <phil at riverbankcomputing.com> wrote:
>> 
>> On 28 Mar 2017, at 5:31 pm, Sambhav Kothari <sambhavs.email at gmail.com> wrote:
>>> 
>>> Hi,
>>> QTreeWidgetItem seems to be no longer hashable in py3.
>>> 
>>> I am using pyqt5.8.1 and py3.6. Is this a known issue?
>> 
>> Python considers a type to be unhashable if it has a comparison operator (which QTreeWidgetItem does).
> 
> int and str are hashable and have comparison operator.

I should have added "and doesn't have an explicit __hash__() implementation".

> Phil, I'm reacll I read a release notes of yours about this change.
> 
> I guess the only immutable property that you can hash is the memory address of the object.
> Its text() property can be changed at any time. No to mention its list of parents. Would that be useful?
> 
>> Should should be able to implement __hash__() in a sub-class that does what you want.
> 
> I'd guess that its the text() and parent list that is interesting to hash and not the item itself.

I could have added an explicit implementation of __hash__() that returned id(self) (which is what would happen automatically for types without a comparison operator) but I prefered to let Python do what it wanted to do. As I suggested you can always implement that in your own sub-class.

Phil


More information about the PyQt mailing list