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