Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 1 | \chapter{Expressions\label{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 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 11 | \begin{productionlist}[*] |
| 12 | \production{name}{\token{othername}} |
| 13 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 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 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 19 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 20 | \section{Arithmetic conversions\label{conversions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 21 | \indexii{arithmetic}{conversion} |
| 22 | |
| 23 | When a description of an arithmetic operator below uses the phrase |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 24 | ``the numeric arguments are converted to a common type,'' the |
| 25 | arguments are coerced using the coercion rules listed at the end of |
Fred Drake | deda9f3 | 2001-06-23 06:06:21 +0000 | [diff] [blame] | 26 | chapter \ref{datamodel}. If both arguments are standard numeric |
| 27 | types, the following coercions are applied: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 28 | |
| 29 | \begin{itemize} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 30 | \item If either argument is a complex number, the other is converted |
| 31 | to complex; |
| 32 | \item otherwise, if either argument is a floating point number, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 33 | the other is converted to floating point; |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 34 | \item otherwise, if either argument is a long integer, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 35 | the other is converted to long integer; |
| 36 | \item otherwise, both must be plain integers and no conversion |
| 37 | is necessary. |
| 38 | \end{itemize} |
| 39 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 40 | Some additional rules apply for certain operators (e.g., a string left |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 41 | argument to the `\%' operator). Extensions can define their own |
| 42 | coercions. |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | \section{Atoms\label{atoms}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 46 | \index{atom} |
| 47 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 48 | Atoms are the most basic elements of expressions. The simplest atoms |
| 49 | are identifiers or literals. Forms enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 50 | reverse quotes or in parentheses, brackets or braces are also |
| 51 | categorized syntactically as atoms. The syntax for atoms is: |
| 52 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 53 | \begin{productionlist} |
| 54 | \production{atom} |
| 55 | {\token{identifier} | \token{literal} | \token{enclosure}} |
| 56 | \production{enclosure} |
Fred Drake | 5381588 | 2002-03-15 23:21:37 +0000 | [diff] [blame] | 57 | {\token{parenth_form} | \token{list_display}} |
| 58 | \productioncont{| \token{dict_display} | \token{string_conversion}} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 59 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 60 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 61 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 62 | \subsection{Identifiers (Names)\label{atom-identifiers}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 63 | \index{name} |
| 64 | \index{identifier} |
| 65 | |
Jeremy Hylton | 53ed917 | 2002-04-01 20:52:24 +0000 | [diff] [blame^] | 66 | An identifier occurring as an atom is a name. See Section 4.1 for |
| 67 | documentation of naming and binding. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 68 | |
| 69 | When the name is bound to an object, evaluation of the atom yields |
| 70 | 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] | 71 | raises a \exception{NameError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 72 | \exindex{NameError} |
| 73 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 74 | \strong{Private name mangling:}% |
| 75 | \indexii{name}{mangling}% |
| 76 | \indexii{private}{names}% |
| 77 | when an identifier that textually occurs in a class definition begins |
| 78 | with two or more underscore characters and does not end in two or more |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 79 | underscores, it is considered a \dfn{private name} of that class. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 80 | Private names are transformed to a longer form before code is |
| 81 | generated for them. The transformation inserts the class name in |
| 82 | front of the name, with leading underscores removed, and a single |
| 83 | underscore inserted in front of the class name. For example, the |
| 84 | identifier \code{__spam} occurring in a class named \code{Ham} will be |
| 85 | transformed to \code{_Ham__spam}. This transformation is independent |
| 86 | of the syntactical context in which the identifier is used. If the |
| 87 | transformed name is extremely long (longer than 255 characters), |
| 88 | implementation defined truncation may happen. If the class name |
| 89 | consists only of underscores, no transformation is done. |
| 90 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 91 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 92 | \subsection{Literals\label{atom-literals}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 93 | \index{literal} |
| 94 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 95 | Python supports string literals and various numeric literals: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 96 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 97 | \begin{productionlist} |
| 98 | \production{literal} |
Fred Drake | 5381588 | 2002-03-15 23:21:37 +0000 | [diff] [blame] | 99 | {\token{stringliteral} | \token{integer} | \token{longinteger}} |
| 100 | \productioncont{| \token{floatnumber} | \token{imagnumber}} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 101 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 102 | |
| 103 | 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] | 104 | integer, long integer, floating point number, complex number) with the |
| 105 | given value. The value may be approximated in the case of floating |
| 106 | point and imaginary (complex) literals. See section \ref{literals} |
| 107 | for details. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 108 | |
| 109 | All literals correspond to immutable data types, and hence the |
| 110 | object's identity is less important than its value. Multiple |
| 111 | evaluations of literals with the same value (either the same |
| 112 | occurrence in the program text or a different occurrence) may obtain |
| 113 | the same object or a different object with the same value. |
| 114 | \indexiii{immutable}{data}{type} |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 115 | \indexii{immutable}{object} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 116 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 117 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 118 | \subsection{Parenthesized forms\label{parenthesized}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 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 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 124 | \begin{productionlist} |
| 125 | \production{parenth_form} |
| 126 | {"(" [\token{expression_list}] ")"} |
| 127 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 129 | A parenthesized expression list yields whatever that expression list |
| 130 | yields: if the list contains at least one comma, it yields a tuple; |
| 131 | otherwise, it yields the single expression that makes up the |
| 132 | expression list. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 133 | |
| 134 | An empty pair of parentheses yields an empty tuple object. Since |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 135 | tuples are immutable, the rules for literals apply (i.e., two |
| 136 | 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] | 137 | \indexii{empty}{tuple} |
| 138 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 139 | 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] | 140 | of the comma operator. The exception is the empty tuple, for which |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 141 | parentheses \emph{are} required --- allowing unparenthesized ``nothing'' |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 142 | in expressions would cause ambiguities and allow common typos to |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 143 | pass uncaught. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 144 | \index{comma} |
| 145 | \indexii{tuple}{display} |
| 146 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 147 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 148 | \subsection{List displays\label{lists}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 149 | \indexii{list}{display} |
Skip Montanaro | b655939 | 2000-09-11 16:31:55 +0000 | [diff] [blame] | 150 | \indexii{list}{comprehensions} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 152 | A list display is a possibly empty series of expressions enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 153 | square brackets: |
| 154 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 155 | \begin{productionlist} |
| 156 | \production{list_display} |
| 157 | {"[" [\token{listmaker}] "]"} |
| 158 | \production{listmaker} |
| 159 | {\token{expression} ( \token{list_for} |
| 160 | | ( "," \token{expression})* [","] )} |
| 161 | \production{list_iter} |
| 162 | {\token{list_for} | \token{list_if}} |
| 163 | \production{list_for} |
| 164 | {"for" \token{expression_list} "in" \token{testlist} |
| 165 | [\token{list_iter}]} |
| 166 | \production{list_if} |
| 167 | {"if" \token{test} [\token{list_iter}]} |
| 168 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 169 | |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 170 | A list display yields a new list object. Its contents are specified |
| 171 | by providing either a list of expressions or a list comprehension. |
Skip Montanaro | b655939 | 2000-09-11 16:31:55 +0000 | [diff] [blame] | 172 | \indexii{list}{comprehensions} |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 173 | When a comma-separated list of expressions is supplied, its elements are |
| 174 | evaluated from left to right and placed into the list object in that |
| 175 | order. When a list comprehension is supplied, it consists of a |
Skip Montanaro | 323fe5d | 2000-08-23 17:03:34 +0000 | [diff] [blame] | 176 | single expression followed by at least one \keyword{for} clause and zero or |
Tim Peters | 20524db | 2001-10-01 20:22:45 +0000 | [diff] [blame] | 177 | more \keyword{for} or \keyword{if} clauses. In this |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 178 | case, the elements of the new list are those that would be produced |
Skip Montanaro | 323fe5d | 2000-08-23 17:03:34 +0000 | [diff] [blame] | 179 | by considering each of the \keyword{for} or \keyword{if} clauses a block, |
Tim Peters | 20524db | 2001-10-01 20:22:45 +0000 | [diff] [blame] | 180 | nesting from |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 181 | left to right, and evaluating the expression to produce a list element |
| 182 | each time the innermost block is reached. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 183 | \obindex{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 184 | \indexii{empty}{list} |
| 185 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 186 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 187 | \subsection{Dictionary displays\label{dict}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 188 | \indexii{dictionary}{display} |
| 189 | |
| 190 | A dictionary display is a possibly empty series of key/datum pairs |
| 191 | enclosed in curly braces: |
| 192 | \index{key} |
| 193 | \index{datum} |
| 194 | \index{key/datum pair} |
| 195 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 196 | \begin{productionlist} |
| 197 | \production{dict_display} |
Fred Drake | 83d14c1 | 2002-03-16 06:35:54 +0000 | [diff] [blame] | 198 | {"\{" [\token{key_datum_list}] "\}"} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 199 | \production{key_datum_list} |
| 200 | {\token{key_datum} ("," \token{key_datum})* [","]} |
| 201 | \production{key_datum} |
| 202 | {\token{expression} ":" \token{expression}} |
| 203 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 204 | |
| 205 | A dictionary display yields a new dictionary object. |
| 206 | \obindex{dictionary} |
| 207 | |
| 208 | The key/datum pairs are evaluated from left to right to define the |
| 209 | entries of the dictionary: each key object is used as a key into the |
| 210 | dictionary to store the corresponding datum. |
| 211 | |
| 212 | 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] | 213 | section \ref{types}. (To summarize,the key type should be hashable, |
| 214 | which excludes all mutable objects.) Clashes between duplicate keys |
| 215 | are not detected; the last datum (textually rightmost in the display) |
| 216 | stored for a given key value prevails. |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 217 | \indexii{immutable}{object} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 218 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 219 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 220 | \subsection{String conversions\label{string-conversions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 221 | \indexii{string}{conversion} |
| 222 | \indexii{reverse}{quotes} |
| 223 | \indexii{backward}{quotes} |
| 224 | \index{back-quotes} |
| 225 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 226 | 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] | 227 | backward) quotes: |
| 228 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 229 | \begin{productionlist} |
| 230 | \production{string_conversion} |
| 231 | {"`" \token{expression_list} "`"} |
| 232 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 234 | A string conversion evaluates the contained expression list and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 235 | converts the resulting object into a string according to rules |
| 236 | specific to its type. |
| 237 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 238 | 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] | 239 | dictionary containing only objects whose type is one of these, the |
| 240 | resulting string is a valid Python expression which can be passed to |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 241 | the built-in function \function{eval()} to yield an expression with the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 242 | same value (or an approximation, if floating point numbers are |
| 243 | involved). |
| 244 | |
| 245 | (In particular, converting a string adds quotes around it and converts |
| 246 | ``funny'' characters to escape sequences that are safe to print.) |
| 247 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 248 | It is illegal to attempt to convert recursive objects (e.g., lists or |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 249 | dictionaries that contain a reference to themselves, directly or |
| 250 | indirectly.) |
| 251 | \obindex{recursive} |
| 252 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 253 | The built-in function \function{repr()} performs exactly the same |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 254 | conversion in its argument as enclosing it in parentheses and reverse |
| 255 | quotes does. The built-in function \function{str()} performs a |
| 256 | similar but more user-friendly conversion. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 257 | \bifuncindex{repr} |
| 258 | \bifuncindex{str} |
| 259 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 260 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 261 | \section{Primaries\label{primaries}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 262 | \index{primary} |
| 263 | |
| 264 | Primaries represent the most tightly bound operations of the language. |
| 265 | Their syntax is: |
| 266 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 267 | \begin{productionlist} |
| 268 | \production{primary} |
| 269 | {\token{atom} | \token{attributeref} |
| 270 | | \token{subscription} | \token{slicing} | \token{call}} |
| 271 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 272 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 273 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 274 | \subsection{Attribute references\label{attribute-references}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 275 | \indexii{attribute}{reference} |
| 276 | |
| 277 | An attribute reference is a primary followed by a period and a name: |
| 278 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 279 | \begin{productionlist} |
| 280 | \production{attributeref} |
| 281 | {\token{primary} "." \token{identifier}} |
| 282 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 283 | |
| 284 | The primary must evaluate to an object of a type that supports |
Fred Drake | 34bafcc | 2001-01-14 02:57:14 +0000 | [diff] [blame] | 285 | attribute references, e.g., a module, list, or an instance. This |
| 286 | object is then asked to produce the attribute whose name is the |
| 287 | identifier. If this attribute is not available, the exception |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 288 | \exception{AttributeError}\exindex{AttributeError} is raised. |
| 289 | Otherwise, the type and value of the object produced is determined by |
| 290 | the object. Multiple evaluations of the same attribute reference may |
| 291 | yield different objects. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 292 | \obindex{module} |
| 293 | \obindex{list} |
| 294 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 295 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 296 | \subsection{Subscriptions\label{subscriptions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 297 | \index{subscription} |
| 298 | |
| 299 | A subscription selects an item of a sequence (string, tuple or list) |
| 300 | or mapping (dictionary) object: |
| 301 | \obindex{sequence} |
| 302 | \obindex{mapping} |
| 303 | \obindex{string} |
| 304 | \obindex{tuple} |
| 305 | \obindex{list} |
| 306 | \obindex{dictionary} |
| 307 | \indexii{sequence}{item} |
| 308 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 309 | \begin{productionlist} |
| 310 | \production{subscription} |
| 311 | {\token{primary} "[" \token{expression_list} "]"} |
| 312 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 313 | |
| 314 | The primary must evaluate to an object of a sequence or mapping type. |
| 315 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 316 | If the primary is a mapping, the expression list must evaluate to an |
| 317 | object whose value is one of the keys of the mapping, and the |
| 318 | subscription selects the value in the mapping that corresponds to that |
| 319 | key. (The expression list is a tuple except if it has exactly one |
| 320 | item.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 322 | If the primary is a sequence, the expression (list) must evaluate to a |
| 323 | plain integer. If this value is negative, the length of the sequence |
| 324 | is added to it (so that, e.g., \code{x[-1]} selects the last item of |
| 325 | \code{x}.) The resulting value must be a nonnegative integer less |
| 326 | than the number of items in the sequence, and the subscription selects |
| 327 | the item whose index is that value (counting from zero). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 328 | |
| 329 | A string's items are characters. A character is not a separate data |
| 330 | type but a string of exactly one character. |
| 331 | \index{character} |
| 332 | \indexii{string}{item} |
| 333 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 334 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 335 | \subsection{Slicings\label{slicings}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 336 | \index{slicing} |
| 337 | \index{slice} |
| 338 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 339 | A slicing selects a range of items in a sequence object (e.g., a |
| 340 | string, tuple or list). Slicings may be used as expressions or as |
| 341 | targets in assignment or del statements. The syntax for a slicing: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 342 | \obindex{sequence} |
| 343 | \obindex{string} |
| 344 | \obindex{tuple} |
| 345 | \obindex{list} |
| 346 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 347 | \begin{productionlist} |
| 348 | \production{slicing} |
| 349 | {\token{simple_slicing} | \token{extended_slicing}} |
| 350 | \production{simple_slicing} |
| 351 | {\token{primary} "[" \token{short_slice} "]"} |
| 352 | \production{extended_slicing} |
| 353 | {\token{primary} "[" \token{slice_list} "]" } |
| 354 | \production{slice_list} |
| 355 | {\token{slice_item} ("," \token{slice_item})* [","]} |
| 356 | \production{slice_item} |
| 357 | {\token{expression} | \token{proper_slice} | \token{ellipsis}} |
| 358 | \production{proper_slice} |
| 359 | {\token{short_slice} | \token{long_slice}} |
| 360 | \production{short_slice} |
| 361 | {[\token{lower_bound}] ":" [\token{upper_bound}]} |
| 362 | \production{long_slice} |
| 363 | {\token{short_slice} ":" [\token{stride}]} |
| 364 | \production{lower_bound} |
| 365 | {\token{expression}} |
| 366 | \production{upper_bound} |
| 367 | {\token{expression}} |
| 368 | \production{stride} |
| 369 | {\token{expression}} |
| 370 | \production{ellipsis} |
| 371 | {"..."} |
| 372 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 374 | There is ambiguity in the formal syntax here: anything that looks like |
| 375 | an expression list also looks like a slice list, so any subscription |
| 376 | can be interpreted as a slicing. Rather than further complicating the |
| 377 | syntax, this is disambiguated by defining that in this case the |
| 378 | interpretation as a subscription takes priority over the |
| 379 | interpretation as a slicing (this is the case if the slice list |
| 380 | contains no proper slice nor ellipses). Similarly, when the slice |
| 381 | list has exactly one short slice and no trailing comma, the |
| 382 | interpretation as a simple slicing takes priority over that as an |
| 383 | extended slicing.\indexii{extended}{slicing} |
| 384 | |
| 385 | The semantics for a simple slicing are as follows. The primary must |
| 386 | evaluate to a sequence object. The lower and upper bound expressions, |
| 387 | if present, must evaluate to plain integers; defaults are zero and the |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 388 | \code{sys.maxint}, respectively. If either bound is negative, the |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 389 | sequence's length is added to it. The slicing now selects all items |
| 390 | with index \var{k} such that |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 391 | \code{\var{i} <= \var{k} < \var{j}} where \var{i} |
| 392 | and \var{j} are the specified lower and upper bounds. This may be an |
| 393 | empty sequence. It is not an error if \var{i} or \var{j} lie outside the |
| 394 | range of valid indexes (such items don't exist so they aren't |
| 395 | selected). |
| 396 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 397 | The semantics for an extended slicing are as follows. The primary |
| 398 | must evaluate to a mapping object, and it is indexed with a key that |
| 399 | is constructed from the slice list, as follows. If the slice list |
| 400 | contains at least one comma, the key is a tuple containing the |
| 401 | conversion of the slice items; otherwise, the conversion of the lone |
| 402 | slice item is the key. The conversion of a slice item that is an |
| 403 | expression is that expression. The conversion of an ellipsis slice |
| 404 | item is the built-in \code{Ellipsis} object. The conversion of a |
| 405 | proper slice is a slice object (see section \ref{types}) whose |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 406 | \member{start}, \member{stop} and \member{step} attributes are the |
| 407 | values of the expressions given as lower bound, upper bound and |
| 408 | stride, respectively, substituting \code{None} for missing |
| 409 | expressions. |
Fred Drake | 99cd573 | 1999-02-12 20:40:09 +0000 | [diff] [blame] | 410 | \withsubitem{(slice object attribute)}{\ttindex{start} |
| 411 | \ttindex{stop}\ttindex{step}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 412 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 413 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 414 | \subsection{Calls\label{calls}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 415 | \index{call} |
| 416 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 417 | 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] | 418 | series of arguments: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 419 | \obindex{callable} |
| 420 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 421 | \begin{productionlist} |
| 422 | \production{call} |
| 423 | {\token{primary} "(" [\token{argument_list} [","]] ")"} |
| 424 | \production{argument_list} |
Fred Drake | 5381588 | 2002-03-15 23:21:37 +0000 | [diff] [blame] | 425 | {\token{positional_arguments} ["," \token{keyword_arguments}} |
| 426 | \productioncont{ ["," "*" \token{expression} ["," "**" \token{expression}]]]} |
| 427 | \productioncont{| \token{keyword_arguments} ["," "*" \token{expression}} |
| 428 | \productioncont{ ["," "**" \token{expression}]]} |
| 429 | \productioncont{| "*" \token{expression} ["," "**" \token{expression}]} |
| 430 | \productioncont{| "**" \token{expression}} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 431 | \production{positional_arguments} |
| 432 | {\token{expression} ("," \token{expression})*} |
| 433 | \production{keyword_arguments} |
| 434 | {\token{keyword_item} ("," \token{keyword_item})*} |
| 435 | \production{keyword_item} |
| 436 | {\token{identifier} "=" \token{expression}} |
| 437 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 439 | A trailing comma may be present after an argument list but does not |
| 440 | affect the semantics. |
| 441 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 442 | The primary must evaluate to a callable object (user-defined |
| 443 | functions, built-in functions, methods of built-in objects, class |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 444 | objects, methods of class instances, and certain class instances |
| 445 | themselves are callable; extensions may define additional callable |
| 446 | object types). All argument expressions are evaluated before the call |
| 447 | is attempted. Please refer to section \ref{function} for the syntax |
| 448 | of formal parameter lists. |
| 449 | |
| 450 | If keyword arguments are present, they are first converted to |
| 451 | positional arguments, as follows. First, a list of unfilled slots is |
| 452 | created for the formal parameters. If there are N positional |
| 453 | arguments, they are placed in the first N slots. Next, for each |
| 454 | keyword argument, the identifier is used to determine the |
| 455 | corresponding slot (if the identifier is the same as the first formal |
| 456 | parameter name, the first slot is used, and so on). If the slot is |
| 457 | already filled, a \exception{TypeError} exception is raised. |
| 458 | Otherwise, the value of the argument is placed in the slot, filling it |
| 459 | (even if the expression is \code{None}, it fills the slot). When all |
| 460 | arguments have been processed, the slots that are still unfilled are |
| 461 | filled with the corresponding default value from the function |
| 462 | definition. (Default values are calculated, once, when the function |
| 463 | is defined; thus, a mutable object such as a list or dictionary used |
| 464 | as default value will be shared by all calls that don't specify an |
| 465 | argument value for the corresponding slot; this should usually be |
| 466 | avoided.) If there are any unfilled slots for which no default value |
| 467 | is specified, a \exception{TypeError} exception is raised. Otherwise, |
| 468 | the list of filled slots is used as the argument list for the call. |
| 469 | |
| 470 | If there are more positional arguments than there are formal parameter |
| 471 | slots, a \exception{TypeError} exception is raised, unless a formal |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 472 | parameter using the syntax \samp{*identifier} is present; in this |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 473 | case, that formal parameter receives a tuple containing the excess |
| 474 | positional arguments (or an empty tuple if there were no excess |
| 475 | positional arguments). |
| 476 | |
| 477 | If any keyword argument does not correspond to a formal parameter |
| 478 | name, a \exception{TypeError} exception is raised, unless a formal |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 479 | parameter using the syntax \samp{**identifier} is present; in this |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 480 | case, that formal parameter receives a dictionary containing the |
| 481 | excess keyword arguments (using the keywords as keys and the argument |
| 482 | values as corresponding values), or a (new) empty dictionary if there |
| 483 | were no excess keyword arguments. |
| 484 | |
Michael W. Hudson | 850d398 | 2001-12-12 11:56:33 +0000 | [diff] [blame] | 485 | If the syntax \samp{*expression} appears in the function call, |
| 486 | \samp{expression} must evaluate to a sequence. Elements from this |
| 487 | sequence are treated as if they were additional positional arguments; |
| 488 | if there are postional arguments \var{x1},...,\var{xN} , and |
| 489 | \samp{expression} evaluates to a sequence \var{y1},...,\var{yM}, this |
| 490 | is equivalent to a call with M+N positional arguments |
| 491 | \var{x1},...,\var{xN},\var{y1},...,\var{yM}. |
| 492 | |
| 493 | A consequence of this is that although the \samp{*expression} syntax |
| 494 | appears \emph{after} any keyword arguments, it is processed |
Fred Drake | b062cb2 | 2001-12-14 16:57:31 +0000 | [diff] [blame] | 495 | \emph{before} the keyword arguments (and the |
| 496 | \samp{**expression} argument, if any -- see below). So: |
Michael W. Hudson | 850d398 | 2001-12-12 11:56:33 +0000 | [diff] [blame] | 497 | |
| 498 | \begin{verbatim} |
| 499 | >>> def f(a, b): |
| 500 | ... print a, b |
| 501 | ... |
| 502 | >>> f(b=1, *(2,)) |
| 503 | 2 1 |
| 504 | >>> f(a=1, *(2,)) |
| 505 | Traceback (most recent call last): |
| 506 | File "<stdin>", line 1, in ? |
| 507 | TypeError: f() got multiple values for keyword argument 'a' |
| 508 | >>> f(1, *(2,)) |
| 509 | 1 2 |
| 510 | \end{verbatim} |
| 511 | |
Fred Drake | b062cb2 | 2001-12-14 16:57:31 +0000 | [diff] [blame] | 512 | It is unusual for both keyword arguments and the |
| 513 | \samp{*expression} syntax to be used in the same call, so in practice |
| 514 | this confusion does not arise. |
Michael W. Hudson | 850d398 | 2001-12-12 11:56:33 +0000 | [diff] [blame] | 515 | |
| 516 | If the syntax \samp{**expression} appears in the function call, |
| 517 | \samp{expression} must evaluate to a (subclass of) dictionary, the |
| 518 | contents of which are treated as additional keyword arguments. In the |
| 519 | case of a keyword appearing in both \samp{expression} and as an |
| 520 | explicit keyword argument, a \exception{TypeError} exception is |
| 521 | raised. |
| 522 | |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 523 | Formal parameters using the syntax \samp{*identifier} or |
| 524 | \samp{**identifier} cannot be used as positional argument slots or |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 525 | as keyword argument names. Formal parameters using the syntax |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 526 | \samp{(sublist)} cannot be used as keyword argument names; the |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 527 | outermost sublist corresponds to a single unnamed argument slot, and |
| 528 | the argument value is assigned to the sublist using the usual tuple |
| 529 | assignment rules after all other parameter processing is done. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 530 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 531 | A call always returns some value, possibly \code{None}, unless it |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 532 | 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] | 533 | of the callable object. |
| 534 | |
| 535 | If it is--- |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 536 | |
| 537 | \begin{description} |
| 538 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 539 | \item[a user-defined function:] The code block for the function is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 540 | executed, passing it the argument list. The first thing the code |
| 541 | block will do is bind the formal parameters to the arguments; this is |
| 542 | described in section \ref{function}. When the code block executes a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 543 | \keyword{return} statement, this specifies the return value of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 544 | function call. |
| 545 | \indexii{function}{call} |
| 546 | \indexiii{user-defined}{function}{call} |
| 547 | \obindex{user-defined function} |
| 548 | \obindex{function} |
| 549 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 550 | \item[a built-in function or method:] The result is up to the |
Fred Drake | 3d83fc3 | 2000-07-31 20:08:23 +0000 | [diff] [blame] | 551 | interpreter; see the \citetitle[../lib/built-in-funcs.html]{Python |
| 552 | Library Reference} for the descriptions of built-in functions and |
| 553 | methods. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 554 | \indexii{function}{call} |
| 555 | \indexii{built-in function}{call} |
| 556 | \indexii{method}{call} |
| 557 | \indexii{built-in method}{call} |
| 558 | \obindex{built-in method} |
| 559 | \obindex{built-in function} |
| 560 | \obindex{method} |
| 561 | \obindex{function} |
| 562 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 563 | \item[a class object:] A new instance of that class is returned. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 564 | \obindex{class} |
| 565 | \indexii{class object}{call} |
| 566 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 567 | \item[a class instance method:] The corresponding user-defined |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 568 | function is called, with an argument list that is one longer than the |
| 569 | argument list of the call: the instance becomes the first argument. |
| 570 | \obindex{class instance} |
| 571 | \obindex{instance} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 572 | \indexii{class instance}{call} |
| 573 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 574 | \item[a class instance:] The class must define a \method{__call__()} |
| 575 | method; the effect is then the same as if that method was called. |
| 576 | \indexii{instance}{call} |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 577 | \withsubitem{(object method)}{\ttindex{__call__()}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 578 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 579 | \end{description} |
| 580 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 581 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 582 | \section{The power operator\label{power}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 583 | |
| 584 | The power operator binds more tightly than unary operators on its |
| 585 | left; it binds less tightly than unary operators on its right. The |
| 586 | syntax is: |
| 587 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 588 | \begin{productionlist} |
| 589 | \production{power} |
| 590 | {\token{primary} ["**" \token{u_expr}]} |
| 591 | \end{productionlist} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 592 | |
| 593 | Thus, in an unparenthesized sequence of power and unary operators, the |
| 594 | operators are evaluated from right to left (this does not constrain |
| 595 | the evaluation order for the operands). |
| 596 | |
| 597 | The power operator has the same semantics as the built-in |
| 598 | \function{pow()} function, when called with two arguments: it yields |
| 599 | its left argument raised to the power of its right argument. The |
| 600 | numeric arguments are first converted to a common type. The result |
| 601 | type is that of the arguments after coercion; if the result is not |
| 602 | expressible in that type (as in raising an integer to a negative |
| 603 | power, or a negative floating point number to a broken power), a |
| 604 | \exception{TypeError} exception is raised. |
| 605 | |
| 606 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 607 | \section{Unary arithmetic operations \label{unary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 608 | \indexiii{unary}{arithmetic}{operation} |
| 609 | \indexiii{unary}{bit-wise}{operation} |
| 610 | |
| 611 | All unary arithmetic (and bit-wise) operations have the same priority: |
| 612 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 613 | \begin{productionlist} |
| 614 | \production{u_expr} |
| 615 | {\token{power} | "-" \token{u_expr} |
Fred Drake | f6eafc3 | 2002-03-18 16:47:14 +0000 | [diff] [blame] | 616 | | "+" \token{u_expr} | "{\~}" \token{u_expr}} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 617 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 618 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 619 | The unary \code{-} (minus) operator yields the negation of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 620 | numeric argument. |
| 621 | \index{negation} |
| 622 | \index{minus} |
| 623 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 624 | The unary \code{+} (plus) operator yields its numeric argument |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 625 | unchanged. |
| 626 | \index{plus} |
| 627 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 628 | The unary \code{\~} (invert) operator yields the bit-wise inversion |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 629 | 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] | 630 | \code{x} is defined as \code{-(x+1)}. It only applies to integral |
| 631 | numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 632 | \index{inversion} |
| 633 | |
| 634 | 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] | 635 | a \exception{TypeError} exception is raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 636 | \exindex{TypeError} |
| 637 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 638 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 639 | \section{Binary arithmetic operations\label{binary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 640 | \indexiii{binary}{arithmetic}{operation} |
| 641 | |
| 642 | The binary arithmetic operations have the conventional priority |
| 643 | levels. Note that some of these operations also apply to certain |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 644 | non-numeric types. Apart from the power operator, there are only two |
| 645 | levels, one for multiplicative operators and one for additive |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 646 | operators: |
| 647 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 648 | \begin{productionlist} |
| 649 | \production{m_expr} |
| 650 | {\token{u_expr} | \token{m_expr} "*" \token{u_expr} |
Fred Drake | 5381588 | 2002-03-15 23:21:37 +0000 | [diff] [blame] | 651 | | \token{m_expr} "/" \token{u_expr}} |
| 652 | \productioncont{| \token{m_expr} "\%" \token{u_expr}} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 653 | \production{a_expr} |
| 654 | {\token{m_expr} | \token{aexpr} "+" \token{m_expr} |
| 655 | \token{aexpr} "-" \token{m_expr}} |
| 656 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 657 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 658 | The \code{*} (multiplication) operator yields the product of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 659 | arguments. The arguments must either both be numbers, or one argument |
Fred Drake | c3b18d7 | 2000-12-07 04:54:02 +0000 | [diff] [blame] | 660 | must be an integer (plain or long) and the other must be a sequence. |
| 661 | In the former case, the numbers are converted to a common type and |
| 662 | then multiplied together. In the latter case, sequence repetition is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 663 | performed; a negative repetition factor yields an empty sequence. |
| 664 | \index{multiplication} |
| 665 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 666 | The \code{/} (division) operator yields the quotient of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 667 | arguments. The numeric arguments are first converted to a common |
| 668 | type. Plain or long integer division yields an integer of the same |
| 669 | type; the result is that of mathematical division with the `floor' |
| 670 | function applied to the result. Division by zero raises the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 671 | \exception{ZeroDivisionError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 672 | \exindex{ZeroDivisionError} |
| 673 | \index{division} |
| 674 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 675 | The \code{\%} (modulo) operator yields the remainder from the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 676 | division of the first argument by the second. The numeric arguments |
| 677 | are first converted to a common type. A zero right argument raises |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 678 | the \exception{ZeroDivisionError} exception. The arguments may be floating |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 679 | point numbers, e.g., \code{3.14\%0.7} equals \code{0.34} (since |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 680 | \code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always |
| 681 | yields a result with the same sign as its second operand (or zero); |
| 682 | the absolute value of the result is strictly smaller than the second |
| 683 | operand. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 684 | \index{modulo} |
| 685 | |
| 686 | The integer division and modulo operators are connected by the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 687 | following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and |
| 688 | modulo are also connected with the built-in function \function{divmod()}: |
| 689 | \code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for |
Fred Drake | 1ea7c75 | 1999-05-06 14:46:35 +0000 | [diff] [blame] | 690 | floating point and complex numbers; there similar identities hold |
| 691 | approximately where \code{x/y} is replaced by \code{floor(x/y)}) or |
| 692 | \code{floor(x/y) - 1} (for floats),\footnote{ |
| 693 | If x is very close to an exact integer multiple of y, it's |
| 694 | possible for \code{floor(x/y)} to be one larger than |
| 695 | \code{(x-x\%y)/y} due to rounding. In such cases, Python returns |
| 696 | the latter result, in order to preserve that \code{divmod(x,y)[0] |
| 697 | * y + x \%{} y} be very close to \code{x}. |
| 698 | } or \code{floor((x/y).real)} (for |
| 699 | complex). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 700 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 701 | The \code{+} (addition) operator yields the sum of its arguments. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 702 | The arguments must either both be numbers or both sequences of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 703 | same type. In the former case, the numbers are converted to a common |
| 704 | type and then added together. In the latter case, the sequences are |
| 705 | concatenated. |
| 706 | \index{addition} |
| 707 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 708 | The \code{-} (subtraction) operator yields the difference of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 709 | arguments. The numeric arguments are first converted to a common |
| 710 | type. |
| 711 | \index{subtraction} |
| 712 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 713 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 714 | \section{Shifting operations\label{shifting}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 715 | \indexii{shifting}{operation} |
| 716 | |
| 717 | The shifting operations have lower priority than the arithmetic |
| 718 | operations: |
| 719 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 720 | \begin{productionlist} |
| 721 | \production{shift_expr} |
| 722 | {\token{a_expr} |
| 723 | | \token{shift_expr} ( "<<" | ">>" ) \token{a_expr}} |
| 724 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 725 | |
| 726 | These operators accept plain or long integers as arguments. The |
| 727 | arguments are converted to a common type. They shift the first |
| 728 | argument to the left or right by the number of bits given by the |
| 729 | second argument. |
| 730 | |
| 731 | A right shift by \var{n} bits is defined as division by |
| 732 | \code{pow(2,\var{n})}. A left shift by \var{n} bits is defined as |
| 733 | 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] | 734 | no overflow check so in that case the operation drops bits and flips |
| 735 | the sign if the result is not less than \code{pow(2,31)} in absolute |
| 736 | value. Negative shift counts raise a \exception{ValueError} |
| 737 | exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 738 | \exindex{ValueError} |
| 739 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 740 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 741 | \section{Binary bit-wise operations\label{bitwise}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 742 | \indexiii{binary}{bit-wise}{operation} |
| 743 | |
| 744 | Each of the three bitwise operations has a different priority level: |
| 745 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 746 | \begin{productionlist} |
| 747 | \production{and_expr} |
| 748 | {\token{shift_expr} | \token{and_expr} "\&" \token{shift_expr}} |
| 749 | \production{xor_expr} |
| 750 | {\token{and_expr} | \token{xor_expr} "\textasciicircum" \token{and_expr}} |
| 751 | \production{or_expr} |
| 752 | {\token{xor_expr} | \token{or_expr} "|" \token{xor_expr}} |
| 753 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 754 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 755 | The \code{\&} operator yields the bitwise AND of its arguments, which |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 756 | must be plain or long integers. The arguments are converted to a |
| 757 | common type. |
| 758 | \indexii{bit-wise}{and} |
| 759 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 760 | The \code{\^} operator yields the bitwise XOR (exclusive OR) of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 761 | arguments, which must be plain or long integers. The arguments are |
| 762 | converted to a common type. |
| 763 | \indexii{bit-wise}{xor} |
| 764 | \indexii{exclusive}{or} |
| 765 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 766 | The \code{|} operator yields the bitwise (inclusive) OR of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 767 | arguments, which must be plain or long integers. The arguments are |
| 768 | converted to a common type. |
| 769 | \indexii{bit-wise}{or} |
| 770 | \indexii{inclusive}{or} |
| 771 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 772 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 773 | \section{Comparisons\label{comparisons}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 774 | \index{comparison} |
| 775 | |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 776 | Unlike C, all comparison operations in Python have the same priority, |
| 777 | which is lower than that of any arithmetic, shifting or bitwise |
| 778 | operation. Also unlike C, expressions like \code{a < b < c} have the |
| 779 | interpretation that is conventional in mathematics: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 780 | \indexii{C}{language} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 781 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 782 | \begin{productionlist} |
| 783 | \production{comparison} |
| 784 | {\token{or_expr} ( \token{comp_operator} \token{or_expr} )*} |
| 785 | \production{comp_operator} |
Fred Drake | 5381588 | 2002-03-15 23:21:37 +0000 | [diff] [blame] | 786 | {"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="} |
| 787 | \productioncont{| "is" ["not"] | ["not"] "in"} |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 788 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 789 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 790 | Comparisons yield integer values: \code{1} for true, \code{0} for false. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 792 | Comparisons can be chained arbitrarily, e.g., \code{x < y <= z} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 793 | equivalent to \code{x < y and y <= z}, except that \code{y} is |
| 794 | evaluated only once (but in both cases \code{z} is not evaluated at all |
| 795 | when \code{x < y} is found to be false). |
| 796 | \indexii{chaining}{comparisons} |
| 797 | |
| 798 | Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are |
| 799 | expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison |
| 800 | 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] | 801 | 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] | 802 | \var{y opy z}, except that each expression is evaluated at most once. |
| 803 | |
| 804 | 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] | 805 | 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] | 806 | perfectly legal (though perhaps not pretty). |
| 807 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 808 | The forms \code{<>} and \code{!=} are equivalent; for consistency with |
| 809 | C, \code{!=} is preferred; where \code{!=} is mentioned below |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 810 | \code{<>} is also accepted. The \code{<>} spelling is considered |
| 811 | obsolescent. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 812 | |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 813 | The operators \code{<}, \code{>}, \code{==}, \code{>=}, \code{<=}, and |
| 814 | \code{!=} compare |
| 815 | the values of two objects. The objects need not have the same type. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 816 | If both are numbers, they are coverted to a common type. Otherwise, |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 817 | objects of different types \emph{always} compare unequal, and are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 818 | ordered consistently but arbitrarily. |
| 819 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 820 | (This unusual definition of comparison was used to simplify the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 821 | definition of operations like sorting and the \keyword{in} and |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 822 | \keyword{not in} operators. In the future, the comparison rules for |
| 823 | objects of different types are likely to change.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 824 | |
| 825 | Comparison of objects of the same type depends on the type: |
| 826 | |
| 827 | \begin{itemize} |
| 828 | |
| 829 | \item |
| 830 | Numbers are compared arithmetically. |
| 831 | |
| 832 | \item |
| 833 | Strings are compared lexicographically using the numeric equivalents |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 834 | (the result of the built-in function \function{ord()}) of their |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 835 | characters. Unicode and 8-bit strings are fully interoperable in this |
| 836 | behavior. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 837 | |
| 838 | \item |
| 839 | Tuples and lists are compared lexicographically using comparison of |
| 840 | corresponding items. |
| 841 | |
| 842 | \item |
Tim Peters | 20524db | 2001-10-01 20:22:45 +0000 | [diff] [blame] | 843 | Mappings (dictionaries) compare equal if and only if their sorted |
| 844 | (key, value) lists compare equal.\footnote{The implementation computes |
| 845 | this efficiently, without constructing lists or sorting.} |
| 846 | Outcomes other than equality are resolved consistently, but are not |
Tim Peters | 1350c07 | 2001-10-01 20:25:26 +0000 | [diff] [blame] | 847 | otherwise defined.\footnote{Earlier versions of Python used |
Tim Peters | 20524db | 2001-10-01 20:22:45 +0000 | [diff] [blame] | 848 | lexicographic comparison of the sorted (key, value) lists, but this |
| 849 | was very expensive for the common case of comparing for equality. An |
| 850 | even earlier version of Python compared dictionaries by identity only, |
| 851 | but this caused surprises because people expected to be able to test |
| 852 | a dictionary for emptiness by comparing it to \code{\{\}}.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 853 | |
| 854 | \item |
| 855 | Most other types compare unequal unless they are the same object; |
| 856 | the choice whether one object is considered smaller or larger than |
| 857 | another one is made arbitrarily but consistently within one |
| 858 | execution of a program. |
| 859 | |
| 860 | \end{itemize} |
| 861 | |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 862 | The operators \keyword{in} and \keyword{not in} test for set |
Fred Drake | ac79e95 | 2001-03-06 07:32:11 +0000 | [diff] [blame] | 863 | membership. \code{\var{x} in \var{s}} evaluates to true if \var{x} |
| 864 | is a member of the set \var{s}, and false otherwise. \code{\var{x} |
| 865 | not in \var{s}} returns the negation of \code{\var{x} in \var{s}}. |
| 866 | The set membership test has traditionally been bound to sequences; an |
| 867 | object is a member of a set if the set is a sequence and contains an |
| 868 | element equal to that object. However, it is possible for an object |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 869 | to support membership tests without being a sequence. In particular, |
| 870 | dictionaries support memership testing as a nicer way of spelling |
| 871 | \code{\var{key} in \var{dict}}; other mapping types may follow suit. |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 872 | |
Fred Drake | 34bafcc | 2001-01-14 02:57:14 +0000 | [diff] [blame] | 873 | For the list and tuple types, \code{\var{x} in \var{y}} is true if and |
Fred Drake | ac79e95 | 2001-03-06 07:32:11 +0000 | [diff] [blame] | 874 | only if there exists an index \var{i} such that |
Fred Drake | 34bafcc | 2001-01-14 02:57:14 +0000 | [diff] [blame] | 875 | \code{\var{x} == \var{y}[\var{i}]} is true. |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 876 | |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 877 | For the Unicode and string types, \code{\var{x} in \var{y}} is true if |
| 878 | and only if there exists an index \var{i} such that \code{\var{x} == |
| 879 | \var{y}[\var{i}]} is true. If \code{\var{x}} is not a string or |
| 880 | Unicode object of length \code{1}, a \exception{TypeError} exception |
| 881 | is raised. |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 882 | |
| 883 | For user-defined classes which define the \method{__contains__()} method, |
| 884 | \code{\var{x} in \var{y}} is true if and only if |
| 885 | \code{\var{y}.__contains__(\var{x})} is true. |
| 886 | |
| 887 | For user-defined classes which do not define \method{__contains__()} and |
Fred Drake | 1156f62 | 2000-09-19 18:10:05 +0000 | [diff] [blame] | 888 | do define \method{__getitem__()}, \code{\var{x} in \var{y}} is true if |
| 889 | and only if there is a non-negative integer index \var{i} such that |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 890 | \code{\var{x} == \var{y}[\var{i}]}, and all lower integer indices |
| 891 | do not raise \exception{IndexError} exception. (If any other exception |
| 892 | is raised, it is as if \keyword{in} raised that exception). |
| 893 | |
| 894 | The operator \keyword{not in} is defined to have the inverse true value |
| 895 | of \keyword{in}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 896 | \opindex{in} |
| 897 | \opindex{not in} |
| 898 | \indexii{membership}{test} |
| 899 | \obindex{sequence} |
| 900 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 901 | The operators \keyword{is} and \keyword{is not} test for object identity: |
| 902 | \code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y} |
| 903 | 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] | 904 | truth value. |
| 905 | \opindex{is} |
| 906 | \opindex{is not} |
| 907 | \indexii{identity}{test} |
| 908 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 909 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 910 | \section{Boolean operations\label{Booleans}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 911 | \indexii{Boolean}{operation} |
| 912 | |
| 913 | Boolean operations have the lowest priority of all Python operations: |
| 914 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 915 | \begin{productionlist} |
| 916 | \production{expression} |
| 917 | {\token{or_test} | \token{lambda_form}} |
| 918 | \production{or_test} |
| 919 | {\token{and_test} | \token{or_test} "or" \token{and_test}} |
| 920 | \production{and_test} |
| 921 | {\token{not_test} | \token{and_test} "and" \token{not_test}} |
| 922 | \production{not_test} |
| 923 | {\token{comparison} | "not" \token{not_test}} |
| 924 | \production{lambda_form} |
| 925 | {"lambda" [\token{parameter_list}]: \token{expression}} |
| 926 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 927 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 928 | In the context of Boolean operations, and also when expressions are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 929 | used by control flow statements, the following values are interpreted |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 930 | as false: \code{None}, numeric zero of all types, empty sequences |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 931 | (strings, tuples and lists), and empty mappings (dictionaries). All |
| 932 | other values are interpreted as true. |
| 933 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 934 | The operator \keyword{not} yields \code{1} if its argument is false, |
| 935 | \code{0} otherwise. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 936 | \opindex{not} |
| 937 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 938 | 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] | 939 | \var{x} is false, its value is returned; otherwise, \var{y} is |
| 940 | evaluated and the resulting value is returned. |
| 941 | \opindex{and} |
| 942 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 943 | 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] | 944 | \var{x} is true, its value is returned; otherwise, \var{y} is |
| 945 | evaluated and the resulting value is returned. |
| 946 | \opindex{or} |
| 947 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 948 | (Note that neither \keyword{and} nor \keyword{or} restrict the value |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 949 | and type they return to \code{0} and \code{1}, but rather return the |
| 950 | last evaluated argument. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 951 | 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] | 952 | replaced by a default value if it is empty, the expression |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 953 | \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] | 954 | invent a value anyway, it does not bother to return a value of the |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 955 | same type as its argument, so e.g., \code{not 'foo'} yields \code{0}, |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 956 | not \code{''}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 957 | |
| 958 | Lambda forms (lambda expressions) have the same syntactic position as |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 959 | expressions. They are a shorthand to create anonymous functions; the |
| 960 | expression \code{lambda \var{arguments}: \var{expression}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 961 | yields a function object that behaves virtually identical to one |
| 962 | defined with |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 963 | |
| 964 | \begin{verbatim} |
| 965 | def name(arguments): |
| 966 | return expression |
| 967 | \end{verbatim} |
| 968 | |
| 969 | See section \ref{function} for the syntax of parameter lists. Note |
| 970 | that functions created with lambda forms cannot contain statements. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 971 | \label{lambda} |
| 972 | \indexii{lambda}{expression} |
| 973 | \indexii{lambda}{form} |
| 974 | \indexii{anonmymous}{function} |
| 975 | |
Fred Drake | 8838269 | 2001-06-05 02:17:02 +0000 | [diff] [blame] | 976 | \strong{Programmer's note:} Prior to Python 2.1, a lambda form defined |
| 977 | inside a function has no access to names defined in the function's |
| 978 | namespace. This is because Python had only two scopes: local and |
| 979 | global. A common work-around was to use default argument values to |
| 980 | pass selected variables into the lambda's namespace, e.g.: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 981 | |
| 982 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 983 | def make_incrementor(increment): |
| 984 | return lambda x, n=increment: x+n |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 985 | \end{verbatim} |
| 986 | |
Fred Drake | 8838269 | 2001-06-05 02:17:02 +0000 | [diff] [blame] | 987 | As of Python 2.1, nested scopes were introduced, and this work-around |
| 988 | has not been necessary. Python 2.1 supports nested scopes in modules |
| 989 | which include the statement \samp{from __future__ import |
| 990 | nested_scopes}, and more recent versions of Python enable nested |
| 991 | scopes by default. This version works starting with Python 2.1: |
| 992 | |
| 993 | \begin{verbatim} |
| 994 | from __future__ import nested_scopes |
| 995 | |
| 996 | def make_incrementor(increment): |
| 997 | return lambda x: x+increment |
| 998 | \end{verbatim} |
| 999 | |
| 1000 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 1001 | \section{Expression lists\label{exprlists}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 1002 | \indexii{expression}{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1003 | |
Fred Drake | cb4638a | 2001-07-06 22:49:53 +0000 | [diff] [blame] | 1004 | \begin{productionlist} |
| 1005 | \production{expression_list} |
| 1006 | {\token{expression} ( "," \token{expression} )* [","]} |
| 1007 | \end{productionlist} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1008 | |
Fred Drake | c009d19 | 2000-04-25 21:09:10 +0000 | [diff] [blame] | 1009 | An expression list containing at least one comma yields a |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 1010 | tuple. The length of the tuple is the number of expressions in the |
| 1011 | list. The expressions are evaluated from left to right. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1012 | \obindex{tuple} |
| 1013 | |
| 1014 | The trailing comma is required only to create a single tuple (a.k.a. a |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1015 | \emph{singleton}); it is optional in all other cases. A single |
Fred Drake | c009d19 | 2000-04-25 21:09:10 +0000 | [diff] [blame] | 1016 | expression without a trailing comma doesn't create a |
| 1017 | tuple, but rather yields the value of that expression. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1018 | (To create an empty tuple, use an empty pair of parentheses: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 1019 | \code{()}.) |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 1020 | \indexii{trailing}{comma} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1021 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 1022 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 1023 | \section{Summary\label{summary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1024 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 1025 | The following table summarizes the operator |
| 1026 | precedences\indexii{operator}{precedence} in Python, from lowest |
| 1027 | precedence (least binding) to highest precedence (most binding). |
| 1028 | Operators in the same box have the same precedence. Unless the syntax |
| 1029 | is explicitly given, operators are binary. Operators in the same box |
| 1030 | group left to right (except for comparisons, which chain from left to |
Fred Drake | 2a22200 | 2000-12-11 22:39:24 +0000 | [diff] [blame] | 1031 | right --- see above, and exponentiation, which groups from right to |
| 1032 | left). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1033 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 1034 | \begin{tableii}{c|l}{textrm}{Operator}{Description} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1035 | \lineii{\keyword{lambda}} {Lambda expression} |
| 1036 | \hline |
| 1037 | \lineii{\keyword{or}} {Boolean OR} |
| 1038 | \hline |
| 1039 | \lineii{\keyword{and}} {Boolean AND} |
| 1040 | \hline |
| 1041 | \lineii{\keyword{not} \var{x}} {Boolean NOT} |
| 1042 | \hline |
| 1043 | \lineii{\keyword{in}, \keyword{not} \keyword{in}}{Membership tests} |
| 1044 | \lineii{\keyword{is}, \keyword{is not}}{Identity tests} |
| 1045 | \lineii{\code{<}, \code{<=}, \code{>}, \code{>=}, |
Fred Drake | 9beee80 | 1998-10-21 00:44:49 +0000 | [diff] [blame] | 1046 | \code{<>}, \code{!=}, \code{==}} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1047 | {Comparisons} |
| 1048 | \hline |
| 1049 | \lineii{\code{|}} {Bitwise OR} |
| 1050 | \hline |
| 1051 | \lineii{\code{\^}} {Bitwise XOR} |
| 1052 | \hline |
| 1053 | \lineii{\code{\&}} {Bitwise AND} |
| 1054 | \hline |
Fred Drake | 24e7a29 | 2001-04-12 12:37:03 +0000 | [diff] [blame] | 1055 | \lineii{\code{<}\code{<}, \code{>}\code{>}} {Shifts} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1056 | \hline |
| 1057 | \lineii{\code{+}, \code{-}}{Addition and subtraction} |
| 1058 | \hline |
Fred Drake | 9beee80 | 1998-10-21 00:44:49 +0000 | [diff] [blame] | 1059 | \lineii{\code{*}, \code{/}, \code{\%}} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1060 | {Multiplication, division, remainder} |
| 1061 | \hline |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1062 | \lineii{\code{+\var{x}}, \code{-\var{x}}} {Positive, negative} |
| 1063 | \lineii{\code{\~\var{x}}} {Bitwise not} |
| 1064 | \hline |
Fred Drake | b8ac009 | 2001-05-09 16:51:49 +0000 | [diff] [blame] | 1065 | \lineii{\code{**}} {Exponentiation} |
| 1066 | \hline |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1067 | \lineii{\code{\var{x}.\var{attribute}}} {Attribute reference} |
| 1068 | \lineii{\code{\var{x}[\var{index}]}} {Subscription} |
| 1069 | \lineii{\code{\var{x}[\var{index}:\var{index}]}} {Slicing} |
| 1070 | \lineii{\code{\var{f}(\var{arguments}...)}} {Function call} |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 1071 | \hline |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 1072 | \lineii{\code{(\var{expressions}\ldots)}} {Binding or tuple display} |
| 1073 | \lineii{\code{[\var{expressions}\ldots]}} {List display} |
| 1074 | \lineii{\code{\{\var{key}:\var{datum}\ldots\}}}{Dictionary display} |
| 1075 | \lineii{\code{`\var{expressions}\ldots`}} {String conversion} |
| 1076 | \end{tableii} |