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