[Python-ideas] IntFlags

Ethan Furman ethan at stoneleaf.us
Sat Mar 7 09:07:38 CET 2015


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
b = Stat.RDONLY
a is b  # False
a == b  # True

c = a
a |= Stat.MANDLOCK
c.MANDLOCK  # 64
b.MANDLOCK  # 0
c is a  # True

repr(a)  # <Stat.MANDLOCK|RDONLY: 65>
repr(b)  # <Stat.RDONLY: 1>

d = b | 32  # undefined value
repr(d)  # <Stat.32|RDONLY: 33>
d.MANDLOCK = True
repr(d)  # <Stat.MANDLOCK|32|RDONLY: 97>

repr(~d)  # <Stat.RELATIME|NODIRATIME|NOATIME|512|APPEND|WRITE|SYNCHONOUS|NOEXEC|NODEV|NOSUID: 8094>

I'm not at all sure I have that last one correct.

--
~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/c99f1ce7/attachment.sig>


More information about the Python-ideas mailing list