On Fri, Mar 12, 2021 at 4:52 PM Ethan Furman firstname.lastname@example.org wrote:
A question that comes up quite a bit on Stackoverflow is how to test to see if a value will result in an Enum member, preferably without having to go through the whole try/except machinery.
A couple versions ago one could use a containment check:
if 1 in Color:
but than was removed as Enums are considered containers of members, not containers of the member values. It was also possible to define one's own `_missing_` method and have it return None or the value passed in, but that has also been locked down to either return a member or raise an exception.
At this point I see three options:
add a `get(value, default=None)` to EnumMeta (similar to `dict.get()`
add a recipe to the docs
Could this be an instance where match-case might become the canonical solution?
I'm probably getting the syntax wrong, but maybe it would be something like:
match value: case MyEnum(): assert isinstance(value, MyEnum) case _: assert not isinstance(value, MyEnum)
"I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler