Skip to content Skip to sidebar Skip to footer

Python Bitarray Set

What is the best way to generate a set of bitarray-like objects so that I can test for membership efficiently. The naive way doesn't seem to work as I expect: >>> from bit

Solution 1:

It appears that bitarray does not maintain the hash invariant:

>>>x = bitarray(b'0000')>>>y = bitarray(b'0000')>>>x == y
True
>>>hash(x) == hash(y)
False

This is a violation of the API for __hash__, as documented:

The only required property is that objects which compare equal have the same hash value

This mean that bitarrays are effectively unhashable and will not work reliably in sets or as dictionary keys.

I would regard this as a bug in the bitarray library. I had never heard of bitarray before, and it doesn't seem to have much documentation. As far as I can see it doesn't even say how equality is supposed to be defined for bitarrays, nor whether they are supposed to be hashable, but it seems that it implements equality and hashing in incompatible ways.

Post a Comment for "Python Bitarray Set"