[Python-ideas] IntFlags

Chris Angelico rosuav at gmail.com
Thu Mar 5 22:22:50 CET 2015


On Fri, Mar 6, 2015 at 4:11 AM, Chris Barker <chris.barker at noaa.gov> wrote:
> On Thu, Mar 5, 2015 at 9:03 AM, Luciano Ramalho <luciano at ramalho.org> wrote:
>>
>> The problem is that a set is by definition unordered, but the position
>> of the bits in BitFlags is crucial,
>
>
> Is it? or is that an implementation detail?
>
> What I'm getting at is that if you use an integer to store bits, then the
> nth bit is, well, the nth bit in the value. But you could conceptually think
> of it as the bit with the name, 'n', in which case order no longer matters.

The position of bits is the values of things in the set. These two are
more-or-less expressing the same concept:

flags1 = {"ReadOnly", "AllowRead", "CloseOnExec"}
flags2 = FLG_READONLY | FLG_ALLOWREAD | FLG_CLOEXEC

The order of elements in the braced set is meaningless, just as the
order of bitflags in the piped list is meaningless. The positions of
bits in the piped set corresponds to the string names in the braced
set. As the other Chris says, it's basically the bit with name "n",
and then we give it a much more convenient alias.

ChrisA


More information about the Python-ideas mailing list