Open utterances-bot opened 2 years ago
A digression: while I would certainly prefer enum.StrEnum
to the use of the Walrus Operator in this case, I do find it a bit annoying that there are multiple ways of getting string-enums in Python.
Just take a look at some of the tables of differences in PEP663 to get an idea of the subtleties you need to consider based on the different options available. I feel it is a similar (although less severe) situation to where there are multiple ways to obtain dataclasses/named-tuples.
Personally, in the past I've used double-inheritance with str
, and enum.Enum
which PEP663 calls "user-mixed enums". I would probably just recommend everyone use StrEnum
from now on, but I feel that generally people are not ready to have their codebase rely on 3.11+ features. I suppose really what I take issue with is that StrEnum
was accepted too late in Python's life. So for now I'm going to continue to just use base Enum
mixed with str
, rather enum.StrEnum
.
Thanks for that Nathan. I also rely heavily on user-mixed enums and look forward to being able to switch over to StrEnum in all my production code. StrEnum will be easier to teach as well.
I found this article valuable: Build Enumerations of Constants With Python's Enum. Amongst other things it has a summary of the value of enums - my favourites are:
Allowing for conveniently grouping related constants in a sort of namespace ... Providing quick and flexible access to enum members ... Facilitating code completion within IDEs and editors Enabling type and error checking with static checkers ...
Walrus Hunting with StrEnum | The When of Python Blog
The Walrus operator finally seemed to have found a practical use case - as a way to make collections of strings or integers without as much boiler plate as the alternative without the operator. But Python has a better approach already - Enum - and in Python 3.11 it is even easier to use with strings (StrEnum). So the Walrus operator still struggles to find a problem for which it is the best solution.
https://when-of-python.github.io/blog/walrus-hunting-with-strenum.html