[Python-ideas] IntFlags

Andrew Barnert abarnert at yahoo.com
Thu Mar 5 10:25:53 CET 2015


On Mar 4, 2015, at 15:56, Chris Angelico <rosuav at gmail.com> wrote:

> 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.

Except with yours, any time you copy the definition (whether by hand, or with an automated tool) from C, where the combined ones almost always come last, they won't be used; with his, they will. So, in the most common use case, you'll get the opposite result.

(Of course if you always pick the _last_ instead of the first or the highest, which gives you the choice for the rare case, but the nice answer for the ubiquitous case. That breaks your analogy with aliases, but it does work the same way as normal class attributes, where the last value wins...)


More information about the Python-ideas mailing list