[Python-ideas] IntFlags

Florian Bruhin me at the-compiler.org
Tue Mar 3 17:55:35 CET 2015


* Ethan Furman <ethan at stoneleaf.us> [2015-03-03 08:40:11 -0800]:
> On 03/03/2015 07:52 AM, Serhiy Storchaka wrote:
> >
> > Enum and IntEnum classes allow constants to have nice str() and repr() representations.
> > 
> > But when integer constants are flags that should be ORed, IntEnum doesn't help, because the result of bitwise OR of two
> > IntEnum instances is int, and this value can't be represented as IntEnum.
> > 
> > We need new type IntFlags. It is like IntEnum, but has differences:
> > 
> > 1. The value of an instance should be not limited to the set of predefined constants. It can be a combination of
> > predefined constants or even arbitrary integer.
> > 
> > 2. The result of "|", "&" and "~" operators for IntFlags arguments should be an instance of the same IntFlags subclass.
> > 
> > 3. It should have nice str() and repr().
> 
> This was in my original Enum code, but stripped out as not being needed at the time.
> 
> If there is sufficient interest (and use-cases) I can add it back in.

I'd gladly use it - for example when communicating with embedded
electronic devices.

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/


More information about the Python-ideas mailing list