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