Validating Enumerated Values

Learn how can we validate enumerated values in Python.

We'll cover the following

The list of valid species isn’t very visible in the previous sections. We’ve essentially buried it inside the from_dict() method, which may become a maintenance problem. When the source data changes, we need to also update this method, something that can be hard to remember and almost as hard to find. If the list of species becomes long, the lines of code could become hard to read.

Using the enum class

Using an explicit enum class with a list of valid values is a way to convert this to purely EAFP processing. Consider using the following to validate species. Doing this means redefining a number of classes:

Get hands-on with 1400+ tech skills courses.