Title: enum.Flag should be more set-like
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: John Belmonte, Manjusaka, ethan.furman, veky
Priority: normal Keywords:

Created on 2019-09-22 11:14 by John Belmonte, last changed 2020-03-26 13:49 by Manjusaka.

Messages (3)
msg352967 - (view) Author: John Belmonte (John Belmonte) Date: 2019-09-22 11:14
I would like Flag class instances to have more set-like abilities:
  1. iteration, to walk through each set bit of the value
  2. len corresponding to #1
  3. subset operator

I may be told "implement it yourself as instance methods", or that #3 has an idiom (a & b is b).  Ideally though, every Flag user should be able to rely on these being implemented consistently.

When trying to implement #1 without devolving into bit fiddling, naturally one might try to use the class iterator.  Unfortunately the semantics of that enumeration include 0, aliases, and compound values.  I've used Flag in several situations and projects, and so far there hasn't been a case where that was the desired semantics.  Interesting though, if #1 were implemented in the standard library, then we could enumerate all bits of the Flag via iteration of `~MyFlag(0)`... though that's obscuring things behind another idiom.

Thank you for considering.
msg365051 - (view) Author: Vedran Čačić (veky) * Date: 2020-03-26 04:47
1. +0 _if_ the implementation is easy to explain. If backward compatibility is an issue, we can always add a property: 
for flag in flags.set:
(though set might imply unorderedness:)
2. -1. Guido said long ago that all lens should be O(1).
(Of course, if you do make it O(1), I have no objection.)
3. +1, absolutely.
msg365072 - (view) Author: Manjusaka (Manjusaka) * Date: 2020-03-26 13:49
1. not sure I gett the Point

2. not sure

3. absolutely yes
Date User Action Args
2020-03-26 13:49:48Manjusakasetnosy: + Manjusaka
messages: + msg365072
2020-03-26 04:47:33vekysetnosy: + veky
messages: + msg365051
2020-03-25 19:25:38ethan.furmansetassignee: ethan.furman

nosy: + ethan.furman
2019-09-22 11:14:02John Belmontecreate