[docs] [issue25866] Reference 3. Data Model: miscellaneous minor cleanups on the word "sequence".

Andrew Barnert report at bugs.python.org
Tue Dec 15 03:12:47 EST 2015


New submission from Andrew Barnert:

None of the below are very serious or likely to mislead anyone using or implementing Python, but...

---

3.3.2. Customizing attribute access

The docs for `__dir__` say:

> A sequence must be returned. dir() converts the returned sequence to a list and sorts it.

At least in CPython and PyPy, this isn't enforced; any iterable can be returned (and it's then converted to a list and sorted). You can even make `__dir__` a generator function. (I've never seen that in the wild--but I have seen code that returns a `map` iterator.)

I think it would be better to say "An iterable must be returned. dir() converts the returned iterable to a list and sorts it."

---

3.3.2.3. __slots__

> This class variable can be assigned a string, iterable, or sequence of strings...

While it's true that you can assign any iterable, it's at best misleading to assign an iterator. For example, if you use `__slots__ = iter('abc')`, you will get a class with descriptors named `a`, `b`, and `c`, but with an empty iterator in `__slots__`. There's probably no reason to actually outlaw that, but it might be worth noting in 3.3.2.3.1 along with the other weird possibilities like assigning a mapping.

---

3.3.6. Emulating container types

The docs still say that the ABCs are in `collections` rather than `collections.abc`.

> ... for mappings, __iter__() should be the same as keys()

No; `__iter__()` should be an iterator over the keys; `keys()` _can_ be an iterator, but generally should be a view instead.

> The membership test operators (in and not in) are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence.

I don't think this should say "through a sequence". They're implemented as iteration through whatever the container is, whether it's a sequence, a set, a linked list, or anything else. The documentation on `__contains__` immediately below clarifies this, but it's probably better to be clear from the start.

----------
assignee: docs at python
components: Documentation
messages: 256445
nosy: abarnert, docs at python
priority: normal
severity: normal
status: open
title: Reference 3. Data Model: miscellaneous minor cleanups on the word "sequence".
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25866>
_______________________________________


More information about the docs mailing list