- является подклассом типа.
Код: Выделить всё
SubType - Экземпляр SubType является подклассом MyABC.
Ниже приведена структура кода, которую я написал:и результат такой, как показано ниже:Код: Выделить всё
from abc import ABCMeta, abstractmethod class MyABC(metaclass=ABCMeta): @abstractmethod def some_abstract_method(self): raise NotImplementedError def some_mixin_method(self): print('some_mixin_method()') self.some_abstract_method() class SubType(type): _types = dict() def __new__(cls, string): if string in cls._types: # subclass of MyABC with given string has already been made self = cls._types[string] else: # subclass of MyABC with given string has not been made name = f'Class_{string}' bases = (MyABC,) dict_ = { 'string': string, 'some_abstract_method': cls._some_abstract_method } # type(name, bases, dict) => new type instance (class) self = super(SubType, cls).__new__(cls, name, bases, dict_) cls._types[string] = self return self @staticmethod def _some_abstract_method(self): print('some_abstract_method()') print(f'string: {self.string}') Class_Foo = SubType('Foo')Поскольку Class_Foo не является подклассом, а экземпляром SubType, я не думаю, что это относится к метаклассу конфликт, но переводчик думает, что это так. Есть ли здесь кто-нибудь, кто может описать, что не так и как это исправить?Код: Выделить всё
Traceback (most recent call last): File " ", line 1, in Class_Foo = SubType('Foo') File "", line 17, in __new__ self = super(SubType, cls).__new__(cls, name, bases, dict_) TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Подробнее здесь: https://stackoverflow.com/questions/785 ... ss-of-type
`TypeError: конфликт метаклассов` для подкласса `type` ⇐ Python
-
Anonymous
`TypeError: конфликт метаклассов` для подкласса `type`
Я создал абстрактный базовый класс MyABC, метакласс которого, грубо говоря, abc.ABCMeta. Затем я хотел создать класс SubType, экземпляры которого являются подклассом MyABC. Поскольку его экземпляр является классом, я подумал, что было бы хорошо сделать его подклассом типа. То есть