枚举的枚举在Python中? [英] Enum of enums in Python?
问题描述
是否可以在Python中枚举枚举枚举?例如,我想要
enumA
enumB
elementA
elementB
enumC
elementC
elementD
而对于我来说,请参阅 elementA
as enumA.enumB.elementA
,或参考 elementD
as enumA.enumC.elementD
。
这可能吗?如果是这样,那么如何?
编辑:以天真的方式实现:
<$ c从枚举导入枚举$ c $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b class EnumA(枚举):
class EnumB(枚举):
member =
print(EnumA.EnumB.member)
它给出:
<枚举'EnumA'>
追溯(最近的最后一次调用):
文件Maps.py,第15行,位于< module>
print(EnumA.EnumB.member)
AttributeError:'EnumA'对象没有属性'member'
您不能使用枚举
stdlib模块。如果你尝试这样做:
class A(枚举):
/ pre>
class B(枚举):
a = 1
b = 2
class C(枚举):
c = 1
d = 2
ABa
...你会得到一个例外:
AttributeError:'A'对象没有属性'a'
这是因为枚举值
A
像A
的实例一样,不像其值类型的实例。就像一个正常的枚举,持有int
value没有int
方法的值,B
不会有枚举
方法。比较:D类(枚举):
a = 1
b = 2
Dabit_length()
当然,明确地访问基础值(
int
或B
类):Davalue.bit_length()
ABvalue.a
...但我怀疑这是你想要的。
那么,你能使用
IntEnum
使用相同的技巧,将枚举
和int
使其枚举值int
值,如其他部分的文档?
不,因为你会分类什么类型?不
枚举
;这已经是你的类型了。您不能使用类型
(任意类的类型)。没有什么可行的。
所以,你必须使用一个不同的设计的Enum实现来实现这一点。幸运的是,有大约69105个不同的PyPI和ActiveState可供选择。
例如,当我在看建立类似于Swift枚举的东西(比Python / Java / etc。枚举更接近ML ADT),有人建议我看看
makeobj
。我忘了这样做,但现在我只是做了,而且:class A(makeobj.Obj):
B类(makeobj.Obj):
a,b = makeobj.keys(2)
class C(makeobj.Obj):
c,d = makeobj.keys(2)
print(AB,ABb,ABbname,ABbvalue)
这给你: / p>
< Object:B - > [a:0,b:1]> <值:B.b = 1> b 1
如果查看其
__ qualname __
而不是用于创建str / repr值的__ name __
,否则它看起来像你想要的一样。它还有一些其他很酷的功能(不完全是我在寻找,但有趣的...)。Is it possible to have an enum of enums in Python? For example, I'd like to have
enumA enumB elementA elementB enumC elementC elementD
And for me to be able to refer to
elementA
asenumA.enumB.elementA
, or to refer toelementD
asenumA.enumC.elementD
.Is this possible? If so, how?
EDIT: When implemented in the naive way:
from enum import Enum class EnumA(Enum): class EnumB(Enum): member = 0 print(EnumA) print(EnumA.EnumB.member)
It gives:
<enum 'EnumA'> Traceback (most recent call last): File "Maps.py", line 15, in <module> print(EnumA.EnumB.member) AttributeError: 'EnumA' object has no attribute 'member'
解决方案You can't do this with the
enum
stdlib module. If you try it:class A(Enum): class B(Enum): a = 1 b = 2 class C(Enum): c = 1 d = 2 A.B.a
… you'll just get an exception like:
AttributeError: 'A' object has no attribute 'a'
This is because the enumeration values of
A
act like instances ofA
, not like instances of their value type. Just like a normal enum holdingint
values doesn't haveint
methods on the values, theB
won't haveEnum
methods. Compare:class D(Enum): a = 1 b = 2 D.a.bit_length()
You can, of course, access the underlying value (the
int
, or theB
class) explicitly:D.a.value.bit_length() A.B.value.a
… but I doubt that's what you want here.
So, could you use the same trick that
IntEnum
uses, of subclassing bothEnum
andint
so that its enumeration values areint
values, as described in the Others section of the docs?No, because what type would you subclass? Not
Enum
; that's already your type. You can't usetype
(the type of arbitrary classes). There's nothing that works.So, you'd have to use a different Enum implementation with a different design to make this work. Fortunately, there are about 69105 different ones on PyPI and ActiveState to choose from.
For example, when I was looking at building something similar to Swift enumerations (which are closer to ML ADTs than Python/Java/etc. enumerations), someone recommended I look at
makeobj
. I forgot to do so, but now I just did, and:class A(makeobj.Obj): class B(makeobj.Obj): a, b = makeobj.keys(2) class C(makeobj.Obj): c, d = makeobj.keys(2) print(A.B, A.B.b, A.B.b.name, A.B.b.value)
This gives you:
<Object: B -> [a:0, b:1]> <Value: B.b = 1> b 1
It might be nice if it looked at its
__qualname__
instead of its__name__
for creating the str/repr values, but otherwise it looks like it does everything you want. And it has some other cool features (not exactly what I was looking for, but interesting…).这篇关于枚举的枚举在Python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!