New submission from Marc Guetg: At the moment it is only possible to combine flags that already exist:
from enum import * class Foo(Flag): A = auto() # 1 B = auto() # 2 AB = A | B # 3 (1 | 2) AC = auto() | A # Fails, but should be 5 (1 | 4) ABD = auto() | A | B # Just taking it one step further to make a point, 11 (1 | 2 | 8) It would be nice to have this for cases when C only makes sense in combination with A but not on its own. A solution to achieve this one would need to change two things in ~/cpython/Lib/enum.py First extend class auto by: class auto: """ Instances are replaced with an appropriate value in Enum class suites. """ value = _auto_null or_value = 0 def __or__(self, other): """ Postpone the real or operation until value creation in _EnumDict """ self.or_value |= other return self And second change one line in _EnumDict: value = value.value changes to: value = value.value | value.or_value Some simple tests show the expected results: print(repr(Foo.A)) # A - 1 print(repr(Foo.B)) # B - 2 print(repr(Foo.AB)) # AB - 3 print(repr(Foo.AC)) # AC - 5 print(repr(Foo.A | Foo.AC)) # AC - 5 print(repr(Foo.A & Foo.AC)) # A - 1 print(repr(Foo.ABD)) # ABD - 11 Would it make sense to enhance python enums with that functionality? ---------- components: Library (Lib) files: test.py messages: 288029 nosy: magu priority: normal severity: normal status: open title: implement __or__ in enum.auto type: enhancement versions: Python 3.6, Python 3.7 Added file: http://bugs.python.org/file46646/test.py _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29594> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com