I am wondering if I can use python enums to enforce specific string inputs from a user into my functions and classes, and avoid having to code the explicit checks against acceptable values.
So instead of this:
e.g.
utilities_supplied = {
'electricity': 'Yes',
'gas': 'No',
'water': 'Yes',
}
def find_utility(utility):
try:
print(utilities_supplied[utility])
except KeyError:
raise KeyError(f'Please choose one of {utilities_supplied.keys()}, \'{utility}\' provided')
I would like to do this:
from enum import Enum
class Utility(Enum):
electricity = 1
gas = 2
water = 3
Utility['electric']
# not a member, I want this to raise an error which lists the acceptable options.
Is it possible for enums to raise an error listing the possible enumeration members?