[issue46550] __slots__ updates despite being read-only

Ronald Oussoren report at bugs.python.org
Fri Jan 28 05:47:46 EST 2022


Ronald Oussoren <ronaldoussoren at mac.com> added the comment:

Python's is behaving as expected here (but see below): the slots definition tells the interpreter which attribute names can be set on an instance and "__slots__" is not one of those attributes in your code.  "a.__slots__ += ..." will try to set the "a.__slots__" attribute (see Eryk's message for documentation on this) and that results in the exception you are seeing. 

What surprised me is that A.__slots__ is mutable at all, the value of that attribute during class construction affects the layout of instances and that layout won't change when you change A.__slots__ later on.

That is:

class A:
   __slots__ = ['a']

a = A()
a.a = ... # OK
a.b = ... # raises AttributeError

A.__slots__ = ['b']
a.a = ... # still OK
a.b = ... # still raises AttributeError

I don't know if this should be considered a bug or that this is intended behaviour.

----------
components: +Interpreter Core -Library (Lib)
nosy: +ronaldoussoren
versions: +Python 3.11, Python 3.9 -Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46550>
_______________________________________


More information about the Python-bugs-list mailing list