[Python-ideas] IntFlags

Georg Brandl g.brandl at gmx.net
Sat Mar 7 21:28:14 CET 2015


On 03/07/2015 04:05 PM, Ethan Furman wrote:

>>> 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
>>
>> Why?
> 
> Because by having mutable instances of Stat we can have more Python operations:
> 
> instead of:
> 
>   x = Stat(some_Stat_value_from_somewhere)
>   x = x | Stat.NOEXEC  # to set the bit
> 
> we can say:
> 
>   x = Stat(some_Stat_value_from_somewhere)
>   x.NOEXEC = True

Please no.  You're making a mutable type out of something that is conceptually
(and in people's minds) an integer.  Remember how long it can take to understand
that

a = 1
a = 2

does not change the integer "1" to now be "2".

Georg



More information about the Python-ideas mailing list