Behavior of auto in Enum and Flag.

Chris Angelico rosuav at gmail.com
Mon Apr 3 12:43:23 EDT 2017


On Tue, Apr 4, 2017 at 2:29 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> It this point I do not.  If you can give us an example Enum and why it's
> necessary to be built like that I might be swayed -- although the odds are
> good that the change will go into aenum instead (it's already a mess with
> the 2.7 compatibility code...).

Here's a counter-example that supports the current behaviour:

>>> from enum import IntFlag, auto
>>> class Spam(IntFlag):
...     FOO = auto()
...     BAR = auto()
...     FOOBAR = FOO | BAR
...     SPAM = auto()
...     HAM = auto()
...     SPAMHAM = SPAM | HAM
...
>>> list(Spam)
[<Spam.FOO: 1>, <Spam.BAR: 2>, <Spam.FOOBAR: 3>, <Spam.SPAM: 4>,
<Spam.HAM: 8>, <Spam.SPAMHAM: 12>]

It makes perfect sense to define combined flags in terms of the
(previously-defined) individual flags. If auto() has to be deferred
until the class is fully defined, this would be a lot harder.

ChrisA



More information about the Python-list mailing list