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 |
| 5 | several others, including types themselves. There is no explicit |
| 6 | Boolean type; use integers instead. |
| 7 | \indexii{built-in}{types} |
| 8 | \indexii{Boolean}{type} |
| 9 | |
| 10 | Some operations are supported by several object types; in particular, |
| 11 | all objects can be compared, tested for truth value, and converted to |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 12 | a string (with the \code{`\textrm{\ldots}`} notation). The latter |
| 13 | conversion is implicitly used when an object is written by the |
| 14 | \keyword{print}\stindex{print} statement. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 15 | |
| 16 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 17 | \subsection{Truth Value Testing \label{truth}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 18 | |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 19 | Any object can be tested for truth value, for use in an \keyword{if} or |
| 20 | \keyword{while} condition or as operand of the Boolean operations below. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 21 | The following values are considered false: |
| 22 | \stindex{if} |
| 23 | \stindex{while} |
| 24 | \indexii{truth}{value} |
| 25 | \indexii{Boolean}{operations} |
| 26 | \index{false} |
| 27 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 28 | \begin{itemize} |
| 29 | |
| 30 | \item \code{None} |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 31 | \withsubitem{(Built-in object)}{\ttindex{None}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 32 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 33 | \item zero of any numeric type, for example, \code{0}, \code{0L}, |
| 34 | \code{0.0}, \code{0j}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 35 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 36 | \item any empty sequence, for example, \code{''}, \code{()}, \code{[]}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 37 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 38 | \item any empty mapping, for example, \code{\{\}}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 39 | |
| 40 | \item instances of user-defined classes, if the class defines a |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 41 | \method{__nonzero__()} or \method{__len__()} method, when that |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 42 | method returns zero.\footnote{Additional information on these |
| 43 | special methods may be found in the \emph{Python Reference Manual}.} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 44 | |
| 45 | \end{itemize} |
| 46 | |
| 47 | All other values are considered true --- so objects of many types are |
| 48 | always true. |
| 49 | \index{true} |
| 50 | |
| 51 | Operations and built-in functions that have a Boolean result always |
| 52 | return \code{0} for false and \code{1} for true, unless otherwise |
| 53 | stated. (Important exception: the Boolean operations |
| 54 | \samp{or}\opindex{or} and \samp{and}\opindex{and} always return one of |
| 55 | their operands.) |
| 56 | |
| 57 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 58 | \subsection{Boolean Operations \label{boolean}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 59 | |
| 60 | These are the Boolean operations, ordered by ascending priority: |
| 61 | \indexii{Boolean}{operations} |
| 62 | |
| 63 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 64 | \lineiii{\var{x} or \var{y}}{if \var{x} is false, then \var{y}, else \var{x}}{(1)} |
| 65 | \lineiii{\var{x} and \var{y}}{if \var{x} is false, then \var{x}, else \var{y}}{(1)} |
| 66 | \hline |
| 67 | \lineiii{not \var{x}}{if \var{x} is false, then \code{1}, else \code{0}}{(2)} |
| 68 | \end{tableiii} |
| 69 | \opindex{and} |
| 70 | \opindex{or} |
| 71 | \opindex{not} |
| 72 | |
| 73 | \noindent |
| 74 | Notes: |
| 75 | |
| 76 | \begin{description} |
| 77 | |
| 78 | \item[(1)] |
| 79 | These only evaluate their second argument if needed for their outcome. |
| 80 | |
| 81 | \item[(2)] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 82 | \samp{not} has a lower priority than non-Boolean operators, so |
| 83 | \code{not \var{a} == \var{b}} is interpreted as \code{not (\var{a} == |
| 84 | \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] | 85 | |
| 86 | \end{description} |
| 87 | |
| 88 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 89 | \subsection{Comparisons \label{comparisons}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 90 | |
| 91 | Comparison operations are supported by all objects. They all have the |
| 92 | same priority (which is higher than that of the Boolean operations). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 93 | Comparisons can be chained arbitrarily; for example, \code{\var{x} < |
| 94 | \var{y} <= \var{z}} is equivalent to \code{\var{x} < \var{y} and |
| 95 | \var{y} <= \var{z}}, except that \var{y} is evaluated only once (but |
| 96 | in both cases \var{z} is not evaluated at all when \code{\var{x} < |
| 97 | \var{y}} is found to be false). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 98 | \indexii{chaining}{comparisons} |
| 99 | |
| 100 | This table summarizes the comparison operations: |
| 101 | |
| 102 | \begin{tableiii}{c|l|c}{code}{Operation}{Meaning}{Notes} |
| 103 | \lineiii{<}{strictly less than}{} |
| 104 | \lineiii{<=}{less than or equal}{} |
| 105 | \lineiii{>}{strictly greater than}{} |
| 106 | \lineiii{>=}{greater than or equal}{} |
| 107 | \lineiii{==}{equal}{} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 108 | \lineiii{!=}{not equal}{(1)} |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 109 | \lineiii{<>}{not equal}{(1)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 110 | \lineiii{is}{object identity}{} |
| 111 | \lineiii{is not}{negated object identity}{} |
| 112 | \end{tableiii} |
| 113 | \indexii{operator}{comparison} |
| 114 | \opindex{==} % XXX *All* others have funny characters < ! > |
| 115 | \opindex{is} |
| 116 | \opindex{is not} |
| 117 | |
| 118 | \noindent |
| 119 | Notes: |
| 120 | |
| 121 | \begin{description} |
| 122 | |
| 123 | \item[(1)] |
| 124 | \code{<>} and \code{!=} are alternate spellings for the same operator. |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 125 | (I couldn't choose between \ABC{} and C! :-) |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 126 | \index{ABC language@\ABC{} language} |
| 127 | \index{language!ABC@\ABC{}} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 128 | \indexii{C}{language} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 129 | \code{!=} is the preferred spelling; \code{<>} is obsolescent. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 130 | |
| 131 | \end{description} |
| 132 | |
| 133 | Objects of different types, except different numeric types, never |
| 134 | compare equal; such objects are ordered consistently but arbitrarily |
| 135 | (so that sorting a heterogeneous array yields a consistent result). |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 136 | Furthermore, some types (for example, file objects) support only a |
| 137 | degenerate notion of comparison where any two objects of that type are |
| 138 | unequal. Again, such objects are ordered arbitrarily but |
| 139 | consistently. |
| 140 | \indexii{object}{numeric} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 141 | \indexii{objects}{comparing} |
| 142 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 143 | Instances of a class normally compare as non-equal unless the class |
| 144 | \withsubitem{(instance method)}{\ttindex{__cmp__()}} |
| 145 | defines the \method{__cmp__()} method. Refer to the \emph{Python |
| 146 | Reference Manual} for information on the use of this method to effect |
| 147 | object comparisons. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 148 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 149 | \strong{Implementation note:} Objects of different types except |
| 150 | numbers are ordered by their type names; objects of the same types |
| 151 | that don't support proper comparison are ordered by their address. |
| 152 | |
| 153 | Two more operations with the same syntactic priority, |
| 154 | \samp{in}\opindex{in} and \samp{not in}\opindex{not in}, are supported |
| 155 | only by sequence types (below). |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 156 | |
| 157 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 158 | \subsection{Numeric Types \label{typesnumeric}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 159 | |
| 160 | There are four numeric types: \dfn{plain integers}, \dfn{long integers}, |
| 161 | \dfn{floating point numbers}, and \dfn{complex numbers}. |
| 162 | Plain integers (also just called \dfn{integers}) |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 163 | 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] | 164 | bits of precision. Long integers have unlimited precision. Floating |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 165 | point numbers are implemented using \ctype{double} in C. All bets on |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 166 | their precision are off unless you happen to know the machine you are |
| 167 | working with. |
| 168 | \indexii{numeric}{types} |
| 169 | \indexii{integer}{types} |
| 170 | \indexii{integer}{type} |
| 171 | \indexiii{long}{integer}{type} |
| 172 | \indexii{floating point}{type} |
| 173 | \indexii{complex number}{type} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 174 | \indexii{C}{language} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 175 | |
| 176 | Complex numbers have a real and imaginary part, which are both |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 177 | implemented using \ctype{double} in C. To extract these parts from |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 178 | a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}. |
| 179 | |
| 180 | Numbers are created by numeric literals or as the result of built-in |
| 181 | functions and operators. Unadorned integer literals (including hex |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 182 | and octal numbers) yield plain integers. Integer literals with an |
| 183 | \character{L} or \character{l} suffix yield long integers |
| 184 | (\character{L} is preferred because \samp{1l} looks too much like |
| 185 | eleven!). Numeric literals containing a decimal point or an exponent |
| 186 | sign yield floating point numbers. Appending \character{j} or |
| 187 | \character{J} to a numeric literal yields a complex number. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 188 | \indexii{numeric}{literals} |
| 189 | \indexii{integer}{literals} |
| 190 | \indexiii{long}{integer}{literals} |
| 191 | \indexii{floating point}{literals} |
| 192 | \indexii{complex number}{literals} |
| 193 | \indexii{hexadecimal}{literals} |
| 194 | \indexii{octal}{literals} |
| 195 | |
| 196 | Python fully supports mixed arithmetic: when a binary arithmetic |
| 197 | operator has operands of different numeric types, the operand with the |
| 198 | ``smaller'' type is converted to that of the other, where plain |
| 199 | integer is smaller than long integer is smaller than floating point is |
| 200 | smaller than complex. |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 201 | Comparisons between numbers of mixed type use the same rule.\footnote{ |
| 202 | As a consequence, the list \code{[1, 2]} is considered equal |
Fred Drake | 82ac24f | 1999-07-02 14:29:14 +0000 | [diff] [blame] | 203 | to \code{[1.0, 2.0]}, and similar for tuples. |
| 204 | } The functions \function{int()}, \function{long()}, \function{float()}, |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 205 | and \function{complex()} can be used |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 206 | to coerce numbers to a specific type. |
| 207 | \index{arithmetic} |
| 208 | \bifuncindex{int} |
| 209 | \bifuncindex{long} |
| 210 | \bifuncindex{float} |
| 211 | \bifuncindex{complex} |
| 212 | |
| 213 | All numeric types support the following operations, sorted by |
| 214 | ascending priority (operations in the same box have the same |
| 215 | priority; all numeric operations have a higher priority than |
| 216 | comparison operations): |
| 217 | |
| 218 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 219 | \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{} |
| 220 | \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{} |
| 221 | \hline |
| 222 | \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{} |
| 223 | \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(1)} |
| 224 | \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{} |
| 225 | \hline |
| 226 | \lineiii{-\var{x}}{\var{x} negated}{} |
| 227 | \lineiii{+\var{x}}{\var{x} unchanged}{} |
| 228 | \hline |
| 229 | \lineiii{abs(\var{x})}{absolute value or magnitude of \var{x}}{} |
| 230 | \lineiii{int(\var{x})}{\var{x} converted to integer}{(2)} |
| 231 | \lineiii{long(\var{x})}{\var{x} converted to long integer}{(2)} |
| 232 | \lineiii{float(\var{x})}{\var{x} converted to floating point}{} |
| 233 | \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] | 234 | \lineiii{\var{c}.conjugate()}{conjugate of the complex number \var{c}}{} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 235 | \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)} |
| 236 | \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{} |
| 237 | \lineiii{\var{x} ** \var{y}}{\var{x} to the power \var{y}}{} |
| 238 | \end{tableiii} |
| 239 | \indexiii{operations on}{numeric}{types} |
Fred Drake | 26b698f | 1999-02-12 18:27:31 +0000 | [diff] [blame] | 240 | \withsubitem{(complex number method)}{\ttindex{conjugate()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 241 | |
| 242 | \noindent |
| 243 | Notes: |
| 244 | \begin{description} |
| 245 | |
| 246 | \item[(1)] |
| 247 | For (plain or long) integer division, the result is an integer. |
| 248 | The result is always rounded towards minus infinity: 1/2 is 0, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 249 | (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result |
| 250 | is a long integer if either operand is a long integer, regardless of |
| 251 | the numeric value. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 252 | \indexii{integer}{division} |
| 253 | \indexiii{long}{integer}{division} |
| 254 | |
| 255 | \item[(2)] |
| 256 | Conversion from floating point to (long or plain) integer may round or |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 257 | truncate as in C; see functions \function{floor()} and |
| 258 | \function{ceil()} in the \refmodule{math}\refbimodindex{math} module |
| 259 | for well-defined conversions. |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 260 | \withsubitem{(in module math)}{\ttindex{floor()}\ttindex{ceil()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 261 | \indexii{numeric}{conversions} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 262 | \indexii{C}{language} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 263 | |
| 264 | \item[(3)] |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 265 | See section \ref{built-in-funcs}, ``Built-in Functions,'' for a full |
| 266 | description. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 267 | |
| 268 | \end{description} |
| 269 | % XXXJH exceptions: overflow (when? what operations?) zerodivision |
| 270 | |
Fred Drake | 4e7c205 | 1999-02-19 15:30:25 +0000 | [diff] [blame] | 271 | \subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 272 | \nodename{Bit-string Operations} |
| 273 | |
| 274 | Plain and long integer types support additional operations that make |
| 275 | sense only for bit-strings. Negative numbers are treated as their 2's |
| 276 | complement value (for long integers, this assumes a sufficiently large |
| 277 | number of bits that no overflow occurs during the operation). |
| 278 | |
| 279 | The priorities of the binary bit-wise operations are all lower than |
| 280 | the numeric operations and higher than the comparisons; the unary |
| 281 | operation \samp{\~} has the same priority as the other unary numeric |
| 282 | operations (\samp{+} and \samp{-}). |
| 283 | |
| 284 | This table lists the bit-string operations sorted in ascending |
| 285 | priority (operations in the same box have the same priority): |
| 286 | |
| 287 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 288 | \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{} |
| 289 | \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{} |
| 290 | \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{} |
| 291 | \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{(1), (2)} |
| 292 | \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{(1), (3)} |
| 293 | \hline |
| 294 | \lineiii{\~\var{x}}{the bits of \var{x} inverted}{} |
| 295 | \end{tableiii} |
| 296 | \indexiii{operations on}{integer}{types} |
| 297 | \indexii{bit-string}{operations} |
| 298 | \indexii{shifting}{operations} |
| 299 | \indexii{masking}{operations} |
| 300 | |
| 301 | \noindent |
| 302 | Notes: |
| 303 | \begin{description} |
| 304 | \item[(1)] Negative shift counts are illegal and cause a |
| 305 | \exception{ValueError} to be raised. |
| 306 | \item[(2)] A left shift by \var{n} bits is equivalent to |
| 307 | multiplication by \code{pow(2, \var{n})} without overflow check. |
| 308 | \item[(3)] A right shift by \var{n} bits is equivalent to |
| 309 | division by \code{pow(2, \var{n})} without overflow check. |
| 310 | \end{description} |
| 311 | |
| 312 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 313 | \subsection{Sequence Types \label{typesseq}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 314 | |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 315 | There are six sequence types: strings, Unicode strings, lists, |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 316 | tuples, buffers, and xrange objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 317 | |
| 318 | Strings literals are written in single or double quotes: |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 319 | \code{'xyzzy'}, \code{"frobozz"}. See chapter 2 of the |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 320 | \citetitle[../ref/strings.html]{Python Reference Manual} for more about |
| 321 | string literals. Unicode strings are much like strings, but are |
| 322 | specified in the syntax using a preceeding \character{u} character: |
| 323 | \code{u'abc'}, \code{u"def"}. Lists are constructed with square brackets, |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 324 | separating items with commas: \code{[a, b, c]}. Tuples are |
| 325 | constructed by the comma operator (not within square brackets), with |
| 326 | or without enclosing parentheses, but an empty tuple must have the |
| 327 | enclosing parentheses, e.g., \code{a, b, c} or \code{()}. A single |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 328 | item tuple must have a trailing comma, e.g., \code{(d,)}. Buffers are |
| 329 | not directly support by Python syntax, but can created by calling the |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 330 | builtin function \function{buffer()}.\bifuncindex{buffer} XRanges |
| 331 | objects are similar to buffers in that there is no specific syntax to |
| 332 | create them, but they are created using the \function{xrange()} |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 333 | function.\bifuncindex{xrange} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 334 | \indexii{sequence}{types} |
| 335 | \indexii{string}{type} |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 336 | \indexii{Unicode}{type} |
| 337 | \indexii{buffer}{type} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 338 | \indexii{tuple}{type} |
| 339 | \indexii{list}{type} |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 340 | \indexii{xrange}{type} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 341 | |
| 342 | Sequence types support the following operations. The \samp{in} and |
| 343 | \samp{not in} operations have the same priorities as the comparison |
| 344 | operations. The \samp{+} and \samp{*} operations have the same |
| 345 | priority as the corresponding numeric operations.\footnote{They must |
| 346 | have since the parser can't tell the type of the operands.} |
| 347 | |
| 348 | This table lists the sequence operations sorted in ascending priority |
| 349 | (operations in the same box have the same priority). In the table, |
| 350 | \var{s} and \var{t} are sequences of the same type; \var{n}, \var{i} |
| 351 | and \var{j} are integers: |
| 352 | |
| 353 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 354 | \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{} |
| 355 | \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is |
| 356 | equal to \var{x}, else \code{1}}{} |
| 357 | \hline |
| 358 | \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 359 | \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} copies of \var{s} concatenated}{(1)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 360 | \hline |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 361 | \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(2)} |
| 362 | \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] | 363 | \hline |
| 364 | \lineiii{len(\var{s})}{length of \var{s}}{} |
| 365 | \lineiii{min(\var{s})}{smallest item of \var{s}}{} |
| 366 | \lineiii{max(\var{s})}{largest item of \var{s}}{} |
| 367 | \end{tableiii} |
| 368 | \indexiii{operations on}{sequence}{types} |
| 369 | \bifuncindex{len} |
| 370 | \bifuncindex{min} |
| 371 | \bifuncindex{max} |
| 372 | \indexii{concatenation}{operation} |
| 373 | \indexii{repetition}{operation} |
| 374 | \indexii{subscript}{operation} |
| 375 | \indexii{slice}{operation} |
| 376 | \opindex{in} |
| 377 | \opindex{not in} |
| 378 | |
| 379 | \noindent |
| 380 | Notes: |
| 381 | |
| 382 | \begin{description} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 383 | \item[(1)] Values of \var{n} less than \code{0} are treated as |
| 384 | \code{0} (which yields an empty sequence of the same type as |
| 385 | \var{s}). |
| 386 | |
| 387 | \item[(2)] If \var{i} or \var{j} is negative, the index is relative to |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 388 | the end of the string, i.e., \code{len(\var{s}) + \var{i}} or |
| 389 | \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is |
| 390 | still \code{0}. |
| 391 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 392 | \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] | 393 | the sequence of items with index \var{k} such that \code{\var{i} <= |
| 394 | \var{k} < \var{j}}. If \var{i} or \var{j} is greater than |
| 395 | \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted, |
| 396 | use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If |
| 397 | \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] | 398 | \end{description} |
| 399 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 400 | |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 401 | \subsubsection{String Methods \label{string-methods}} |
| 402 | |
| 403 | These are the string methods which both 8-bit strings and Unicode |
| 404 | objects support: |
| 405 | |
| 406 | \begin{methoddesc}[string]{capitalize}{} |
| 407 | Return a copy of the string with only its first character capitalized. |
| 408 | \end{methoddesc} |
| 409 | |
| 410 | \begin{methoddesc}[string]{center}{width} |
| 411 | Return centered in a string of length \var{width}. Padding is done |
| 412 | using spaces. |
| 413 | \end{methoddesc} |
| 414 | |
| 415 | \begin{methoddesc}[string]{count}{sub\optional{, start\optional{, end}}} |
| 416 | Return the number of occurrences of substring \var{sub} in string |
| 417 | S\code{[\var{start}:\var{end}]}. Optional arguments \var{start} and |
| 418 | \var{end} are interpreted as in slice notation. |
| 419 | \end{methoddesc} |
| 420 | |
| 421 | \begin{methoddesc}[string]{encode}{\optional{encoding\optional{,errors}}} |
| 422 | Return an encoded version of the string. Default encoding is the current |
| 423 | default string encoding. \var{errors} may be given to set a different |
| 424 | error handling scheme. The default for \var{errors} is |
| 425 | \code{'strict'}, meaning that encoding errors raise a |
| 426 | \exception{ValueError}. Other possible values are \code{'ignore'} and |
| 427 | \code{'replace'}. |
| 428 | \end{methoddesc} |
| 429 | |
| 430 | \begin{methoddesc}[string]{endswith}{suffix\optional{, start\optional{, end}}} |
| 431 | Return true if the string ends with the specified \var{suffix}, |
| 432 | otherwise return false. With optional \var{start}, test beginning at |
| 433 | that position. With optional \var{end}, stop comparing at that position. |
| 434 | \end{methoddesc} |
| 435 | |
| 436 | \begin{methoddesc}[string]{expandtabs}{\optional{tabsize}} |
| 437 | Return a copy of the string where all tab characters are expanded |
| 438 | using spaces. If \var{tabsize} is not given, a tab size of \code{8} |
| 439 | characters is assumed. |
| 440 | \end{methoddesc} |
| 441 | |
| 442 | \begin{methoddesc}[string]{find}{sub\optional{, start\optional{, end}}} |
| 443 | Return the lowest index in the string where substring \var{sub} is |
| 444 | found, such that \var{sub} is contained in the range [\var{start}, |
| 445 | \var{end}). Optional arguments \var{start} and \var{end} are |
| 446 | interpreted as in slice notation. Return \code{-1} if \var{sub} is |
| 447 | not found. |
| 448 | \end{methoddesc} |
| 449 | |
| 450 | \begin{methoddesc}[string]{index}{sub\optional{, start\optional{, end}}} |
| 451 | Like \method{find()}, but raise \exception{ValueError} when the |
| 452 | substring is not found. |
| 453 | \end{methoddesc} |
| 454 | |
| 455 | \begin{methoddesc}[string]{isalnum}{} |
| 456 | Return true if all characters in the string are alphanumeric and there |
| 457 | is at least one character, false otherwise. |
| 458 | \end{methoddesc} |
| 459 | |
| 460 | \begin{methoddesc}[string]{isalpha}{} |
| 461 | Return true if all characters in the string are alphabetic and there |
| 462 | is at least one character, false otherwise. |
| 463 | \end{methoddesc} |
| 464 | |
| 465 | \begin{methoddesc}[string]{isdigit}{} |
| 466 | Return true if there are only digit characters, false otherwise. |
| 467 | \end{methoddesc} |
| 468 | |
| 469 | \begin{methoddesc}[string]{islower}{} |
| 470 | Return true if all cased characters in the string are lowercase and |
| 471 | there is at least one cased character, false otherwise. |
| 472 | \end{methoddesc} |
| 473 | |
| 474 | \begin{methoddesc}[string]{isspace}{} |
| 475 | Return true if there are only whitespace characters in the string and |
| 476 | the string is not empty, false otherwise. |
| 477 | \end{methoddesc} |
| 478 | |
| 479 | \begin{methoddesc}[string]{istitle}{} |
| 480 | Return true if the string is a titlecased string, i.e.\ uppercase |
| 481 | characters may only follow uncased characters and lowercase characters |
| 482 | only cased ones. Return false otherwise. |
| 483 | \end{methoddesc} |
| 484 | |
| 485 | \begin{methoddesc}[string]{isupper}{} |
| 486 | Return true if all cased characters in the string are uppercase and |
| 487 | there is at least one cased character, false otherwise. |
| 488 | \end{methoddesc} |
| 489 | |
| 490 | \begin{methoddesc}[string]{join}{seq} |
| 491 | Return a string which is the concatenation of the strings in the |
| 492 | sequence \var{seq}. The separator between elements is the string |
| 493 | providing this method. |
| 494 | \end{methoddesc} |
| 495 | |
| 496 | \begin{methoddesc}[string]{ljust}{width} |
| 497 | Return the string left justified in a string of length \var{width}. |
| 498 | Padding is done using spaces. The original string is returned if |
| 499 | \var{width} is less than \code{len(\var{s})}. |
| 500 | \end{methoddesc} |
| 501 | |
| 502 | \begin{methoddesc}[string]{lower}{} |
| 503 | Return a copy of the string converted to lowercase. |
| 504 | \end{methoddesc} |
| 505 | |
| 506 | \begin{methoddesc}[string]{lstrip}{} |
| 507 | Return a copy of the string with leading whitespace removed. |
| 508 | \end{methoddesc} |
| 509 | |
| 510 | \begin{methoddesc}[string]{replace}{old, new\optional{, maxsplit}} |
| 511 | Return a copy of the string with all occurrences of substring |
| 512 | \var{old} replaced by \var{new}. If the optional argument |
| 513 | \var{maxsplit} is given, only the first \var{maxsplit} occurrences are |
| 514 | replaced. |
| 515 | \end{methoddesc} |
| 516 | |
| 517 | \begin{methoddesc}[string]{rfind}{sub \optional{,start \optional{,end}}} |
| 518 | Return the highest index in the string where substring \var{sub} is |
| 519 | found, such that \var{sub} is contained within s[start,end]. Optional |
| 520 | arguments \var{start} and \var{end} are interpreted as in slice |
| 521 | notation. Return \code{-1} on failure. |
| 522 | \end{methoddesc} |
| 523 | |
| 524 | \begin{methoddesc}[string]{rindex}{sub\optional{, start\optional{, end}}} |
| 525 | Like \method{rfind()} but raises \exception{ValueError} when the |
| 526 | substring \var{sub} is not found. |
| 527 | \end{methoddesc} |
| 528 | |
| 529 | \begin{methoddesc}[string]{rjust}{width} |
| 530 | Return the string right justified in a string of length \var{width}. |
| 531 | Padding is done using spaces. The original string is returned if |
| 532 | \var{width} is less than \code{len(\var{s})}. |
| 533 | \end{methoddesc} |
| 534 | |
| 535 | \begin{methoddesc}[string]{rstrip}{} |
| 536 | Return a copy of the string with trailing whitespace removed. |
| 537 | \end{methoddesc} |
| 538 | |
| 539 | \begin{methoddesc}[string]{split}{\optional{sep \optional{,maxsplit}}} |
| 540 | Return a list of the words in the string, using \var{sep} as the |
| 541 | delimiter string. If \var{maxsplit} is given, at most \var{maxsplit} |
| 542 | splits are done. If \var{sep} is not specified or \code{None}, any |
| 543 | whitespace string is a separator. |
| 544 | \end{methoddesc} |
| 545 | |
| 546 | \begin{methoddesc}[string]{splitlines}{\optional{keepends}} |
| 547 | Return a list of the lines in the string, breaking at line |
| 548 | boundaries. Line breaks are not included in the resulting list unless |
| 549 | \var{keepends} is given and true. |
| 550 | \end{methoddesc} |
| 551 | |
| 552 | \begin{methoddesc}[string]{startswith}{prefix\optional{, start\optional{, end}}} |
| 553 | Return true if string starts with the \var{prefix}, otherwise |
| 554 | return false. With optional \var{start}, test string beginning at |
| 555 | that position. With optional \var{end}, stop comparing string at that |
| 556 | position. |
| 557 | \end{methoddesc} |
| 558 | |
| 559 | \begin{methoddesc}[string]{strip}{} |
| 560 | Return a copy of the string with leading and trailing whitespace |
| 561 | removed. |
| 562 | \end{methoddesc} |
| 563 | |
| 564 | \begin{methoddesc}[string]{swapcase}{} |
| 565 | Return a copy of the string with uppercase characters converted to |
| 566 | lowercase and vice versa. |
| 567 | \end{methoddesc} |
| 568 | |
| 569 | \begin{methoddesc}[string]{title}{} |
| 570 | Return a titlecased version of, i.e.\ words start with uppercase |
| 571 | characters, all remaining cased characters are lowercase. |
| 572 | \end{methoddesc} |
| 573 | |
| 574 | \begin{methoddesc}[string]{translate}{table\optional{, deletechars}} |
| 575 | Return a copy of the string where all characters occurring in the |
| 576 | optional argument \var{deletechars} are removed, and the remaining |
| 577 | characters have been mapped through the given translation table, which |
| 578 | must be a string of length 256. |
| 579 | \end{methoddesc} |
| 580 | |
| 581 | \begin{methoddesc}[string]{upper}{} |
| 582 | Return a copy of the string converted to uppercase. |
| 583 | \end{methoddesc} |
| 584 | |
| 585 | |
| 586 | \subsubsection{String Formatting Operations \label{typesseq-strings}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 587 | |
| 588 | String objects have one unique built-in operation: the \code{\%} |
| 589 | operator (modulo) with a string left argument interprets this string |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 590 | as a C \cfunction{sprintf()} format string to be applied to the |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 591 | right argument, and returns the string resulting from this formatting |
| 592 | operation. |
| 593 | |
| 594 | The right argument should be a tuple with one item for each argument |
| 595 | required by the format string; if the string requires a single |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 596 | argument, the right argument may also be a single non-tuple |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 597 | object.\footnote{A tuple object in this case should be a singleton. |
| 598 | } The following format characters are understood: |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 599 | \code{\%}, \code{c}, \code{s}, \code{i}, \code{d}, \code{u}, \code{o}, |
| 600 | \code{x}, \code{X}, \code{e}, \code{E}, \code{f}, \code{g}, \code{G}. |
| 601 | Width and precision may be a \code{*} to specify that an integer argument |
| 602 | specifies the actual width or precision. The flag characters |
Fred Drake | 6d20caa | 1999-04-21 18:17:11 +0000 | [diff] [blame] | 603 | \code{-}, \code{+}, blank, \code{\#} and \code{0} are understood. The |
| 604 | size specifiers \code{h}, \code{l} or \code{L} may be present but are |
| 605 | ignored. The \code{\%s} conversion takes any Python object and |
| 606 | converts it to a string using \code{str()} before formatting it. The |
| 607 | ANSI features \code{\%p} and \code{\%n} are not supported. Since |
| 608 | Python strings have an explicit length, \code{\%s} conversions don't |
| 609 | assume that \code{'\e0'} is the end of the string. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 610 | |
| 611 | For safety reasons, floating point precisions are clipped to 50; |
| 612 | \code{\%f} conversions for numbers whose absolute value is over 1e25 |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 613 | are replaced by \code{\%g} conversions.\footnote{ |
| 614 | These numbers are fairly arbitrary. They are intended to |
| 615 | avoid printing endless strings of meaningless digits without hampering |
| 616 | correct use and without having to know the exact precision of floating |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 617 | point values on a particular machine. |
| 618 | } All other errors raise exceptions. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 619 | |
| 620 | If the right argument is a dictionary (or any kind of mapping), then |
| 621 | the formats in the string must have a parenthesized key into that |
| 622 | dictionary inserted immediately after the \character{\%} character, |
| 623 | and each format formats the corresponding entry from the mapping. |
| 624 | For example: |
| 625 | |
| 626 | \begin{verbatim} |
| 627 | >>> count = 2 |
| 628 | >>> language = 'Python' |
| 629 | >>> print '%(language)s has %(count)03d quote types.' % vars() |
| 630 | Python has 002 quote types. |
| 631 | \end{verbatim} |
| 632 | |
| 633 | In this case no \code{*} specifiers may occur in a format (since they |
| 634 | require a sequential parameter list). |
| 635 | |
| 636 | Additional string operations are defined in standard module |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 637 | \refmodule{string} and in built-in module \refmodule{re}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 638 | \refstmodindex{string} |
Fred Drake | 66da9d6 | 1998-08-07 18:57:18 +0000 | [diff] [blame] | 639 | \refstmodindex{re} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 640 | |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 641 | |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 642 | \subsubsection{XRange Type \label{typesseq-xrange}} |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 643 | |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 644 | The xrange\indexii{xrange}{type} type is an immutable sequence which is |
| 645 | commonly used for looping. The advantage of the xrange type is that an |
| 646 | 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] | 647 | size of the range it represents. There are no consistent performance |
| 648 | advantages. |
| 649 | |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 650 | XRange objects behave like tuples, and offer a single method: |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 651 | |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 652 | \begin{methoddesc}[xrange]{tolist}{} |
| 653 | Return a list object which represents the same values as the xrange |
Fred Drake | 107b967 | 2000-08-14 15:37:59 +0000 | [diff] [blame] | 654 | object. |
| 655 | \end{methoddesc} |
| 656 | |
| 657 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 658 | \subsubsection{Mutable Sequence Types \label{typesseq-mutable}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 659 | |
| 660 | List objects support additional operations that allow in-place |
| 661 | modification of the object. |
| 662 | These operations would be supported by other mutable sequence types |
| 663 | (when added to the language) as well. |
| 664 | Strings and tuples are immutable sequence types and such objects cannot |
| 665 | be modified once created. |
| 666 | The following operations are defined on mutable sequence types (where |
| 667 | \var{x} is an arbitrary object): |
| 668 | \indexiii{mutable}{sequence}{types} |
| 669 | \indexii{list}{type} |
| 670 | |
| 671 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 672 | \lineiii{\var{s}[\var{i}] = \var{x}} |
| 673 | {item \var{i} of \var{s} is replaced by \var{x}}{} |
| 674 | \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}} |
| 675 | {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{} |
| 676 | \lineiii{del \var{s}[\var{i}:\var{j}]} |
| 677 | {same as \code{\var{s}[\var{i}:\var{j}] = []}}{} |
| 678 | \lineiii{\var{s}.append(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 679 | {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] | 680 | \lineiii{\var{s}.extend(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 681 | {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] | 682 | \lineiii{\var{s}.count(\var{x})} |
| 683 | {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{} |
| 684 | \lineiii{\var{s}.index(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 685 | {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] | 686 | \lineiii{\var{s}.insert(\var{i}, \var{x})} |
| 687 | {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]} |
| 688 | if \code{\var{i} >= 0}}{} |
| 689 | \lineiii{\var{s}.pop(\optional{\var{i}})} |
| 690 | {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(4)} |
| 691 | \lineiii{\var{s}.remove(\var{x})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 692 | {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(3)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 693 | \lineiii{\var{s}.reverse()} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 694 | {reverses the items of \var{s} in place}{(5)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 695 | \lineiii{\var{s}.sort(\optional{\var{cmpfunc}})} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 696 | {sort the items of \var{s} in place}{(5), (6)} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 697 | \end{tableiii} |
| 698 | \indexiv{operations on}{mutable}{sequence}{types} |
| 699 | \indexiii{operations on}{sequence}{types} |
| 700 | \indexiii{operations on}{list}{type} |
| 701 | \indexii{subscript}{assignment} |
| 702 | \indexii{slice}{assignment} |
| 703 | \stindex{del} |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 704 | \withsubitem{(list method)}{ |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 705 | \ttindex{append()}\ttindex{extend()}\ttindex{count()}\ttindex{index()} |
| 706 | \ttindex{insert()}\ttindex{pop()}\ttindex{remove()}\ttindex{reverse()} |
Fred Drake | e839199 | 1998-11-25 17:09:19 +0000 | [diff] [blame] | 707 | \ttindex{sort()}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 708 | \noindent |
| 709 | Notes: |
| 710 | \begin{description} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 711 | \item[(1)] The C implementation of Python has historically accepted |
| 712 | multiple parameters and implicitly joined them into a tuple; this |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 713 | 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] | 714 | deprecated since Python 1.4. |
| 715 | |
| 716 | \item[(2)] Raises an exception when \var{x} is not a list object. The |
| 717 | \method{extend()} method is experimental and not supported by |
| 718 | mutable sequence types other than lists. |
| 719 | |
| 720 | \item[(3)] Raises \exception{ValueError} when \var{x} is not found in |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 721 | \var{s}. |
| 722 | |
Peter Schneider-Kamp | f917bf6 | 2000-08-01 00:07:17 +0000 | [diff] [blame] | 723 | \item[(4)] The \method{pop()} method is only supported by the list and |
Fred Drake | fbd3b45 | 2000-07-31 23:42:23 +0000 | [diff] [blame] | 724 | array types. The optional argument \var{i} defaults to \code{-1}, |
| 725 | so that by default the last item is removed and returned. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 726 | |
| 727 | \item[(5)] The \method{sort()} and \method{reverse()} methods modify the |
| 728 | list in place for economy of space when sorting or reversing a large |
| 729 | list. They don't return the sorted or reversed list to remind you |
| 730 | of this side effect. |
| 731 | |
| 732 | \item[(6)] The \method{sort()} method takes an optional argument |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 733 | specifying a comparison function of two arguments (list items) which |
Fred Drake | 68921df | 1999-08-09 17:05:12 +0000 | [diff] [blame] | 734 | should return \code{-1}, \code{0} or \code{1} depending on whether |
| 735 | the first argument is considered smaller than, equal to, or larger |
| 736 | than the second argument. Note that this slows the sorting process |
| 737 | down considerably; e.g. to sort a list in reverse order it is much |
| 738 | faster to use calls to the methods \method{sort()} and |
| 739 | \method{reverse()} than to use the built-in function |
| 740 | \function{sort()} with a comparison function that reverses the |
| 741 | ordering of the elements. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 742 | \end{description} |
| 743 | |
| 744 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 745 | \subsection{Mapping Types \label{typesmapping}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 746 | \indexii{mapping}{types} |
| 747 | \indexii{dictionary}{type} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 748 | |
| 749 | A \dfn{mapping} object maps values of one type (the key type) to |
| 750 | arbitrary objects. Mappings are mutable objects. There is currently |
| 751 | only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are |
| 752 | almost arbitrary values. The only types of values not acceptable as |
| 753 | keys are values containing lists or dictionaries or other mutable |
| 754 | types that are compared by value rather than by object identity. |
| 755 | Numeric types used for keys obey the normal rules for numeric |
| 756 | comparison: if two numbers compare equal (e.g. \code{1} and |
| 757 | \code{1.0}) then they can be used interchangeably to index the same |
| 758 | dictionary entry. |
| 759 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 760 | Dictionaries are created by placing a comma-separated list of |
| 761 | \code{\var{key}: \var{value}} pairs within braces, for example: |
| 762 | \code{\{'jack': 4098, 'sjoerd': 4127\}} or |
| 763 | \code{\{4098: 'jack', 4127: 'sjoerd'\}}. |
| 764 | |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 765 | The following operations are defined on mappings (where \var{a} and |
| 766 | \var{b} are mappings, \var{k} is a key, and \var{v} and \var{x} are |
| 767 | arbitrary objects): |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 768 | \indexiii{operations on}{mapping}{types} |
| 769 | \indexiii{operations on}{dictionary}{type} |
| 770 | \stindex{del} |
| 771 | \bifuncindex{len} |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 772 | \withsubitem{(dictionary method)}{ |
| 773 | \ttindex{clear()} |
| 774 | \ttindex{copy()} |
| 775 | \ttindex{has_key()} |
| 776 | \ttindex{items()} |
| 777 | \ttindex{keys()} |
| 778 | \ttindex{update()} |
| 779 | \ttindex{values()} |
Fred Drake | e839199 | 1998-11-25 17:09:19 +0000 | [diff] [blame] | 780 | \ttindex{get()}} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 781 | |
| 782 | \begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes} |
| 783 | \lineiii{len(\var{a})}{the number of items in \var{a}}{} |
| 784 | \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] | 785 | \lineiii{\var{a}[\var{k}] = \var{v}} |
| 786 | {set \code{\var{a}[\var{k}]} to \var{v}} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 787 | {} |
| 788 | \lineiii{del \var{a}[\var{k}]} |
| 789 | {remove \code{\var{a}[\var{k}]} from \var{a}} |
| 790 | {(1)} |
| 791 | \lineiii{\var{a}.clear()}{remove all items from \code{a}}{} |
| 792 | \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{} |
| 793 | \lineiii{\var{a}.has_key(\var{k})} |
| 794 | {\code{1} if \var{a} has a key \var{k}, else \code{0}} |
| 795 | {} |
| 796 | \lineiii{\var{a}.items()} |
| 797 | {a copy of \var{a}'s list of (\var{key}, \var{value}) pairs} |
| 798 | {(2)} |
| 799 | \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(2)} |
| 800 | \lineiii{\var{a}.update(\var{b})} |
Fred Drake | 1e75e17 | 2000-07-31 16:34:46 +0000 | [diff] [blame] | 801 | {\code{for k in \var{b}.keys(): \var{a}[k] = \var{b}[k]}} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 802 | {(3)} |
| 803 | \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(2)} |
| 804 | \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})} |
| 805 | {\code{\var{a}[\var{k}]} if \code{\var{a}.has_key(\var{k})}, |
| 806 | else \var{x}} |
| 807 | {(4)} |
Guido van Rossum | 8141cf5 | 2000-08-08 16:15:49 +0000 | [diff] [blame] | 808 | \lineiii{\var{a}.setdefault(\var{k}\optional{, \var{x}})} |
| 809 | {\code{\var{a}[\var{k}]} if \code{\var{a}.has_key(\var{k})}, |
| 810 | else \var{x} (also setting it)} |
| 811 | {(5)} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 812 | \end{tableiii} |
| 813 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 814 | \noindent |
| 815 | Notes: |
| 816 | \begin{description} |
Fred Drake | 9c5cc14 | 1999-06-10 22:37:34 +0000 | [diff] [blame] | 817 | \item[(1)] Raises a \exception{KeyError} exception if \var{k} is not |
| 818 | in the map. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 819 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 820 | \item[(2)] Keys and values are listed in random order. If |
| 821 | \method{keys()} and \method{values()} are called with no intervening |
| 822 | modifications to the dictionary, the two lists will directly |
| 823 | correspond. This allows the creation of \code{(\var{value}, |
| 824 | \var{key})} pairs using \function{map()}: \samp{pairs = map(None, |
| 825 | \var{a}.values(), \var{a}.keys())}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 826 | |
| 827 | \item[(3)] \var{b} must be of the same type as \var{a}. |
| 828 | |
| 829 | \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] | 830 | 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] | 831 | 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] | 832 | |
| 833 | \item[(5)] \function{setdefault()} is like \function{get()}, except |
| 834 | that if \var{k} is missing, \var{x} is both returned and inserted into |
| 835 | the dictionary as the value of \var{k}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 836 | \end{description} |
| 837 | |
| 838 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 839 | \subsection{Other Built-in Types \label{typesother}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 840 | |
| 841 | The interpreter supports several other kinds of objects. |
| 842 | Most of these support only one or two operations. |
| 843 | |
Fred Drake | 4e7c205 | 1999-02-19 15:30:25 +0000 | [diff] [blame] | 844 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 845 | \subsubsection{Modules \label{typesmodules}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 846 | |
| 847 | The only special operation on a module is attribute access: |
| 848 | \code{\var{m}.\var{name}}, where \var{m} is a module and \var{name} |
| 849 | accesses a name defined in \var{m}'s symbol table. Module attributes |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 850 | can be assigned to. (Note that the \keyword{import} statement is not, |
Fred Drake | d0421dd | 1998-08-24 17:57:20 +0000 | [diff] [blame] | 851 | strictly speaking, an operation on a module object; \code{import |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 852 | \var{foo}} does not require a module object named \var{foo} to exist, |
| 853 | rather it requires an (external) \emph{definition} for a module named |
| 854 | \var{foo} somewhere.) |
| 855 | |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 856 | A special member of every module is \member{__dict__}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 857 | This is the dictionary containing the module's symbol table. |
| 858 | Modifying this dictionary will actually change the module's symbol |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 859 | table, but direct assignment to the \member{__dict__} attribute is not |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 860 | possible (i.e., you can write \code{\var{m}.__dict__['a'] = 1}, which |
| 861 | defines \code{\var{m}.a} to be \code{1}, but you can't write |
| 862 | \code{\var{m}.__dict__ = \{\}}. |
| 863 | |
Fred Drake | 4e7c205 | 1999-02-19 15:30:25 +0000 | [diff] [blame] | 864 | Modules built into the interpreter are written like this: |
| 865 | \code{<module 'sys' (built-in)>}. If loaded from a file, they are |
| 866 | written as \code{<module 'os' from '/usr/local/lib/python1.5/os.pyc'>}. |
| 867 | |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 868 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 869 | \subsubsection{Classes and Class Instances \label{typesobjects}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 870 | \nodename{Classes and Instances} |
| 871 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 872 | See chapters 3 and 7 of the \citetitle[../ref/ref.html]{Python |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 873 | Reference Manual} for these. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 874 | |
Fred Drake | 4e7c205 | 1999-02-19 15:30:25 +0000 | [diff] [blame] | 875 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 876 | \subsubsection{Functions \label{typesfunctions}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 877 | |
| 878 | Function objects are created by function definitions. The only |
| 879 | operation on a function object is to call it: |
| 880 | \code{\var{func}(\var{argument-list})}. |
| 881 | |
| 882 | There are really two flavors of function objects: built-in functions |
| 883 | and user-defined functions. Both support the same operation (to call |
| 884 | the function), but the implementation is different, hence the |
| 885 | different object types. |
| 886 | |
| 887 | The implementation adds two special read-only attributes: |
| 888 | \code{\var{f}.func_code} is a function's \dfn{code |
| 889 | object}\obindex{code} (see below) and \code{\var{f}.func_globals} is |
| 890 | the dictionary used as the function's global name space (this is the |
| 891 | same as \code{\var{m}.__dict__} where \var{m} is the module in which |
| 892 | the function \var{f} was defined). |
| 893 | |
| 894 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 895 | \subsubsection{Methods \label{typesmethods}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 896 | \obindex{method} |
| 897 | |
| 898 | Methods are functions that are called using the attribute notation. |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 899 | There are two flavors: built-in methods (such as \method{append()} on |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 900 | lists) and class instance methods. Built-in methods are described |
| 901 | with the types that support them. |
| 902 | |
| 903 | The implementation adds two special read-only attributes to class |
Fred Drake | d0421dd | 1998-08-24 17:57:20 +0000 | [diff] [blame] | 904 | instance methods: \code{\var{m}.im_self} is the object on which the |
| 905 | method operates, and \code{\var{m}.im_func} is the function |
| 906 | implementing the method. Calling \code{\var{m}(\var{arg-1}, |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 907 | \var{arg-2}, \textrm{\ldots}, \var{arg-n})} is completely equivalent to |
Fred Drake | d0421dd | 1998-08-24 17:57:20 +0000 | [diff] [blame] | 908 | calling \code{\var{m}.im_func(\var{m}.im_self, \var{arg-1}, |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 909 | \var{arg-2}, \textrm{\ldots}, \var{arg-n})}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 910 | |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 911 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for more |
| 912 | information. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 913 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 914 | |
| 915 | \subsubsection{Code Objects \label{bltin-code-objects}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 916 | \obindex{code} |
| 917 | |
| 918 | Code objects are used by the implementation to represent |
| 919 | ``pseudo-compiled'' executable Python code such as a function body. |
| 920 | They differ from function objects because they don't contain a |
| 921 | reference to their global execution environment. Code objects are |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 922 | returned by the built-in \function{compile()} function and can be |
| 923 | extracted from function objects through their \member{func_code} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 924 | attribute. |
| 925 | \bifuncindex{compile} |
Fred Drake | e839199 | 1998-11-25 17:09:19 +0000 | [diff] [blame] | 926 | \withsubitem{(function object attribute)}{\ttindex{func_code}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 927 | |
| 928 | A code object can be executed or evaluated by passing it (instead of a |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 929 | source string) to the \keyword{exec} statement or the built-in |
| 930 | \function{eval()} function. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 931 | \stindex{exec} |
| 932 | \bifuncindex{eval} |
| 933 | |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 934 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for more |
| 935 | information. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 936 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 937 | |
| 938 | \subsubsection{Type Objects \label{bltin-type-objects}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 939 | |
| 940 | Type objects represent the various object types. An object's type is |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 941 | accessed by the built-in function \function{type()}. There are no special |
| 942 | operations on types. The standard module \module{types} defines names |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 943 | for all standard built-in types. |
| 944 | \bifuncindex{type} |
| 945 | \refstmodindex{types} |
| 946 | |
| 947 | Types are written like this: \code{<type 'int'>}. |
| 948 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 949 | |
| 950 | \subsubsection{The Null Object \label{bltin-null-object}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 951 | |
| 952 | This object is returned by functions that don't explicitly return a |
| 953 | value. It supports no special operations. There is exactly one null |
| 954 | object, named \code{None} (a built-in name). |
| 955 | |
| 956 | It is written as \code{None}. |
| 957 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 958 | |
| 959 | \subsubsection{The Ellipsis Object \label{bltin-ellipsis-object}} |
Guido van Rossum | b193c95 | 1998-07-24 15:02:02 +0000 | [diff] [blame] | 960 | |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 961 | This object is used by extended slice notation (see the |
| 962 | \citetitle[../ref/ref.html]{Python Reference Manual}). It supports no |
| 963 | special operations. There is exactly one ellipsis object, named |
| 964 | \constant{Ellipsis} (a built-in name). |
Guido van Rossum | b193c95 | 1998-07-24 15:02:02 +0000 | [diff] [blame] | 965 | |
| 966 | It is written as \code{Ellipsis}. |
| 967 | |
Fred Drake | c3fcd6f | 1999-04-21 13:58:17 +0000 | [diff] [blame] | 968 | \subsubsection{File Objects\obindex{file} |
| 969 | \label{bltin-file-objects}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 970 | |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 971 | File objects are implemented using C's \code{stdio} package and can be |
| 972 | created with the built-in function |
| 973 | \function{open()}\bifuncindex{open} described in section |
Fred Drake | 130072d | 1998-10-28 20:08:35 +0000 | [diff] [blame] | 974 | \ref{built-in-funcs}, ``Built-in Functions.'' They are also returned |
| 975 | by some other built-in functions and methods, e.g., |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 976 | \function{os.popen()} and \function{os.fdopen()} and the |
Fred Drake | 130072d | 1998-10-28 20:08:35 +0000 | [diff] [blame] | 977 | \method{makefile()} method of socket objects. |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 978 | \refstmodindex{os} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 979 | \refbimodindex{socket} |
| 980 | |
| 981 | 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] | 982 | \exception{IOError} is raised. This includes situations where the |
| 983 | 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] | 984 | device or writing a file opened for reading. |
| 985 | |
| 986 | Files have the following methods: |
| 987 | |
| 988 | |
| 989 | \begin{methoddesc}[file]{close}{} |
| 990 | Close the file. A closed file cannot be read or written anymore. |
| 991 | \end{methoddesc} |
| 992 | |
| 993 | \begin{methoddesc}[file]{flush}{} |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 994 | Flush the internal buffer, like \code{stdio}'s \cfunction{fflush()}. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 995 | \end{methoddesc} |
| 996 | |
| 997 | \begin{methoddesc}[file]{isatty}{} |
| 998 | Return \code{1} if the file is connected to a tty(-like) device, else |
| 999 | \code{0}. |
| 1000 | \end{methoddesc} |
| 1001 | |
| 1002 | \begin{methoddesc}[file]{fileno}{} |
| 1003 | Return the integer ``file descriptor'' that is used by the underlying |
| 1004 | implementation to request I/O operations from the operating system. |
| 1005 | This can be useful for other, lower level interfaces that use file |
Fred Drake | 84538cd | 1998-11-30 21:51:25 +0000 | [diff] [blame] | 1006 | descriptors, e.g. module \module{fcntl} or \function{os.read()} and friends. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1007 | \refbimodindex{fcntl} |
| 1008 | \end{methoddesc} |
| 1009 | |
| 1010 | \begin{methoddesc}[file]{read}{\optional{size}} |
| 1011 | 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] | 1012 | \EOF{} before obtaining \var{size} bytes). If the \var{size} |
| 1013 | argument is negative or omitted, read all data until \EOF{} is |
| 1014 | reached. The bytes are returned as a string object. An empty |
| 1015 | string is returned when \EOF{} is encountered immediately. (For |
| 1016 | certain files, like ttys, it makes sense to continue reading after |
| 1017 | an \EOF{} is hit.) Note that this method may call the underlying |
| 1018 | C function \cfunction{fread()} more than once in an effort to |
| 1019 | acquire as close to \var{size} bytes as possible. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1020 | \end{methoddesc} |
| 1021 | |
| 1022 | \begin{methoddesc}[file]{readline}{\optional{size}} |
| 1023 | Read one entire line from the file. A trailing newline character is |
Fred Drake | ea003fc | 1999-04-05 21:59:15 +0000 | [diff] [blame] | 1024 | kept in the string\footnote{ |
| 1025 | The advantage of leaving the newline on is that an empty string |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1026 | can be returned to mean \EOF{} without being ambiguous. Another |
| 1027 | advantage is that (in cases where it might matter, e.g. if you |
| 1028 | want to make an exact copy of a file while scanning its lines) |
| 1029 | 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] | 1030 | or not (yes this happens!). |
| 1031 | } (but may be absent when a file ends with an |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1032 | incomplete line). If the \var{size} argument is present and |
| 1033 | non-negative, it is a maximum byte count (including the trailing |
| 1034 | newline) and an incomplete line may be returned. |
| 1035 | An empty string is returned when \EOF{} is hit |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1036 | immediately. Note: Unlike \code{stdio}'s \cfunction{fgets()}, the returned |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1037 | string contains null characters (\code{'\e 0'}) if they occurred in the |
| 1038 | input. |
| 1039 | \end{methoddesc} |
| 1040 | |
| 1041 | \begin{methoddesc}[file]{readlines}{\optional{sizehint}} |
| 1042 | Read until \EOF{} using \method{readline()} and return a list containing |
| 1043 | the lines thus read. If the optional \var{sizehint} argument is |
| 1044 | present, instead of reading up to \EOF{}, whole lines totalling |
| 1045 | approximately \var{sizehint} bytes (possibly after rounding up to an |
| 1046 | internal buffer size) are read. |
| 1047 | \end{methoddesc} |
| 1048 | |
| 1049 | \begin{methoddesc}[file]{seek}{offset\optional{, whence}} |
| 1050 | Set the file's current position, like \code{stdio}'s \cfunction{fseek()}. |
| 1051 | The \var{whence} argument is optional and defaults to \code{0} |
| 1052 | (absolute file positioning); other values are \code{1} (seek |
| 1053 | relative to the current position) and \code{2} (seek relative to the |
| 1054 | file's end). There is no return value. |
| 1055 | \end{methoddesc} |
| 1056 | |
| 1057 | \begin{methoddesc}[file]{tell}{} |
| 1058 | Return the file's current position, like \code{stdio}'s |
| 1059 | \cfunction{ftell()}. |
| 1060 | \end{methoddesc} |
| 1061 | |
| 1062 | \begin{methoddesc}[file]{truncate}{\optional{size}} |
| 1063 | Truncate the file's size. If the optional size argument present, the |
| 1064 | file is truncated to (at most) that size. The size defaults to the |
| 1065 | current position. Availability of this function depends on the |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1066 | operating system version (for example, not all \UNIX{} versions support this |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1067 | operation). |
| 1068 | \end{methoddesc} |
| 1069 | |
| 1070 | \begin{methoddesc}[file]{write}{str} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 1071 | Write a string to the file. There is no return value. Note: Due to |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1072 | buffering, the string may not actually show up in the file until |
| 1073 | the \method{flush()} or \method{close()} method is called. |
| 1074 | \end{methoddesc} |
| 1075 | |
| 1076 | \begin{methoddesc}[file]{writelines}{list} |
| 1077 | Write a list of strings to the file. There is no return value. |
| 1078 | (The name is intended to match \method{readlines()}; |
| 1079 | \method{writelines()} does not add line separators.) |
| 1080 | \end{methoddesc} |
| 1081 | |
| 1082 | |
| 1083 | File objects also offer the following attributes: |
| 1084 | |
| 1085 | \begin{memberdesc}[file]{closed} |
| 1086 | Boolean indicating the current state of the file object. This is a |
| 1087 | read-only attribute; the \method{close()} method changes the value. |
| 1088 | \end{memberdesc} |
| 1089 | |
| 1090 | \begin{memberdesc}[file]{mode} |
| 1091 | The I/O mode for the file. If the file was created using the |
| 1092 | \function{open()} built-in function, this will be the value of the |
| 1093 | \var{mode} parameter. This is a read-only attribute. |
| 1094 | \end{memberdesc} |
| 1095 | |
| 1096 | \begin{memberdesc}[file]{name} |
| 1097 | If the file object was created using \function{open()}, the name of |
| 1098 | the file. Otherwise, some string that indicates the source of the |
| 1099 | file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only |
| 1100 | attribute. |
| 1101 | \end{memberdesc} |
| 1102 | |
| 1103 | \begin{memberdesc}[file]{softspace} |
| 1104 | Boolean that indicates whether a space character needs to be printed |
| 1105 | before another value when using the \keyword{print} statement. |
| 1106 | Classes that are trying to simulate a file object should also have a |
| 1107 | writable \member{softspace} attribute, which should be initialized to |
| 1108 | zero. This will be automatic for classes implemented in Python; types |
Fred Drake | 4de96c2 | 2000-08-12 03:36:23 +0000 | [diff] [blame] | 1109 | implemented in C will have to provide a writable \member{softspace} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1110 | attribute. |
| 1111 | \end{memberdesc} |
| 1112 | |
Fred Drake | 9474d86 | 1999-02-12 22:05:33 +0000 | [diff] [blame] | 1113 | \subsubsection{Internal Objects \label{typesinternal}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1114 | |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 1115 | See the \citetitle[../ref/ref.html]{Python Reference Manual} for this |
Fred Drake | 512bb72 | 2000-08-18 03:12:38 +0000 | [diff] [blame^] | 1116 | information. It describes stack frame objects, traceback objects, and |
| 1117 | slice objects. |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1118 | |
| 1119 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1120 | \subsection{Special Attributes \label{specialattrs}} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1121 | |
| 1122 | The implementation adds a few special read-only attributes to several |
| 1123 | object types, where they are relevant: |
| 1124 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1125 | \begin{memberdescni}{__dict__} |
| 1126 | A dictionary of some sort used to store an |
| 1127 | object's (writable) attributes. |
| 1128 | \end{memberdescni} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1129 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1130 | \begin{memberdescni}{__methods__} |
| 1131 | List of the methods of many built-in object types, |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1132 | e.g., \code{[].__methods__} yields |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1133 | \code{['append', 'count', 'index', 'insert', 'pop', 'remove', |
| 1134 | 'reverse', 'sort']}. |
| 1135 | \end{memberdescni} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1136 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1137 | \begin{memberdescni}{__members__} |
| 1138 | Similar to \member{__methods__}, but lists data attributes. |
| 1139 | \end{memberdescni} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1140 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1141 | \begin{memberdescni}{__class__} |
| 1142 | The class to which a class instance belongs. |
| 1143 | \end{memberdescni} |
Fred Drake | 64e3b43 | 1998-07-24 13:56:11 +0000 | [diff] [blame] | 1144 | |
Fred Drake | 7a2f066 | 1998-09-10 18:25:58 +0000 | [diff] [blame] | 1145 | \begin{memberdescni}{__bases__} |
| 1146 | The tuple of base classes of a class object. |
| 1147 | \end{memberdescni} |