[New-bugs-announce] [issue35048] Can't reassign __class__ despite the assigned class having identical slots

Dan Snider report at bugs.python.org
Tue Oct 23 06:07:18 EDT 2018


New submission from Dan Snider <mr.assume.away at gmail.com>:

>>> class a(dict): __slots__ = '__dict__', 'x'

>>> class b(dict): __slots__ = '__dict__', 'x'

>>> self = a(); self.__class__ = b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    self=a(); self.__class__ = b
TypeError: __class__ assignment: 'b' object layout differs from 'a'

This always occurs when __dict__ and/or __weakref__ are defined as slots, even when both classes have otherwise identical slots. This behavior appears to contradict what the docs say wrt to __class__ assignment, which is (in its entirety):

"__class__ assignment works only if both classes have the same __slots__. "

Not sure if this is just a case of ambiguous documentation and intentional behavior or not. Since two classes with identical slots will always have identical internal struct layouts, I can't see a reason for this error.

----------
messages: 328296
nosy: bup
priority: normal
severity: normal
status: open
title: Can't reassign __class__ despite the assigned class having identical slots
versions: Python 3.7

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


More information about the New-bugs-announce mailing list