枚举属性的属性?
我不确定是否可以这样做,但是我想将更多属性保存到枚举属性(例如值)。
示例:
class Unit(Enum):
mm = "millimeter"
unit.mm.m.value
将返回“毫米”。
现在,我想访问一个简短的版本,如果我可以实施某些东西,那将是很棒的,它会导致 unit.mm.upper_case
返回“毫米”。
我知道我可以创建一个额外的功能,该功能将 unit.mm
作为属性,并返回所需的输出,但是我敢肯定有更好的方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以纯粹从枚举定义的名称和值部分得出其他信息,则可以使用上面的雅克(Jacques)属性或方法方法。
描述了如何覆盖
__新的__
方法将附加信息附加到每个枚举成员而不会影响能力使用unit.mm.m.value
获取定义。if you can derive the additional info purely from the name and value parts of your enum definition, you can use the property or method approach from Jacques above.
There's a second example in the enum module docs that describes how to override the
__new__
method to attach additional info to each enum member, without impacting the ability to useUnit.mm.value
to fetch the definition.您可以在
enum
会员之类的上定义附加属性:这是文档参考。
You can define an additional property on an
Enum
member like so:Here is the docs reference.