Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多重继承元类冲突_Python_Python 3.x_Pyqt_Pyqt5_Multiple Inheritance - Fatal编程技术网

Python 多重继承元类冲突

Python 多重继承元类冲突,python,python-3.x,pyqt,pyqt5,multiple-inheritance,Python,Python 3.x,Pyqt,Pyqt5,Multiple Inheritance,我需要一个类的双重继承。 我尝试了几种语法,但我不理解元类的概念 from PyQt5.QtGui import QStandardItem from configparser import ConfigParser class FinalClass(ConfigParser, QStandardItem): def __init__(self, param): ConfigParser.__init__(self) QStandardItem.__in

我需要一个类的双重继承。 我尝试了几种语法,但我不理解元类的概念

from PyQt5.QtGui import QStandardItem
from configparser import ConfigParser

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

您案例中的问题是,您尝试从中继承的类具有不同的元类:

>>> type(QStandardItem)
<class 'sip.wrappertype'> 
>>> type(ConfigParser)
<class 'abc.ABCMeta'>
如果你想要更详细的描述,这是一个好的开始


然而,我并不认为在这种情况下使用多重继承是一个好主意,特别是将多重继承与QObject一起使用可能会很棘手。也许最好将ConfigParser对象存储为实例变量,并在需要时使用它。

这里没有元类。你面临什么问题?@MartijnPieters-他可能指的是该类定义将产生的错误:
TypeError:metaclass冲突:派生类的元类必须是其所有基元类的(非严格)子类
@mata:ah,因此
QStandardItem
可能使用元类。这里真正的问题是,为什么OP试图将
QStandardItem
ConfigParser
混合在一个类中。我需要这个,因为我使用QStandardItemModel和QTableView。每一行都是FinalClass的一个实例,每一个实例都是一个文件。@Mauricio。这不是使用多重继承的好理由。只需将
FinalClass
作为
QStandardItem
的子类,然后委托给
ConfigParser
的内部实例即可。或者,更好的是,忘记使用标准项,而是使用Qt的模型/视图体系结构来创建一个新的项目。非常感谢您的解释和可行的答案。仅与个人物品一起使用,以确保安全。你说得对,我将选择ConfigParser的一个实例,否则它会引发其他问题。感谢您的简明描述。在我读这篇文章之前,我非常迷茫。在我的环境中(Python 3.7.2+Anaconda+PyQt5 5.12+PyQt5_sip 4.19.14),PyQt5.QtCore导入pyqtWrapperType的
不存在。似乎在某个时候元类从
PyQt5.QtCore.pyqtWrapperType
更改为
sip.wrappertype
。我会更新答案。
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)