【发布时间】:2018-10-13 23:29:02
【问题描述】:
我在 Python 中定义了以下枚举:
class Unit(Enum):
GRAM = ("g")
KILOGRAM = ("kg", GRAM, 1000.0)
def __init__(self, symbol, base_unit = None, multiplier = 1.0):
self.symbol = symbol
self.multiplier = multiplier
self.base_unit = self if base_unit is None else base_unit
我希望如此
print(Unit.GRAM.base_unit)
print(Unit.KILOGRAM.base_unit)
会回来
Unit.GRAM
Unit.GRAM
然而,我得到的却很混乱
Unit.GRAM
g
为什么会这样?
【问题讨论】:
-
Python enum - getting value of enum on string conversion... 的可能重复项...另外,请查看如何在另一个(未接受)答案中表示
str单位 -
这不是那个问题的重复。这个问题是询问在类完全构建之前成员如何相互引用。
标签: python python-3.x