New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation for len() fails to mention that it works on sets #63561
Comments
The help text for the len() built-in function says:
This omits to mention that len() works on sets too. I suggest this be changed to:
Similarly, the documentation for len() says:
I suggest this be changed to
(Of course, strictly speaking, len() accepts any object with a __len__ method, but sequences, mappings and sets are the ones that are built-in to the Python core, and so these are the ones it is important to mention in the help and the documentation.) |
"Return the number of items of a container" sounds simple and accurate to me. |
I considered suggesting "container", but the problem is that "container" is used elsewhere to mean "object supporting the 'in' operator" (in particular, collections.abc.Container has a __contains__ method but no __len__ method). The abstract base class for "object with a length" is collections.abc.Sized, but I don't think using the term "sized" would be clear to users. |
Perhaps it would be better to say that "the argument may be any object with a __len__, such as the commonly used Python sequence and container types str, bytes, tuple, list, dict, and set". After all, there are other built in types it works on as well: bytearray, frozenset, memoryview. For the other, "the number of items in a sequence or container type" would mostly cover it, but at the cost of being a bit obscure. Perhaps that's OK for the help text, though, since it is supposed to be a reminder, not the full documentation. Another even more obscure alternative would be "the number of items in a Sized object", which would also be more accurate (since an object with a __len__ doesn't *have* to conform fully to the sequence or container ABCs...nor does a container *have* to implement __len__). |
__len__ is an implementation detail for experts. Beginners don't need |
I thought we were talking about the reference guide, not the tutorial? |
Well, the patch is for the builtins documentation as well as len.__doc__. |
I agree this would be best. |
I would prefer 'collections with a known size' but 'collections' should be good enough for the doc string. The manual can follow with examples. |
I also prefer collection. |
I think it's better to do: While it's true that most sequences are clearly containers (e.g. lists), the same is not so evident for other types like bytes or strings. |
Here's a revised patch using Ezio's suggestion ("Return the number of items of a sequence or container"). |
My objection to 'container' is that it is inaccurate and leads to inaccurate mental models. A set is like a non-exclusive club or association, defined either by rule or roster, not like a box or room, which contain exclusively. I am 'in' the set Python Developers, but am not contained by it. Some decades ago I was hindered by the notion that a set is like a box (container). A web search indicates that the top hits all have variations on 'well-defined, unordered *collection* of objects, considered as an object in itself' -- wikipedia, mathisfun, wikia, brittanica, math.ku.edu. We do a disservice to call a set a container. It is true that many Python collections are implemented by containing references to objects (a roster) but ranges are not (a parameterized rule). The *collections* module is properly named. |
Here's a revised patch for Terry ("Return the number of items of a sequence or collection.") |
This is a very simple docs patch so could we have it committed please? |
I'll apply this (if only to bring this vacuous discussion to a close). |
Raymond, I was planning to do this today along with other small patches (already done). Just say so and I will take it. |
Thanks Terry. |
New changeset 95d487abbfd8 by Terry Jan Reedy in branch '2.7': New changeset 8fcbe41e1242 by Terry Jan Reedy in branch '3.4': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: