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