[Tutor] subtyping builtin type

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Dec 31 22:11:44 CET 2013


On 31/12/2013 17:53, eryksun wrote:
> 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

Not found on the windows CHM file.  Looks like another bug report to 
keep our lazy, bone idle core developers like that Zach what's his name 
busy over the New Year :)

>
> 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.

Thank you for this detailed explanation.

Happy New Year to your good self and everybody else.

Let's hope 2014 is more code, less bugs.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list