Ronald Oussoren <ronaldousso...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue46550>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to