7

can somebody describe the following exception? What is the "object layout" and how it is defined? Thanks

Traceback (most recent call last):
  File "test_gui.py", line 5, in <module>
    suite = AlgorithmEngine('gui_suite')
  File "/home/honza/Research/Voiar/algorithm.py", line 169, in __init__
    self.algorithms = self._initAlgorithms()
  File "/home/honza/Research/Voiar/algorithm.py", line 232, in _initAlgorithms
    self._initGUIAlgorithm(obj)
  File "/home/honza/Research/Voiar/algorithm.py", line 218, in _initGUIAlgorithm
    cls.__bases__ = bases
TypeError: __bases__ assignment: 'QWidget' object layout differs from 'GUIAlgorithm'
honzas
  • 1,761
  • 4
  • 16
  • 26

2 Answers2

7

It means that you tried to change the type of an object (by assigning to __bases__) and the new type wasn't compatible with the old one. This happens when the underlying C data structure isn't the same for both types.

See http://www.mail-archive.com/python-list@python.org/msg52950.html for a list of differences between types that might trigger this exception.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • 2
    It is slightly disappointing how many caveats in general one runs into when going into the (exposed) internals of Python--the consistency and cleanness of Python just breaks down there. This here is just one example. – Erik Kaplun Aug 13 '12 at 19:57
  • 2
    @ErikKaplun replacing `__bases__` on the fly is hackery and cannot therefore contribute to the overall impression of a language :) – kolypto Aug 29 '20 at 17:46
  • Gonna add going from a pure Python class at the start to adding in `collections.abc.Mapping` in the hierarchy to assist with making my original `__class__` behave more like a dict. Assigning to `self.__class__` (again a pure Python class) then give this exact error. Take away subclassing `collections.abc.Mapping` and error goes away. – JL Peyret Oct 01 '21 at 05:28
0

In my case, the error occurred when I tried to use change the __class__ of an object that also has __slots__, like this:

class Base:
    __slots__ = ('a', 'b', 'c')

class Child(Base):
    pass


obj = Base()
obj.__class__ = Child  
# -> TypeError: __class__ assignment: 'Child' object layout differs from 'Base'
kolypto
  • 31,774
  • 17
  • 105
  • 99