[Python-ideas] IntFlags

Neil Girdhar mistersheik at gmail.com
Fri Mar 6 05:35:46 CET 2015


FYI:

ctypes already has a BitFlags/BitFields mechanism.

class Flags_bits(ctypes.LittleEndianStructure):
    _fields_ = [
            ("logout", c_uint8, 1),
            ("userswitch", c_uint8, 1),
            ("suspend", c_uint8, 1),
            ("idle", c_uint8, 1),
        ]


On Thu, Mar 5, 2015 at 11:26 PM, Neil Girdhar <mistersheik at gmail.com> wrote:

> Even if you constrain yourself to the BitFlags rather than the more
> general BitFields, I strongly disagree with the interface that people are
> proposing involving & and ~ operators.  In general, good interface design
> reflects the way we think about objects — not their underlying
> representation.  The fact is that a BitSet's main operations are set and
> clear individual bits.  It is as if the BitFlags are a namedtuple with
> Boolean elements whose underlying storage happens to be an integer.
> Therefore, the interface that makes the most sense is member access:
>
> my_bit_flags.some_bit = True
> my_bit_flags.some_bit = False
>
> I don't see the justification for writing these as
>
> my_bit_flags |= TheBitFlagsClass.some_bit
> my_bit_flags &= ~TheBitFlagsClass.some_bit
>
> The second line is particularly terrible because it exposes you to making
> mistakes like:
>
> my_bit_flags &= TheBitFlagsClass.some_bit
> my_bit_flags |= ~TheBitFlagsClass.some_bit
>
> — both of which are meaningless.
>
> It also makes it hard to convert code between the alternate implementation
> of using a namedtuple.  It should be easy to do that in my opinion.
>
> Best,
>
> Neil
>
> On Thu, Mar 5, 2015 at 12:57 PM, Serhiy Storchaka <storchaka at gmail.com>
> wrote:
>
>> On 05.03.15 19:29, Neil Girdhar wrote:
>>
>>> Have you looked at my IntFields generalization of IntFlags?  It seems
>>> that many of your examples (permissions, e.g.) are better expressed with
>>> fields than with flags.
>>>
>>
>> It looks too complicated for such simple case. And it has an interface
>> incompatible with plain int.
>>
>>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> --
>>
>> --- You received this message because you are subscribed to a topic in
>> the Google Groups "python-ideas" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/
>> topic/python-ideas/L5KfCEXFaII/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> python-ideas+unsubscribe at googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150305/e596953a/attachment-0001.html>


More information about the Python-ideas mailing list