[Tutor] subtyping builtin type

eryksun eryksun at gmail.com
Tue Dec 31 18:53:12 CET 2013


On Tue, Dec 31, 2013 at 11:21 AM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> The glossary entry for __slots__ states "A declaration inside a class that
> saves memory by pre-declaring space for instance attributes and eliminating
> instance dictionaries. Though popular, the technique is somewhat tricky to
> get right and is best reserved for rare cases where there are large numbers
> of instances in a memory-critical application."  I'll admit that I really
> don't understand what's tricky about it, can someone explain please.

Refer to the language reference:

http://docs.python.org/3/reference/datamodel.html#notes-on-using-slots

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.

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.


More information about the Tutor mailing list