下面的代码:
class A(object):
__slots__ = 'a'
def __init__(self, a):
self.a = a
print (A(4).a) # 4
class B(A):
a = 4
__slots__ = 'b'
B(4)
打击:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 4, in __init__
AttributeError: 'B' object attribute 'a' is read-only
我不明白这种行为 - 这如何使 'a' 只读?我能以某种方式完成这项工作吗?