29 Dec
2017
29 Dec
'17
11:26 a.m.
On Fri, Dec 29, 2017 at 7:18 PM, Steven D'Aprano <steve@pearwood.info> wrote:
Since ints don't provide a set-like interface, they aren't strictly speaking bitsets. But in any case, nobody is stopping people from using sets of enum values.
I'm not sure what "set-like interface" you'd be looking for, but the built-in set type has a lot of the same operations as an integer does, and the semantics are virtually identical to a set of bits. The only one you really lack is __contains__, which could easily be added: class BitSet(int): def __contains__(self, bit): return (self & bit) == bit
x = BitSet(1|2|8|32) 2 in x True 4 in x False
Set union, intersection, etc are all provided using the same operators in sets and ints. ChrisA