[Tutor] subtyping builtin type

Zachary Ware zachary.ware+pytut at gmail.com
Tue Dec 31 19:22:41 CET 2013


On Tue, Dec 31, 2013 at 11:53 AM, eryksun <eryksun at gmail.com> wrote:
> Minor correction:
>
> It says str requires empty __slots__, but that's a bug in the docs.
> It's referring to 2.x str. Else this thread wouldn't exist. In 3.x,
> str is basically the unicode type from 2.x. Its  __itemsize__ is 0
> because the character array is allocated separately. Actually in
> CPython 3.3 there's a compact string object (i.e.
> PyCompactUnicodeObject), but that's not used for a subclass. Appending
> new slots to an instance poses no problem for a subclass of str.

It is still true for bytes objects, though.  Thanks for pointing that
out, it is now fixed.

> Regarding __class__ assignment, I'll add that it also breaks if the
> types include a __dict__ or __weakref__ slot in addition to other
> slots.
>
> For example, this works fine:
>
>     class C: __slots__ = '__weakref__',
>     class D: __slots__ = '__weakref__',
>
>     >>> C().__class__ = D
>
> But adding another slot pushes __weakref__ out of its expected
> position in the layout, so the test for a compatible layout fails:
>
>     class C: __slots__ = '__weakref__', 'a'
>     class D: __slots__ = '__weakref__', 'a'
>
>     >>> C().__class__ = D
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     TypeError: __class__ assignment: 'C' object layout differs from 'D'
>
> The layout is identical, but the test it uses can't see that.

Would you mind opening an issue for this?  This looks like it may be
fixable (or the doc should be updated).

-- 
Zach


More information about the Tutor mailing list