[Python-ideas] a set of enum.Enum values rather than the construction of bit-sets as the "norm"?

Chris Angelico rosuav at gmail.com
Fri Dec 29 06:26:22 EST 2017

On Fri, Dec 29, 2017 at 7:18 PM, Steven D'Aprano <steve at 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
>>> 4 in x

Set union, intersection, etc are all provided using the same operators
in sets and ints.


More information about the Python-ideas mailing list