Message274372
> All my questions pertain to Flags.
Ah, okay.
> You said what to me seem like two contradictory things:
>
>> Not having 2 named has different consequences for Flag vs IntFlag
>> (although *neither is an error*): Flag: no combination of flags will
>> ever have the 2 bit set [...]
and
>> if MyFlags is a Flag then MyFlags(impossible_combination) *will raise an exception.*
First, let me state the intended use of Flags: primary flags (single-bit flags) will have values of powers of 2:
--> class Perm(Flag):
... R = 4
... W = 2
... X = 1
...
--> Perm.R | Perm.W | Perm. X
<Perm.R|W|X: 7>
-->
If nicer names are desired for certain combinations (aka secondary flags, or multi-bit), then name them:
--> class Perm(Flag):
... ...
... RWX = 7
...
--> Perm.R | Perm.W | Perm.X
<Perm.RWX: 7>
If, for whatever strange reason, you don't give a certain power of 2 a name, Flag doesn't care:
--> class Perm(Flag):
... R = 8
... W = 4
... X = 1
...
--> Perm.R | Perm.W | Perm. X
<Perm.R|W|X: 13>
-->
But trying to use that missing value will be an error:
--> Perm(6)
Traceback (most recent call last):
...
ValueError: 6 is not a valid MyFlags
This is what I was referring to as "not naming a bit is not an error, but using an impossible value is". What I missed in your example was that, although you hadn't named 2, you still had multi-bit values that included the 2 bit. In other words, in my example there will never be a combination that has the 2 bit set, while in yours (because of your weird values) it is possible.
> Are you saying that after I write
>
> class MyFlags(Flags):
> b001 = 1
> b011 = 3
> b100 = 4
> b110 = 6
>
> this is _not_ an error, but if after that I call
>
> print(MyFlags(7))
>
> it _is_ an error?
No, that's not what I'm saying, and hopefully my explanation above clears that up. |
|
Date |
User |
Action |
Args |
2016-09-04 17:54:47 | ethan.furman | set | recipients:
+ ethan.furman, barry, rhettinger, vstinner, ezio.melotti, r.david.murray, eli.bendersky, python-dev, martin.panter, serhiy.storchaka, veky |
2016-09-04 17:54:47 | ethan.furman | set | messageid: <1473011687.01.0.460941717232.issue23591@psf.upfronthosting.co.za> |
2016-09-04 17:54:46 | ethan.furman | link | issue23591 messages |
2016-09-04 17:54:46 | ethan.furman | create | |
|