[Python-Dev] PEP 557: Data Classes (original) (raw)

Ethan Furman ethan at stoneleaf.us
Mon Sep 18 14:37:09 EDT 2017


On 09/11/2017 03:28 PM, Guido van Rossum wrote:

Oddly I don't like the enum (flag names get too long that way), but I do agree with everything else Barry said (it should be a trivalue flag and please don't name it cmp).

Hmmm, named constants are one of the motivating factors for having an Enum type. It's easy to keep the name a reasonable length, however: export them into the module-level namespace. re is an excellent example; the existing flags were moved into a FlagEnum, and then (for backwards compatibility) aliased back to the module level:

 class RegexFlag(enum.IntFlag):
     ASCII = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
     IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
     LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
     UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
     MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
     DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
     VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
     A = ASCII
     I = IGNORECASE
     L = LOCALE
     U = UNICODE
     M = MULTILINE
     S = DOTALL
     X = VERBOSE
     # sre extensions (experimental, don't rely on these)
     TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
     T = TEMPLATE
     DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
 globals().update(RegexFlag.__members__)

So we can still do re.I instead of re.RegexFlag.I.

Likewise, if we had:

 class Compare(enum.Enum):
     NONE = 'each instance is an island'
     EQUAL = 'instances can be equal to each other'
     ORDERED = 'instances can be ordered and/or equal'
 globals().update(Compare.__members__)

then we can still use, for example, EQUAL, but get the more informative repr and str when we need to.

-- Ethan



More information about the Python-Dev mailing list