[Python-ideas] IntFlags

Chris Angelico rosuav at gmail.com
Thu Mar 5 00:56:57 CET 2015


On Thu, Mar 5, 2015 at 10:42 AM, Andrew Barnert
<abarnert at yahoo.com.dmarc.invalid> wrote:
> On Mar 4, 2015, at 7:58, Serhiy Storchaka <storchaka at gmail.com> wrote:
>
>> On 04.03.15 17:31, Chris Angelico wrote:
>>> On Thu, Mar 5, 2015 at 2:17 AM, Andrew Barnert
>>> <abarnert at yahoo.com.dmarc.invalid> wrote:
>>>> Another issue that came up was that C flags often have "combined" names that are ambiguous: RDWR = RDONLY | WRONLY), which is fine until you want a repr (in C, it's just going to print 3); does it have to be smart enough to show RDWR? (Or, worse, RDWR | CLOEXEC.)
>>> That could probably be handled by going through the flags in iteration
>>> order. If the flag is present, emit it and move on. Something like
>>> this:
>>
>> Yes, something like this, but with iterating flags in descended sorted order, and with special case for negative value.
>
> I think the very fact that the two of you immediately knew which order was obvious, but you chose the opposite one as the obvious one, proves that it's not obvious.

Actually, we chose the same thing, only in slightly different ways.
Serhiy suggested (in effect) sorting the flags by value and stepping
through from highest to lowest, which enforces that the combined flags
will be the ones picked. I suggested putting the responsibility onto
the class author - if you want the combined ones to be used, place
them first - which is like how aliasing works (the first one with a
given value is used in str/repr, any others are aliases). That's a
relatively minor point, and it depends on whether there'd ever be a
time when you want to provide a combined flag that _isn't_ used in
str/repr; if there is, you need my plan, but if not, go with the
simpler route.

ChrisA


More information about the Python-ideas mailing list