classification
Title: add __iter__ to enum.Flag members
Type: enhancement Stage:
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: Guy Gangemi, barry, eli.bendersky, ethan.furman, rhettinger
Priority: normal Keywords:

Created on 2017-12-05 03:44 by Guy Gangemi, last changed 2018-01-30 00:29 by ethan.furman.

Messages (2)
msg307629 - (view) Author: Guy Gangemi (Guy Gangemi) Date: 2017-12-05 03:44
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
msg311204 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2018-01-30 00:29
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?


* as of version 2.0.10, available on PyPI, and I am its author
History
Date User Action Args
2018-01-30 00:29:57ethan.furmansetversions: + Python 3.8
nosy: + barry, rhettinger, eli.bendersky

messages: + msg311204

stage: needs patch ->
2018-01-18 19:31:45ethan.furmansetassignee: ethan.furman
stage: needs patch
2017-12-05 05:05:41berker.peksagsetnosy: + ethan.furman
2017-12-05 03:44:12Guy Gangemicreate