A certain situation in Python recently alarmed me, and its reason is still not completely clear after a little research. The following class definitions appear to work flawlessly and will produce what is intended:
class A: __slots__ = 'a', 'b'
class B(A): __slots__ = ()
class C(A): __slots__ = ()
class D(B, C): __slots__ = ()
These are four classes arranged in a diamond inheritance pattern. However, a somewhat similar pattern is not allowed. The following class definitions seem as though they should function the same as the first:
class B: __slots__ = 'a', 'b'
class C: __slots__ = 'a', 'b'
class D(B, C): __slots__ = ()
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
class D(B, C): __slots__ = ()
TypeError: multiple bases have instance lay-out conflict
However, a TypeError is raised in this example. So three questions arise: (1) Is this a bug in Python, considering the slot names? (2) What would justify such an answer? (3) What is the best workaround?
References:
By forcing a constraint that no class defines __slots__, a special object class could be constructed with the characteristics desired for all child classes. The class is registered as an alias for regular objects.
This code can be used as a building block to make
tkinterthread-safe by cloning its classes. TheAffinityclass automatically ensures that code is executed on a single thread, preventing GUI errors.