Author ethan.furman
Recipients barry, eli.bendersky, ethan.furman, ezio.melotti, haypo, martin.panter, python-dev, r.david.murray, rhettinger, serhiy.storchaka, veky
Date 2016-09-04.17:54:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473011687.01.0.460941717232.issue23591@psf.upfronthosting.co.za>
In-reply-to
Content
> 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.
History
Date User Action Args
2016-09-04 17:54:47ethan.furmansetrecipients: + ethan.furman, barry, rhettinger, haypo, ezio.melotti, r.david.murray, eli.bendersky, python-dev, martin.panter, serhiy.storchaka, veky
2016-09-04 17:54:47ethan.furmansetmessageid: <1473011687.01.0.460941717232.issue23591@psf.upfronthosting.co.za>
2016-09-04 17:54:46ethan.furmanlinkissue23591 messages
2016-09-04 17:54:46ethan.furmancreate