From c67a649b3812891b23f9e6468c641808a9b2dfaa Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 19 Aug 2020 14:37:46 +0200 Subject: [PATCH] Explain that slotted classes don't support multiple inheritance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #667 Co-authored-by: Tin Tvrtković --- docs/glossary.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/glossary.rst b/docs/glossary.rst index a355d98bf..72964f351 100644 --- a/docs/glossary.rst +++ b/docs/glossary.rst @@ -10,8 +10,9 @@ Glossary This is the type of class you get by default both with and without ``attrs``. slotted classes - A class that has no ``__dict__`` attribute and `defines `_ its attributes in a ``__slots__`` attribute instead. - In ``attrs``, they are created by passing ``slots=True`` to ``@attr.s``. + A class whose instances have no ``__dict__`` attribute and `define `_ their attributes in a ``__slots__`` attribute instead. + In ``attrs``, they are created by passing ``slots=True`` to ``@attr.s`` (and are on by default in `attr.define`/`attr.mutable`/`attr.frozen`. + Their main advantage is that they use less memory on CPython [#pypy]_. @@ -36,6 +37,8 @@ Glossary - Slotted classes can inherit from other classes just like non-slotted classes, but some of the benefits of slotted classes are lost if you do that. If you must inherit from other classes, try to inherit only from other slotted classes. + - However, `it's not possible `_ to inherit from more than one class that has attributes in ``__slots__`` (you will get an ``TypeError: multiple bases have instance lay-out conflict``). + - Slotted classes must implement :meth:`__getstate__ ` and :meth:`__setstate__ ` to be serializable with `pickle` protocol 0 and 1. Therefore, ``attrs`` creates these methods automatically for ``slots=True`` classes (Python 2 uses protocol 0 by default).