[issue23591] enum: Add Flags and IntFlags

Ethan Furman report at bugs.python.org
Sat Sep 3 13:37:59 EDT 2016


Ethan Furman added the comment:

> 1) IntFlags is not simply the meet of int and Flags (like IntEnum is the
> meet of int and Enum,
> https://docs.python.org/3.5/library/enum.html#others)?
> It seems a very different class.

No, I think it is.  The differences between Enum and IntEnum are entirely because IntEnum members are also ints, so they can do anything an int can do (but they lose their IntEnum-ness when non-Enum operations are performed on them).  Similarly, the differences between Flag and IntFlag are entirely because IntFlag members are also ints, so can do whatever ints can do (and, similarly, will lose their IntFlag-ness when non-Flag operations are performed on them).

> 2) (more important) If I give names to 1, 3, 4, and 6, you're in fact
> saying that MyFlags(7) == MyFlags(5) (since 2 is never set)?

(I think you messed up your example, as 4 | 1 == 5 and 6 | 1 == 7 -- 2 is never a factor.)

Absolutely not.  5 != 7, so the two will never be equal no matter which of Flag | IntFlag you are using.

Moreover, if MyFlags is a Flag then MyFlags(impossible_combination) will raise an exception.  If MyFlags is an IntFlag then MyFlags(impossible_combination) will have the value of <impossible_combination> and the repr will show all the non-specified bits as base-2 numbers and the specified bits as names.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23591>
_______________________________________


More information about the Python-bugs-list mailing list