mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	Clarify where support for negative indices fall in the language hierarchy.
This commit is contained in:
		
							parent
							
								
									476a31ef1e
								
							
						
					
					
						commit
						f77c1d6adb
					
				
					 1 changed files with 12 additions and 5 deletions
				
			
		| 
						 | 
					@ -518,11 +518,18 @@ whose value is one of the keys of the mapping, and the subscription selects the
 | 
				
			||||||
value in the mapping that corresponds to that key.  (The expression list is a
 | 
					value in the mapping that corresponds to that key.  (The expression list is a
 | 
				
			||||||
tuple except if it has exactly one item.)
 | 
					tuple except if it has exactly one item.)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
If the primary is a sequence, the expression (list) must evaluate to an integer.
 | 
					If the primary is a sequence, the expression (list) must evaluate to an integer
 | 
				
			||||||
If this value is negative, the length of the sequence is added to it (so that,
 | 
					or a slice (as discussed in the following section).
 | 
				
			||||||
e.g., ``x[-1]`` selects the last item of ``x``.)  The resulting value must be a
 | 
					
 | 
				
			||||||
nonnegative integer less than the number of items in the sequence, and the
 | 
					The formal syntax makes no special provision for negative indices in
 | 
				
			||||||
subscription selects the item whose index is that value (counting from zero).
 | 
					sequences; however, built-in sequences all provide a :meth:`__getitem__`
 | 
				
			||||||
 | 
					method that interprets negative indices by adding the length of the sequence
 | 
				
			||||||
 | 
					to the index (so that ``x[-1]`` selects the last item of ``x``).  The
 | 
				
			||||||
 | 
					resulting value must be a nonnegative integer less than the number of items in
 | 
				
			||||||
 | 
					the sequence, and the subscription selects the item whose index is that value
 | 
				
			||||||
 | 
					(counting from zero). Since the support for negative indices and slicing
 | 
				
			||||||
 | 
					occurs in the object's :meth:`__getitem__` method, subclasses overriding
 | 
				
			||||||
 | 
					this method will need to explicitly add that support.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. index::
 | 
					.. index::
 | 
				
			||||||
   single: character
 | 
					   single: character
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue