1、I first used Python version 2.7, and through pip installed enum
module.
from enum import Enum
class Format(Enum):
json = 0
other = 1
@staticmethod
def exist(ele):
if Format.__members__.has_key(ele):
return True
return False
class Weather(Enum):
good = 0
bad = 1
@staticmethod
def exist(ele):
if Weather.__members__.has_key(ele):
return True
return False
Format.exist('json')
Which works well, but I want to improve the code.
2、So I thought a better way might be like this:
from enum import Enum
class BEnum(Enum):
@staticmethod
def exist(ele):
if BEnum.__members__.has_key(ele)
return True
return False
class Format(Enum):
json = 0
other = 1
class Weather(Enum):
good = 0
bad = 1
Format.exist('json')
However this results in an error, because BEnum.__members__
is a class variable.
How can I get this to work?