| 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="next" href="pass.html" /> |
| 13 | <link rel="prev" href="assert.html" /> |
| 14 | <link rel="parent" href="simple.html" /> |
| 15 | <link rel="next" href="augassign.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.3 Assignment statements </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.2 Assert statements" |
| 25 | href="assert.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6. Simple statements" |
| 28 | href="simple.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.3.1 Augmented assignment statements" |
| 31 | href="augassign.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="assert.html">6.2 Assert statements</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="augassign.html">6.3.1 Augmented assignment statements</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION008300000000000000000"></A><A NAME="assignment"></A> |
| 56 | <BR> |
| 57 | 6.3 Assignment statements |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | Assignment statements<a id='l2h-465' xml:id='l2h-465'></a> are used to |
| 62 | (re)bind names to values and to modify attributes or items of mutable |
| 63 | objects: |
| 64 | <a id='l2h-466' xml:id='l2h-466'></a><a id='l2h-467' xml:id='l2h-467'></a><a id='l2h-468' xml:id='l2h-468'></a><a id='l2h-469' xml:id='l2h-469'></a> |
| 65 | <P> |
| 66 | <dl><dd class="grammar"> |
| 67 | <div class="productions"> |
| 68 | <table> |
| 69 | <tr> |
| 70 | <td><a id='tok-assignment_stmt' xml:id='tok-assignment_stmt'>assignment_stmt</a></td> |
| 71 | <td>::=</td> |
| 72 | <td>(<a class='grammartoken' href="assignment.html#tok-target_list">target_list</a> "=")+ <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a></td></tr> |
| 73 | <tr> |
| 74 | <td><a id='tok-target_list' xml:id='tok-target_list'>target_list</a></td> |
| 75 | <td>::=</td> |
| 76 | <td><a class='grammartoken' href="assignment.html#tok-target">target</a> ("," <a class='grammartoken' href="assignment.html#tok-target">target</a>)* [","]</td></tr> |
| 77 | <tr> |
| 78 | <td><a id='tok-target' xml:id='tok-target'>target</a></td> |
| 79 | <td>::=</td> |
| 80 | <td><a class='grammartoken' href="identifiers.html#tok-identifier">identifier</a></td></tr> |
| 81 | <tr> |
| 82 | <td></td> |
| 83 | <td></td> |
| 84 | <td><code>| "(" <a class='grammartoken' href="assignment.html#tok-target_list">target_list</a> ")"</code></td></tr> |
| 85 | <tr> |
| 86 | <td></td> |
| 87 | <td></td> |
| 88 | <td><code>| "[" <a class='grammartoken' href="assignment.html#tok-target_list">target_list</a> "]"</code></td></tr> |
| 89 | <tr> |
| 90 | <td></td> |
| 91 | <td></td> |
| 92 | <td><code>| <a class='grammartoken' href="attribute-references.html#tok-attributeref">attributeref</a></code></td></tr> |
| 93 | <tr> |
| 94 | <td></td> |
| 95 | <td></td> |
| 96 | <td><code>| <a class='grammartoken' href="subscriptions.html#tok-subscription">subscription</a></code></td></tr> |
| 97 | <tr> |
| 98 | <td></td> |
| 99 | <td></td> |
| 100 | <td><code>| <a class='grammartoken' href="slicings.html#tok-slicing">slicing</a></code></td></tr> |
| 101 | </table> |
| 102 | </div> |
| 103 | <a class="grammar-footer" |
| 104 | href="grammar.txt" type="text/plain" |
| 105 | >Download entire grammar as text.</a> |
| 106 | </dd></dl> |
| 107 | |
| 108 | <P> |
| 109 | (See section <A href="primaries.html#primaries">5.3</A> for the syntax definitions for the last |
| 110 | three symbols.) |
| 111 | |
| 112 | <P> |
| 113 | An assignment statement evaluates the expression list (remember that |
| 114 | this can be a single expression or a comma-separated list, the latter |
| 115 | yielding a tuple) and assigns the single resulting object to each of |
| 116 | the target lists, from left to right. |
| 117 | <a id='l2h-470' xml:id='l2h-470'></a> |
| 118 | <P> |
| 119 | Assignment is defined recursively depending on the form of the target |
| 120 | (list). When a target is part of a mutable object (an attribute |
| 121 | reference, subscription or slicing), the mutable object must |
| 122 | ultimately perform the assignment and decide about its validity, and |
| 123 | may raise an exception if the assignment is unacceptable. The rules |
| 124 | observed by various types and the exceptions raised are given with the |
| 125 | definition of the object types (see section <A href="types.html#types">3.2</A>). |
| 126 | <a id='l2h-482' xml:id='l2h-482'></a> |
| 127 | <a id='l2h-471' xml:id='l2h-471'></a> |
| 128 | <P> |
| 129 | Assignment of an object to a target list is recursively defined as |
| 130 | follows. |
| 131 | <a id='l2h-472' xml:id='l2h-472'></a> |
| 132 | <P> |
| 133 | |
| 134 | <UL> |
| 135 | <LI>If the target list is a single target: The object is assigned to that |
| 136 | target. |
| 137 | |
| 138 | <P> |
| 139 | </LI> |
| 140 | <LI>If the target list is a comma-separated list of targets: The object |
| 141 | must be a sequence with the same number of items as there are |
| 142 | targets in the target list, and the items are assigned, from left to |
| 143 | right, to the corresponding targets. (This rule is relaxed as of |
| 144 | Python 1.5; in earlier versions, the object had to be a tuple. Since |
| 145 | strings are sequences, an assignment like "<tt class="samp">a, b = "xy"</tt>" is |
| 146 | now legal as long as the string has the right length.) |
| 147 | |
| 148 | <P> |
| 149 | </LI> |
| 150 | </UL> |
| 151 | |
| 152 | <P> |
| 153 | Assignment of an object to a single target is recursively defined as |
| 154 | follows. |
| 155 | |
| 156 | <P> |
| 157 | |
| 158 | |
| 159 | <UL> |
| 160 | <LI>If the target is an identifier (name): |
| 161 | |
| 162 | <P> |
| 163 | |
| 164 | <UL> |
| 165 | <LI>If the name does not occur in a <tt class="keyword">global</tt> statement in the current |
| 166 | code block: the name is bound to the object in the current local |
| 167 | namespace. |
| 168 | <a id='l2h-473' xml:id='l2h-473'></a> |
| 169 | <P> |
| 170 | </LI> |
| 171 | <LI>Otherwise: the name is bound to the object in the current global |
| 172 | namespace. |
| 173 | |
| 174 | <P> |
| 175 | </LI> |
| 176 | </UL> |
| 177 | <P> |
| 178 | The name is rebound if it was already bound. This may cause the |
| 179 | reference count for the object previously bound to the name to reach |
| 180 | zero, causing the object to be deallocated and its |
| 181 | destructor<a id='l2h-483' xml:id='l2h-483'></a> (if it has one) to be called. |
| 182 | |
| 183 | <P> |
| 184 | </LI> |
| 185 | <LI>If the target is a target list enclosed in parentheses or in square |
| 186 | brackets: The object must be a sequence with the same number of items |
| 187 | as there are targets in the target list, and its items are assigned, |
| 188 | from left to right, to the corresponding targets. |
| 189 | |
| 190 | <P> |
| 191 | </LI> |
| 192 | <LI>If the target is an attribute reference: The primary expression in the |
| 193 | reference is evaluated. It should yield an object with assignable |
| 194 | attributes; if this is not the case, <tt class="exception">TypeError</tt> is raised. That |
| 195 | object is then asked to assign the assigned object to the given |
| 196 | attribute; if it cannot perform the assignment, it raises an exception |
| 197 | (usually but not necessarily <tt class="exception">AttributeError</tt>). |
| 198 | <a id='l2h-474' xml:id='l2h-474'></a> |
| 199 | <P> |
| 200 | </LI> |
| 201 | <LI>If the target is a subscription: The primary expression in the |
| 202 | reference is evaluated. It should yield either a mutable sequence |
| 203 | object (such as a list) or a mapping object (such as a dictionary). Next, |
| 204 | the subscript expression is evaluated. |
| 205 | <a id='l2h-475' xml:id='l2h-475'></a><a id='l2h-476' xml:id='l2h-476'></a> |
| 206 | <P> |
| 207 | If the primary is a mutable sequence object (such as a list), the subscript |
| 208 | must yield a plain integer. If it is negative, the sequence's length |
| 209 | is added to it. The resulting value must be a nonnegative integer |
| 210 | less than the sequence's length, and the sequence is asked to assign |
| 211 | the assigned object to its item with that index. If the index is out |
| 212 | of range, <tt class="exception">IndexError</tt> is raised (assignment to a subscripted |
| 213 | sequence cannot add new items to a list). |
| 214 | <a id='l2h-477' xml:id='l2h-477'></a><a id='l2h-478' xml:id='l2h-478'></a> |
| 215 | <P> |
| 216 | If the primary is a mapping object (such as a dictionary), the subscript must |
| 217 | have a type compatible with the mapping's key type, and the mapping is |
| 218 | then asked to create a key/datum pair which maps the subscript to |
| 219 | the assigned object. This can either replace an existing key/value |
| 220 | pair with the same key value, or insert a new key/value pair (if no |
| 221 | key with the same value existed). |
| 222 | <a id='l2h-479' xml:id='l2h-479'></a><a id='l2h-480' xml:id='l2h-480'></a> |
| 223 | <P> |
| 224 | </LI> |
| 225 | <LI>If the target is a slicing: The primary expression in the reference is |
| 226 | evaluated. It should yield a mutable sequence object (such as a list). The |
| 227 | assigned object should be a sequence object of the same type. Next, |
| 228 | the lower and upper bound expressions are evaluated, insofar they are |
| 229 | present; defaults are zero and the sequence's length. The bounds |
| 230 | should evaluate to (small) integers. If either bound is negative, the |
| 231 | sequence's length is added to it. The resulting bounds are clipped to |
| 232 | lie between zero and the sequence's length, inclusive. Finally, the |
| 233 | sequence object is asked to replace the slice with the items of the |
| 234 | assigned sequence. The length of the slice may be different from the |
| 235 | length of the assigned sequence, thus changing the length of the |
| 236 | target sequence, if the object allows it. |
| 237 | <a id='l2h-481' xml:id='l2h-481'></a> |
| 238 | <P> |
| 239 | </LI> |
| 240 | </UL> |
| 241 | |
| 242 | <P> |
| 243 | (In the current implementation, the syntax for targets is taken |
| 244 | to be the same as for expressions, and invalid syntax is rejected |
| 245 | during the code generation phase, causing less detailed error |
| 246 | messages.) |
| 247 | |
| 248 | <P> |
| 249 | WARNING: Although the definition of assignment implies that overlaps |
| 250 | between the left-hand side and the right-hand side are `safe' (for example |
| 251 | "<tt class="samp">a, b = b, a</tt>" swaps two variables), overlaps <em>within</em> the |
| 252 | collection of assigned-to variables are not safe! For instance, the |
| 253 | following program prints "<tt class="samp">[0, 2]</tt>": |
| 254 | |
| 255 | <P> |
| 256 | <div class="verbatim"><pre> |
| 257 | x = [0, 1] |
| 258 | i = 0 |
| 259 | i, x[i] = 1, 2 |
| 260 | print x |
| 261 | </pre></div> |
| 262 | |
| 263 | <P> |
| 264 | |
| 265 | <p><br /></p><hr class='online-navigation' /> |
| 266 | <div class='online-navigation'> |
| 267 | <!--Table of Child-Links--> |
| 268 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 269 | |
| 270 | <UL CLASS="ChildLinks"> |
| 271 | <LI><A href="augassign.html">6.3.1 Augmented assignment statements</a> |
| 272 | </ul> |
| 273 | <!--End of Table of Child-Links--> |
| 274 | </div> |
| 275 | |
| 276 | <DIV CLASS="navigation"> |
| 277 | <div class='online-navigation'> |
| 278 | <p></p><hr /> |
| 279 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 280 | <tr> |
| 281 | <td class='online-navigation'><a rel="prev" title="6.2 Assert statements" |
| 282 | href="assert.html"><img src='../icons/previous.png' |
| 283 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 284 | <td class='online-navigation'><a rel="parent" title="6. Simple statements" |
| 285 | href="simple.html"><img src='../icons/up.png' |
| 286 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 287 | <td class='online-navigation'><a rel="next" title="6.3.1 Augmented assignment statements" |
| 288 | href="augassign.html"><img src='../icons/next.png' |
| 289 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 290 | <td align="center" width="100%">Python Reference Manual</td> |
| 291 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 292 | href="contents.html"><img src='../icons/contents.png' |
| 293 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 294 | <td class='online-navigation'><img src='../icons/blank.png' |
| 295 | border='0' height='32' alt='' width='32' /></td> |
| 296 | <td class='online-navigation'><a rel="index" title="Index" |
| 297 | href="genindex.html"><img src='../icons/index.png' |
| 298 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 299 | </tr></table> |
| 300 | <div class='online-navigation'> |
| 301 | <b class="navlabel">Previous:</b> |
| 302 | <a class="sectref" rel="prev" href="assert.html">6.2 Assert statements</A> |
| 303 | <b class="navlabel">Up:</b> |
| 304 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 305 | <b class="navlabel">Next:</b> |
| 306 | <a class="sectref" rel="next" href="augassign.html">6.3.1 Augmented assignment statements</A> |
| 307 | </div> |
| 308 | </div> |
| 309 | <hr /> |
| 310 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 311 | </DIV> |
| 312 | <!--End of Navigation Panel--> |
| 313 | <ADDRESS> |
| 314 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 315 | </ADDRESS> |
| 316 | </BODY> |
| 317 | </HTML> |