Python 从枚举返回None
我有一个简单的枚举:Python 从枚举返回None,python,python-3.7,Python,Python 3.7,我有一个简单的枚举: from enum import Enum class ColorEnum(Enum): RED = 1 GREEN = 2 BLUE = 3 @classmethod def _missing_(cls, value): if value is None: return None super()._missing_(value) 正如您所看到的,如果将None传递给Co
from enum import Enum
class ColorEnum(Enum):
RED = 1
GREEN = 2
BLUE = 3
@classmethod
def _missing_(cls, value):
if value is None:
return None
super()._missing_(value)
正如您所看到的,如果将None传递给ColorEnum init,它将返回None。这适用于Python3.6。但当Python 3.7出现ValueError故障时,它不适用于Python 3.7:
ValueError:None不是有效的ColorEnum
对于Python 3.6,方法\uuuuu new\uuuu
如下所示:
class Enum(metaclass=EnumMeta):
"""Generic enumeration.
Derive from this class to define new enumerations.
"""
def __new__(cls, value):
# all enum instances are actually created during class construction
# without calling this method; this method is called by the metaclass'
# __call__ (i.e. Color(3) ), and by pickle
if type(value) is cls:
# For lookups like Color(Color.RED)
return value
# by-value search for a matching enum member
# see if it's in the reverse mapping (for hashable values)
try:
if value in cls._value2member_map_:
return cls._value2member_map_[value]
except TypeError:
# not there, now do long search -- O(n) behavior
for member in cls._member_map_.values():
if member._value_ == value:
return member
# still not found -- try _missing_ hook
return cls._missing_(value)
对于Python 3.7:
class Enum(metaclass=EnumMeta):
"""Generic enumeration.
Derive from this class to define new enumerations.
"""
def __new__(cls, value):
# all enum instances are actually created during class construction
# without calling this method; this method is called by the metaclass'
# __call__ (i.e. Color(3) ), and by pickle
if type(value) is cls:
# For lookups like Color(Color.RED)
return value
# by-value search for a matching enum member
# see if it's in the reverse mapping (for hashable values)
try:
return cls._value2member_map_[value]
except KeyError:
# Not found, no need to do long O(n) search
pass
except TypeError:
# not there, now do long search -- O(n) behavior
for member in cls._member_map_.values():
if member._value_ == value:
return member
# still not found -- try _missing_ hook
try:
exc = None
result = cls._missing_(value)
except Exception as e:
exc = e
result = None
if isinstance(result, cls):
return result
else:
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__name__))
if result is None and exc is None:
raise ve_exc
elif exc is None:
exc = TypeError(
'error in %s._missing_: returned %r instead of None or a valid member'
% (cls.__name__, result)
)
exc.__context__ = ve_exc
raise exc
因此,如果没有传递给init,我希望没有。我如何存档它?当您实例化一个
Enum
时,您会期望返回一个Enum实例。也就是说,foo=ColorEnum(bar)
以foo
作为None
结尾将是非常令人惊讶的行为。您不应该期望ColorEnum(…)
返回None
,您应该直接将None
分配给foo
。例如:foo=bar和ColorEnum(bar)
或尝试:foo=ColorEnum(bar)除了ValueError:foo=None
。当您实例化Enum
时,您会期望返回一个Enum实例。也就是说,foo=ColorEnum(bar)
以foo
作为None
结尾将是非常令人惊讶的行为。您不应该期望ColorEnum(…)
返回None
,您应该直接将None
分配给foo
。例如:foo=bar和ColorEnum(bar)
或尝试:foo=ColorEnum(bar),除了value错误:foo=None
。