Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. I get an error on Multiple inheritance

All I am trying to do is inherit from two different classes.

from traits.api import HasTraits
from PyQt4 import QtCore, QtGui, uic
class Main_Excel_Class(HasTraits,QtGui.QMainWindow):
   pass

I had the "metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"

error initially. But I resolved it by putting in a __metaclass__ attribute:

from traits.api import HasTraits
from PyQt4 import QtCore, QtGui, uic

class Main_Excel_Class_Meta(type(HasTraits), type(QtGui.QMainWindow)):
     pass   

class Main_Excel_Class(HasTraits,QtGui.QMainWindow):
      __metaclass__ = Main_Excel_Class_Meta

But now I end up getting the

"TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict"

error. I tried looking into other similar questions but i honestly did not understand much.Any insights as to how to approach to solve this problem would be very much appreciated. Thankyou

like image 841
iLovePython Avatar asked May 29 '26 10:05

iLovePython


1 Answers

I finally resolved it with a little research. Apparently the error had to do something with the __slots__ attribute's conflicts which is used when allocating heap memory for the new type.Here are the changes I made :

from traits.api import HasTraits
from PyQt4 import QtCore, QtGui, uic

class Main_Excel_Class_Meta(type(HasTraits), type(QtGui.QMainWindow)):
 pass   

class HasTraits(QtGui.QMainWindow):
    pass

class Main_Excel_Class(HasTraits):
    __metaclass__ = Main_Excel_Class_Meta

For a better understanding of the working I had suggest you check this post

http://mcjeff.blogspot.in/2009/05/odd-python-errors.html

like image 71
iLovePython Avatar answered May 31 '26 22:05

iLovePython