[Python-ideas] IntFlags
Ethan Furman
ethan at stoneleaf.us
Sat Mar 7 15:50:38 CET 2015
On 03/07/2015 02:50 AM, Serhiy Storchaka wrote:
> 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__)
And that can still work -- if the flag is accessed from the class (Stat.RDONLY) it will always be the value assigned
(1); if it is accessed from a member, it will be the value assigned /if set/, otherwise 0.
This also has the advantage of supporting both C style operations (x = Stat.WRITE | Stat.APPEND), or the more customary
Python operations (x = Stat.WRITE; x.APPEND = True).
--
~Ethan~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150307/0927f716/attachment.sig>
More information about the Python-ideas
mailing list