Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ref / slicings.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ref.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ref.html" title='Python Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="calls.html" />
<link rel="prev" href="subscriptions.html" />
<link rel="parent" href="primaries.html" />
<link rel="next" href="calls.html" />
<meta name='aesop' content='information' />
<title>5.3.3 Slicings</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.3.2 Subscriptions"
href="subscriptions.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5.3 Primaries"
href="primaries.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.3.4 Calls"
href="calls.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="subscriptions.html">5.3.2 Subscriptions</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="primaries.html">5.3 Primaries</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="calls.html">5.3.4 Calls</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION007330000000000000000"></A><A NAME="slicings"></A>
<a id='l2h-378' xml:id='l2h-378'></a>
<BR>
5.3.3 Slicings
</H2>
<P>
A slicing selects a range of items in a sequence object (e.g., a
string, tuple or list). Slicings may be used as expressions or as
targets in assignment or del statements. The syntax for a slicing:
<a id='l2h-379' xml:id='l2h-379'></a><a id='l2h-380' xml:id='l2h-380'></a><a id='l2h-381' xml:id='l2h-381'></a><a id='l2h-382' xml:id='l2h-382'></a>
<P>
<dl><dd class="grammar">
<div class="productions">
<table>
<tr>
<td><a id='tok-slicing' xml:id='tok-slicing'>slicing</a></td>
<td>::=</td>
<td><a class='grammartoken' href="slicings.html#tok-simple_slicing">simple_slicing</a> | <a class='grammartoken' href="slicings.html#tok-extended_slicing">extended_slicing</a></td></tr>
<tr>
<td><a id='tok-simple_slicing' xml:id='tok-simple_slicing'>simple_slicing</a></td>
<td>::=</td>
<td><a class='grammartoken' href="primaries.html#tok-primary">primary</a> "[" <a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> "]"</td></tr>
<tr>
<td><a id='tok-extended_slicing' xml:id='tok-extended_slicing'>extended_slicing</a></td>
<td>::=</td>
<td><a class='grammartoken' href="primaries.html#tok-primary">primary</a> "[" <a class='grammartoken' href="slicings.html#tok-slice_list">slice_list</a> "]" </td></tr>
<tr>
<td><a id='tok-slice_list' xml:id='tok-slice_list'>slice_list</a></td>
<td>::=</td>
<td><a class='grammartoken' href="slicings.html#tok-slice_item">slice_item</a> ("," <a class='grammartoken' href="slicings.html#tok-slice_item">slice_item</a>)* [","]</td></tr>
<tr>
<td><a id='tok-slice_item' xml:id='tok-slice_item'>slice_item</a></td>
<td>::=</td>
<td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a> | <a class='grammartoken' href="slicings.html#tok-proper_slice">proper_slice</a> | <a class='grammartoken' href="slicings.html#tok-ellipsis">ellipsis</a></td></tr>
<tr>
<td><a id='tok-proper_slice' xml:id='tok-proper_slice'>proper_slice</a></td>
<td>::=</td>
<td><a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> | <a class='grammartoken' href="slicings.html#tok-long_slice">long_slice</a></td></tr>
<tr>
<td><a id='tok-short_slice' xml:id='tok-short_slice'>short_slice</a></td>
<td>::=</td>
<td>[<a class='grammartoken' href="slicings.html#tok-lower_bound">lower_bound</a>] ":" [<a class='grammartoken' href="slicings.html#tok-upper_bound">upper_bound</a>]</td></tr>
<tr>
<td><a id='tok-long_slice' xml:id='tok-long_slice'>long_slice</a></td>
<td>::=</td>
<td><a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> ":" [<a class='grammartoken' href="slicings.html#tok-stride">stride</a>]</td></tr>
<tr>
<td><a id='tok-lower_bound' xml:id='tok-lower_bound'>lower_bound</a></td>
<td>::=</td>
<td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr>
<tr>
<td><a id='tok-upper_bound' xml:id='tok-upper_bound'>upper_bound</a></td>
<td>::=</td>
<td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr>
<tr>
<td><a id='tok-stride' xml:id='tok-stride'>stride</a></td>
<td>::=</td>
<td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr>
<tr>
<td><a id='tok-ellipsis' xml:id='tok-ellipsis'>ellipsis</a></td>
<td>::=</td>
<td>"..."</td></tr>
</table>
</div>
<a class="grammar-footer"
href="grammar.txt" type="text/plain"
>Download entire grammar as text.</a>
</dd></dl>
<P>
There is ambiguity in the formal syntax here: anything that looks like
an expression list also looks like a slice list, so any subscription
can be interpreted as a slicing. Rather than further complicating the
syntax, this is disambiguated by defining that in this case the
interpretation as a subscription takes priority over the
interpretation as a slicing (this is the case if the slice list
contains no proper slice nor ellipses). Similarly, when the slice
list has exactly one short slice and no trailing comma, the
interpretation as a simple slicing takes priority over that as an
extended slicing.<a id='l2h-383' xml:id='l2h-383'></a>
<P>
The semantics for a simple slicing are as follows. The primary must
evaluate to a sequence object. The lower and upper bound expressions,
if present, must evaluate to plain integers; defaults are zero and the
<code>sys.maxint</code>, respectively. If either bound is negative, the
sequence's length is added to it. The slicing now selects all items
with index <var>k</var> such that
<code><var>i</var> &lt;= <var>k</var> &lt; <var>j</var></code> where <var>i</var>
and <var>j</var> are the specified lower and upper bounds. This may be an
empty sequence. It is not an error if <var>i</var> or <var>j</var> lie outside the
range of valid indexes (such items don't exist so they aren't
selected).
<P>
The semantics for an extended slicing are as follows. The primary
must evaluate to a mapping object, and it is indexed with a key that
is constructed from the slice list, as follows. If the slice list
contains at least one comma, the key is a tuple containing the
conversion of the slice items; otherwise, the conversion of the lone
slice item is the key. The conversion of a slice item that is an
expression is that expression. The conversion of an ellipsis slice
item is the built-in <code>Ellipsis</code> object. The conversion of a
proper slice is a slice object (see section <A href="types.html#types">3.2</A>) whose
<tt class="member">start</tt>, <tt class="member">stop</tt> and <tt class="member">step</tt> attributes are the
values of the expressions given as lower bound, upper bound and
stride, respectively, substituting <code>None</code> for missing
expressions.
<a id='l2h-385' xml:id='l2h-385'></a>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.3.2 Subscriptions"
href="subscriptions.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5.3 Primaries"
href="primaries.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.3.4 Calls"
href="calls.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="subscriptions.html">5.3.2 Subscriptions</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="primaries.html">5.3 Primaries</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="calls.html">5.3.4 Calls</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>