python - Adding a property to an Enum
问题描述
I would like to have an Enum
with internal property for each enum value, this property can't be set or initialized by the user, but once the user has created Enum
from a value, he would be able to read (get) this internal property based on the value.
e.g.
class Channels(Enum):
Email = 1, True # True is the value of the internal property for value 1
Sms = 2, True # True is the value of the internal property for value 2
Log = 3, False # False is the value of the internal property for value 3
Problem is that currently in order to create an instance of this class I need to:
x = Channels((1, True))
and that's bad. I want to be able to still create instances like: x = Channels(1)
(i.e. True
is the internal property of 1, it shouldn't be specified by the user).
My second try was:
class Channels2(Enum):
@DynamicClassAttribute
def internal_property(self):
if self.value == 1:
return True
elif self.value == 2:
return True
elif self.value == 3:
return False
Email = 1
Sms = 2
Log = 3
And this seems to be working (x = Channels(1)
is working and x.internal_property
returns True
, as it should).
The problem here is that it feels not so efficient, executing those if statements every time the internal_property
is accessed.. Is there any other way making the Enum
treat internal_property
as a an extra field of the instance (on top of Enum
regular name
and value
fields)?
解决方案
您可以使用__new__()
此处记录的方法来自定义枚举对象的值。例如,您可以使用原始定义,例如:
from enum import Enum
class Channels(Enum):
def __new__(cls, value, internal_property):
obj = object.__new__(cls)
obj._value_ = value
obj.internal_property = internal_property
return obj
email = 1, True
sms = 2, True
log = 3, False
print(Channels.email)
# Coordinate.email
print(Channels.email.value)
# 1
print(Channels(3))
# Coordinate.log
print(Channels(3).internal_property)
# False
print(Channels(1).internal_property)
#True
推荐阅读
- python - 可以使用 tf.concat 获得组合的元组张量吗?
- python - BeautifulSoup - 突出显示其他文本中的锚文本
- owl - OWL DL:我可以让一个班级和一个人拥有相同的 IRI 吗?
- swift - 如何使用 Realm (Swift) 在对象中添加和更新值?
- edit - 如果要修改页面中的墨迹笔迹应该如何实现
- haskell - Haskell - 如何在列表理解中使用 if then else?
- jquery - 按钮放大不起作用
- c# - 用键填充字典(2)以匹配字典(1)
- python - 如何在 asyncio 中使用阻塞函数
- java - 是否有用于检索给定 SQLCODE 的完整错误消息信息的 Db2 标量函数?