编辑:人们说它是
Python: How does inheritance of __slots__ in subclasses actually work?
是的。不是的。上述问题的公认答案甚至都无助于或使我有点理解。但我接受的答案是
Usage of __slots__?
有我想要的答案
inside
可能是真的。
在编码下面的代码块时,我卡住了。有没有python忽略的地方
__slots__
是吗?
假设一个演示代码如下。(gobject是一个抽象对象,用于制作gtk小部件。)
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 ^*
请解释原因
D
一点也没有
AttributeError
.