1

The code below:

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)

blows with:

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

I don't understand this behavior - how does this make 'a' read only? Can I make this work somehow?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Good find - it does mention that the class variable overrides the slot descriptor but I would like some more insight as to how exactly – Mr_and_Mrs_D Feb 03 '21 at 16:28

0 Answers0