Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1 | \section{Built-in Types \label{types}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 2 | |
| 3 | The following sections describe the standard types that are built into |
| 4 | the interpreter. These are the numeric types, sequence types, and |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 5 | several others, including types themselves. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 6 | \indexii{built-in}{types} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 7 | |
| 8 | Some operations are supported by several object types; in particular, |
| 9 | all objects can be compared, tested for truth value, and converted to |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 10 | a string (with the \code{`\textrm{\ldots}`} notation). The latter |
| 11 | conversion is implicitly used when an object is written by the |
| 12 | \keyword{print}\stindex{print} statement. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 13 | |
| 14 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 15 | \subsection{Truth Value Testing \label{truth}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 16 | |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 17 | Any object can be tested for truth value, for use in an \keyword{if} or |
| 18 | \keyword{while} condition or as operand of the Boolean operations below. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 19 | The following values are considered false: |
| 20 | \stindex{if} |
| 21 | \stindex{while} |
| 22 | \indexii{truth}{value} |
| 23 | \indexii{Boolean}{operations} |
| 24 | \index{false} |
| 25 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 26 | \begin{itemize} |
| 27 | |
| 28 | \item \code{None} |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 29 | \withsubitem{(Built-in object)}{\ttindex{None}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 31 | \item \code{False} |
| 32 | \withsubitem{(Built-in object)}{\ttindex{False}} |
| 33 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 34 | \item zero of any numeric type, for example, \code{0}, \code{0L}, |
| 35 | \code{0.0}, \code{0j}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 36 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 37 | \item any empty sequence, for example, \code{''}, \code{()}, \code{[]}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 38 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 39 | \item any empty mapping, for example, \code{\{\}}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 40 | |
| 41 | \item instances of user-defined classes, if the class defines a |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 42 | \method{__nonzero__()} or \method{__len__()} method, when that |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 43 | method returns zero.\footnote{Additional information on these |
Fred Drake | 66571cc | 2000-09-09 03:30:34 +0000 | [diff] [blame] | 44 | special methods may be found in the \citetitle[../ref/ref.html]{Python |
| 45 | Reference Manual}.} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 46 | |
| 47 | \end{itemize} |
| 48 | |
| 49 | All other values are considered true --- so objects of many types are |
| 50 | always true. |
| 51 | \index{true} |
| 52 | |
| 53 | Operations and built-in functions that have a Boolean result always |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 54 | return \code{0} or \code{False} for false and \code{1} or \code{True} |
| 55 | for true, unless otherwise stated. (Important exception: the Boolean |
| 56 | operations \samp{or}\opindex{or} and \samp{and}\opindex{and} always |
| 57 | return one of their operands.) |
| 58 | \index{False} |
| 59 | \index{True} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 60 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 61 | \subsection{Boolean Operations \label{boolean}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 62 | |
| 63 | These are the Boolean operations, ordered by ascending priority: |
| 64 | \indexii{Boolean}{operations} |
| 65 | |
| 66 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 67 | \lineiii{\var{x} or \var{y}} |
| 68 | {if \var{x} is false, then \var{y}, else \var{x}}{(1)} |
| 69 | \lineiii{\var{x} and \var{y}} |
| 70 | {if \var{x} is false, then \var{x}, else \var{y}}{(1)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 71 | \hline |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 72 | \lineiii{not \var{x}} |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 73 | {if \var{x} is false, then \code{True}, else \code{False}}{(2)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 74 | \end{tableiii} |
| 75 | \opindex{and} |
| 76 | \opindex{or} |
| 77 | \opindex{not} |
| 78 | |
| 79 | \noindent |
| 80 | Notes: |
| 81 | |
| 82 | \begin{description} |
| 83 | |
| 84 | \item[(1)] |
| 85 | These only evaluate their second argument if needed for their outcome. |
| 86 | |
| 87 | \item[(2)] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 88 | \samp{not} has a lower priority than non-Boolean operators, so |
| 89 | \code{not \var{a} == \var{b}} is interpreted as \code{not (\var{a} == |
| 90 | \var{b})}, and \code{\var{a} == not \var{b}} is a syntax error. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 91 | |
| 92 | \end{description} |
| 93 | |
| 94 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 95 | \subsection{Comparisons \label{comparisons}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 96 | |
| 97 | Comparison operations are supported by all objects. They all have the |
| 98 | same priority (which is higher than that of the Boolean operations). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 99 | Comparisons can be chained arbitrarily; for example, \code{\var{x} < |
| 100 | \var{y} <= \var{z}} is equivalent to \code{\var{x} < \var{y} and |
| 101 | \var{y} <= \var{z}}, except that \var{y} is evaluated only once (but |
| 102 | in both cases \var{z} is not evaluated at all when \code{\var{x} < |
| 103 | \var{y}} is found to be false). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 104 | \indexii{chaining}{comparisons} |
| 105 | |
| 106 | This table summarizes the comparison operations: |
| 107 | |
| 108 | \begin{tableiii}{c|l|c}{code}{Operation}{Meaning}{Notes} |
| 109 | \lineiii{<}{strictly less than}{} |
| 110 | \lineiii{<=}{less than or equal}{} |
| 111 | \lineiii{>}{strictly greater than}{} |
| 112 | \lineiii{>=}{greater than or equal}{} |
| 113 | \lineiii{==}{equal}{} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 114 | \lineiii{!=}{not equal}{(1)} |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame] | 115 | \lineiii{<>}{not equal}{(1)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 116 | \lineiii{is}{object identity}{} |
| 117 | \lineiii{is not}{negated object identity}{} |
| 118 | \end{tableiii} |
| 119 | \indexii{operator}{comparison} |
| 120 | \opindex{==} % XXX *All* others have funny characters < ! > |
| 121 | \opindex{is} |
| 122 | \opindex{is not} |
| 123 | |
| 124 | \noindent |
| 125 | Notes: |
| 126 | |
| 127 | \begin{description} |
| 128 | |
| 129 | \item[(1)] |
| 130 | \code{<>} and \code{!=} are alternate spellings for the same operator. |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 131 | (I couldn't choose between \ABC{} and C! :-) |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 132 | \index{ABC language@\ABC{} language} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 133 | \index{language!ABC@\ABC} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 134 | \indexii{C}{language} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 135 | \code{!=} is the preferred spelling; \code{<>} is obsolescent. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 136 | |
| 137 | \end{description} |
| 138 | |
| 139 | Objects of different types, except different numeric types, never |
| 140 | compare equal; such objects are ordered consistently but arbitrarily |
| 141 | (so that sorting a heterogeneous array yields a consistent result). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 142 | Furthermore, some types (for example, file objects) support only a |
| 143 | degenerate notion of comparison where any two objects of that type are |
| 144 | unequal. Again, such objects are ordered arbitrarily but |
| 145 | consistently. |
| 146 | \indexii{object}{numeric} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 147 | \indexii{objects}{comparing} |
| 148 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 149 | Instances of a class normally compare as non-equal unless the class |
| 150 | \withsubitem{(instance method)}{\ttindex{__cmp__()}} |
Fred Drake | 66571cc | 2000-09-09 03:30:34 +0000 | [diff] [blame] | 151 | defines the \method{__cmp__()} method. Refer to the |
| 152 | \citetitle[../ref/customization.html]{Python Reference Manual} for |
| 153 | information on the use of this method to effect object comparisons. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 154 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 155 | \strong{Implementation note:} Objects of different types except |
| 156 | numbers are ordered by their type names; objects of the same types |
| 157 | that don't support proper comparison are ordered by their address. |
| 158 | |
| 159 | Two more operations with the same syntactic priority, |
| 160 | \samp{in}\opindex{in} and \samp{not in}\opindex{not in}, are supported |
| 161 | only by sequence types (below). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 162 | |
| 163 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 164 | \subsection{Numeric Types \label{typesnumeric}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 166 | There are four distinct numeric types: \dfn{plain integers}, |
| 167 | \dfn{long integers}, |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 168 | \dfn{floating point numbers}, and \dfn{complex numbers}. |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 169 | In addition, Booleans are a subtype of plain integers. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 170 | Plain integers (also just called \dfn{integers}) |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 171 | are implemented using \ctype{long} in C, which gives them at least 32 |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 172 | bits of precision. Long integers have unlimited precision. Floating |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 173 | point numbers are implemented using \ctype{double} in C. All bets on |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 174 | their precision are off unless you happen to know the machine you are |
| 175 | working with. |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 176 | \obindex{numeric} |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 177 | \obindex{Boolean} |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 178 | \obindex{integer} |
| 179 | \obindex{long integer} |
| 180 | \obindex{floating point} |
| 181 | \obindex{complex number} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 182 | \indexii{C}{language} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 183 | |
| 184 | Complex numbers have a real and imaginary part, which are both |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 185 | implemented using \ctype{double} in C. To extract these parts from |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 186 | a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 187 | |
| 188 | Numbers are created by numeric literals or as the result of built-in |
| 189 | functions and operators. Unadorned integer literals (including hex |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 190 | and octal numbers) yield plain integers. Integer literals with an |
| 191 | \character{L} or \character{l} suffix yield long integers |
| 192 | (\character{L} is preferred because \samp{1l} looks too much like |
| 193 | eleven!). Numeric literals containing a decimal point or an exponent |
| 194 | sign yield floating point numbers. Appending \character{j} or |
| 195 | \character{J} to a numeric literal yields a complex number. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 196 | \indexii{numeric}{literals} |
| 197 | \indexii{integer}{literals} |
| 198 | \indexiii{long}{integer}{literals} |
| 199 | \indexii{floating point}{literals} |
| 200 | \indexii{complex number}{literals} |
| 201 | \indexii{hexadecimal}{literals} |
| 202 | \indexii{octal}{literals} |
| 203 | |
| 204 | Python fully supports mixed arithmetic: when a binary arithmetic |
| 205 | operator has operands of different numeric types, the operand with the |
| 206 | ``smaller'' type is converted to that of the other, where plain |
| 207 | integer is smaller than long integer is smaller than floating point is |
| 208 | smaller than complex. |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 209 | Comparisons between numbers of mixed type use the same rule.\footnote{ |
| 210 | As a consequence, the list \code{[1, 2]} is considered equal |
Fred Drake | 82ac24f | 1999-07-02 14:29:14 +0000 | [diff] [blame] | 211 | to \code{[1.0, 2.0]}, and similar for tuples. |
| 212 | } The functions \function{int()}, \function{long()}, \function{float()}, |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 213 | and \function{complex()} can be used |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 214 | to coerce numbers to a specific type. |
| 215 | \index{arithmetic} |
| 216 | \bifuncindex{int} |
| 217 | \bifuncindex{long} |
| 218 | \bifuncindex{float} |
| 219 | \bifuncindex{complex} |
| 220 | |
| 221 | All numeric types support the following operations, sorted by |
| 222 | ascending priority (operations in the same box have the same |
| 223 | priority; all numeric operations have a higher priority than |
| 224 | comparison operations): |
| 225 | |
| 226 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 227 | \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{} |
| 228 | \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{} |
| 229 | \hline |
| 230 | \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{} |
| 231 | \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(1)} |
| 232 | \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{} |
| 233 | \hline |
| 234 | \lineiii{-\var{x}}{\var{x} negated}{} |
| 235 | \lineiii{+\var{x}}{\var{x} unchanged}{} |
| 236 | \hline |
| 237 | \lineiii{abs(\var{x})}{absolute value or magnitude of \var{x}}{} |
| 238 | \lineiii{int(\var{x})}{\var{x} converted to integer}{(2)} |
| 239 | \lineiii{long(\var{x})}{\var{x} converted to long integer}{(2)} |
| 240 | \lineiii{float(\var{x})}{\var{x} converted to floating point}{} |
| 241 | \lineiii{complex(\var{re},\var{im})}{a complex number with real part \var{re}, imaginary part \var{im}. \var{im} defaults to zero.}{} |
Fred Drake | 26b698f | 1999-02-12 18:27:31 +0000 | [diff] [blame] | 242 | \lineiii{\var{c}.conjugate()}{conjugate of the complex number \var{c}}{} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 243 | \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)} |
| 244 | \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{} |
| 245 | \lineiii{\var{x} ** \var{y}}{\var{x} to the power \var{y}}{} |
| 246 | \end{tableiii} |
| 247 | \indexiii{operations on}{numeric}{types} |
Fred Drake | 26b698f | 1999-02-12 18:27:31 +0000 | [diff] [blame] | 248 | \withsubitem{(complex number method)}{\ttindex{conjugate()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 249 | |
| 250 | \noindent |
| 251 | Notes: |
| 252 | \begin{description} |
| 253 | |
| 254 | \item[(1)] |
| 255 | For (plain or long) integer division, the result is an integer. |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 256 | The result is always rounded towards minus infinity: 1/2 is 0, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 257 | (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result |
| 258 | is a long integer if either operand is a long integer, regardless of |
| 259 | the numeric value. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 260 | \indexii{integer}{division} |
| 261 | \indexiii{long}{integer}{division} |
| 262 | |
| 263 | \item[(2)] |
| 264 | Conversion from floating point to (long or plain) integer may round or |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 265 | truncate as in C; see functions \function{floor()} and |
| 266 | \function{ceil()} in the \refmodule{math}\refbimodindex{math} module |
| 267 | for well-defined conversions. |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 268 | \withsubitem{(in module math)}{\ttindex{floor()}\ttindex{ceil()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 269 | \indexii{numeric}{conversions} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 270 | \indexii{C}{language} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 271 | |
| 272 | \item[(3)] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 273 | See section \ref{built-in-funcs}, ``Built-in Functions,'' for a full |
| 274 | description. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 275 | |
| 276 | \end{description} |
| 277 | % XXXJH exceptions: overflow (when? what operations?) zerodivision |
| 278 | |
Fred Drake | 4e7c205 | 1999-02-19 15:30:25 +0000 | [diff] [blame] | 279 | \subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 280 | \nodename{Bit-string Operations} |
| 281 | |
| 282 | Plain and long integer types support additional operations that make |
| 283 | sense only for bit-strings. Negative numbers are treated as their 2's |
| 284 | complement value (for long integers, this assumes a sufficiently large |
| 285 | number of bits that no overflow occurs during the operation). |
| 286 | |
| 287 | The priorities of the binary bit-wise operations are all lower than |
| 288 | the numeric operations and higher than the comparisons; the unary |
| 289 | operation \samp{\~} has the same priority as the other unary numeric |
| 290 | operations (\samp{+} and \samp{-}). |
| 291 | |
| 292 | This table lists the bit-string operations sorted in ascending |
| 293 | priority (operations in the same box have the same priority): |
| 294 | |
| 295 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 296 | \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{} |
| 297 | \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{} |
| 298 | \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{} |
| 299 | \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{(1), (2)} |
| 300 | \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{(1), (3)} |
| 301 | \hline |
| 302 | \lineiii{\~\var{x}}{the bits of \var{x} inverted}{} |
| 303 | \end{tableiii} |
| 304 | \indexiii{operations on}{integer}{types} |
| 305 | \indexii{bit-string}{operations} |
| 306 | \indexii{shifting}{operations} |
| 307 | \indexii{masking}{operations} |
| 308 | |
| 309 | \noindent |
| 310 | Notes: |
| 311 | \begin{description} |
| 312 | \item[(1)] Negative shift counts are illegal and cause a |
| 313 | \exception{ValueError} to be raised. |
| 314 | \item[(2)] A left shift by \var{n} bits is equivalent to |
| 315 | multiplication by \code{pow(2, \var{n})} without overflow check. |
| 316 | \item[(3)] A right shift by \var{n} bits is equivalent to |
| 317 | division by \code{pow(2, \var{n})} without overflow check. |
| 318 | \end{description} |
| 319 | |
| 320 | |
Fred Drake | 93656e7 | 2001-05-02 20:18:03 +0000 | [diff] [blame] | 321 | \subsection{Iterator Types \label{typeiter}} |
| 322 | |
Fred Drake | f42cc45 | 2001-05-03 04:39:10 +0000 | [diff] [blame] | 323 | \versionadded{2.2} |
Fred Drake | 93656e7 | 2001-05-02 20:18:03 +0000 | [diff] [blame] | 324 | \index{iterator protocol} |
| 325 | \index{protocol!iterator} |
| 326 | \index{sequence!iteration} |
| 327 | \index{container!iteration over} |
| 328 | |
| 329 | Python supports a concept of iteration over containers. This is |
| 330 | implemented using two distinct methods; these are used to allow |
| 331 | user-defined classes to support iteration. Sequences, described below |
| 332 | in more detail, always support the iteration methods. |
| 333 | |
| 334 | One method needs to be defined for container objects to provide |
| 335 | iteration support: |
| 336 | |
| 337 | \begin{methoddesc}[container]{__iter__}{} |
Greg Ward | 54f6509 | 2001-07-26 21:01:21 +0000 | [diff] [blame] | 338 | Return an iterator object. The object is required to support the |
Fred Drake | 93656e7 | 2001-05-02 20:18:03 +0000 | [diff] [blame] | 339 | iterator protocol described below. If a container supports |
| 340 | different types of iteration, additional methods can be provided to |
| 341 | specifically request iterators for those iteration types. (An |
| 342 | example of an object supporting multiple forms of iteration would be |
| 343 | a tree structure which supports both breadth-first and depth-first |
| 344 | traversal.) This method corresponds to the \member{tp_iter} slot of |
| 345 | the type structure for Python objects in the Python/C API. |
| 346 | \end{methoddesc} |
| 347 | |
| 348 | The iterator objects themselves are required to support the following |
| 349 | two methods, which together form the \dfn{iterator protocol}: |
| 350 | |
| 351 | \begin{methoddesc}[iterator]{__iter__}{} |
| 352 | Return the iterator object itself. This is required to allow both |
| 353 | containers and iterators to be used with the \keyword{for} and |
| 354 | \keyword{in} statements. This method corresponds to the |
| 355 | \member{tp_iter} slot of the type structure for Python objects in |
| 356 | the Python/C API. |
| 357 | \end{methoddesc} |
| 358 | |
Fred Drake | f42cc45 | 2001-05-03 04:39:10 +0000 | [diff] [blame] | 359 | \begin{methoddesc}[iterator]{next}{} |
Fred Drake | 93656e7 | 2001-05-02 20:18:03 +0000 | [diff] [blame] | 360 | Return the next item from the container. If there are no further |
| 361 | items, raise the \exception{StopIteration} exception. This method |
| 362 | corresponds to the \member{tp_iternext} slot of the type structure |
| 363 | for Python objects in the Python/C API. |
| 364 | \end{methoddesc} |
| 365 | |
| 366 | Python defines several iterator objects to support iteration over |
| 367 | general and specific sequence types, dictionaries, and other more |
| 368 | specialized forms. The specific types are not important beyond their |
| 369 | implementation of the iterator protocol. |
| 370 | |
| 371 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 372 | \subsection{Sequence Types \label{typesseq}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 373 | |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 374 | There are six sequence types: strings, Unicode strings, lists, |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame] | 375 | tuples, buffers, and xrange objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 376 | |
| 377 | Strings literals are written in single or double quotes: |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 378 | \code{'xyzzy'}, \code{"frobozz"}. See chapter 2 of the |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 379 | \citetitle[../ref/strings.html]{Python Reference Manual} for more about |
| 380 | string literals. Unicode strings are much like strings, but are |
| 381 | specified in the syntax using a preceeding \character{u} character: |
| 382 | \code{u'abc'}, \code{u"def"}. Lists are constructed with square brackets, |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 383 | separating items with commas: \code{[a, b, c]}. Tuples are |
| 384 | constructed by the comma operator (not within square brackets), with |
| 385 | or without enclosing parentheses, but an empty tuple must have the |
| 386 | enclosing parentheses, e.g., \code{a, b, c} or \code{()}. A single |
Guido van Rossum | 5fe2c13 | 2001-07-05 15:27:19 +0000 | [diff] [blame] | 387 | item tuple must have a trailing comma, e.g., \code{(d,)}. |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 388 | \obindex{sequence} |
| 389 | \obindex{string} |
| 390 | \obindex{Unicode} |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 391 | \obindex{tuple} |
| 392 | \obindex{list} |
Guido van Rossum | 5fe2c13 | 2001-07-05 15:27:19 +0000 | [diff] [blame] | 393 | |
| 394 | Buffer objects are not directly supported by Python syntax, but can be |
| 395 | created by calling the builtin function |
| 396 | \function{buffer()}.\bifuncindex{buffer}. They don't support |
| 397 | concatenation or repetition. |
| 398 | \obindex{buffer} |
| 399 | |
| 400 | Xrange objects are similar to buffers in that there is no specific |
| 401 | syntax to create them, but they are created using the \function{xrange()} |
| 402 | function.\bifuncindex{xrange} They don't support slicing, |
| 403 | concatenation or repetition, and using \code{in}, \code{not in}, |
| 404 | \function{min()} or \function{max()} on them is inefficient. |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 405 | \obindex{xrange} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | 5fe2c13 | 2001-07-05 15:27:19 +0000 | [diff] [blame] | 407 | Most sequence types support the following operations. The \samp{in} and |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 408 | \samp{not in} operations have the same priorities as the comparison |
| 409 | operations. The \samp{+} and \samp{*} operations have the same |
| 410 | priority as the corresponding numeric operations.\footnote{They must |
| 411 | have since the parser can't tell the type of the operands.} |
| 412 | |
| 413 | This table lists the sequence operations sorted in ascending priority |
| 414 | (operations in the same box have the same priority). In the table, |
| 415 | \var{s} and \var{t} are sequences of the same type; \var{n}, \var{i} |
| 416 | and \var{j} are integers: |
| 417 | |
| 418 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 419 | \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{} |
| 420 | \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is |
| 421 | equal to \var{x}, else \code{1}}{} |
| 422 | \hline |
| 423 | \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{} |
Fred Drake | d800cff | 2001-08-28 14:56:05 +0000 | [diff] [blame] | 424 | \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} shallow copies of \var{s} concatenated}{(1)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 425 | \hline |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 426 | \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(2)} |
| 427 | \lineiii{\var{s}[\var{i}:\var{j}]}{slice of \var{s} from \var{i} to \var{j}}{(2), (3)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 428 | \hline |
| 429 | \lineiii{len(\var{s})}{length of \var{s}}{} |
| 430 | \lineiii{min(\var{s})}{smallest item of \var{s}}{} |
| 431 | \lineiii{max(\var{s})}{largest item of \var{s}}{} |
| 432 | \end{tableiii} |
| 433 | \indexiii{operations on}{sequence}{types} |
| 434 | \bifuncindex{len} |
| 435 | \bifuncindex{min} |
| 436 | \bifuncindex{max} |
| 437 | \indexii{concatenation}{operation} |
| 438 | \indexii{repetition}{operation} |
| 439 | \indexii{subscript}{operation} |
| 440 | \indexii{slice}{operation} |
| 441 | \opindex{in} |
| 442 | \opindex{not in} |
| 443 | |
| 444 | \noindent |
| 445 | Notes: |
| 446 | |
| 447 | \begin{description} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 448 | \item[(1)] Values of \var{n} less than \code{0} are treated as |
| 449 | \code{0} (which yields an empty sequence of the same type as |
Fred Drake | d800cff | 2001-08-28 14:56:05 +0000 | [diff] [blame] | 450 | \var{s}). Note also that the copies are shallow; nested structures |
| 451 | are not copied. This often haunts new Python programmers; consider: |
| 452 | |
| 453 | \begin{verbatim} |
| 454 | >>> lists = [[]] * 3 |
| 455 | >>> lists |
| 456 | [[], [], []] |
| 457 | >>> lists[0].append(3) |
| 458 | >>> lists |
| 459 | [[3], [3], [3]] |
| 460 | \end{verbatim} |
| 461 | |
| 462 | What has happened is that \code{lists} is a list containing three |
| 463 | copies of the list \code{[[]]} (a one-element list containing an |
| 464 | empty list), but the contained list is shared by each copy. You can |
| 465 | create a list of different lists this way: |
| 466 | |
| 467 | \begin{verbatim} |
| 468 | >>> lists = [[] for i in range(3)] |
| 469 | >>> lists[0].append(3) |
| 470 | >>> lists[1].append(5) |
| 471 | >>> lists[2].append(7) |
| 472 | >>> lists |
| 473 | [[3], [5], [7]] |
| 474 | \end{verbatim} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 475 | |
| 476 | \item[(2)] If \var{i} or \var{j} is negative, the index is relative to |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 477 | the end of the string: \code{len(\var{s}) + \var{i}} or |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 478 | \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is |
| 479 | still \code{0}. |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 480 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 481 | \item[(3)] The slice of \var{s} from \var{i} to \var{j} is defined as |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 482 | the sequence of items with index \var{k} such that \code{\var{i} <= |
| 483 | \var{k} < \var{j}}. If \var{i} or \var{j} is greater than |
| 484 | \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted, |
| 485 | use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If |
| 486 | \var{i} is greater than or equal to \var{j}, the slice is empty. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 487 | \end{description} |
| 488 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 489 | |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 490 | \subsubsection{String Methods \label{string-methods}} |
| 491 | |
| 492 | These are the string methods which both 8-bit strings and Unicode |
| 493 | objects support: |
| 494 | |
| 495 | \begin{methoddesc}[string]{capitalize}{} |
| 496 | Return a copy of the string with only its first character capitalized. |
| 497 | \end{methoddesc} |
| 498 | |
| 499 | \begin{methoddesc}[string]{center}{width} |
| 500 | Return centered in a string of length \var{width}. Padding is done |
| 501 | using spaces. |
| 502 | \end{methoddesc} |
| 503 | |
| 504 | \begin{methoddesc}[string]{count}{sub\optional{, start\optional{, end}}} |
| 505 | Return the number of occurrences of substring \var{sub} in string |
| 506 | S\code{[\var{start}:\var{end}]}. Optional arguments \var{start} and |
| 507 | \var{end} are interpreted as in slice notation. |
| 508 | \end{methoddesc} |
| 509 | |
Fred Drake | 6048ce9 | 2001-12-10 16:43:08 +0000 | [diff] [blame] | 510 | \begin{methoddesc}[string]{decode}{\optional{encoding\optional{, errors}}} |
| 511 | Decodes the string using the codec registered for \var{encoding}. |
| 512 | \var{encoding} defaults to the default string encoding. \var{errors} |
| 513 | may be given to set a different error handling scheme. The default is |
| 514 | \code{'strict'}, meaning that encoding errors raise |
| 515 | \exception{ValueError}. Other possible values are \code{'ignore'} and |
| 516 | \code{replace'}. |
| 517 | \versionadded{2.2} |
| 518 | \end{methoddesc} |
| 519 | |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 520 | \begin{methoddesc}[string]{encode}{\optional{encoding\optional{,errors}}} |
| 521 | Return an encoded version of the string. Default encoding is the current |
| 522 | default string encoding. \var{errors} may be given to set a different |
| 523 | error handling scheme. The default for \var{errors} is |
| 524 | \code{'strict'}, meaning that encoding errors raise a |
| 525 | \exception{ValueError}. Other possible values are \code{'ignore'} and |
| 526 | \code{'replace'}. |
Fred Drake | 1dba66c | 2000-10-25 21:03:55 +0000 | [diff] [blame] | 527 | \versionadded{2.0} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 528 | \end{methoddesc} |
| 529 | |
| 530 | \begin{methoddesc}[string]{endswith}{suffix\optional{, start\optional{, end}}} |
| 531 | Return true if the string ends with the specified \var{suffix}, |
| 532 | otherwise return false. With optional \var{start}, test beginning at |
| 533 | that position. With optional \var{end}, stop comparing at that position. |
| 534 | \end{methoddesc} |
| 535 | |
| 536 | \begin{methoddesc}[string]{expandtabs}{\optional{tabsize}} |
| 537 | Return a copy of the string where all tab characters are expanded |
| 538 | using spaces. If \var{tabsize} is not given, a tab size of \code{8} |
| 539 | characters is assumed. |
| 540 | \end{methoddesc} |
| 541 | |
| 542 | \begin{methoddesc}[string]{find}{sub\optional{, start\optional{, end}}} |
| 543 | Return the lowest index in the string where substring \var{sub} is |
| 544 | found, such that \var{sub} is contained in the range [\var{start}, |
| 545 | \var{end}). Optional arguments \var{start} and \var{end} are |
| 546 | interpreted as in slice notation. Return \code{-1} if \var{sub} is |
| 547 | not found. |
| 548 | \end{methoddesc} |
| 549 | |
| 550 | \begin{methoddesc}[string]{index}{sub\optional{, start\optional{, end}}} |
| 551 | Like \method{find()}, but raise \exception{ValueError} when the |
| 552 | substring is not found. |
| 553 | \end{methoddesc} |
| 554 | |
| 555 | \begin{methoddesc}[string]{isalnum}{} |
| 556 | Return true if all characters in the string are alphanumeric and there |
| 557 | is at least one character, false otherwise. |
| 558 | \end{methoddesc} |
| 559 | |
| 560 | \begin{methoddesc}[string]{isalpha}{} |
| 561 | Return true if all characters in the string are alphabetic and there |
| 562 | is at least one character, false otherwise. |
| 563 | \end{methoddesc} |
| 564 | |
| 565 | \begin{methoddesc}[string]{isdigit}{} |
| 566 | Return true if there are only digit characters, false otherwise. |
| 567 | \end{methoddesc} |
| 568 | |
| 569 | \begin{methoddesc}[string]{islower}{} |
| 570 | Return true if all cased characters in the string are lowercase and |
| 571 | there is at least one cased character, false otherwise. |
| 572 | \end{methoddesc} |
| 573 | |
| 574 | \begin{methoddesc}[string]{isspace}{} |
| 575 | Return true if there are only whitespace characters in the string and |
| 576 | the string is not empty, false otherwise. |
| 577 | \end{methoddesc} |
| 578 | |
| 579 | \begin{methoddesc}[string]{istitle}{} |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 580 | Return true if the string is a titlecased string: uppercase |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 581 | characters may only follow uncased characters and lowercase characters |
| 582 | only cased ones. Return false otherwise. |
| 583 | \end{methoddesc} |
| 584 | |
| 585 | \begin{methoddesc}[string]{isupper}{} |
| 586 | Return true if all cased characters in the string are uppercase and |
| 587 | there is at least one cased character, false otherwise. |
| 588 | \end{methoddesc} |
| 589 | |
| 590 | \begin{methoddesc}[string]{join}{seq} |
| 591 | Return a string which is the concatenation of the strings in the |
| 592 | sequence \var{seq}. The separator between elements is the string |
| 593 | providing this method. |
| 594 | \end{methoddesc} |
| 595 | |
| 596 | \begin{methoddesc}[string]{ljust}{width} |
| 597 | Return the string left justified in a string of length \var{width}. |
| 598 | Padding is done using spaces. The original string is returned if |
| 599 | \var{width} is less than \code{len(\var{s})}. |
| 600 | \end{methoddesc} |
| 601 | |
| 602 | \begin{methoddesc}[string]{lower}{} |
| 603 | Return a copy of the string converted to lowercase. |
| 604 | \end{methoddesc} |
| 605 | |
| 606 | \begin{methoddesc}[string]{lstrip}{} |
| 607 | Return a copy of the string with leading whitespace removed. |
| 608 | \end{methoddesc} |
| 609 | |
| 610 | \begin{methoddesc}[string]{replace}{old, new\optional{, maxsplit}} |
| 611 | Return a copy of the string with all occurrences of substring |
| 612 | \var{old} replaced by \var{new}. If the optional argument |
| 613 | \var{maxsplit} is given, only the first \var{maxsplit} occurrences are |
| 614 | replaced. |
| 615 | \end{methoddesc} |
| 616 | |
| 617 | \begin{methoddesc}[string]{rfind}{sub \optional{,start \optional{,end}}} |
| 618 | Return the highest index in the string where substring \var{sub} is |
| 619 | found, such that \var{sub} is contained within s[start,end]. Optional |
| 620 | arguments \var{start} and \var{end} are interpreted as in slice |
| 621 | notation. Return \code{-1} on failure. |
| 622 | \end{methoddesc} |
| 623 | |
| 624 | \begin{methoddesc}[string]{rindex}{sub\optional{, start\optional{, end}}} |
| 625 | Like \method{rfind()} but raises \exception{ValueError} when the |
| 626 | substring \var{sub} is not found. |
| 627 | \end{methoddesc} |
| 628 | |
| 629 | \begin{methoddesc}[string]{rjust}{width} |
| 630 | Return the string right justified in a string of length \var{width}. |
| 631 | Padding is done using spaces. The original string is returned if |
| 632 | \var{width} is less than \code{len(\var{s})}. |
| 633 | \end{methoddesc} |
| 634 | |
| 635 | \begin{methoddesc}[string]{rstrip}{} |
| 636 | Return a copy of the string with trailing whitespace removed. |
| 637 | \end{methoddesc} |
| 638 | |
| 639 | \begin{methoddesc}[string]{split}{\optional{sep \optional{,maxsplit}}} |
| 640 | Return a list of the words in the string, using \var{sep} as the |
| 641 | delimiter string. If \var{maxsplit} is given, at most \var{maxsplit} |
| 642 | splits are done. If \var{sep} is not specified or \code{None}, any |
| 643 | whitespace string is a separator. |
| 644 | \end{methoddesc} |
| 645 | |
| 646 | \begin{methoddesc}[string]{splitlines}{\optional{keepends}} |
| 647 | Return a list of the lines in the string, breaking at line |
| 648 | boundaries. Line breaks are not included in the resulting list unless |
| 649 | \var{keepends} is given and true. |
| 650 | \end{methoddesc} |
| 651 | |
| 652 | \begin{methoddesc}[string]{startswith}{prefix\optional{, start\optional{, end}}} |
| 653 | Return true if string starts with the \var{prefix}, otherwise |
| 654 | return false. With optional \var{start}, test string beginning at |
| 655 | that position. With optional \var{end}, stop comparing string at that |
| 656 | position. |
| 657 | \end{methoddesc} |
| 658 | |
| 659 | \begin{methoddesc}[string]{strip}{} |
| 660 | Return a copy of the string with leading and trailing whitespace |
| 661 | removed. |
| 662 | \end{methoddesc} |
| 663 | |
| 664 | \begin{methoddesc}[string]{swapcase}{} |
| 665 | Return a copy of the string with uppercase characters converted to |
| 666 | lowercase and vice versa. |
| 667 | \end{methoddesc} |
| 668 | |
| 669 | \begin{methoddesc}[string]{title}{} |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 670 | Return a titlecased version of the string: words start with uppercase |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 671 | characters, all remaining cased characters are lowercase. |
| 672 | \end{methoddesc} |
| 673 | |
| 674 | \begin{methoddesc}[string]{translate}{table\optional{, deletechars}} |
| 675 | Return a copy of the string where all characters occurring in the |
| 676 | optional argument \var{deletechars} are removed, and the remaining |
| 677 | characters have been mapped through the given translation table, which |
| 678 | must be a string of length 256. |
| 679 | \end{methoddesc} |
| 680 | |
| 681 | \begin{methoddesc}[string]{upper}{} |
| 682 | Return a copy of the string converted to uppercase. |
| 683 | \end{methoddesc} |
| 684 | |
| 685 | |
| 686 | \subsubsection{String Formatting Operations \label{typesseq-strings}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 687 | |
Fred Drake | b38784e | 2001-12-03 22:15:56 +0000 | [diff] [blame] | 688 | \index{formatting, string (\%{})} |
Fred Drake | ab2dc1d | 2001-12-26 20:06:40 +0000 | [diff] [blame] | 689 | \index{interpolation, string (\%{})} |
Fred Drake | 66d32b1 | 2000-09-14 17:57:42 +0000 | [diff] [blame] | 690 | \index{string!formatting} |
Fred Drake | ab2dc1d | 2001-12-26 20:06:40 +0000 | [diff] [blame] | 691 | \index{string!interpolation} |
Fred Drake | 66d32b1 | 2000-09-14 17:57:42 +0000 | [diff] [blame] | 692 | \index{printf-style formatting} |
| 693 | \index{sprintf-style formatting} |
Fred Drake | b38784e | 2001-12-03 22:15:56 +0000 | [diff] [blame] | 694 | \index{\protect\%{} formatting} |
Fred Drake | ab2dc1d | 2001-12-26 20:06:40 +0000 | [diff] [blame] | 695 | \index{\protect\%{} interpolation} |
Fred Drake | 66d32b1 | 2000-09-14 17:57:42 +0000 | [diff] [blame] | 696 | |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 697 | String and Unicode objects have one unique built-in operation: the |
Fred Drake | ab2dc1d | 2001-12-26 20:06:40 +0000 | [diff] [blame] | 698 | \code{\%} operator (modulo). This is also known as the string |
| 699 | \emph{formatting} or \emph{interpolation} operator. Given |
| 700 | \code{\var{format} \% \var{values}} (where \var{format} is a string or |
| 701 | Unicode object), \code{\%} conversion specifications in \var{format} |
| 702 | are replaced with zero or more elements of \var{values}. The effect |
| 703 | is similar to the using \cfunction{sprintf()} in the C language. If |
| 704 | \var{format} is a Unicode object, or if any of the objects being |
| 705 | converted using the \code{\%s} conversion are Unicode objects, the |
| 706 | result will be a Unicode object as well. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 707 | |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 708 | If \var{format} requires a single argument, \var{values} may be a |
| 709 | single non-tuple object. \footnote{A tuple object in this case should |
| 710 | be a singleton.} Otherwise, \var{values} must be a tuple with |
| 711 | exactly the number of items specified by the format string, or a |
| 712 | single mapping object (for example, a dictionary). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 713 | |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 714 | A conversion specifier contains two or more characters and has the |
| 715 | following components, which must occur in this order: |
| 716 | |
| 717 | \begin{enumerate} |
| 718 | \item The \character{\%} character, which marks the start of the |
| 719 | specifier. |
| 720 | \item Mapping key value (optional), consisting of an identifier in |
| 721 | parentheses (for example, \code{(somename)}). |
| 722 | \item Conversion flags (optional), which affect the result of some |
| 723 | conversion types. |
| 724 | \item Minimum field width (optional). If specified as an |
| 725 | \character{*} (asterisk), the actual width is read from the |
| 726 | next element of the tuple in \var{values}, and the object to |
| 727 | convert comes after the minimum field width and optional |
| 728 | precision. |
| 729 | \item Precision (optional), given as a \character{.} (dot) followed |
| 730 | by the precision. If specified as \character{*} (an |
| 731 | asterisk), the actual width is read from the next element of |
| 732 | the tuple in \var{values}, and the value to convert comes after |
| 733 | the precision. |
| 734 | \item Length modifier (optional). |
| 735 | \item Conversion type. |
| 736 | \end{enumerate} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 737 | |
| 738 | If the right argument is a dictionary (or any kind of mapping), then |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 739 | the formats in the string \emph{must} have a parenthesized key into |
| 740 | that dictionary inserted immediately after the \character{\%} |
| 741 | character, and each format formats the corresponding entry from the |
| 742 | mapping. For example: |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 743 | |
| 744 | \begin{verbatim} |
| 745 | >>> count = 2 |
| 746 | >>> language = 'Python' |
| 747 | >>> print '%(language)s has %(count)03d quote types.' % vars() |
| 748 | Python has 002 quote types. |
| 749 | \end{verbatim} |
| 750 | |
| 751 | In this case no \code{*} specifiers may occur in a format (since they |
| 752 | require a sequential parameter list). |
| 753 | |
Fred Drake | 8c071d4 | 2001-01-26 20:48:35 +0000 | [diff] [blame] | 754 | The conversion flag characters are: |
| 755 | |
| 756 | \begin{tableii}{c|l}{character}{Flag}{Meaning} |
| 757 | \lineii{\#}{The value conversion will use the ``alternate form'' |
| 758 | (where defined below).} |
| 759 | \lineii{0}{The conversion will be zero padded.} |
| 760 | \lineii{-}{The converted value is left adjusted (overrides |
| 761 | \character{-}).} |
| 762 | \lineii{{~}}{(a space) A blank should be left before a positive number |
| 763 | (or empty string) produced by a signed conversion.} |
| 764 | \lineii{+}{A sign character (\character{+} or \character{-}) will |
| 765 | precede the conversion (overrides a "space" flag).} |
| 766 | \end{tableii} |
| 767 | |
| 768 | The length modifier may be \code{h}, \code{l}, and \code{L} may be |
| 769 | present, but are ignored as they are not necessary for Python. |
| 770 | |
| 771 | The conversion types are: |
| 772 | |
| 773 | \begin{tableii}{c|l}{character}{Conversion}{Meaning} |
| 774 | \lineii{d}{Signed integer decimal.} |
| 775 | \lineii{i}{Signed integer decimal.} |
| 776 | \lineii{o}{Unsigned octal.} |
| 777 | \lineii{u}{Unsigned decimal.} |
| 778 | \lineii{x}{Unsigned hexidecimal (lowercase).} |
| 779 | \lineii{X}{Unsigned hexidecimal (uppercase).} |
| 780 | \lineii{e}{Floating point exponential format (lowercase).} |
| 781 | \lineii{E}{Floating point exponential format (uppercase).} |
| 782 | \lineii{f}{Floating point decimal format.} |
| 783 | \lineii{F}{Floating point decimal format.} |
| 784 | \lineii{g}{Same as \character{e} if exponent is greater than -4 or |
| 785 | less than precision, \character{f} otherwise.} |
| 786 | \lineii{G}{Same as \character{E} if exponent is greater than -4 or |
| 787 | less than precision, \character{F} otherwise.} |
| 788 | \lineii{c}{Single character (accepts integer or single character |
| 789 | string).} |
| 790 | \lineii{r}{String (converts any python object using |
| 791 | \function{repr()}).} |
| 792 | \lineii{s}{String (converts any python object using |
| 793 | \function{str()}).} |
| 794 | \lineii{\%}{No argument is converted, results in a \character{\%} |
| 795 | character in the result. (The complete specification is |
| 796 | \code{\%\%}.)} |
| 797 | \end{tableii} |
| 798 | |
| 799 | % XXX Examples? |
| 800 | |
| 801 | |
| 802 | Since Python strings have an explicit length, \code{\%s} conversions |
| 803 | do not assume that \code{'\e0'} is the end of the string. |
| 804 | |
| 805 | For safety reasons, floating point precisions are clipped to 50; |
| 806 | \code{\%f} conversions for numbers whose absolute value is over 1e25 |
| 807 | are replaced by \code{\%g} conversions.\footnote{ |
| 808 | These numbers are fairly arbitrary. They are intended to |
| 809 | avoid printing endless strings of meaningless digits without hampering |
| 810 | correct use and without having to know the exact precision of floating |
| 811 | point values on a particular machine. |
| 812 | } All other errors raise exceptions. |
| 813 | |
Fred Drake | 14f5c5f | 2001-12-03 18:33:13 +0000 | [diff] [blame] | 814 | Additional string operations are defined in standard modules |
| 815 | \refmodule{string}\refstmodindex{string} and |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 816 | \refmodule{re}.\refstmodindex{re} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 817 | |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 818 | |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame] | 819 | \subsubsection{XRange Type \label{typesseq-xrange}} |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 820 | |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 821 | The xrange\obindex{xrange} type is an immutable sequence which is |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame] | 822 | commonly used for looping. The advantage of the xrange type is that an |
| 823 | xrange object will always take the same amount of memory, no matter the |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 824 | size of the range it represents. There are no consistent performance |
| 825 | advantages. |
| 826 | |
Guido van Rossum | 3f56166 | 2001-07-05 13:27:48 +0000 | [diff] [blame] | 827 | XRange objects have very little behavior: they only support indexing |
| 828 | and the \function{len()} function. |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 829 | |
| 830 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 831 | \subsubsection{Mutable Sequence Types \label{typesseq-mutable}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 832 | |
| 833 | List objects support additional operations that allow in-place |
| 834 | modification of the object. |
| 835 | These operations would be supported by other mutable sequence types |
| 836 | (when added to the language) as well. |
| 837 | Strings and tuples are immutable sequence types and such objects cannot |
| 838 | be modified once created. |
| 839 | The following operations are defined on mutable sequence types (where |
| 840 | \var{x} is an arbitrary object): |
| 841 | \indexiii{mutable}{sequence}{types} |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 842 | \obindex{list} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 843 | |
| 844 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 845 | \lineiii{\var{s}[\var{i}] = \var{x}} |
| 846 | {item \var{i} of \var{s} is replaced by \var{x}}{} |
| 847 | \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}} |
| 848 | {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{} |
| 849 | \lineiii{del \var{s}[\var{i}:\var{j}]} |
| 850 | {same as \code{\var{s}[\var{i}:\var{j}] = []}}{} |
| 851 | \lineiii{\var{s}.append(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 852 | {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{(1)} |
Barry Warsaw | afd974c | 1998-10-09 16:39:58 +0000 | [diff] [blame] | 853 | \lineiii{\var{s}.extend(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 854 | {same as \code{\var{s}[len(\var{s}):len(\var{s})] = \var{x}}}{(2)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 855 | \lineiii{\var{s}.count(\var{x})} |
| 856 | {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{} |
| 857 | \lineiii{\var{s}.index(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 858 | {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(3)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 859 | \lineiii{\var{s}.insert(\var{i}, \var{x})} |
| 860 | {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]} |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 861 | if \code{\var{i} >= 0}}{(4)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 862 | \lineiii{\var{s}.pop(\optional{\var{i}})} |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 863 | {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(5)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 864 | \lineiii{\var{s}.remove(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 865 | {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(3)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 866 | \lineiii{\var{s}.reverse()} |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 867 | {reverses the items of \var{s} in place}{(6)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 868 | \lineiii{\var{s}.sort(\optional{\var{cmpfunc}})} |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 869 | {sort the items of \var{s} in place}{(6), (7)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 870 | \end{tableiii} |
| 871 | \indexiv{operations on}{mutable}{sequence}{types} |
| 872 | \indexiii{operations on}{sequence}{types} |
| 873 | \indexiii{operations on}{list}{type} |
| 874 | \indexii{subscript}{assignment} |
| 875 | \indexii{slice}{assignment} |
| 876 | \stindex{del} |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 877 | \withsubitem{(list method)}{ |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 878 | \ttindex{append()}\ttindex{extend()}\ttindex{count()}\ttindex{index()} |
| 879 | \ttindex{insert()}\ttindex{pop()}\ttindex{remove()}\ttindex{reverse()} |
Fred Drake | e839199 | 1998-11-25 17:09:19 +0000 | [diff] [blame] | 880 | \ttindex{sort()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 881 | \noindent |
| 882 | Notes: |
| 883 | \begin{description} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 884 | \item[(1)] The C implementation of Python has historically accepted |
| 885 | multiple parameters and implicitly joined them into a tuple; this |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 886 | no longer works in Python 2.0. Use of this misfeature has been |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 887 | deprecated since Python 1.4. |
| 888 | |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 889 | \item[(2)] Raises an exception when \var{x} is not a list object. The |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 890 | \method{extend()} method is experimental and not supported by |
| 891 | mutable sequence types other than lists. |
| 892 | |
| 893 | \item[(3)] Raises \exception{ValueError} when \var{x} is not found in |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 894 | \var{s}. |
| 895 | |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 896 | \item[(4)] When a negative index is passed as the first parameter to |
| 897 | the \method{insert()} method, the new element is prepended to the |
| 898 | sequence. |
| 899 | |
| 900 | \item[(5)] The \method{pop()} method is only supported by the list and |
Fred Drake | fbd3b45 | 2000-07-31 23:42:23 +0000 | [diff] [blame] | 901 | array types. The optional argument \var{i} defaults to \code{-1}, |
| 902 | so that by default the last item is removed and returned. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 903 | |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 904 | \item[(6)] The \method{sort()} and \method{reverse()} methods modify the |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 905 | list in place for economy of space when sorting or reversing a large |
Skip Montanaro | 41d7d58 | 2001-07-25 16:18:19 +0000 | [diff] [blame] | 906 | list. To remind you that they operate by side effect, they don't return |
| 907 | the sorted or reversed list. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 908 | |
Fred Drake | ef428a2 | 2001-10-26 18:57:14 +0000 | [diff] [blame] | 909 | \item[(7)] The \method{sort()} method takes an optional argument |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 910 | specifying a comparison function of two arguments (list items) which |
Tim Peters | 599db7d | 2001-09-29 01:08:19 +0000 | [diff] [blame] | 911 | should return a negative, zero or positive number depending on whether |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 912 | the first argument is considered smaller than, equal to, or larger |
| 913 | than the second argument. Note that this slows the sorting process |
| 914 | down considerably; e.g. to sort a list in reverse order it is much |
| 915 | faster to use calls to the methods \method{sort()} and |
| 916 | \method{reverse()} than to use the built-in function |
| 917 | \function{sort()} with a comparison function that reverses the |
| 918 | ordering of the elements. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 919 | \end{description} |
| 920 | |
| 921 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 922 | \subsection{Mapping Types \label{typesmapping}} |
Fred Drake | 0b4e25d | 2000-10-04 04:21:19 +0000 | [diff] [blame] | 923 | \obindex{mapping} |
| 924 | \obindex{dictionary} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 925 | |
| 926 | A \dfn{mapping} object maps values of one type (the key type) to |
| 927 | arbitrary objects. Mappings are mutable objects. There is currently |
| 928 | only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are |
| 929 | almost arbitrary values. The only types of values not acceptable as |
| 930 | keys are values containing lists or dictionaries or other mutable |
| 931 | types that are compared by value rather than by object identity. |
| 932 | Numeric types used for keys obey the normal rules for numeric |
| 933 | comparison: if two numbers compare equal (e.g. \code{1} and |
| 934 | \code{1.0}) then they can be used interchangeably to index the same |
| 935 | dictionary entry. |
| 936 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 937 | Dictionaries are created by placing a comma-separated list of |
| 938 | \code{\var{key}: \var{value}} pairs within braces, for example: |
| 939 | \code{\{'jack': 4098, 'sjoerd': 4127\}} or |
| 940 | \code{\{4098: 'jack', 4127: 'sjoerd'\}}. |
| 941 | |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 942 | The following operations are defined on mappings (where \var{a} and |
| 943 | \var{b} are mappings, \var{k} is a key, and \var{v} and \var{x} are |
| 944 | arbitrary objects): |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 945 | \indexiii{operations on}{mapping}{types} |
| 946 | \indexiii{operations on}{dictionary}{type} |
| 947 | \stindex{del} |
| 948 | \bifuncindex{len} |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 949 | \withsubitem{(dictionary method)}{ |
| 950 | \ttindex{clear()} |
| 951 | \ttindex{copy()} |
| 952 | \ttindex{has_key()} |
| 953 | \ttindex{items()} |
| 954 | \ttindex{keys()} |
| 955 | \ttindex{update()} |
| 956 | \ttindex{values()} |
Fred Drake | e839199 | 1998-11-25 17:09:19 +0000 | [diff] [blame] | 957 | \ttindex{get()}} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 958 | |
| 959 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 960 | \lineiii{len(\var{a})}{the number of items in \var{a}}{} |
| 961 | \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)} |
Fred Drake | 1e75e17 | 2000-07-31 16:34:46 +0000 | [diff] [blame] | 962 | \lineiii{\var{a}[\var{k}] = \var{v}} |
| 963 | {set \code{\var{a}[\var{k}]} to \var{v}} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 964 | {} |
| 965 | \lineiii{del \var{a}[\var{k}]} |
| 966 | {remove \code{\var{a}[\var{k}]} from \var{a}} |
| 967 | {(1)} |
| 968 | \lineiii{\var{a}.clear()}{remove all items from \code{a}}{} |
| 969 | \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{} |
Guido van Rossum | 8b3d6ca | 2001-04-23 13:22:59 +0000 | [diff] [blame] | 970 | \lineiii{\var{a}.has_key(\var{k})} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 971 | {\code{1} if \var{a} has a key \var{k}, else \code{0}} |
| 972 | {} |
Guido van Rossum | 8b3d6ca | 2001-04-23 13:22:59 +0000 | [diff] [blame] | 973 | \lineiii{\var{k} \code{in} \var{a}} |
| 974 | {Equivalent to \var{a}.has_key(\var{k})} |
Fred Drake | c6d8f8d | 2001-05-25 04:24:37 +0000 | [diff] [blame] | 975 | {(2)} |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 976 | \lineiii{\var{k} not in \var{a}} |
Guido van Rossum | 8b3d6ca | 2001-04-23 13:22:59 +0000 | [diff] [blame] | 977 | {Equivalent to \code{not} \var{a}.has_key(\var{k})} |
Fred Drake | c6d8f8d | 2001-05-25 04:24:37 +0000 | [diff] [blame] | 978 | {(2)} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 979 | \lineiii{\var{a}.items()} |
| 980 | {a copy of \var{a}'s list of (\var{key}, \var{value}) pairs} |
Fred Drake | c6d8f8d | 2001-05-25 04:24:37 +0000 | [diff] [blame] | 981 | {(3)} |
Fred Drake | 4a6c5c5 | 2001-06-12 03:31:56 +0000 | [diff] [blame] | 982 | \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(3)} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 983 | \lineiii{\var{a}.update(\var{b})} |
Fred Drake | 1e75e17 | 2000-07-31 16:34:46 +0000 | [diff] [blame] | 984 | {\code{for k in \var{b}.keys(): \var{a}[k] = \var{b}[k]}} |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 985 | {} |
Fred Drake | 4a6c5c5 | 2001-06-12 03:31:56 +0000 | [diff] [blame] | 986 | \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(3)} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 987 | \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})} |
Fred Drake | 4cacec5 | 2001-04-21 05:56:06 +0000 | [diff] [blame] | 988 | {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}}, |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 989 | else \var{x}} |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 990 | {(4)} |
Guido van Rossum | 8141cf5 | 2000-08-08 16:15:49 +0000 | [diff] [blame] | 991 | \lineiii{\var{a}.setdefault(\var{k}\optional{, \var{x}})} |
Fred Drake | 4cacec5 | 2001-04-21 05:56:06 +0000 | [diff] [blame] | 992 | {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}}, |
Guido van Rossum | 8141cf5 | 2000-08-08 16:15:49 +0000 | [diff] [blame] | 993 | else \var{x} (also setting it)} |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 994 | {(5)} |
Guido van Rossum | ff63f20 | 2000-12-12 22:03:47 +0000 | [diff] [blame] | 995 | \lineiii{\var{a}.popitem()} |
| 996 | {remove and return an arbitrary (\var{key}, \var{value}) pair} |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 997 | {(6)} |
Fred Drake | c6d8f8d | 2001-05-25 04:24:37 +0000 | [diff] [blame] | 998 | \lineiii{\var{a}.iteritems()} |
| 999 | {return an iterator over (\var{key}, \var{value}) pairs} |
| 1000 | {(2)} |
| 1001 | \lineiii{\var{a}.iterkeys()} |
| 1002 | {return an iterator over the mapping's keys} |
| 1003 | {(2)} |
| 1004 | \lineiii{\var{a}.itervalues()} |
| 1005 | {return an iterator over the mapping's values} |
| 1006 | {(2)} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 1007 | \end{tableiii} |
| 1008 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1009 | \noindent |
| 1010 | Notes: |
| 1011 | \begin{description} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 1012 | \item[(1)] Raises a \exception{KeyError} exception if \var{k} is not |
| 1013 | in the map. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1014 | |
Fred Drake | c6d8f8d | 2001-05-25 04:24:37 +0000 | [diff] [blame] | 1015 | \item[(2)] \versionadded{2.2} |
| 1016 | |
| 1017 | \item[(3)] Keys and values are listed in random order. If |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1018 | \method{keys()} and \method{values()} are called with no intervening |
| 1019 | modifications to the dictionary, the two lists will directly |
| 1020 | correspond. This allows the creation of \code{(\var{value}, |
Fred Drake | 4a6c5c5 | 2001-06-12 03:31:56 +0000 | [diff] [blame] | 1021 | \var{key})} pairs using \function{zip()}: \samp{pairs = |
| 1022 | zip(\var{a}.values(), \var{a}.keys())}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1023 | |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 1024 | \item[(4)] Never raises an exception if \var{k} is not in the map, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1025 | instead it returns \var{x}. \var{x} is optional; when \var{x} is not |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 1026 | provided and \var{k} is not in the map, \code{None} is returned. |
Guido van Rossum | 8141cf5 | 2000-08-08 16:15:49 +0000 | [diff] [blame] | 1027 | |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 1028 | \item[(5)] \function{setdefault()} is like \function{get()}, except |
Guido van Rossum | 8141cf5 | 2000-08-08 16:15:49 +0000 | [diff] [blame] | 1029 | that if \var{k} is missing, \var{x} is both returned and inserted into |
| 1030 | the dictionary as the value of \var{k}. |
Guido van Rossum | ff63f20 | 2000-12-12 22:03:47 +0000 | [diff] [blame] | 1031 | |
Barry Warsaw | e9218a1 | 2001-06-26 20:32:59 +0000 | [diff] [blame] | 1032 | \item[(6)] \function{popitem()} is useful to destructively iterate |
Guido van Rossum | ff63f20 | 2000-12-12 22:03:47 +0000 | [diff] [blame] | 1033 | over a dictionary, as often used in set algorithms. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1034 | \end{description} |
| 1035 | |
| 1036 | |
Fred Drake | 99de218 | 2001-10-30 06:23:14 +0000 | [diff] [blame] | 1037 | \subsection{File Objects |
| 1038 | \label{bltin-file-objects}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1039 | |
Fred Drake | 99de218 | 2001-10-30 06:23:14 +0000 | [diff] [blame] | 1040 | File objects\obindex{file} are implemented using C's \code{stdio} |
| 1041 | package and can be created with the built-in constructor |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 1042 | \function{file()}\bifuncindex{file} described in section |
Tim Peters | 003047a | 2001-10-30 05:54:04 +0000 | [diff] [blame] | 1043 | \ref{built-in-funcs}, ``Built-in Functions.''\footnote{\function{file()} |
| 1044 | is new in Python 2.2. The older built-in \function{open()} is an |
| 1045 | alias for \function{file()}.} |
| 1046 | They are also returned |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1047 | by some other built-in functions and methods, such as |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 1048 | \function{os.popen()} and \function{os.fdopen()} and the |
Fred Drake | 130072d | 1998-10-28 20:08:35 +0000 | [diff] [blame] | 1049 | \method{makefile()} method of socket objects. |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 1050 | \refstmodindex{os} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1051 | \refbimodindex{socket} |
| 1052 | |
| 1053 | When a file operation fails for an I/O-related reason, the exception |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 1054 | \exception{IOError} is raised. This includes situations where the |
| 1055 | operation is not defined for some reason, like \method{seek()} on a tty |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1056 | device or writing a file opened for reading. |
| 1057 | |
| 1058 | Files have the following methods: |
| 1059 | |
| 1060 | |
| 1061 | \begin{methoddesc}[file]{close}{} |
| 1062 | Close the file. A closed file cannot be read or written anymore. |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1063 | Any operation which requires that the file be open will raise a |
| 1064 | \exception{ValueError} after the file has been closed. Calling |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1065 | \method{close()} more than once is allowed. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1066 | \end{methoddesc} |
| 1067 | |
| 1068 | \begin{methoddesc}[file]{flush}{} |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1069 | Flush the internal buffer, like \code{stdio}'s |
| 1070 | \cfunction{fflush()}. This may be a no-op on some file-like |
| 1071 | objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1072 | \end{methoddesc} |
| 1073 | |
| 1074 | \begin{methoddesc}[file]{isatty}{} |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1075 | Return true if the file is connected to a tty(-like) device, else |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1076 | false. \note{If a file-like object is not associated |
| 1077 | with a real file, this method should \emph{not} be implemented.} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1078 | \end{methoddesc} |
| 1079 | |
| 1080 | \begin{methoddesc}[file]{fileno}{} |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1081 | \index{file descriptor} |
| 1082 | \index{descriptor, file} |
| 1083 | Return the integer ``file descriptor'' that is used by the |
| 1084 | underlying implementation to request I/O operations from the |
| 1085 | operating system. This can be useful for other, lower level |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1086 | interfaces that use file descriptors, such as the |
| 1087 | \refmodule{fcntl}\refbimodindex{fcntl} module or |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1088 | \function{os.read()} and friends. \note{File-like objects |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1089 | which do not have a real file descriptor should \emph{not} provide |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1090 | this method!} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1091 | \end{methoddesc} |
| 1092 | |
| 1093 | \begin{methoddesc}[file]{read}{\optional{size}} |
| 1094 | Read at most \var{size} bytes from the file (less if the read hits |
Fred Drake | f4cbada | 1999-04-14 14:31:53 +0000 | [diff] [blame] | 1095 | \EOF{} before obtaining \var{size} bytes). If the \var{size} |
| 1096 | argument is negative or omitted, read all data until \EOF{} is |
| 1097 | reached. The bytes are returned as a string object. An empty |
| 1098 | string is returned when \EOF{} is encountered immediately. (For |
| 1099 | certain files, like ttys, it makes sense to continue reading after |
| 1100 | an \EOF{} is hit.) Note that this method may call the underlying |
| 1101 | C function \cfunction{fread()} more than once in an effort to |
| 1102 | acquire as close to \var{size} bytes as possible. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1103 | \end{methoddesc} |
| 1104 | |
| 1105 | \begin{methoddesc}[file]{readline}{\optional{size}} |
| 1106 | Read one entire line from the file. A trailing newline character is |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 1107 | kept in the string\footnote{ |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 1108 | The advantage of leaving the newline on is that an empty string |
| 1109 | can be returned to mean \EOF{} without being ambiguous. Another |
| 1110 | advantage is that (in cases where it might matter, for example. if you |
| 1111 | want to make an exact copy of a file while scanning its lines) |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1112 | you can tell whether the last line of a file ended in a newline |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 1113 | or not (yes this happens!). |
| 1114 | } (but may be absent when a file ends with an |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1115 | incomplete line). If the \var{size} argument is present and |
| 1116 | non-negative, it is a maximum byte count (including the trailing |
| 1117 | newline) and an incomplete line may be returned. |
| 1118 | An empty string is returned when \EOF{} is hit |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1119 | immediately. \note{Unlike \code{stdio}'s \cfunction{fgets()}, the |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1120 | returned string contains null characters (\code{'\e 0'}) if they |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1121 | occurred in the input.} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1122 | \end{methoddesc} |
| 1123 | |
| 1124 | \begin{methoddesc}[file]{readlines}{\optional{sizehint}} |
| 1125 | Read until \EOF{} using \method{readline()} and return a list containing |
| 1126 | the lines thus read. If the optional \var{sizehint} argument is |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 1127 | present, instead of reading up to \EOF, whole lines totalling |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1128 | approximately \var{sizehint} bytes (possibly after rounding up to an |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1129 | internal buffer size) are read. Objects implementing a file-like |
| 1130 | interface may choose to ignore \var{sizehint} if it cannot be |
| 1131 | implemented, or cannot be implemented efficiently. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1132 | \end{methoddesc} |
| 1133 | |
Guido van Rossum | 20ab9e9 | 2001-01-17 01:18:00 +0000 | [diff] [blame] | 1134 | \begin{methoddesc}[file]{xreadlines}{} |
Fred Drake | 82f93c6 | 2001-04-22 01:56:51 +0000 | [diff] [blame] | 1135 | Equivalent to |
| 1136 | \function{xreadlines.xreadlines(\var{file})}.\refstmodindex{xreadlines} |
| 1137 | (See the \refmodule{xreadlines} module for more information.) |
| 1138 | \versionadded{2.1} |
Guido van Rossum | 20ab9e9 | 2001-01-17 01:18:00 +0000 | [diff] [blame] | 1139 | \end{methoddesc} |
| 1140 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1141 | \begin{methoddesc}[file]{seek}{offset\optional{, whence}} |
| 1142 | Set the file's current position, like \code{stdio}'s \cfunction{fseek()}. |
| 1143 | The \var{whence} argument is optional and defaults to \code{0} |
| 1144 | (absolute file positioning); other values are \code{1} (seek |
| 1145 | relative to the current position) and \code{2} (seek relative to the |
Fred Drake | 19ae783 | 2001-01-04 05:16:39 +0000 | [diff] [blame] | 1146 | file's end). There is no return value. Note that if the file is |
| 1147 | opened for appending (mode \code{'a'} or \code{'a+'}), any |
| 1148 | \method{seek()} operations will be undone at the next write. If the |
| 1149 | file is only opened for writing in append mode (mode \code{'a'}), |
| 1150 | this method is essentially a no-op, but it remains useful for files |
| 1151 | opened in append mode with reading enabled (mode \code{'a+'}). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1152 | \end{methoddesc} |
| 1153 | |
| 1154 | \begin{methoddesc}[file]{tell}{} |
| 1155 | Return the file's current position, like \code{stdio}'s |
| 1156 | \cfunction{ftell()}. |
| 1157 | \end{methoddesc} |
| 1158 | |
| 1159 | \begin{methoddesc}[file]{truncate}{\optional{size}} |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 1160 | Truncate the file's size. If the optional \var{size} argument is |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1161 | present, the file is truncated to (at most) that size. The size |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 1162 | defaults to the current position. The current file position is |
| 1163 | not changed. Note that if a specified size exceeds the file's |
| 1164 | current size, the result is platform-dependent: possibilities |
| 1165 | include that file may remain unchanged, increase to the specified |
| 1166 | size as if zero-filled, or increase to the specified size with |
| 1167 | undefined new content. |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 1168 | Availability: Windows, many \UNIX variants. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1169 | \end{methoddesc} |
| 1170 | |
| 1171 | \begin{methoddesc}[file]{write}{str} |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1172 | Write a string to the file. There is no return value. Due to |
Fred Drake | 3c48ef7 | 2001-01-09 22:47:46 +0000 | [diff] [blame] | 1173 | buffering, the string may not actually show up in the file until |
| 1174 | the \method{flush()} or \method{close()} method is called. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1175 | \end{methoddesc} |
| 1176 | |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1177 | \begin{methoddesc}[file]{writelines}{sequence} |
| 1178 | Write a sequence of strings to the file. The sequence can be any |
| 1179 | iterable object producing strings, typically a list of strings. |
| 1180 | There is no return value. |
Fred Drake | 3c48ef7 | 2001-01-09 22:47:46 +0000 | [diff] [blame] | 1181 | (The name is intended to match \method{readlines()}; |
| 1182 | \method{writelines()} does not add line separators.) |
| 1183 | \end{methoddesc} |
| 1184 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1185 | |
Fred Drake | 038d264 | 2001-09-22 04:34:48 +0000 | [diff] [blame] | 1186 | Files support the iterator protocol. Each iteration returns the same |
| 1187 | result as \code{\var{file}.readline()}, and iteration ends when the |
| 1188 | \method{readline()} method returns an empty string. |
| 1189 | |
| 1190 | |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1191 | File objects also offer a number of other interesting attributes. |
| 1192 | These are not required for file-like objects, but should be |
| 1193 | implemented if they make sense for the particular object. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1194 | |
| 1195 | \begin{memberdesc}[file]{closed} |
| 1196 | Boolean indicating the current state of the file object. This is a |
| 1197 | read-only attribute; the \method{close()} method changes the value. |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1198 | It may not be available on all file-like objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1199 | \end{memberdesc} |
| 1200 | |
| 1201 | \begin{memberdesc}[file]{mode} |
| 1202 | The I/O mode for the file. If the file was created using the |
| 1203 | \function{open()} built-in function, this will be the value of the |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1204 | \var{mode} parameter. This is a read-only attribute and may not be |
| 1205 | present on all file-like objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1206 | \end{memberdesc} |
| 1207 | |
| 1208 | \begin{memberdesc}[file]{name} |
| 1209 | If the file object was created using \function{open()}, the name of |
| 1210 | the file. Otherwise, some string that indicates the source of the |
| 1211 | file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only |
Fred Drake | 752ba39 | 2000-09-19 15:18:51 +0000 | [diff] [blame] | 1212 | attribute and may not be present on all file-like objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1213 | \end{memberdesc} |
| 1214 | |
| 1215 | \begin{memberdesc}[file]{softspace} |
| 1216 | Boolean that indicates whether a space character needs to be printed |
| 1217 | before another value when using the \keyword{print} statement. |
| 1218 | Classes that are trying to simulate a file object should also have a |
| 1219 | writable \member{softspace} attribute, which should be initialized to |
Fred Drake | 66571cc | 2000-09-09 03:30:34 +0000 | [diff] [blame] | 1220 | zero. This will be automatic for most classes implemented in Python |
| 1221 | (care may be needed for objects that override attribute access); types |
| 1222 | implemented in C will have to provide a writable |
| 1223 | \member{softspace} attribute. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1224 | \note{This attribute is not used to control the |
Fred Drake | 51f53df | 2000-09-20 04:48:20 +0000 | [diff] [blame] | 1225 | \keyword{print} statement, but to allow the implementation of |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1226 | \keyword{print} to keep track of its internal state.} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1227 | \end{memberdesc} |
| 1228 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1229 | |
Fred Drake | 99de218 | 2001-10-30 06:23:14 +0000 | [diff] [blame] | 1230 | \subsection{Other Built-in Types \label{typesother}} |
| 1231 | |
| 1232 | The interpreter supports several other kinds of objects. |
| 1233 | Most of these support only one or two operations. |
| 1234 | |
| 1235 | |
| 1236 | \subsubsection{Modules \label{typesmodules}} |
| 1237 | |
| 1238 | The only special operation on a module is attribute access: |
| 1239 | \code{\var{m}.\var{name}}, where \var{m} is a module and \var{name} |
| 1240 | accesses a name defined in \var{m}'s symbol table. Module attributes |
| 1241 | can be assigned to. (Note that the \keyword{import} statement is not, |
| 1242 | strictly speaking, an operation on a module object; \code{import |
| 1243 | \var{foo}} does not require a module object named \var{foo} to exist, |
| 1244 | rather it requires an (external) \emph{definition} for a module named |
| 1245 | \var{foo} somewhere.) |
| 1246 | |
| 1247 | A special member of every module is \member{__dict__}. |
| 1248 | This is the dictionary containing the module's symbol table. |
| 1249 | Modifying this dictionary will actually change the module's symbol |
| 1250 | table, but direct assignment to the \member{__dict__} attribute is not |
| 1251 | possible (you can write \code{\var{m}.__dict__['a'] = 1}, which |
| 1252 | defines \code{\var{m}.a} to be \code{1}, but you can't write |
| 1253 | \code{\var{m}.__dict__ = \{\}}. |
| 1254 | |
| 1255 | Modules built into the interpreter are written like this: |
| 1256 | \code{<module 'sys' (built-in)>}. If loaded from a file, they are |
| 1257 | written as \code{<module 'os' from |
| 1258 | '/usr/local/lib/python\shortversion/os.pyc'>}. |
| 1259 | |
| 1260 | |
| 1261 | \subsubsection{Classes and Class Instances \label{typesobjects}} |
| 1262 | \nodename{Classes and Instances} |
| 1263 | |
| 1264 | See chapters 3 and 7 of the \citetitle[../ref/ref.html]{Python |
| 1265 | Reference Manual} for these. |
| 1266 | |
| 1267 | |
| 1268 | \subsubsection{Functions \label{typesfunctions}} |
| 1269 | |
| 1270 | Function objects are created by function definitions. The only |
| 1271 | operation on a function object is to call it: |
| 1272 | \code{\var{func}(\var{argument-list})}. |
| 1273 | |
| 1274 | There are really two flavors of function objects: built-in functions |
| 1275 | and user-defined functions. Both support the same operation (to call |
| 1276 | the function), but the implementation is different, hence the |
| 1277 | different object types. |
| 1278 | |
| 1279 | The implementation adds two special read-only attributes: |
| 1280 | \code{\var{f}.func_code} is a function's \dfn{code |
| 1281 | object}\obindex{code} (see below) and \code{\var{f}.func_globals} is |
| 1282 | the dictionary used as the function's global namespace (this is the |
| 1283 | same as \code{\var{m}.__dict__} where \var{m} is the module in which |
| 1284 | the function \var{f} was defined). |
| 1285 | |
| 1286 | Function objects also support getting and setting arbitrary |
| 1287 | attributes, which can be used to, e.g. attach metadata to functions. |
| 1288 | Regular attribute dot-notation is used to get and set such |
| 1289 | attributes. \emph{Note that the current implementation only supports |
| 1290 | function attributes on user-defined functions. Function attributes on |
| 1291 | built-in functions may be supported in the future.} |
| 1292 | |
| 1293 | Functions have another special attribute \code{\var{f}.__dict__} |
| 1294 | (a.k.a. \code{\var{f}.func_dict}) which contains the namespace used to |
| 1295 | support function attributes. \code{__dict__} and \code{func_dict} can |
| 1296 | be accessed directly or set to a dictionary object. A function's |
| 1297 | dictionary cannot be deleted. |
| 1298 | |
| 1299 | \subsubsection{Methods \label{typesmethods}} |
| 1300 | \obindex{method} |
| 1301 | |
| 1302 | Methods are functions that are called using the attribute notation. |
| 1303 | There are two flavors: built-in methods (such as \method{append()} on |
| 1304 | lists) and class instance methods. Built-in methods are described |
| 1305 | with the types that support them. |
| 1306 | |
| 1307 | The implementation adds two special read-only attributes to class |
| 1308 | instance methods: \code{\var{m}.im_self} is the object on which the |
| 1309 | method operates, and \code{\var{m}.im_func} is the function |
| 1310 | implementing the method. Calling \code{\var{m}(\var{arg-1}, |
| 1311 | \var{arg-2}, \textrm{\ldots}, \var{arg-n})} is completely equivalent to |
| 1312 | calling \code{\var{m}.im_func(\var{m}.im_self, \var{arg-1}, |
| 1313 | \var{arg-2}, \textrm{\ldots}, \var{arg-n})}. |
| 1314 | |
| 1315 | Class instance methods are either \emph{bound} or \emph{unbound}, |
| 1316 | referring to whether the method was accessed through an instance or a |
| 1317 | class, respectively. When a method is unbound, its \code{im_self} |
| 1318 | attribute will be \code{None} and if called, an explicit \code{self} |
| 1319 | object must be passed as the first argument. In this case, |
| 1320 | \code{self} must be an instance of the unbound method's class (or a |
| 1321 | subclass of that class), otherwise a \code{TypeError} is raised. |
| 1322 | |
| 1323 | Like function objects, methods objects support getting |
| 1324 | arbitrary attributes. However, since method attributes are actually |
| 1325 | stored on the underlying function object (\code{meth.im_func}), |
| 1326 | setting method attributes on either bound or unbound methods is |
| 1327 | disallowed. Attempting to set a method attribute results in a |
| 1328 | \code{TypeError} being raised. In order to set a method attribute, |
| 1329 | you need to explicitly set it on the underlying function object: |
| 1330 | |
| 1331 | \begin{verbatim} |
| 1332 | class C: |
| 1333 | def method(self): |
| 1334 | pass |
| 1335 | |
| 1336 | c = C() |
| 1337 | c.method.im_func.whoami = 'my name is c' |
| 1338 | \end{verbatim} |
| 1339 | |
| 1340 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for more |
| 1341 | information. |
| 1342 | |
| 1343 | |
| 1344 | \subsubsection{Code Objects \label{bltin-code-objects}} |
| 1345 | \obindex{code} |
| 1346 | |
| 1347 | Code objects are used by the implementation to represent |
| 1348 | ``pseudo-compiled'' executable Python code such as a function body. |
| 1349 | They differ from function objects because they don't contain a |
| 1350 | reference to their global execution environment. Code objects are |
| 1351 | returned by the built-in \function{compile()} function and can be |
| 1352 | extracted from function objects through their \member{func_code} |
| 1353 | attribute. |
| 1354 | \bifuncindex{compile} |
| 1355 | \withsubitem{(function object attribute)}{\ttindex{func_code}} |
| 1356 | |
| 1357 | A code object can be executed or evaluated by passing it (instead of a |
| 1358 | source string) to the \keyword{exec} statement or the built-in |
| 1359 | \function{eval()} function. |
| 1360 | \stindex{exec} |
| 1361 | \bifuncindex{eval} |
| 1362 | |
| 1363 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for more |
| 1364 | information. |
| 1365 | |
| 1366 | |
| 1367 | \subsubsection{Type Objects \label{bltin-type-objects}} |
| 1368 | |
| 1369 | Type objects represent the various object types. An object's type is |
| 1370 | accessed by the built-in function \function{type()}. There are no special |
| 1371 | operations on types. The standard module \module{types} defines names |
| 1372 | for all standard built-in types. |
| 1373 | \bifuncindex{type} |
| 1374 | \refstmodindex{types} |
| 1375 | |
| 1376 | Types are written like this: \code{<type 'int'>}. |
| 1377 | |
| 1378 | |
| 1379 | \subsubsection{The Null Object \label{bltin-null-object}} |
| 1380 | |
| 1381 | This object is returned by functions that don't explicitly return a |
| 1382 | value. It supports no special operations. There is exactly one null |
| 1383 | object, named \code{None} (a built-in name). |
| 1384 | |
| 1385 | It is written as \code{None}. |
| 1386 | |
| 1387 | |
| 1388 | \subsubsection{The Ellipsis Object \label{bltin-ellipsis-object}} |
| 1389 | |
| 1390 | This object is used by extended slice notation (see the |
| 1391 | \citetitle[../ref/ref.html]{Python Reference Manual}). It supports no |
| 1392 | special operations. There is exactly one ellipsis object, named |
| 1393 | \constant{Ellipsis} (a built-in name). |
| 1394 | |
| 1395 | It is written as \code{Ellipsis}. |
| 1396 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame^] | 1397 | \subsubsection{Boolean Values} |
| 1398 | |
| 1399 | Boolean values are the two constant objects \code{False} and |
| 1400 | \code{True}. They are used to represent truth values (although other |
| 1401 | values can also be considered false or true). In numeric contexts |
| 1402 | (for example when used as the argument to an arithmetic operator), |
| 1403 | they behave like the integers 0 and 1, respectively. The built-in |
| 1404 | function \function{bool()} can be used to cast any value to a Boolean, |
| 1405 | if the value can be interpreted as a truth value (see section Truth |
| 1406 | Value Testing above). |
| 1407 | |
| 1408 | They are written as \code{False} and \code{True}, respectively. |
| 1409 | \index{False} |
| 1410 | \index{True} |
| 1411 | \indexii{Boolean}{values} |
| 1412 | |
Fred Drake | 99de218 | 2001-10-30 06:23:14 +0000 | [diff] [blame] | 1413 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 1414 | \subsubsection{Internal Objects \label{typesinternal}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1415 | |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 1416 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for this |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame] | 1417 | information. It describes stack frame objects, traceback objects, and |
| 1418 | slice objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1419 | |
| 1420 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1421 | \subsection{Special Attributes \label{specialattrs}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1422 | |
| 1423 | The implementation adds a few special read-only attributes to several |
| 1424 | object types, where they are relevant: |
| 1425 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1426 | \begin{memberdesc}[object]{__dict__} |
| 1427 | A dictionary or other mapping object used to store an |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1428 | object's (writable) attributes. |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1429 | \end{memberdesc} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1430 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1431 | \begin{memberdesc}[object]{__methods__} |
Fred Drake | 3570551 | 2001-12-03 17:32:27 +0000 | [diff] [blame] | 1432 | \deprecated{2.2}{Use the built-in function \function{dir()} to get a |
| 1433 | list of an object's attributes. This attribute is no longer available.} |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1434 | \end{memberdesc} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1435 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1436 | \begin{memberdesc}[object]{__members__} |
Fred Drake | 3570551 | 2001-12-03 17:32:27 +0000 | [diff] [blame] | 1437 | \deprecated{2.2}{Use the built-in function \function{dir()} to get a |
| 1438 | list of an object's attributes. This attribute is no longer available.} |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1439 | \end{memberdesc} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1440 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1441 | \begin{memberdesc}[instance]{__class__} |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1442 | The class to which a class instance belongs. |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1443 | \end{memberdesc} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1444 | |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1445 | \begin{memberdesc}[class]{__bases__} |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 1446 | The tuple of base classes of a class object. If there are no base |
| 1447 | classes, this will be an empty tuple. |
Fred Drake | a776cea | 2000-11-06 20:17:37 +0000 | [diff] [blame] | 1448 | \end{memberdesc} |