This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add empty __slots__ to collections.abc abstract base classes
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: daniel.urban, eric.araujo, giampaolo.rodola, gvanrossum, ncoghlan, python-dev, rhettinger, stutzbach
Priority: normal Keywords: patch

Created on 2011-02-26 12:24 by daniel.urban, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
collections_abc_slots.diff daniel.urban, 2011-02-26 12:24 Patch (py3k branch) review
Messages (8)
msg129534 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-02-26 12:24
Currently instances of classes which inherit an ABC in collections.abc will have a __dict__.  This can be a problem for example a tree-like data structure. It would make sense to inherit for example MutableMapping, but that would possibly mean, that every node in the tree would have a __dict__, which is probably a waste of memory.

A workaround for this problem is not inheriting the ABC, and using ABCMeta.register (and optionally adding the mixin methods explicitly), but this feels like a hack.

The attached patch adds an empty __slots__ to the ABCs in collections.abc.  I excluded the mapping views (MappingView, KeysView, ItemsView and ValuesView), because they can't have an empty __slots__, and I think using a nonempty __slots__ possibly can cause problems in some cases with multiple inheritance.
msg129570 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2011-02-26 16:31
+1.  I've bumped into exactly this problem (https://github.com/DanielStutzbach/blist/issues/closed#issue/29)

I'm not intimately familiar with how __slots__ works.  Are there any drawbacks to adding an empty __slots__ to the ABCs?
msg129578 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-02-26 17:31
Guido?
msg129625 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-02-27 13:57
I like the idea, and it seems to work as expected (i.e. an empty __slots__ doesn't conflict with inheritance from a C defined type or a type with non-empty __slots__).

However, __slots__ is one of the sections of the type machinery I'm least familiar with, so hopefully Guido will weigh in.
msg129641 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2011-02-27 17:57
I think the idea is reasonable. (I haven't checked the patch.)
msg129661 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-02-27 22:00
It looks like everyone is in favor of the change.  I'll apply the patch after adding some comments and tests.
msg131763 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-22 18:46
New changeset d50a71994f51 by Raymond Hettinger in branch 'default':
Issue #11333: Add __slots__ to the collections ABCs.
http://hg.python.org/cpython/rev/d50a71994f51
msg131764 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-22 18:47
Thanks Daniel.
History
Date User Action Args
2022-04-11 14:57:13adminsetgithub: 55542
2011-03-22 18:47:12rhettingersetstatus: open -> closed

messages: + msg131764
resolution: accepted
nosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban, python-dev
2011-03-22 18:46:35python-devsetnosy: + python-dev
messages: + msg131763
2011-02-27 22:58:43rhettingersetfiles: - annotations_workaround.py
nosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban
2011-02-27 22:58:34rhettingersetnosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban
messages: - msg129664
2011-02-27 22:58:10rhettingersetfiles: + annotations_workaround.py
nosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban
messages: + msg129664
2011-02-27 22:00:06rhettingersetassignee: ncoghlan -> rhettinger
messages: + msg129661
nosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban
2011-02-27 17:57:01gvanrossumsetassignee: gvanrossum -> ncoghlan
messages: + msg129641
nosy: gvanrossum, rhettinger, ncoghlan, giampaolo.rodola, stutzbach, eric.araujo, daniel.urban
2011-02-27 13:57:06ncoghlansetnosy: + ncoghlan
messages: + msg129625
2011-02-26 17:31:49rhettingersetassignee: gvanrossum

messages: + msg129578
nosy: + gvanrossum
2011-02-26 16:36:32eric.araujosetnosy: + eric.araujo
2011-02-26 16:31:07stutzbachsetnosy: rhettinger, giampaolo.rodola, stutzbach, daniel.urban
messages: + msg129570
2011-02-26 13:58:20giampaolo.rodolasetnosy: + giampaolo.rodola
2011-02-26 12:24:33daniel.urbancreate