[Python-ideas] IntFlags

Chris Angelico rosuav at gmail.com
Tue Mar 3 23:16:47 CET 2015


On Wed, Mar 4, 2015 at 9:04 AM, Cameron Simpson <cs at zip.com.au> wrote:
> I agree. I presume IntEnum|IntEnum might return an IntFlags? Or would that
> make for a TypeError or ValueError and one be expected to start with
> IntFlags?

That should be a sanity error, but since an IntEnum devolves to an
int, it just produces a nonsensical integer. It definitely shouldn't
become an IntFlags. There are two distinctly different use-cases here:

class Color(IntEnum):
    black = 0
    red = 1
    green = 2
    orange = 3
    blue = 4
    magenta = 5
    cyan = 6
    white = 7

class FileMode(IntFlags):
    owner_read = 0o400
    owner_write= 0o200
    owner_exec = 0o100
    group_read = 0o040
    group_write= 0o020
    group_exec = 0o010
    other_read = 0o004
    other_write= 0o002
    other_exec = 0o001

With colors, it makes no sense to combine them in any way (addition,
bitwise or, etc). You can't put blue and cyan together and expect to
get something usable. File modes are meant to be combined, and their
values are deliberately chosen to make this possible. So
IntEnum|IntEnum is most likely going to be combining values that were
assigned sequentially or arbitrarily, which isn't likely to be very
useful.

ChrisA


More information about the Python-ideas mailing list