-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-138178: Doc: Clarify that range is an immutable sequence #138184
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
Conversation
Doc/library/functions.rst
Outdated
@@ -1736,7 +1736,9 @@ are always available. They are listed here in alphabetical order. | |||
Rather than being a function, :class:`range` is actually an immutable | |||
sequence type, as documented in :ref:`typesseq-range` and :ref:`typesseq`. | |||
|
|||
|
|||
The object returned by :class:`range` is an :term:`iterator` and supports iteration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not totally correct to say "the object returned by range
", because range
itself is the object. Instead, we should say something like "the range
type is an iterator
".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback and corrections!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or along the lines of "an instance of range
is an iterator"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn’t correct - range
isn’t an iterator, it’s a sequence (though it is iterable). Note how you can iterate the same range object multiple times, and next()
fails. There’s a separate range_iterator
type, like list_iterator
etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yeah. Let's just say "range
supports iteration" or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have update with this
The :class:
range type is iterable and supports iteration.
Please let me know , if this needs further improvement .
Doc/library/functions.rst
Outdated
See also :ref:`iterator` for more information about iterators and how to implement | ||
custom iterable classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of iterators throughout the documentation, but this makes it seem like range
is special. I think we should remove this note -- the iterator
term already contains lots of information about iterators.
range()
is an iterator
Doc/library/functions.rst
Outdated
The :class:`range` type is iterable and supports iteration. | ||
.. function:: repr(object) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The :class:`range` type is iterable and supports iteration. | |
.. function:: repr(object) | |
The :class:`range` type is iterable and supports iteration. | |
.. function:: repr(object) |
Please maintain the 2 line gap.
Saying all of "is [a] sequence", "is iterable" and "supports iteration" feels redundant. What about just linking the mention of "sequence" in the previous sentence to the sequence glossary entry, which begins
? Or perhaps say "iterable sequence" if you think the extra emphasis is helpful |
Why add that it "is iterable and supports iteration", especially right after saying that it's a sequence, which already implies that it's iterable? The phrase "is iterable and supports iteration" even seems redundant in itself. What does "is iterable" mean if not "supports iteration"? |
Should I update this and add |
In this version pr looks correct (despite entirely misleading title and description). Yet, I don't this this tiny change makes sense, as all existing three links already have reference to collections.abc.Sequence, which also mention glossary. |
@skirpichev Thank you for clarifying . |
Do whatever you wish, but please correct misleading title/description. |
range()
is an iterator
When we call In [1]: s = set("abc")
In [2]: next(s)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 1
----> 1 next(s)
TypeError: 'set' object is not an iterator
In [3]: iter(s)
Out[3]: <set_iterator at 0x107a4ca40>
In [4]: it = iter(s)
In [5]: next(it)
Out[5]: 'c'
P.S. It can be handy to remember that https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a link to Sequence Types (typeseq). Is it necessary to add a link to the sequence term in glossary?
![]() I agree. ISTM there are too many links and this would confuse the reader. The sequence term doesn't mention range() but it mentions list, str, tuple and bytes. I would rather keep the current wording as I don't think it's necessary to overload the number of definitions. The "Ranges" link says:
In addition, changing it for |
Thank you for your feedback! Please let me know if any further changes are needed. |
I'm in favor of closing this, that is, leave the docs as is. |
(GH#138178)
This PR Clarifies that :class:range is an immutable :term:sequence type.
Please let me know if this fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou!
📚 Documentation preview 📚: https://cpython-previews--138184.org.readthedocs.build/