Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 1 | \chapter{Expressions} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | \index{expression} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 4 | This chapter explains the meaning of the elements of expressions in |
| 5 | Python. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 7 | \strong{Syntax Notes:} In this and the following chapters, extended |
| 8 | BNF\index{BNF} notation will be used to describe syntax, not lexical |
| 9 | analysis. When (one alternative of) a syntax rule has the form |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 10 | |
| 11 | \begin{verbatim} |
| 12 | name: othername |
| 13 | \end{verbatim} |
| 14 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 15 | and no semantics are given, the semantics of this form of \code{name} |
| 16 | are the same as for \code{othername}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 17 | \index{syntax} |
| 18 | |
| 19 | \section{Arithmetic conversions} |
| 20 | \indexii{arithmetic}{conversion} |
| 21 | |
| 22 | When a description of an arithmetic operator below uses the phrase |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 23 | ``the numeric arguments are converted to a common type,'' the |
| 24 | arguments are coerced using the coercion rules listed at the end of |
| 25 | chapter 3. If both arguments are standard numeric types, the |
| 26 | following coercions are applied: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 27 | |
| 28 | \begin{itemize} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 29 | \item If either argument is a complex number, the other is converted |
| 30 | to complex; |
| 31 | \item otherwise, if either argument is a floating point number, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 32 | the other is converted to floating point; |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 33 | \item otherwise, if either argument is a long integer, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 34 | the other is converted to long integer; |
| 35 | \item otherwise, both must be plain integers and no conversion |
| 36 | is necessary. |
| 37 | \end{itemize} |
| 38 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 39 | Some additional rules apply for certain operators (e.g. a string left |
| 40 | argument to the `\%' operator). Extensions can define their own |
| 41 | coercions. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 42 | \section{Atoms} |
| 43 | \index{atom} |
| 44 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 45 | Atoms are the most basic elements of expressions. The simplest atoms |
| 46 | are identifiers or literals. Forms enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 47 | reverse quotes or in parentheses, brackets or braces are also |
| 48 | categorized syntactically as atoms. The syntax for atoms is: |
| 49 | |
| 50 | \begin{verbatim} |
| 51 | atom: identifier | literal | enclosure |
| 52 | enclosure: parenth_form|list_display|dict_display|string_conversion |
| 53 | \end{verbatim} |
| 54 | |
| 55 | \subsection{Identifiers (Names)} |
| 56 | \index{name} |
| 57 | \index{identifier} |
| 58 | |
| 59 | An identifier occurring as an atom is a reference to a local, global |
| 60 | or built-in name binding. If a name is assigned to anywhere in a code |
| 61 | block (even in unreachable code), and is not mentioned in a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 62 | \keyword{global} statement in that code block, then it refers to a local |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 63 | name throughout that code block. When it is not assigned to anywhere |
| 64 | in the block, or when it is assigned to but also explicitly listed in |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 65 | a \keyword{global} statement, it refers to a global name if one exists, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 66 | else to a built-in name (and this binding may dynamically change). |
| 67 | \indexii{name}{binding} |
| 68 | \index{code block} |
| 69 | \stindex{global} |
| 70 | \indexii{built-in}{name} |
| 71 | \indexii{global}{name} |
| 72 | |
| 73 | When the name is bound to an object, evaluation of the atom yields |
| 74 | that object. When a name is not bound, an attempt to evaluate it |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 75 | raises a \exception{NameError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 76 | \exindex{NameError} |
| 77 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 78 | \strong{Private name mangling:}% |
| 79 | \indexii{name}{mangling}% |
| 80 | \indexii{private}{names}% |
| 81 | when an identifier that textually occurs in a class definition begins |
| 82 | with two or more underscore characters and does not end in two or more |
| 83 | underscores, it is considered a ``private name'' of that class. |
| 84 | Private names are transformed to a longer form before code is |
| 85 | generated for them. The transformation inserts the class name in |
| 86 | front of the name, with leading underscores removed, and a single |
| 87 | underscore inserted in front of the class name. For example, the |
| 88 | identifier \code{__spam} occurring in a class named \code{Ham} will be |
| 89 | transformed to \code{_Ham__spam}. This transformation is independent |
| 90 | of the syntactical context in which the identifier is used. If the |
| 91 | transformed name is extremely long (longer than 255 characters), |
| 92 | implementation defined truncation may happen. If the class name |
| 93 | consists only of underscores, no transformation is done. |
| 94 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 95 | \subsection{Literals} |
| 96 | \index{literal} |
| 97 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 98 | Python supports string literals and various numeric literals: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 99 | |
| 100 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 101 | literal: stringliteral | integer | longinteger | floatnumber | imagnumber |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 102 | \end{verbatim} |
| 103 | |
| 104 | Evaluation of a literal yields an object of the given type (string, |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 105 | integer, long integer, floating point number, complex number) with the |
| 106 | given value. The value may be approximated in the case of floating |
| 107 | point and imaginary (complex) literals. See section \ref{literals} |
| 108 | for details. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 109 | |
| 110 | All literals correspond to immutable data types, and hence the |
| 111 | object's identity is less important than its value. Multiple |
| 112 | evaluations of literals with the same value (either the same |
| 113 | occurrence in the program text or a different occurrence) may obtain |
| 114 | the same object or a different object with the same value. |
| 115 | \indexiii{immutable}{data}{type} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 116 | \indexii{immutable}{objects} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 117 | |
| 118 | \subsection{Parenthesized forms} |
| 119 | \index{parenthesized form} |
| 120 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 121 | A parenthesized form is an optional expression list enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 122 | parentheses: |
| 123 | |
| 124 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 125 | parenth_form: "(" [expression_list] ")" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 126 | \end{verbatim} |
| 127 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 128 | A parenthesized expression list yields whatever that expression list |
| 129 | yields: if the list contains at least one comma, it yields a tuple; |
| 130 | otherwise, it yields the single expression that makes up the |
| 131 | expression list. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 132 | |
| 133 | An empty pair of parentheses yields an empty tuple object. Since |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 134 | tuples are immutable, the rules for literals apply (i.e., two |
| 135 | occurrences of the empty tuple may or may not yield the same object). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 136 | \indexii{empty}{tuple} |
| 137 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 138 | Note that tuples are not formed by the parentheses, but rather by use |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 139 | of the comma operator. The exception is the empty tuple, for which |
| 140 | parentheses {\em are} required --- allowing unparenthesized ``nothing'' |
| 141 | in expressions would cause ambiguities and allow common typos to |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 142 | pass uncaught. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 143 | \index{comma} |
| 144 | \indexii{tuple}{display} |
| 145 | |
| 146 | \subsection{List displays} |
| 147 | \indexii{list}{display} |
| 148 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 149 | A list display is a possibly empty series of expressions enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 150 | square brackets: |
| 151 | |
| 152 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 153 | list_display: "[" [expression_list] "]" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 154 | \end{verbatim} |
| 155 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 156 | A list display yields a new list object. If it has no expression |
| 157 | list, the list object has no items. Otherwise, the elements of the |
| 158 | expression list are evaluated from left to right and inserted in the |
| 159 | list object in that order. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 160 | \obindex{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 161 | \indexii{empty}{list} |
| 162 | |
| 163 | \subsection{Dictionary displays} \label{dict} |
| 164 | \indexii{dictionary}{display} |
| 165 | |
| 166 | A dictionary display is a possibly empty series of key/datum pairs |
| 167 | enclosed in curly braces: |
| 168 | \index{key} |
| 169 | \index{datum} |
| 170 | \index{key/datum pair} |
| 171 | |
| 172 | \begin{verbatim} |
| 173 | dict_display: "{" [key_datum_list] "}" |
| 174 | key_datum_list: key_datum ("," key_datum)* [","] |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 175 | key_datum: expression ":" expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 176 | \end{verbatim} |
| 177 | |
| 178 | A dictionary display yields a new dictionary object. |
| 179 | \obindex{dictionary} |
| 180 | |
| 181 | The key/datum pairs are evaluated from left to right to define the |
| 182 | entries of the dictionary: each key object is used as a key into the |
| 183 | dictionary to store the corresponding datum. |
| 184 | |
| 185 | Restrictions on the types of the key values are listed earlier in |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 186 | section \ref{types}. (To summarize,the key type should be hashable, |
| 187 | which excludes all mutable objects.) Clashes between duplicate keys |
| 188 | are not detected; the last datum (textually rightmost in the display) |
| 189 | stored for a given key value prevails. |
| 190 | \indexii{immutable}{objects} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 191 | |
| 192 | \subsection{String conversions} |
| 193 | \indexii{string}{conversion} |
| 194 | \indexii{reverse}{quotes} |
| 195 | \indexii{backward}{quotes} |
| 196 | \index{back-quotes} |
| 197 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 198 | A string conversion is an expression list enclosed in reverse (a.k.a. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 199 | backward) quotes: |
| 200 | |
| 201 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 202 | string_conversion: "`" expression_list "`" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 203 | \end{verbatim} |
| 204 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 205 | A string conversion evaluates the contained expression list and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 206 | converts the resulting object into a string according to rules |
| 207 | specific to its type. |
| 208 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 209 | If the object is a string, a number, \code{None}, or a tuple, list or |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 210 | dictionary containing only objects whose type is one of these, the |
| 211 | resulting string is a valid Python expression which can be passed to |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 212 | the built-in function \function{eval()} to yield an expression with the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 213 | same value (or an approximation, if floating point numbers are |
| 214 | involved). |
| 215 | |
| 216 | (In particular, converting a string adds quotes around it and converts |
| 217 | ``funny'' characters to escape sequences that are safe to print.) |
| 218 | |
| 219 | It is illegal to attempt to convert recursive objects (e.g. lists or |
| 220 | dictionaries that contain a reference to themselves, directly or |
| 221 | indirectly.) |
| 222 | \obindex{recursive} |
| 223 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 224 | The built-in function \function{repr()} performs exactly the same |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 225 | conversion in its argument as enclosing it in parentheses and reverse |
| 226 | quotes does. The built-in function \function{str()} performs a |
| 227 | similar but more user-friendly conversion. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 228 | \bifuncindex{repr} |
| 229 | \bifuncindex{str} |
| 230 | |
| 231 | \section{Primaries} \label{primaries} |
| 232 | \index{primary} |
| 233 | |
| 234 | Primaries represent the most tightly bound operations of the language. |
| 235 | Their syntax is: |
| 236 | |
| 237 | \begin{verbatim} |
| 238 | primary: atom | attributeref | subscription | slicing | call |
| 239 | \end{verbatim} |
| 240 | |
| 241 | \subsection{Attribute references} |
| 242 | \indexii{attribute}{reference} |
| 243 | |
| 244 | An attribute reference is a primary followed by a period and a name: |
| 245 | |
| 246 | \begin{verbatim} |
| 247 | attributeref: primary "." identifier |
| 248 | \end{verbatim} |
| 249 | |
| 250 | The primary must evaluate to an object of a type that supports |
| 251 | attribute references, e.g. a module or a list. This object is then |
| 252 | asked to produce the attribute whose name is the identifier. If this |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 253 | attribute is not available, the exception |
| 254 | \exception{AttributeError}\exindex{AttributeError} is raised. |
| 255 | Otherwise, the type and value of the object produced is determined by |
| 256 | the object. Multiple evaluations of the same attribute reference may |
| 257 | yield different objects. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 258 | \obindex{module} |
| 259 | \obindex{list} |
| 260 | |
| 261 | \subsection{Subscriptions} |
| 262 | \index{subscription} |
| 263 | |
| 264 | A subscription selects an item of a sequence (string, tuple or list) |
| 265 | or mapping (dictionary) object: |
| 266 | \obindex{sequence} |
| 267 | \obindex{mapping} |
| 268 | \obindex{string} |
| 269 | \obindex{tuple} |
| 270 | \obindex{list} |
| 271 | \obindex{dictionary} |
| 272 | \indexii{sequence}{item} |
| 273 | |
| 274 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 275 | subscription: primary "[" expression_list "]" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 276 | \end{verbatim} |
| 277 | |
| 278 | The primary must evaluate to an object of a sequence or mapping type. |
| 279 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 280 | If the primary is a mapping, the expression list must evaluate to an |
| 281 | object whose value is one of the keys of the mapping, and the |
| 282 | subscription selects the value in the mapping that corresponds to that |
| 283 | key. (The expression list is a tuple except if it has exactly one |
| 284 | item.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 286 | If the primary is a sequence, the expression (list) must evaluate to a |
| 287 | plain integer. If this value is negative, the length of the sequence |
| 288 | is added to it (so that, e.g., \code{x[-1]} selects the last item of |
| 289 | \code{x}.) The resulting value must be a nonnegative integer less |
| 290 | than the number of items in the sequence, and the subscription selects |
| 291 | the item whose index is that value (counting from zero). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 292 | |
| 293 | A string's items are characters. A character is not a separate data |
| 294 | type but a string of exactly one character. |
| 295 | \index{character} |
| 296 | \indexii{string}{item} |
| 297 | |
| 298 | \subsection{Slicings} |
| 299 | \index{slicing} |
| 300 | \index{slice} |
| 301 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 302 | A slicing selects a range of items in a sequence object (e.g., a |
| 303 | string, tuple or list). Slicings may be used as expressions or as |
| 304 | targets in assignment or del statements. The syntax for a slicing: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 305 | \obindex{sequence} |
| 306 | \obindex{string} |
| 307 | \obindex{tuple} |
| 308 | \obindex{list} |
| 309 | |
| 310 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 311 | slicing: simple_slicing | extended_slicing |
| 312 | simple_slicing: primary "[" short_slice "]" |
| 313 | extended_slicing: primary "[" slice_list "]" |
| 314 | slice_list: slice_item ("," slice_item)* [","] |
| 315 | slice_item: expression | proper_slice | ellipsis |
| 316 | proper_slice: short_slice | long_slice |
| 317 | short_slice: [lower_bound] ":" [upper_bound] |
| 318 | long_slice: short_slice ":" [stride] |
| 319 | lower_bound: expression |
| 320 | upper_bound: expression |
| 321 | stride: expression |
| 322 | ellipsis: "..." |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 323 | \end{verbatim} |
| 324 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 325 | There is ambiguity in the formal syntax here: anything that looks like |
| 326 | an expression list also looks like a slice list, so any subscription |
| 327 | can be interpreted as a slicing. Rather than further complicating the |
| 328 | syntax, this is disambiguated by defining that in this case the |
| 329 | interpretation as a subscription takes priority over the |
| 330 | interpretation as a slicing (this is the case if the slice list |
| 331 | contains no proper slice nor ellipses). Similarly, when the slice |
| 332 | list has exactly one short slice and no trailing comma, the |
| 333 | interpretation as a simple slicing takes priority over that as an |
| 334 | extended slicing.\indexii{extended}{slicing} |
| 335 | |
| 336 | The semantics for a simple slicing are as follows. The primary must |
| 337 | evaluate to a sequence object. The lower and upper bound expressions, |
| 338 | if present, must evaluate to plain integers; defaults are zero and the |
| 339 | sequence's length, respectively. If either bound is negative, the |
| 340 | sequence's length is added to it. The slicing now selects all items |
| 341 | with index \var{k} such that |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 342 | \code{\var{i} <= \var{k} < \var{j}} where \var{i} |
| 343 | and \var{j} are the specified lower and upper bounds. This may be an |
| 344 | empty sequence. It is not an error if \var{i} or \var{j} lie outside the |
| 345 | range of valid indexes (such items don't exist so they aren't |
| 346 | selected). |
| 347 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 348 | The semantics for an extended slicing are as follows. The primary |
| 349 | must evaluate to a mapping object, and it is indexed with a key that |
| 350 | is constructed from the slice list, as follows. If the slice list |
| 351 | contains at least one comma, the key is a tuple containing the |
| 352 | conversion of the slice items; otherwise, the conversion of the lone |
| 353 | slice item is the key. The conversion of a slice item that is an |
| 354 | expression is that expression. The conversion of an ellipsis slice |
| 355 | item is the built-in \code{Ellipsis} object. The conversion of a |
| 356 | proper slice is a slice object (see section \ref{types}) whose |
| 357 | \code{start}, \code{stop} and \code{step} attributes are the values of |
| 358 | the expressions given as lower bound, upper bound and stride, |
| 359 | respectively, substituting \code{None} for missing expressions. |
| 360 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 361 | \subsection{Calls} \label{calls} |
| 362 | \index{call} |
| 363 | |
| 364 | A call calls a callable object (e.g. a function) with a possibly empty |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 365 | series of arguments: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 366 | \obindex{callable} |
| 367 | |
| 368 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 369 | call: primary "(" [argument_list [","]] ")" |
| 370 | argument_list: positional_arguments ["," keyword_arguments] |
| 371 | | keyword_arguments |
| 372 | positional_arguments: expression ("," expression)* |
| 373 | keyword_arguments: keyword_item ("," keyword_item)* |
| 374 | keyword_item: identifier "=" expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 375 | \end{verbatim} |
| 376 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 377 | A trailing comma may be present after an argument list but does not |
| 378 | affect the semantics. |
| 379 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 380 | The primary must evaluate to a callable object (user-defined |
| 381 | functions, built-in functions, methods of built-in objects, class |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 382 | objects, methods of class instances, and certain class instances |
| 383 | themselves are callable; extensions may define additional callable |
| 384 | object types). All argument expressions are evaluated before the call |
| 385 | is attempted. Please refer to section \ref{function} for the syntax |
| 386 | of formal parameter lists. |
| 387 | |
| 388 | If keyword arguments are present, they are first converted to |
| 389 | positional arguments, as follows. First, a list of unfilled slots is |
| 390 | created for the formal parameters. If there are N positional |
| 391 | arguments, they are placed in the first N slots. Next, for each |
| 392 | keyword argument, the identifier is used to determine the |
| 393 | corresponding slot (if the identifier is the same as the first formal |
| 394 | parameter name, the first slot is used, and so on). If the slot is |
| 395 | already filled, a \exception{TypeError} exception is raised. |
| 396 | Otherwise, the value of the argument is placed in the slot, filling it |
| 397 | (even if the expression is \code{None}, it fills the slot). When all |
| 398 | arguments have been processed, the slots that are still unfilled are |
| 399 | filled with the corresponding default value from the function |
| 400 | definition. (Default values are calculated, once, when the function |
| 401 | is defined; thus, a mutable object such as a list or dictionary used |
| 402 | as default value will be shared by all calls that don't specify an |
| 403 | argument value for the corresponding slot; this should usually be |
| 404 | avoided.) If there are any unfilled slots for which no default value |
| 405 | is specified, a \exception{TypeError} exception is raised. Otherwise, |
| 406 | the list of filled slots is used as the argument list for the call. |
| 407 | |
| 408 | If there are more positional arguments than there are formal parameter |
| 409 | slots, a \exception{TypeError} exception is raised, unless a formal |
| 410 | parameter using the syntax ``\code{*identifier}'' is present; in this |
| 411 | case, that formal parameter receives a tuple containing the excess |
| 412 | positional arguments (or an empty tuple if there were no excess |
| 413 | positional arguments). |
| 414 | |
| 415 | If any keyword argument does not correspond to a formal parameter |
| 416 | name, a \exception{TypeError} exception is raised, unless a formal |
| 417 | parameter using the syntax ``\code{**identifier}'' is present; in this |
| 418 | case, that formal parameter receives a dictionary containing the |
| 419 | excess keyword arguments (using the keywords as keys and the argument |
| 420 | values as corresponding values), or a (new) empty dictionary if there |
| 421 | were no excess keyword arguments. |
| 422 | |
| 423 | Formal parameters using the syntax ``\code{*identifier}'' or |
| 424 | ``\code{**identifier}'' cannot be used as positional argument slots or |
| 425 | as keyword argument names. Formal parameters using the syntax |
| 426 | ``\code{(sublist)}'' cannot be used as keyword argument names; the |
| 427 | outermost sublist corresponds to a single unnamed argument slot, and |
| 428 | the argument value is assigned to the sublist using the usual tuple |
| 429 | assignment rules after all other parameter processing is done. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 430 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 431 | A call always returns some value, possibly \code{None}, unless it |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 432 | raises an exception. How this value is computed depends on the type |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 433 | of the callable object. |
| 434 | |
| 435 | If it is--- |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 436 | |
| 437 | \begin{description} |
| 438 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 439 | \item[a user-defined function:] The code block for the function is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 440 | executed, passing it the argument list. The first thing the code |
| 441 | block will do is bind the formal parameters to the arguments; this is |
| 442 | described in section \ref{function}. When the code block executes a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 443 | \keyword{return} statement, this specifies the return value of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 444 | function call. |
| 445 | \indexii{function}{call} |
| 446 | \indexiii{user-defined}{function}{call} |
| 447 | \obindex{user-defined function} |
| 448 | \obindex{function} |
| 449 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 450 | \item[a built-in function or method:] The result is up to the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 451 | interpreter; see the library reference manual for the descriptions of |
| 452 | built-in functions and methods. |
| 453 | \indexii{function}{call} |
| 454 | \indexii{built-in function}{call} |
| 455 | \indexii{method}{call} |
| 456 | \indexii{built-in method}{call} |
| 457 | \obindex{built-in method} |
| 458 | \obindex{built-in function} |
| 459 | \obindex{method} |
| 460 | \obindex{function} |
| 461 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 462 | \item[a class object:] A new instance of that class is returned. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 463 | \obindex{class} |
| 464 | \indexii{class object}{call} |
| 465 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 466 | \item[a class instance method:] The corresponding user-defined |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 467 | function is called, with an argument list that is one longer than the |
| 468 | argument list of the call: the instance becomes the first argument. |
| 469 | \obindex{class instance} |
| 470 | \obindex{instance} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 471 | \indexii{class instance}{call} |
| 472 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 473 | \item[a class instance:] The class must define a \method{__call__()} |
| 474 | method; the effect is then the same as if that method was called. |
| 475 | \indexii{instance}{call} |
| 476 | \ttindex{__call__} |
| 477 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 478 | \end{description} |
| 479 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 480 | |
| 481 | \section{The power operator} |
| 482 | |
| 483 | The power operator binds more tightly than unary operators on its |
| 484 | left; it binds less tightly than unary operators on its right. The |
| 485 | syntax is: |
| 486 | |
| 487 | \begin{verbatim} |
| 488 | power: primary ["**" u_expr] |
| 489 | \end{verbatim} |
| 490 | |
| 491 | Thus, in an unparenthesized sequence of power and unary operators, the |
| 492 | operators are evaluated from right to left (this does not constrain |
| 493 | the evaluation order for the operands). |
| 494 | |
| 495 | The power operator has the same semantics as the built-in |
| 496 | \function{pow()} function, when called with two arguments: it yields |
| 497 | its left argument raised to the power of its right argument. The |
| 498 | numeric arguments are first converted to a common type. The result |
| 499 | type is that of the arguments after coercion; if the result is not |
| 500 | expressible in that type (as in raising an integer to a negative |
| 501 | power, or a negative floating point number to a broken power), a |
| 502 | \exception{TypeError} exception is raised. |
| 503 | |
| 504 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 505 | \section{Unary arithmetic operations} |
| 506 | \indexiii{unary}{arithmetic}{operation} |
| 507 | \indexiii{unary}{bit-wise}{operation} |
| 508 | |
| 509 | All unary arithmetic (and bit-wise) operations have the same priority: |
| 510 | |
| 511 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 512 | u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 513 | \end{verbatim} |
| 514 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 515 | The unary \code{-} (minus) operator yields the negation of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 516 | numeric argument. |
| 517 | \index{negation} |
| 518 | \index{minus} |
| 519 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 520 | The unary \code{+} (plus) operator yields its numeric argument |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 521 | unchanged. |
| 522 | \index{plus} |
| 523 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 524 | The unary \code{~} (invert) operator yields the bit-wise inversion |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 525 | of its plain or long integer argument. The bit-wise inversion of |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 526 | \code{x} is defined as \code{-(x+1)}. It only applies to integral |
| 527 | numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 528 | \index{inversion} |
| 529 | |
| 530 | In all three cases, if the argument does not have the proper type, |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 531 | a \exception{TypeError} exception is raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 532 | \exindex{TypeError} |
| 533 | |
| 534 | \section{Binary arithmetic operations} |
| 535 | \indexiii{binary}{arithmetic}{operation} |
| 536 | |
| 537 | The binary arithmetic operations have the conventional priority |
| 538 | levels. Note that some of these operations also apply to certain |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 539 | non-numeric types. Apart from the power operator, there are only two |
| 540 | levels, one for multiplicative operators and one for additive |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 541 | operators: |
| 542 | |
| 543 | \begin{verbatim} |
| 544 | m_expr: u_expr | m_expr "*" u_expr |
| 545 | | m_expr "/" u_expr | m_expr "%" u_expr |
| 546 | a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr |
| 547 | \end{verbatim} |
| 548 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 549 | The \code{*} (multiplication) operator yields the product of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 550 | arguments. The arguments must either both be numbers, or one argument |
| 551 | must be a plain integer and the other must be a sequence. In the |
| 552 | former case, the numbers are converted to a common type and then |
| 553 | multiplied together. In the latter case, sequence repetition is |
| 554 | performed; a negative repetition factor yields an empty sequence. |
| 555 | \index{multiplication} |
| 556 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 557 | The \code{/} (division) operator yields the quotient of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 558 | arguments. The numeric arguments are first converted to a common |
| 559 | type. Plain or long integer division yields an integer of the same |
| 560 | type; the result is that of mathematical division with the `floor' |
| 561 | function applied to the result. Division by zero raises the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 562 | \exception{ZeroDivisionError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 563 | \exindex{ZeroDivisionError} |
| 564 | \index{division} |
| 565 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 566 | The \code{\%} (modulo) operator yields the remainder from the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 567 | division of the first argument by the second. The numeric arguments |
| 568 | are first converted to a common type. A zero right argument raises |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 569 | the \exception{ZeroDivisionError} exception. The arguments may be floating |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 570 | point numbers, e.g. \code{3.14\%0.7} equals \code{0.34} (since |
| 571 | \code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always |
| 572 | yields a result with the same sign as its second operand (or zero); |
| 573 | the absolute value of the result is strictly smaller than the second |
| 574 | operand. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 575 | \index{modulo} |
| 576 | |
| 577 | The integer division and modulo operators are connected by the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 578 | following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and |
| 579 | modulo are also connected with the built-in function \function{divmod()}: |
| 580 | \code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 581 | floating point and complex numbers; there a similar identity holds where |
| 582 | \code{x/y} is replaced by \code{floor(x/y)}) or |
| 583 | \code{floor((x/y).real)}, respectively. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 584 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 585 | The \code{+} (addition) operator yields the sum of its arguments. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 586 | The arguments must either both be numbers or both sequences of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 587 | same type. In the former case, the numbers are converted to a common |
| 588 | type and then added together. In the latter case, the sequences are |
| 589 | concatenated. |
| 590 | \index{addition} |
| 591 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 592 | The \code{-} (subtraction) operator yields the difference of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 593 | arguments. The numeric arguments are first converted to a common |
| 594 | type. |
| 595 | \index{subtraction} |
| 596 | |
| 597 | \section{Shifting operations} |
| 598 | \indexii{shifting}{operation} |
| 599 | |
| 600 | The shifting operations have lower priority than the arithmetic |
| 601 | operations: |
| 602 | |
| 603 | \begin{verbatim} |
| 604 | shift_expr: a_expr | shift_expr ( "<<" | ">>" ) a_expr |
| 605 | \end{verbatim} |
| 606 | |
| 607 | These operators accept plain or long integers as arguments. The |
| 608 | arguments are converted to a common type. They shift the first |
| 609 | argument to the left or right by the number of bits given by the |
| 610 | second argument. |
| 611 | |
| 612 | A right shift by \var{n} bits is defined as division by |
| 613 | \code{pow(2,\var{n})}. A left shift by \var{n} bits is defined as |
| 614 | multiplication with \code{pow(2,\var{n})}; for plain integers there is |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 615 | no overflow check so in that case the operation drops bits and flips |
| 616 | the sign if the result is not less than \code{pow(2,31)} in absolute |
| 617 | value. Negative shift counts raise a \exception{ValueError} |
| 618 | exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 619 | \exindex{ValueError} |
| 620 | |
| 621 | \section{Binary bit-wise operations} |
| 622 | \indexiii{binary}{bit-wise}{operation} |
| 623 | |
| 624 | Each of the three bitwise operations has a different priority level: |
| 625 | |
| 626 | \begin{verbatim} |
| 627 | and_expr: shift_expr | and_expr "&" shift_expr |
| 628 | xor_expr: and_expr | xor_expr "^" and_expr |
| 629 | or_expr: xor_expr | or_expr "|" xor_expr |
| 630 | \end{verbatim} |
| 631 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 632 | The \code{\&} operator yields the bitwise AND of its arguments, which |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 633 | must be plain or long integers. The arguments are converted to a |
| 634 | common type. |
| 635 | \indexii{bit-wise}{and} |
| 636 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 637 | The \code{\^} operator yields the bitwise XOR (exclusive OR) of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 638 | arguments, which must be plain or long integers. The arguments are |
| 639 | converted to a common type. |
| 640 | \indexii{bit-wise}{xor} |
| 641 | \indexii{exclusive}{or} |
| 642 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 643 | The \code{|} operator yields the bitwise (inclusive) OR of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 644 | arguments, which must be plain or long integers. The arguments are |
| 645 | converted to a common type. |
| 646 | \indexii{bit-wise}{or} |
| 647 | \indexii{inclusive}{or} |
| 648 | |
| 649 | \section{Comparisons} |
| 650 | \index{comparison} |
| 651 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 652 | Contrary to \C, all comparison operations in Python have the same |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 653 | priority, which is lower than that of any arithmetic, shifting or |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 654 | bitwise operation. Also contrary to \C, expressions like |
| 655 | \code{a < b < c} have the interpretation that is conventional in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 656 | mathematics: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 657 | \indexii{C}{language} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 658 | |
| 659 | \begin{verbatim} |
| 660 | comparison: or_expr (comp_operator or_expr)* |
| 661 | comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" |
| 662 | \end{verbatim} |
| 663 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 664 | Comparisons yield integer values: \code{1} for true, \code{0} for false. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 665 | |
| 666 | Comparisons can be chained arbitrarily, e.g. \code{x < y <= z} is |
| 667 | equivalent to \code{x < y and y <= z}, except that \code{y} is |
| 668 | evaluated only once (but in both cases \code{z} is not evaluated at all |
| 669 | when \code{x < y} is found to be false). |
| 670 | \indexii{chaining}{comparisons} |
| 671 | |
| 672 | Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are |
| 673 | expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison |
| 674 | operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 675 | to \var{a opa b} \keyword{and} \var{b opb c} \keyword{and} \ldots |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 676 | \var{y opy z}, except that each expression is evaluated at most once. |
| 677 | |
| 678 | Note that \var{a opa b opb c} doesn't imply any kind of comparison |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 679 | between \var{a} and \var{c}, so that, e.g., \code{x < y > z} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 680 | perfectly legal (though perhaps not pretty). |
| 681 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 682 | The forms \code{<>} and \code{!=} are equivalent; for consistency with |
| 683 | C, \code{!=} is preferred; where \code{!=} is mentioned below |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 684 | \code{<>} is also acceptable. At some point in the (far) future, |
| 685 | \code{<>} may become obsolete. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 686 | |
| 687 | The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare |
| 688 | the values of two objects. The objects needn't have the same type. |
| 689 | If both are numbers, they are coverted to a common type. Otherwise, |
| 690 | objects of different types {\em always} compare unequal, and are |
| 691 | ordered consistently but arbitrarily. |
| 692 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 693 | (This unusual definition of comparison was used to simplify the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 694 | definition of operations like sorting and the \keyword{in} and |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 695 | \keyword{not in} operators. In the future, the comparison rules for |
| 696 | objects of different types are likely to change.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 697 | |
| 698 | Comparison of objects of the same type depends on the type: |
| 699 | |
| 700 | \begin{itemize} |
| 701 | |
| 702 | \item |
| 703 | Numbers are compared arithmetically. |
| 704 | |
| 705 | \item |
| 706 | Strings are compared lexicographically using the numeric equivalents |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 707 | (the result of the built-in function \function{ord()}) of their |
| 708 | characters. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 709 | |
| 710 | \item |
| 711 | Tuples and lists are compared lexicographically using comparison of |
| 712 | corresponding items. |
| 713 | |
| 714 | \item |
| 715 | Mappings (dictionaries) are compared through lexicographic |
| 716 | comparison of their sorted (key, value) lists.% |
| 717 | \footnote{This is expensive since it requires sorting the keys first, |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 718 | but it is about the only sensible definition. An earlier version of |
| 719 | Python compared dictionaries by identity only, but this caused |
| 720 | surprises because people expected to be able to test a dictionary for |
| 721 | emptiness by comparing it to \code{\{\}}.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 722 | |
| 723 | \item |
| 724 | Most other types compare unequal unless they are the same object; |
| 725 | the choice whether one object is considered smaller or larger than |
| 726 | another one is made arbitrarily but consistently within one |
| 727 | execution of a program. |
| 728 | |
| 729 | \end{itemize} |
| 730 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 731 | The operators \keyword{in} and \keyword{not in} test for sequence |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 732 | membership: if \var{y} is a sequence, \code{\var{x} in \var{y}} is |
| 733 | true if and only if there exists an index \var{i} such that |
| 734 | \code{\var{x} = \var{y}[\var{i}]}. |
| 735 | \code{\var{x} not in \var{y}} yields the inverse truth value. The |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 736 | exception \exception{TypeError} is raised when \var{y} is not a sequence, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 737 | or when \var{y} is a string and \var{x} is not a string of length one.% |
| 738 | \footnote{The latter restriction is sometimes a nuisance.} |
| 739 | \opindex{in} |
| 740 | \opindex{not in} |
| 741 | \indexii{membership}{test} |
| 742 | \obindex{sequence} |
| 743 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 744 | The operators \keyword{is} and \keyword{is not} test for object identity: |
| 745 | \code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y} |
| 746 | are the same object. \code{\var{x} is not \var{y}} yields the inverse |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 747 | truth value. |
| 748 | \opindex{is} |
| 749 | \opindex{is not} |
| 750 | \indexii{identity}{test} |
| 751 | |
| 752 | \section{Boolean operations} \label{Booleans} |
| 753 | \indexii{Boolean}{operation} |
| 754 | |
| 755 | Boolean operations have the lowest priority of all Python operations: |
| 756 | |
| 757 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 758 | expression: or_test | lambda_form |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 759 | or_test: and_test | or_test "or" and_test |
| 760 | and_test: not_test | and_test "and" not_test |
| 761 | not_test: comparison | "not" not_test |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 762 | lambda_form: "lambda" [parameter_list]: expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 763 | \end{verbatim} |
| 764 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 765 | In the context of Boolean operations, and also when expressions are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 766 | used by control flow statements, the following values are interpreted |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 767 | as false: \code{None}, numeric zero of all types, empty sequences |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 768 | (strings, tuples and lists), and empty mappings (dictionaries). All |
| 769 | other values are interpreted as true. |
| 770 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 771 | The operator \keyword{not} yields \code{1} if its argument is false, |
| 772 | \code{0} otherwise. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 773 | \opindex{not} |
| 774 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 775 | The expression \code{\var{x} and \var{y}} first evaluates \var{x}; if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 776 | \var{x} is false, its value is returned; otherwise, \var{y} is |
| 777 | evaluated and the resulting value is returned. |
| 778 | \opindex{and} |
| 779 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 780 | The expression \code{\var{x} or \var{y}} first evaluates \var{x}; if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 781 | \var{x} is true, its value is returned; otherwise, \var{y} is |
| 782 | evaluated and the resulting value is returned. |
| 783 | \opindex{or} |
| 784 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 785 | (Note that neither \keyword{and} nor \keyword{or} restrict the value |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 786 | and type they return to \code{0} and \code{1}, but rather return the |
| 787 | last evaluated argument. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 788 | This is sometimes useful, e.g., if \code{s} is a string that should be |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 789 | replaced by a default value if it is empty, the expression |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 790 | \code{s or 'foo'} yields the desired value. Because \keyword{not} has to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 791 | invent a value anyway, it does not bother to return a value of the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 792 | same type as its argument, so e.g. \code{not 'foo'} yields \code{0}, |
| 793 | not \code{''}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 794 | |
| 795 | Lambda forms (lambda expressions) have the same syntactic position as |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 796 | expressions. They are a shorthand to create anonymous functions; the |
| 797 | expression \code{lambda \var{arguments}: \var{expression}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 798 | yields a function object that behaves virtually identical to one |
| 799 | defined with |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 800 | |
| 801 | \begin{verbatim} |
| 802 | def name(arguments): |
| 803 | return expression |
| 804 | \end{verbatim} |
| 805 | |
| 806 | See section \ref{function} for the syntax of parameter lists. Note |
| 807 | that functions created with lambda forms cannot contain statements. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 808 | \label{lambda} |
| 809 | \indexii{lambda}{expression} |
| 810 | \indexii{lambda}{form} |
| 811 | \indexii{anonmymous}{function} |
| 812 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 813 | \strong{Programmer's note:} a lambda form defined inside a function |
| 814 | has no access to names defined in the function's namespace. This is |
| 815 | because Python has only two scopes: local and global. A common |
| 816 | work-around is to use default argument values to pass selected |
| 817 | variables into the lambda's namespace, e.g.: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 818 | |
| 819 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 820 | def make_incrementor(increment): |
| 821 | return lambda x, n=increment: x+n |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 822 | \end{verbatim} |
| 823 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 824 | \section{Expression lists and expression lists} |
| 825 | \indexii{expression}{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 827 | \begin{verbatim} |
| 828 | expression_list: expression ("," expression)* [","] |
| 829 | \end{verbatim} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 831 | An expression (expression) list containing at least one comma yields a |
| 832 | tuple. The length of the tuple is the number of expressions in the |
| 833 | list. The expressions are evaluated from left to right. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 834 | \obindex{tuple} |
| 835 | |
| 836 | The trailing comma is required only to create a single tuple (a.k.a. a |
| 837 | {\em singleton}); it is optional in all other cases. A single |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 838 | expression (expression) without a trailing comma doesn't create a |
| 839 | tuple, but rather yields the value of that expression (expression). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 840 | (To create an empty tuple, use an empty pair of parentheses: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 841 | \code{()}.) |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 842 | \indexii{trailing}{comma} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 843 | |
| 844 | \section{Summary} |
| 845 | |
| 846 | The following table summarizes the operator precedences in Python, |
| 847 | from lowest precedence (least binding) to highest precedence (most |
| 848 | binding). Operators in the same box have the same precedence. Unless |
| 849 | the syntax is explicitly given, operators are binary. Operators in |
| 850 | the same box group left to right (except for comparisons, which |
| 851 | chain from left to right --- see above). |
| 852 | |
| 853 | \begin{center} |
| 854 | \begin{tabular}{|c|c|} |
| 855 | \hline |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 856 | \keyword{or} & Boolean OR \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 857 | \hline |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 858 | \keyword{and} & Boolean AND \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 859 | \hline |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 860 | \keyword{not} \var{x} & Boolean NOT \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 861 | \hline |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 862 | \keyword{in}, \keyword{not} \keyword{in} & Membership tests \\ |
| 863 | \keyword{is}, \keyword{is not} & Identity tests \\ |
Guido van Rossum | c85be6a | 1998-05-16 02:11:10 +0000 | [diff] [blame] | 864 | \code{<}, \code{<=}, \code{>}, \code{>=}, \code{<>}, \code{!=}, \code{==} & |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 865 | Comparisons \\ |
| 866 | \hline |
| 867 | \code{|} & Bitwise OR \\ |
| 868 | \hline |
| 869 | \code{\^} & Bitwise XOR \\ |
| 870 | \hline |
| 871 | \code{\&} & Bitwise AND \\ |
| 872 | \hline |
| 873 | \code{<<}, \code{>>} & Shifts \\ |
| 874 | \hline |
| 875 | \code{+}, \code{-} & Addition and subtraction \\ |
| 876 | \hline |
| 877 | \code{*}, \code{/}, \code{\%} & Multiplication, division, remainder \\ |
| 878 | \hline |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 879 | \code{**} & Power \\ |
| 880 | \hline |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 881 | \code{+\var{x}}, \code{-\var{x}} & Positive, negative \\ |
| 882 | \code{\~\var{x}} & Bitwise not \\ |
| 883 | \hline |
| 884 | \code{\var{x}.\var{attribute}} & Attribute reference \\ |
| 885 | \code{\var{x}[\var{index}]} & Subscription \\ |
| 886 | \code{\var{x}[\var{index}:\var{index}]} & Slicing \\ |
| 887 | \code{\var{f}(\var{arguments}...)} & Function call \\ |
| 888 | \hline |
| 889 | \code{(\var{expressions}\ldots)} & Binding or tuple display \\ |
| 890 | \code{[\var{expressions}\ldots]} & List display \\ |
| 891 | \code{\{\var{key}:\var{datum}\ldots\}} & Dictionary display \\ |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame^] | 892 | \code{`\var{expressions}\ldots`} & String conversion \\ |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 893 | \hline |
| 894 | \end{tabular} |
| 895 | \end{center} |