
On Tue, Aug 31, 2021 at 9:17 AM Ricky Teachey ricky@teachey.org wrote:
Can someone explain why enum-vs-string is being discussed as if it is an either-or choice? Why not just call the enum class using the input so that you can supply a string or enum?I understand this would not be a really great choice for a flags enum or int enum, but for single-choice strings, it seems like a reasonable approach to me.
<SNIP>
...in the past, I have used enums like this in my own code mostly as 1. a documentation feature so I can easily remember what the valid choices are, and 2. an easy error catcher since you get a nice exception when you supply an invalid string to the enum class:
Whoops, the repl session was incorrect, apologies. Hopefully it was clear what I meant. Corrected version below.
class MyEnum(Enum):
... a = "a" ... b = "b" ...
MyEnum("c")
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\program files\python39\lib\enum.py", line 384, in __call__ return cls.__new__(cls, value) File "c:\program files\python39\lib\enum.py", line 702, in __new__ raise ve_exc ValueError: 'c' is not a valid MyEnum
--- Ricky.
"I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler