Minimal repro to illustrate my question:
class A(int):
pass
class B(int):
pass
a = A(1)
a.__class__ = B
results in TypeError: __class__ assignment: 'B' object layout differs from 'A'
The very same code for float
won’t throw any error:
class C(float):
pass
class D(float):
pass
c = C(1.2)
c.__class__ = D
I also found out that if I add __slots__ = ()
attribute to both A
and B
definitions no errors will appear in the __class__
reassignment.
Is this different behavior for int
and float
an expected one?
P.S. Real code is in the autoreload extension for jupyter, where it tries to reload the changed class and if it’s a child of int
it throws this error.
Python version 3.10.8.