Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ref / augassign.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="prev" href="assignment.html" />
<link rel="parent" href="assignment.html" />
<link rel="next" href="pass.html" />
<meta name='aesop' content='information' />
<title>6.3.1 Augmented assignment statements </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="6.3 Assignment statements"
href="assignment.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="6.3 Assignment statements"
href="assignment.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="6.4 The pass statement"
href="pass.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="assignment.html">6.3 Assignment statements</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="assignment.html">6.3 Assignment statements</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pass.html">6.4 The pass statement</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION008310000000000000000"></A><A NAME="augassign"></A>
<BR>
6.3.1 Augmented assignment statements
</H2>
<P>
Augmented assignment is the combination, in a single statement, of a binary
operation and an assignment statement:
<a id='l2h-484' xml:id='l2h-484'></a><a id='l2h-485' xml:id='l2h-485'></a>
<P>
<dl><dd class="grammar">
<div class="productions">
<table>
<tr>
<td><a id='tok-augmented_assignment_stmt' xml:id='tok-augmented_assignment_stmt'>augmented_assignment_stmt</a></td>
<td>::=</td>
<td><a class='grammartoken' href="assignment.html#tok-target">target</a> <a class='grammartoken' href="augassign.html#tok-augop">augop</a> <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a></td></tr>
<tr>
<td><a id='tok-augop' xml:id='tok-augop'>augop</a></td>
<td>::=</td>
<td>"+=" | "-=" | "*=" | "/=" | "%=" | "**="</td></tr>
<tr>
<td></td>
<td></td>
<td><code>| "&gt;&gt;=" | "&lt;&lt;=" | "&amp;=" | "^=" | "|="</code></td></tr>
</table>
</div>
<a class="grammar-footer"
href="grammar.txt" type="text/plain"
>Download entire grammar as text.</a>
</dd></dl>
<P>
(See section&nbsp;<A href="primaries.html#primaries">5.3</A> for the syntax definitions for the last
three symbols.)
<P>
An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression
list, performs the binary operation specific to the type of assignment
on the two operands, and assigns the result to the original
target. The target is only evaluated once.
<P>
An augmented assignment expression like <code>x += 1</code> can be rewritten as
<code>x = x + 1</code> to achieve a similar, but not exactly equal effect. In the
augmented version, <code>x</code> is only evaluated once. Also, when possible, the
actual operation is performed <em>in-place</em>, meaning that rather than
creating a new object and assigning that to the target, the old object is
modified instead.
<P>
With the exception of assigning to tuples and multiple targets in a single
statement, the assignment done by augmented assignment statements is handled
the same way as normal assignments. Similarly, with the exception of the
possible <em>in-place</em> behavior, the binary operation performed by
augmented assignment is the same as the normal binary operations.
<P>
For targets which are attribute references, the initial value is
retrieved with a <tt class="method">getattr()</tt> and the result is assigned with a
<tt class="method">setattr()</tt>. Notice that the two methods do not necessarily
refer to the same variable. When <tt class="method">getattr()</tt> refers to a class
variable, <tt class="method">setattr()</tt> still writes to an instance variable.
For example:
<P>
<div class="verbatim"><pre>
class A:
x = 3 # class variable
a = A()
a.x += 1 # writes a.x as 4 leaving A.x as 3
</pre></div>
<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="6.3 Assignment statements"
href="assignment.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="6.3 Assignment statements"
href="assignment.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="6.4 The pass statement"
href="pass.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="assignment.html">6.3 Assignment statements</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="assignment.html">6.3 Assignment statements</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pass.html">6.4 The pass statement</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>