[Python-ideas] IntFlags

Serhiy Storchaka storchaka at gmail.com
Sat Mar 7 11:50:46 CET 2015


On 07.03.15 10:07, Ethan Furman wrote:
> On 03/03/2015 07:52 AM, Serhiy Storchaka wrote:
>
>> 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().
>
> As long as we are dreaming  :)
>
> class Stat(IntFlag):
>      RDONLY = 1
>      NOSUID = 2
>      NODEV = 4
>      NOEXEC = 8
>      SYNCHRONOUS = 16
>      MANDLOCK = 64
>      WRITE = 128
>      APPEND = 256
>      NOATIME = 1024
>      NODIRATIME = 2048
>      RELATIME = 4096
>
> a = Stat.RDONLY  # creates a new instance of Stat, not a singleton

IntFlags is purposed to replace existing integer constants (as IntEnum).

globals().update(Stat.__members__)



More information about the Python-ideas mailing list