I am using enums in python3 for the first time but unsure if I am using them correctly.
I want to restrict the 'allowed values' for a class attribute ArticleMetadata.article_type
to those defined within an ArticleTypes
Enum class.
Here's my code:
from enum import Enum
class ArticleMetadata(object):
ArticleTypes = Enum('ArticleTypes', 'original modified', module=__name__)
def is_in_enum(self, value, Enum):
"""Verify that a value is contained within a defined Enum object.
Raises:
ValueError: If the value is not contained within the defined Enum.
"""
if value in Enum.__members__:
return True
else:
raise ValueError("Value {0} not in Enum list.".format(value))
def __init__(self, **kwargs):
"""Set class attributes."""
self.article_type = (kwargs['article_type'] if 'article_type' in kwargs.keys()
and self.is_in_enum(kwargs['article_type'], self.ArticleTypes)
else None
)
Usage
> foo = ArticleMetadata(article_type='original')
> foo.article_type
'original'
> bar = ArticleMetadata(article_type='invalid')
*** ValueError: Value invalid not in Enum list.
This code works, but having to write an is_in_enum
method seems clunky, and I would have thought that this functionality would be natively part of enums somehow??
Also, no errors are raised if I later run foo.article_type = 'invalid'
, which doesn't seem right!
Am I missing a common usage pattern?