New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add __iter__ to enum.Flag members #76399
Comments
I'm proposing to extend enum.Flag member functionality so it is iterable in a manner similar to enum.Flag subclasses. from enum import Flag, auto
class FlagIter(Flag):
def __iter__(self):
for memeber in self._member_map_.values():
if member in self:
yield member
class Colour(FlagIter):
RED = auto()
GREEN = auto()
BLUE = auto()
YELLOW = RED | GREEN
cyan = Colour.GREEN | Colour.Blue
print(*Colour) # Colour.RED Colour.GREEN Colour.BLUE Colour.YELLOW
# Without the enhancement, 'not iterable' is thrown for these
print(*cyan) # Colour.GREEN Colour.BLUE
print(*Colour.YELLOW) # Colour.RED Colour.GREEN Colour.YELLOW
print(*~Colour.RED) # Colour.GREEN Colour.BLUE |
This functionality is now in the third-party aenum* library. Are there any strong use-cases for this behavior such that it should be in the stdlib?
|
Problem: What to do when the Flag has compound members? class Color(Flag):
BLACK = 0
RED = 1
GREEN = 2
BLUE = 4
PURPLE = RED|BLUE
WHITE = RED|GREEN|BLUE I think the answer is: only return the single members. So --> list(Color.WHITE) |
What does aenum do and has there been any feedback on it? To me I would see what you suggest as surprising but I don't use enums often (I should use them more!) so take that with a grain of salt, and also surprising != wrong/not good. |
Final outcome:
|
Flag
andIntFlag
members iterable #22221Flag.__iter__
#23368Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: