Using Python 3.4 I want to test whether an Enum class contains a member with a certain name.
Example:
class Constants(Enum):
One = 1
Two = 2
Three = 3
print(Constants['One'])
print(Constants['Four'])
gives:
Constants.One
File "C:Python34libenum.py", line 258, in __getitem__
return cls._member_map_[name]
KeyError: 'Four'
I could catch the KeyError
and take the exception as indication of existence but maybe there is a more elegant way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…