Skip to content
Advertisement

How to check if string exists in Enum of strings?

I have created the following Enum:

JavaScript

I have inherited from str, too, so that I can do things such as:

JavaScript

I would now like to be able to check if a string is in this Enum, such as:

JavaScript

I have tried adding the following method to the class:

JavaScript

However, when I run this code:

JavaScript

I get this exception:

JavaScript

Advertisement

Answer

I just bumped into this problem today (2020-12-09); I had to change a number of subpackages for Python 3.8.

Perhaps an alternative to the other solutions here is the following, inspired by the excellent answer here to a similar question, as well as @MadPhysicist’s answer on this page:

JavaScript

Tests (python >= 3.7; tested up to 3.10):

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement