Opened 3 years ago
#6748 enhancement new
Document the use of Flags and FlagConstant with specific values
|Reported by:||Richard Wall||Owned by:|
The constants howto doesn't mention that you can supply a value to FlagConstant which is then used as the base for the following FlagConstant attributes without a specific value.
I guess this is useful for skipping particular bits.
In : from twisted.python.constants import Flags, FlagConstant In : class BITS(Flags): ....: one = FlagConstant() ....: two = FlagConstant() ....: eight = FlagConstant(8) ....: sixteen = FlagConstant() ....: In : BITS.one Out: <BITS=one> In : BITS.one.value Out: 1 In : BITS.two.value Out: 2 In : BITS.eight.value Out: 8 In : BITS.sixteen.value Out: 16
You can also supply a number which isn't a power of two in which case all those bits will be shifted by subsequent FlagConstants. Not sure how that would be useful.
In : class FLAGS2(Flags): three = FlagConstant(3) four = FlagConstant() ....: eight = FlagConstant() ....: In : FLAGS2.four.value Out: 6 In : FLAGS2.eight.value Out: 12 In : bin(FLAGS2.eight.value) Out: '0b1100' In : bin(FLAGS2.four.value) Out: '0b110'
I raised this ticket while trying to add missing epydoc to FlagConstant in #6570