枚举属性的属性?

发布于 2025-02-12 07:51:23 字数 327 浏览 0 评论 0 原文

我不确定是否可以这样做,但是我想将更多属性保存到枚举属性(例如值)。

示例:

class Unit(Enum):
    mm = "millimeter"

unit.mm.m.value 将返回“毫米”。

现在,我想访问一个简短的版本,如果我可以实施某些东西,那将是很棒的,它会导致 unit.mm.upper_case 返回“毫米”。

我知道我可以创建一个额外的功能,该功能将 unit.mm 作为属性,并返回所需的输出,但是我敢肯定有更好的方法。

I'm not sure, if something like this is possible, but I would like to save more attributes to enum attributes (like value).

Example:

class Unit(Enum):
    mm = "millimeter"

Unit.mm.value would return "millimeter".

Now I would like to access a short version, it would be awesome, if I could implement something, that results in Unit.mm.upper_case returning "Millimeter".

I know I could create an extra function, that takes Unit.mm as an attribute, and returns the desired output, but I'm sure there is a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2025-02-19 07:51:23

如果您可以纯粹从枚举定义的名称和值部分得出其他信息,则可以使用上面的雅克(Jacques)属性或方法方法。

描述了如何覆盖 __新的__ 方法将附加信息附加到每个枚举成员而不会影响能力使用 unit.mm.m.value 获取定义。

>>> class Coordinate(bytes, Enum):
...     """
...     Coordinate with binary codes that can be indexed by the int code.
...     """
...     def __new__(cls, value, label, unit):
...         obj = bytes.__new__(cls, [value])
...         obj._value_ = value
...         obj.label = label
...         obj.unit = unit
...         return obj
...     PX = (0, 'P.X', 'km')
...     PY = (1, 'P.Y', 'km')
...     VX = (2, 'V.X', 'km/s')
...     VY = (3, 'V.Y', 'km/s')
...

>>> print(Coordinate['PY'])
Coordinate.PY

>>> print(Coordinate(3))
Coordinate.VY

>>> print(Coordinate.VY.value)
3

>>> print(Coordinate.VY.unit)
km/s

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 use Unit.mm.value to fetch the definition.

>>> class Coordinate(bytes, Enum):
...     """
...     Coordinate with binary codes that can be indexed by the int code.
...     """
...     def __new__(cls, value, label, unit):
...         obj = bytes.__new__(cls, [value])
...         obj._value_ = value
...         obj.label = label
...         obj.unit = unit
...         return obj
...     PX = (0, 'P.X', 'km')
...     PY = (1, 'P.Y', 'km')
...     VX = (2, 'V.X', 'km/s')
...     VY = (3, 'V.Y', 'km/s')
...

>>> print(Coordinate['PY'])
Coordinate.PY

>>> print(Coordinate(3))
Coordinate.VY

>>> print(Coordinate.VY.value)
3

>>> print(Coordinate.VY.unit)
km/s
笑梦风尘 2025-02-19 07:51:23

您可以在 enum 会员之类的上定义附加属性:

from enum import Enum

class Unit(Enum):
    mm = 'millimeter'

    @property
    def capitalized(self):
        # self is the member here, e.g. Unit.mm
        return self.value.capitalize()

print(Unit.mm.capitalized)

>>>Millimeter

这是文档参考

You can define an additional property on an Enum member like so:

from enum import Enum

class Unit(Enum):
    mm = 'millimeter'

    @property
    def capitalized(self):
        # self is the member here, e.g. Unit.mm
        return self.value.capitalize()

print(Unit.mm.capitalized)

>>>Millimeter

Here is the docs reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文