[Python-ideas] IntFlags

Ethan Furman ethan at stoneleaf.us
Sat Mar 7 21:38:20 CET 2015


On 03/07/2015 12:28 PM, Georg Brandl wrote:
> 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".

Good point.  To do something like that the name would have be BitFlags or something no so solidly tied to "immutable".
At any rate, for this to work would require `int(x)` around every call to a lower-level API, and that's a non-starter.

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


More information about the Python-ideas mailing list