Skip to content

Conversation

@encukou
Copy link
Member

@encukou encukou commented Nov 26, 2025

This merges the Subscriptions and Slicings sections in the language reference. From the parser's point of view, slicing is a special case of subscription, and it makes sense to document it that way too.

The PR also touches Data model documentation: I've tried to move details of the runtime behaviour there, and to de-duplicate (and cross-link) as needed.
Similar for the slice attribute docs.

Todo:

  • Add a glossary entry for subscript -- the term this uses consistently for the key/index/whatever in the square brackets.

📚 Documentation preview 📚: https://cpython-previews--141985.org.readthedocs.build/

encukou and others added 7 commits September 10, 2025 17:59
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Blaise Pabon <blaise@gmail.com>
Comment on lines +1075 to +1078
>>> demo[1, 2, 3]
subscripted with (1, 2, 3)
>>> demo[1:2, 3]
subscripted with (slice(1, 2, None), 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find using examples that require bits from several examples ago and are in a different section a tad odd. We are also missing colons.

by a comma, to specify a one-element tuple::

>>> demo['spam',]
subscripted with ('spam',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above.

>>> number_names[:]
['zero', 'one', 'two']
>>> number_names[::2]
['zero', 'two', 'four']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an example of a negative index here (the more the merrier ;-)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And deletion perhaps? :-3

Suggested change
['zero', 'two', 'four']
['zero', 'two', 'four']
>>> number_names[:-3]
['zero', 'one', 'two']
>>> del number_names[4:]
>>> number_names
['zero', 'one', 'two', 'three']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And deletion perhaps? :-3

My previous comment still applies :-)

Comment on lines 935 to 936
Subscriptions and slicing
-------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Subscriptions and slicing
-------------------------
Subscription and slicing
------------------------

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other section names are plural¹. What do you think about continuing that?

Suggested change
Subscriptions and slicing
-------------------------
Subscriptions and slicings
--------------------------

¹ except about two, let's fix them when their time comes?

Comment on lines 1069 to 1070
Tuple subscription
^^^^^^^^^^^^^^^^^^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this sounds like a section about subscripting tuples specifically. Maybe "Comma-separated subscription" instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tuple subscription
^^^^^^^^^^^^^^^^^^
Comma-separated subscripts
^^^^^^^^^^^^^^^^^^^^^^^^^^

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Copy link
Member Author

@encukou encukou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for going through this!

Comment on lines 935 to 936
Subscriptions and slicing
-------------------------
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other section names are plural¹. What do you think about continuing that?

Suggested change
Subscriptions and slicing
-------------------------
Subscriptions and slicings
--------------------------

¹ except about two, let's fix them when their time comes?

>>> number_names[:]
['zero', 'one', 'two']
>>> number_names[::2]
['zero', 'two', 'four']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And deletion perhaps? :-3

Suggested change
['zero', 'two', 'four']
['zero', 'two', 'four']
>>> number_names[:-3]
['zero', 'one', 'two']
>>> del number_names[4:]
>>> number_names
['zero', 'one', 'two', 'three']

Comment on lines 1069 to 1070
Tuple subscription
^^^^^^^^^^^^^^^^^^
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tuple subscription
^^^^^^^^^^^^^^^^^^
Comma-separated subscripts
^^^^^^^^^^^^^^^^^^^^^^^^^^

Comment on lines +1075 to +1078
>>> demo[1, 2, 3]
subscripted with (1, 2, 3)
>>> demo[1:2, 3]
subscripted with (slice(1, 2, None), 3)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find using examples that require bits from several examples ago and are in a different section a tad odd.

True, but the SubscriptionDemo is a bit too big to define every time it's used. Would the following work? (added whenever demo is first used in a section).

Suggested change
>>> demo[1, 2, 3]
subscripted with (1, 2, 3)
>>> demo[1:2, 3]
subscripted with (slice(1, 2, None), 3)
# continuing with the SubscriptionDemo instance defined above:
>>> demo[1, 2, 3]
subscripted with (1, 2, 3)
>>> demo[1:2, 3]
subscripted with (slice(1, 2, None), 3)

We are also missing colons.

What do you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Like in my above comments, the demo actually prints subscripted with: and not subscripted with .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but the SubscriptionDemo is a bit too big to define every time it's used. Would the following work? (added whenever demo is first used in a section).

I agree, this is also a bit like the issue with imports. The comment is an improvement, although still not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants