EDIT : People saying its a duplicate of Python: How does inheritance of __slots__ in subclasses actually work?. It is not. The accepted answer of the the above question doesn't even help or make me understand it a bit. But the answer I accepted, says that Usage of __slots__? has the answer I want inside
it, which could be true.
While coding the following block got me stuck. Is there any place where Python ignores __slots__
?
Assume a demo code as follows. (GObject is an abstract object which is used in making Gtk widgets.)
class A:
__slots__ = ("x", "y")
def __init__(self, x, y):
self.x = x
self.y = y
class B(A):
__slots__ = ("z",)
def __init__(self, x, y, z):
super().__init__(x, y)
self.z = z
class C(A):
__slots__ = ("w",)
def __init__(self, x, y, z):
super().__init__(x, y)
self.z = z
class D(GObject.Object):
__slots__ = ("w",)
def __init__(self, z):
super().__init__()
self.z = z
b = B(1, 2, 3)
#c = C(1, 2, 3) # Results in AttributeError: 'C' object has no attribute 'z'
d = D(10) # No Error! ^^
#b.p = 3 # AttributeError
d.p = 3 # No Error ^*
Please explain the reason why D
doesn't get any AttributeError
.