-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
bpo-32218: make Flag and IntFlag members iterable
#22221
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -728,6 +728,10 @@ def __contains__(self, other): | |
| type(other).__qualname__, self.__class__.__qualname__)) | ||
| return other._value_ & self._value_ == other._value_ | ||
|
|
||
| def __iter__(self): | ||
| members, extra_flags = _decompose(self.__class__, self.value) | ||
| return (m for m in members if m._value_ != 0) | ||
|
|
||
|
Comment on lines
+731
to
+734
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I can think of two efficient ways:
return (v for v in self.__class__ if v.value != 0 and (v.value & (v.value-1) == 0) and v in self)
@staticmethod
def _bits(n):
while n:
b = n & (~n+1)
yield b
n ^= b
def __iter__(self):
return (self.__class__(v) for v in self._bits(self._value_))in fact
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. proposal (2) is 2x faster than the implementation in this PR, for an enum with only about 6 values... >>> from enum import Flag, auto
>>> class Foo(Flag):
... NONE = 0
... A = auto()
... B = auto()
... C = auto()
... C2 = C
... AB = A | B
... D = auto()
... E = auto()
... F = auto()
...
>>> x = Foo.A | Foo.B | Foo.D | Foo.F
>>> import timeit
>>> timeit.timeit('list(x)', globals=locals(), number=10000)
0.6332780510000013proposal (2): >>> timeit.timeit('list(x)', globals=locals(), number=10000)
0.36078684099999236
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have become increasingly unhappy with the the multiple calls to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I do have ideas on it, and intended to open a separate issue for refactoring Regarding bugs, when it's used by >>> class Foo(Flag):
... A = auto()
... B = auto()
... C = auto()
... BC = B | C
>>> repr(Foo.BC)
'<Foo.BC: 6>'
>>> repr(Foo.A | Foo.BC)
'<Foo.BC|C|B|A: 7>'
Comment on lines
+731
to
+734
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the use of
I'd argue that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the zero-value flag should not be contained in a non-zero flag, but I have no problem with a compound flag showing up if its bits are present. You are welcome to try and convince me otherwise, but it will take more than "no one wants that behavior".
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I just learned that >>> class Foo(Flag):
... NONE = 0
... A = 1
... B = 2
... C = 4
... AB = A | B
>>> Foo.AB in (Foo.AB | Foo.C)
True
>>> (Foo.A|Foo.C) in (Foo.AB | Foo.C)
TrueIf that's the case, then actually it's OK for >>> set().issubset({1, 2})
TrueI would not have made the API this way. |
||
| def __repr__(self): | ||
| cls = self.__class__ | ||
| if self._name_ is not None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `enum.Flag` and `enum.IntFlag` members are now iterable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The meaning of "members" should be clarified. Specifically, the iteration intentionally omits aliases and compound members.
The tests should verify this.
I would describe it as "iterating over the bits of a flag value", because that's what it does. Using "bits" should be OK since
Flagis documented as supporting bitwise operators.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.