有没有办法让自定义元类与enum34包(Python 3之前版)配合使用?
import enum
import six
class MyMeta(type):
def __new__(cls, class_name, bases, class_dict):
print("Does something useful.")
return super(MyMeta, cls).__new__(cls, class_name, bases, class_dict)
@six.add_metaclass(MyMeta)
class MyClass(object):
def __init__(self, *args, **kwargs):
pass
MyEnum = enum.Enum('MyEnum', [('One', 1), ('Two', 2), ('Three', 3)], type=MyClass)
以上内容目前在enum_class = super(EnumMeta, metacls).__new__(metacls, cls, bases, classdict)
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
答案 0 :(得分:1)
您应该从MyMeta
的元类派生enum.Enum
:
class MyMeta(type(enum.Enum)):
...
答案 1 :(得分:1)
而不是enum.Enum。你应该只写Enum。那个问题就解决了。
例如:
class MyEnumClass(Enum):
........