Iterate through members of enum.Flag
My proposal is that iterating through a member of a Flag enum will return all the constituent members. Demonstration Setup: class FlagEnum(enum.Flag): A = enum.auto() B = enum.auto() C = enum.auto() FlagCombo = FlagEnum.A | FlagEnum.B My proposed change would cause the following to occur:
print(list(FlagCombo)) [FlagEnum.B, FlagEnum.A] print(list(FlagEnum.A)) [FlagEnum.A]
Compared to the current implementation:
print(list(FlagCombo)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'FlagEnum' object is not iterable
What do you all think?
On 9/10/21 4:19 AM, matthew@matthewstocks.co.uk wrote:
My proposal is that iterating through a member of a Flag enum will return all the constituent members.
It's a good proposal, and is already in 3.11. If you want the functionality now you can use the aenum [0] library. -- ~Ethan~ [0] https://pypi.org/project/aenum/
participants (2)
-
Ethan Furman
-
matthew@matthewstocks.co.uk