Override a sloted attribute with a class variable in python

27 Views Asked by At

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?

0

There are 0 best solutions below