Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="assignment.html" /> | |
13 | <link rel="parent" href="assignment.html" /> | |
14 | <link rel="next" href="pass.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>6.3.1 Augmented assignment statements </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="6.3 Assignment statements" | |
24 | href="assignment.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="6.3 Assignment statements" | |
27 | href="assignment.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="6.4 The pass statement" | |
30 | href="pass.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Reference Manual</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="assignment.html">6.3 Assignment statements</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="assignment.html">6.3 Assignment statements</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="pass.html">6.4 The pass statement</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION008310000000000000000"></A><A NAME="augassign"></A> | |
55 | <BR> | |
56 | 6.3.1 Augmented assignment statements | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | Augmented assignment is the combination, in a single statement, of a binary | |
61 | operation and an assignment statement: | |
62 | <a id='l2h-484' xml:id='l2h-484'></a><a id='l2h-485' xml:id='l2h-485'></a> | |
63 | ||
64 | <P> | |
65 | <dl><dd class="grammar"> | |
66 | <div class="productions"> | |
67 | <table> | |
68 | <tr> | |
69 | <td><a id='tok-augmented_assignment_stmt' xml:id='tok-augmented_assignment_stmt'>augmented_assignment_stmt</a></td> | |
70 | <td>::=</td> | |
71 | <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> | |
72 | <tr> | |
73 | <td><a id='tok-augop' xml:id='tok-augop'>augop</a></td> | |
74 | <td>::=</td> | |
75 | <td>"+=" | "-=" | "*=" | "/=" | "%=" | "**="</td></tr> | |
76 | <tr> | |
77 | <td></td> | |
78 | <td></td> | |
79 | <td><code>| ">>=" | "<<=" | "&=" | "^=" | "|="</code></td></tr> | |
80 | </table> | |
81 | </div> | |
82 | <a class="grammar-footer" | |
83 | href="grammar.txt" type="text/plain" | |
84 | >Download entire grammar as text.</a> | |
85 | </dd></dl> | |
86 | ||
87 | <P> | |
88 | (See section <A href="primaries.html#primaries">5.3</A> for the syntax definitions for the last | |
89 | three symbols.) | |
90 | ||
91 | <P> | |
92 | An augmented assignment evaluates the target (which, unlike normal | |
93 | assignment statements, cannot be an unpacking) and the expression | |
94 | list, performs the binary operation specific to the type of assignment | |
95 | on the two operands, and assigns the result to the original | |
96 | target. The target is only evaluated once. | |
97 | ||
98 | <P> | |
99 | An augmented assignment expression like <code>x += 1</code> can be rewritten as | |
100 | <code>x = x + 1</code> to achieve a similar, but not exactly equal effect. In the | |
101 | augmented version, <code>x</code> is only evaluated once. Also, when possible, the | |
102 | actual operation is performed <em>in-place</em>, meaning that rather than | |
103 | creating a new object and assigning that to the target, the old object is | |
104 | modified instead. | |
105 | ||
106 | <P> | |
107 | With the exception of assigning to tuples and multiple targets in a single | |
108 | statement, the assignment done by augmented assignment statements is handled | |
109 | the same way as normal assignments. Similarly, with the exception of the | |
110 | possible <em>in-place</em> behavior, the binary operation performed by | |
111 | augmented assignment is the same as the normal binary operations. | |
112 | ||
113 | <P> | |
114 | For targets which are attribute references, the initial value is | |
115 | retrieved with a <tt class="method">getattr()</tt> and the result is assigned with a | |
116 | <tt class="method">setattr()</tt>. Notice that the two methods do not necessarily | |
117 | refer to the same variable. When <tt class="method">getattr()</tt> refers to a class | |
118 | variable, <tt class="method">setattr()</tt> still writes to an instance variable. | |
119 | For example: | |
120 | ||
121 | <P> | |
122 | <div class="verbatim"><pre> | |
123 | class A: | |
124 | x = 3 # class variable | |
125 | a = A() | |
126 | a.x += 1 # writes a.x as 4 leaving A.x as 3 | |
127 | </pre></div> | |
128 | ||
129 | <P> | |
130 | ||
131 | <DIV CLASS="navigation"> | |
132 | <div class='online-navigation'> | |
133 | <p></p><hr /> | |
134 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
135 | <tr> | |
136 | <td class='online-navigation'><a rel="prev" title="6.3 Assignment statements" | |
137 | href="assignment.html"><img src='../icons/previous.png' | |
138 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
139 | <td class='online-navigation'><a rel="parent" title="6.3 Assignment statements" | |
140 | href="assignment.html"><img src='../icons/up.png' | |
141 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
142 | <td class='online-navigation'><a rel="next" title="6.4 The pass statement" | |
143 | href="pass.html"><img src='../icons/next.png' | |
144 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
145 | <td align="center" width="100%">Python Reference Manual</td> | |
146 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
147 | href="contents.html"><img src='../icons/contents.png' | |
148 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
149 | <td class='online-navigation'><img src='../icons/blank.png' | |
150 | border='0' height='32' alt='' width='32' /></td> | |
151 | <td class='online-navigation'><a rel="index" title="Index" | |
152 | href="genindex.html"><img src='../icons/index.png' | |
153 | border='0' height='32' alt='Index' width='32' /></A></td> | |
154 | </tr></table> | |
155 | <div class='online-navigation'> | |
156 | <b class="navlabel">Previous:</b> | |
157 | <a class="sectref" rel="prev" href="assignment.html">6.3 Assignment statements</A> | |
158 | <b class="navlabel">Up:</b> | |
159 | <a class="sectref" rel="parent" href="assignment.html">6.3 Assignment statements</A> | |
160 | <b class="navlabel">Next:</b> | |
161 | <a class="sectref" rel="next" href="pass.html">6.4 The pass statement</A> | |
162 | </div> | |
163 | </div> | |
164 | <hr /> | |
165 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
166 | </DIV> | |
167 | <!--End of Navigation Panel--> | |
168 | <ADDRESS> | |
169 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
170 | </ADDRESS> | |
171 | </BODY> | |
172 | </HTML> |