Multiple inheritance metaclass conflict

Mauricio

I need a double inheritance for a class. I tried several syntaxes but I don't understand the concept of metaclass.

from PyQt5.QtGui import QStandardItem
from configparser import ConfigParser

class FinalClass(ConfigParser, QStandardItem):
    def __init__(self, param):
        ConfigParser.__init__(self)
        QStandardItem.__init__(self)
mata

The problem in your case is that the classes you try to inherit from have different metaclasses:

>>> type(QStandardItem)
<class 'sip.wrappertype'> 
>>> type(ConfigParser)
<class 'abc.ABCMeta'>

Therefore python can't decide which should be the metaclass for the newly created class. In this case, it would have to be a class inheriting from both sip.wrappertype (or PyQt5.QtCore.pyqtWrapperType for older PyQt5 versions) and ABCMeta.

Therefore the metaclass conflict could be resolved by explicitly introducing such a class as metaclass like this:

from PyQt5.QtGui import QStandardItem
from configparser import ConfigParser

class FinalMeta(type(QStandardItem), type(ConfigParser)):
    pass

class FinalClass(ConfigParser, QStandardItem, metaclass=FinalMeta):
    def __init__(self, param):
        ConfigParser.__init__(self)
        QStandardItem.__init__(self)

If you want a more detailed description, this article is a good start.

However I'm not really convinced that using multiple inheritance for this situaction is such a good idea, specially using multiple inheritance together with QObjects can be tricky. Maybe it would be better to just store the ConfigParser object as an instance variable and use that when needed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use multiple inheritance with a metaclass?

From Dev

Inheritance of metaclass

From Dev

Inheritance of metaclass

From Java

Understanding metaclass and inheritance in Python

From Dev

Python metaclass conflict/Type error

From Dev

TypeError: metaclass conflict: the metaclass of a derived. Django 1.8, python 2.7

From Dev

Metaclass configure. Python version conflict

From Dev

Data conflict with inheritance in Javascript

From Dev

Enum inheritance conflict

From Dev

Enum inheritance conflict

From Dev

Why would I prefer metaclass over inheritance from a superclass in Python

From Dev

about Multiple inheritance and virtual inheritance

From Dev

multiple inheritance without virtual inheritance

From Dev

Multiple inheritance or Multiple interfaces

From Dev

inherited property conflict in coredata with inheritance model

From Dev

python 2.7: TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

From Dev

Naming conflict with multiple namespaces?

From Dev

Multiple rsync versions conflict

From Dev

Bootstrap Multiple Class Conflict

From Java

Use multiple conflict_target in ON CONFLICT clause

From Dev

Multiple inheritance of QObject

From Dev

Multiple inheritance workaround in Java?

From Dev

Abstract classes and Multiple Inheritance

From Dev

Imitate multiple inheritance

From Dev

Confused about multiple inheritance

From Dev

Ambiguity with [] operator and multiple inheritance

From Dev

Multiple level inheritance in JPA

From Java

Java Multiple Inheritance

From Dev

About multiple inheritance and ambiguity

Related Related

HotTag

Archive