blob: f4c0c8af6c979bad8197704f0af8912e285f3c25 [file] [log] [blame]
Fred Drake7a2f0661998-09-10 18:25:58 +00001\section{Built-in Types \label{types}}
Fred Drake64e3b431998-07-24 13:56:11 +00002
3The following sections describe the standard types that are built into
4the interpreter. These are the numeric types, sequence types, and
5several others, including types themselves. There is no explicit
6Boolean type; use integers instead.
7\indexii{built-in}{types}
8\indexii{Boolean}{type}
9
10Some operations are supported by several object types; in particular,
11all objects can be compared, tested for truth value, and converted to
Fred Drake84538cd1998-11-30 21:51:25 +000012a string (with the \code{`\textrm{\ldots}`} notation). The latter
13conversion is implicitly used when an object is written by the
14\keyword{print}\stindex{print} statement.
Fred Drake64e3b431998-07-24 13:56:11 +000015
16
Fred Drake7a2f0661998-09-10 18:25:58 +000017\subsection{Truth Value Testing \label{truth}}
Fred Drake64e3b431998-07-24 13:56:11 +000018
Fred Drake84538cd1998-11-30 21:51:25 +000019Any object can be tested for truth value, for use in an \keyword{if} or
20\keyword{while} condition or as operand of the Boolean operations below.
Fred Drake64e3b431998-07-24 13:56:11 +000021The following values are considered false:
22\stindex{if}
23\stindex{while}
24\indexii{truth}{value}
25\indexii{Boolean}{operations}
26\index{false}
27
Fred Drake64e3b431998-07-24 13:56:11 +000028\begin{itemize}
29
30\item \code{None}
Fred Drake7a2f0661998-09-10 18:25:58 +000031 \withsubitem{(Built-in object)}{\ttindex{None}}
Fred Drake64e3b431998-07-24 13:56:11 +000032
Fred Drake38e5d272000-04-03 20:13:55 +000033\item zero of any numeric type, for example, \code{0}, \code{0L},
34 \code{0.0}, \code{0j}.
Fred Drake64e3b431998-07-24 13:56:11 +000035
Fred Drake38e5d272000-04-03 20:13:55 +000036\item any empty sequence, for example, \code{''}, \code{()}, \code{[]}.
Fred Drake64e3b431998-07-24 13:56:11 +000037
Fred Drake38e5d272000-04-03 20:13:55 +000038\item any empty mapping, for example, \code{\{\}}.
Fred Drake64e3b431998-07-24 13:56:11 +000039
40\item instances of user-defined classes, if the class defines a
Fred Drake7a2f0661998-09-10 18:25:58 +000041 \method{__nonzero__()} or \method{__len__()} method, when that
Fred Drake38e5d272000-04-03 20:13:55 +000042 method returns zero.\footnote{Additional information on these
43special methods may be found in the \emph{Python Reference Manual}.}
Fred Drake64e3b431998-07-24 13:56:11 +000044
45\end{itemize}
46
47All other values are considered true --- so objects of many types are
48always true.
49\index{true}
50
51Operations and built-in functions that have a Boolean result always
52return \code{0} for false and \code{1} for true, unless otherwise
53stated. (Important exception: the Boolean operations
54\samp{or}\opindex{or} and \samp{and}\opindex{and} always return one of
55their operands.)
56
57
Fred Drake7a2f0661998-09-10 18:25:58 +000058\subsection{Boolean Operations \label{boolean}}
Fred Drake64e3b431998-07-24 13:56:11 +000059
60These are the Boolean operations, ordered by ascending priority:
61\indexii{Boolean}{operations}
62
63\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
64 \lineiii{\var{x} or \var{y}}{if \var{x} is false, then \var{y}, else \var{x}}{(1)}
65 \lineiii{\var{x} and \var{y}}{if \var{x} is false, then \var{x}, else \var{y}}{(1)}
66 \hline
67 \lineiii{not \var{x}}{if \var{x} is false, then \code{1}, else \code{0}}{(2)}
68\end{tableiii}
69\opindex{and}
70\opindex{or}
71\opindex{not}
72
73\noindent
74Notes:
75
76\begin{description}
77
78\item[(1)]
79These only evaluate their second argument if needed for their outcome.
80
81\item[(2)]
Fred Drake38e5d272000-04-03 20:13:55 +000082\samp{not} has a lower priority than non-Boolean operators, so
83\code{not \var{a} == \var{b}} is interpreted as \code{not (\var{a} ==
84\var{b})}, and \code{\var{a} == not \var{b}} is a syntax error.
Fred Drake64e3b431998-07-24 13:56:11 +000085
86\end{description}
87
88
Fred Drake7a2f0661998-09-10 18:25:58 +000089\subsection{Comparisons \label{comparisons}}
Fred Drake64e3b431998-07-24 13:56:11 +000090
91Comparison operations are supported by all objects. They all have the
92same priority (which is higher than that of the Boolean operations).
Fred Drake38e5d272000-04-03 20:13:55 +000093Comparisons can be chained arbitrarily; for example, \code{\var{x} <
94\var{y} <= \var{z}} is equivalent to \code{\var{x} < \var{y} and
95\var{y} <= \var{z}}, except that \var{y} is evaluated only once (but
96in both cases \var{z} is not evaluated at all when \code{\var{x} <
97\var{y}} is found to be false).
Fred Drake64e3b431998-07-24 13:56:11 +000098\indexii{chaining}{comparisons}
99
100This table summarizes the comparison operations:
101
102\begin{tableiii}{c|l|c}{code}{Operation}{Meaning}{Notes}
103 \lineiii{<}{strictly less than}{}
104 \lineiii{<=}{less than or equal}{}
105 \lineiii{>}{strictly greater than}{}
106 \lineiii{>=}{greater than or equal}{}
107 \lineiii{==}{equal}{}
108 \lineiii{<>}{not equal}{(1)}
109 \lineiii{!=}{not equal}{(1)}
110 \lineiii{is}{object identity}{}
111 \lineiii{is not}{negated object identity}{}
112\end{tableiii}
113\indexii{operator}{comparison}
114\opindex{==} % XXX *All* others have funny characters < ! >
115\opindex{is}
116\opindex{is not}
117
118\noindent
119Notes:
120
121\begin{description}
122
123\item[(1)]
124\code{<>} and \code{!=} are alternate spellings for the same operator.
125(I couldn't choose between \ABC{} and \C{}! :-)
126\index{ABC language@\ABC{} language}
127\index{language!ABC@\ABC{}}
128\indexii{C@\C{}}{language}
Fred Drake38e5d272000-04-03 20:13:55 +0000129\code{!=} is the preferred spelling; \code{<>} is obsolescent.
Fred Drake64e3b431998-07-24 13:56:11 +0000130
131\end{description}
132
133Objects of different types, except different numeric types, never
134compare equal; such objects are ordered consistently but arbitrarily
135(so that sorting a heterogeneous array yields a consistent result).
Fred Drake38e5d272000-04-03 20:13:55 +0000136Furthermore, some types (for example, file objects) support only a
137degenerate notion of comparison where any two objects of that type are
138unequal. Again, such objects are ordered arbitrarily but
139consistently.
140\indexii{object}{numeric}
Fred Drake64e3b431998-07-24 13:56:11 +0000141\indexii{objects}{comparing}
142
Fred Drake38e5d272000-04-03 20:13:55 +0000143Instances of a class normally compare as non-equal unless the class
144\withsubitem{(instance method)}{\ttindex{__cmp__()}}
145defines the \method{__cmp__()} method. Refer to the \emph{Python
146Reference Manual} for information on the use of this method to effect
147object comparisons.
Fred Drake64e3b431998-07-24 13:56:11 +0000148
Fred Drake38e5d272000-04-03 20:13:55 +0000149\strong{Implementation note:} Objects of different types except
150numbers are ordered by their type names; objects of the same types
151that don't support proper comparison are ordered by their address.
152
153Two more operations with the same syntactic priority,
154\samp{in}\opindex{in} and \samp{not in}\opindex{not in}, are supported
155only by sequence types (below).
Fred Drake64e3b431998-07-24 13:56:11 +0000156
157
Fred Drake7a2f0661998-09-10 18:25:58 +0000158\subsection{Numeric Types \label{typesnumeric}}
Fred Drake64e3b431998-07-24 13:56:11 +0000159
160There are four numeric types: \dfn{plain integers}, \dfn{long integers},
161\dfn{floating point numbers}, and \dfn{complex numbers}.
162Plain integers (also just called \dfn{integers})
Fred Drake38e5d272000-04-03 20:13:55 +0000163are implemented using \ctype{long} in C, which gives them at least 32
Fred Drake64e3b431998-07-24 13:56:11 +0000164bits of precision. Long integers have unlimited precision. Floating
Fred Drake38e5d272000-04-03 20:13:55 +0000165point numbers are implemented using \ctype{double} in C. All bets on
Fred Drake64e3b431998-07-24 13:56:11 +0000166their precision are off unless you happen to know the machine you are
167working with.
168\indexii{numeric}{types}
169\indexii{integer}{types}
170\indexii{integer}{type}
171\indexiii{long}{integer}{type}
172\indexii{floating point}{type}
173\indexii{complex number}{type}
Fred Drake38e5d272000-04-03 20:13:55 +0000174\indexii{C}{language}
Fred Drake64e3b431998-07-24 13:56:11 +0000175
176Complex numbers have a real and imaginary part, which are both
Fred Drake38e5d272000-04-03 20:13:55 +0000177implemented using \ctype{double} in C. To extract these parts from
Fred Drake64e3b431998-07-24 13:56:11 +0000178a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}.
179
180Numbers are created by numeric literals or as the result of built-in
181functions and operators. Unadorned integer literals (including hex
Fred Drake38e5d272000-04-03 20:13:55 +0000182and octal numbers) yield plain integers. Integer literals with an
183\character{L} or \character{l} suffix yield long integers
184(\character{L} is preferred because \samp{1l} looks too much like
185eleven!). Numeric literals containing a decimal point or an exponent
186sign yield floating point numbers. Appending \character{j} or
187\character{J} to a numeric literal yields a complex number.
Fred Drake64e3b431998-07-24 13:56:11 +0000188\indexii{numeric}{literals}
189\indexii{integer}{literals}
190\indexiii{long}{integer}{literals}
191\indexii{floating point}{literals}
192\indexii{complex number}{literals}
193\indexii{hexadecimal}{literals}
194\indexii{octal}{literals}
195
196Python fully supports mixed arithmetic: when a binary arithmetic
197operator has operands of different numeric types, the operand with the
198``smaller'' type is converted to that of the other, where plain
199integer is smaller than long integer is smaller than floating point is
200smaller than complex.
Fred Drakeea003fc1999-04-05 21:59:15 +0000201Comparisons between numbers of mixed type use the same rule.\footnote{
202 As a consequence, the list \code{[1, 2]} is considered equal
Fred Drake82ac24f1999-07-02 14:29:14 +0000203 to \code{[1.0, 2.0]}, and similar for tuples.
204} The functions \function{int()}, \function{long()}, \function{float()},
Fred Drake84538cd1998-11-30 21:51:25 +0000205and \function{complex()} can be used
Fred Drake64e3b431998-07-24 13:56:11 +0000206to coerce numbers to a specific type.
207\index{arithmetic}
208\bifuncindex{int}
209\bifuncindex{long}
210\bifuncindex{float}
211\bifuncindex{complex}
212
213All numeric types support the following operations, sorted by
214ascending priority (operations in the same box have the same
215priority; all numeric operations have a higher priority than
216comparison operations):
217
218\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
219 \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{}
220 \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{}
221 \hline
222 \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{}
223 \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(1)}
224 \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{}
225 \hline
226 \lineiii{-\var{x}}{\var{x} negated}{}
227 \lineiii{+\var{x}}{\var{x} unchanged}{}
228 \hline
229 \lineiii{abs(\var{x})}{absolute value or magnitude of \var{x}}{}
230 \lineiii{int(\var{x})}{\var{x} converted to integer}{(2)}
231 \lineiii{long(\var{x})}{\var{x} converted to long integer}{(2)}
232 \lineiii{float(\var{x})}{\var{x} converted to floating point}{}
233 \lineiii{complex(\var{re},\var{im})}{a complex number with real part \var{re}, imaginary part \var{im}. \var{im} defaults to zero.}{}
Fred Drake26b698f1999-02-12 18:27:31 +0000234 \lineiii{\var{c}.conjugate()}{conjugate of the complex number \var{c}}{}
Fred Drake64e3b431998-07-24 13:56:11 +0000235 \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)}
236 \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{}
237 \lineiii{\var{x} ** \var{y}}{\var{x} to the power \var{y}}{}
238\end{tableiii}
239\indexiii{operations on}{numeric}{types}
Fred Drake26b698f1999-02-12 18:27:31 +0000240\withsubitem{(complex number method)}{\ttindex{conjugate()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000241
242\noindent
243Notes:
244\begin{description}
245
246\item[(1)]
247For (plain or long) integer division, the result is an integer.
248The result is always rounded towards minus infinity: 1/2 is 0,
Fred Drake38e5d272000-04-03 20:13:55 +0000249(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result
250is a long integer if either operand is a long integer, regardless of
251the numeric value.
Fred Drake64e3b431998-07-24 13:56:11 +0000252\indexii{integer}{division}
253\indexiii{long}{integer}{division}
254
255\item[(2)]
256Conversion from floating point to (long or plain) integer may round or
Fred Drake84538cd1998-11-30 21:51:25 +0000257truncate as in \C{}; see functions \function{floor()} and \function{ceil()} in
Fred Drake38e5d272000-04-03 20:13:55 +0000258module \refmodule{math}\refbimodindex{math} for well-defined conversions.
Fred Drake9474d861999-02-12 22:05:33 +0000259\withsubitem{(in module math)}{\ttindex{floor()}\ttindex{ceil()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000260\indexii{numeric}{conversions}
Fred Drake64e3b431998-07-24 13:56:11 +0000261\indexii{C@\C{}}{language}
262
263\item[(3)]
Fred Drake38e5d272000-04-03 20:13:55 +0000264See section \ref{built-in-funcs}, ``Built-in Functions,'' for a full
265description.
Fred Drake64e3b431998-07-24 13:56:11 +0000266
267\end{description}
268% XXXJH exceptions: overflow (when? what operations?) zerodivision
269
Fred Drake4e7c2051999-02-19 15:30:25 +0000270\subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}}
Fred Drake64e3b431998-07-24 13:56:11 +0000271\nodename{Bit-string Operations}
272
273Plain and long integer types support additional operations that make
274sense only for bit-strings. Negative numbers are treated as their 2's
275complement value (for long integers, this assumes a sufficiently large
276number of bits that no overflow occurs during the operation).
277
278The priorities of the binary bit-wise operations are all lower than
279the numeric operations and higher than the comparisons; the unary
280operation \samp{\~} has the same priority as the other unary numeric
281operations (\samp{+} and \samp{-}).
282
283This table lists the bit-string operations sorted in ascending
284priority (operations in the same box have the same priority):
285
286\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
287 \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{}
288 \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{}
289 \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{}
290 \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{(1), (2)}
291 \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{(1), (3)}
292 \hline
293 \lineiii{\~\var{x}}{the bits of \var{x} inverted}{}
294\end{tableiii}
295\indexiii{operations on}{integer}{types}
296\indexii{bit-string}{operations}
297\indexii{shifting}{operations}
298\indexii{masking}{operations}
299
300\noindent
301Notes:
302\begin{description}
303\item[(1)] Negative shift counts are illegal and cause a
304\exception{ValueError} to be raised.
305\item[(2)] A left shift by \var{n} bits is equivalent to
306multiplication by \code{pow(2, \var{n})} without overflow check.
307\item[(3)] A right shift by \var{n} bits is equivalent to
308division by \code{pow(2, \var{n})} without overflow check.
309\end{description}
310
311
Fred Drake7a2f0661998-09-10 18:25:58 +0000312\subsection{Sequence Types \label{typesseq}}
Fred Drake64e3b431998-07-24 13:56:11 +0000313
314There are three sequence types: strings, lists and tuples.
315
316Strings literals are written in single or double quotes:
Fred Drake38e5d272000-04-03 20:13:55 +0000317\code{'xyzzy'}, \code{"frobozz"}. See chapter 2 of the
Fred Drake37f15741999-11-10 16:21:37 +0000318\citetitle[../ref/ref.html]{Python Reference Manual} for more about
319string literals. Lists are constructed with square brackets,
320separating items with commas: \code{[a, b, c]}. Tuples are
321constructed by the comma operator (not within square brackets), with
322or without enclosing parentheses, but an empty tuple must have the
323enclosing parentheses, e.g., \code{a, b, c} or \code{()}. A single
324item tuple must have a trailing comma, e.g., \code{(d,)}.
Fred Drake64e3b431998-07-24 13:56:11 +0000325\indexii{sequence}{types}
326\indexii{string}{type}
327\indexii{tuple}{type}
328\indexii{list}{type}
329
330Sequence types support the following operations. The \samp{in} and
331\samp{not in} operations have the same priorities as the comparison
332operations. The \samp{+} and \samp{*} operations have the same
333priority as the corresponding numeric operations.\footnote{They must
334have since the parser can't tell the type of the operands.}
335
336This table lists the sequence operations sorted in ascending priority
337(operations in the same box have the same priority). In the table,
338\var{s} and \var{t} are sequences of the same type; \var{n}, \var{i}
339and \var{j} are integers:
340
341\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
342 \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{}
343 \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is
344equal to \var{x}, else \code{1}}{}
345 \hline
346 \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
Fred Drake38e5d272000-04-03 20:13:55 +0000347 \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} copies of \var{s} concatenated}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000348 \hline
Fred Drake38e5d272000-04-03 20:13:55 +0000349 \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(2)}
350 \lineiii{\var{s}[\var{i}:\var{j}]}{slice of \var{s} from \var{i} to \var{j}}{(2), (3)}
Fred Drake64e3b431998-07-24 13:56:11 +0000351 \hline
352 \lineiii{len(\var{s})}{length of \var{s}}{}
353 \lineiii{min(\var{s})}{smallest item of \var{s}}{}
354 \lineiii{max(\var{s})}{largest item of \var{s}}{}
355\end{tableiii}
356\indexiii{operations on}{sequence}{types}
357\bifuncindex{len}
358\bifuncindex{min}
359\bifuncindex{max}
360\indexii{concatenation}{operation}
361\indexii{repetition}{operation}
362\indexii{subscript}{operation}
363\indexii{slice}{operation}
364\opindex{in}
365\opindex{not in}
366
367\noindent
368Notes:
369
370\begin{description}
Fred Drake38e5d272000-04-03 20:13:55 +0000371\item[(1)] Values of \var{n} less than \code{0} are treated as
372 \code{0} (which yields an empty sequence of the same type as
373 \var{s}).
374
375\item[(2)] If \var{i} or \var{j} is negative, the index is relative to
Fred Drake64e3b431998-07-24 13:56:11 +0000376 the end of the string, i.e., \code{len(\var{s}) + \var{i}} or
377 \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is
378 still \code{0}.
379
Fred Drake38e5d272000-04-03 20:13:55 +0000380\item[(3)] The slice of \var{s} from \var{i} to \var{j} is defined as
Fred Drake64e3b431998-07-24 13:56:11 +0000381 the sequence of items with index \var{k} such that \code{\var{i} <=
382 \var{k} < \var{j}}. If \var{i} or \var{j} is greater than
383 \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted,
384 use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If
385 \var{i} is greater than or equal to \var{j}, the slice is empty.
Fred Drake64e3b431998-07-24 13:56:11 +0000386\end{description}
387
Fred Drake9474d861999-02-12 22:05:33 +0000388
389\subsubsection{More String Operations \label{typesseq-strings}}
Fred Drake64e3b431998-07-24 13:56:11 +0000390
391String objects have one unique built-in operation: the \code{\%}
392operator (modulo) with a string left argument interprets this string
393as a \C{} \cfunction{sprintf()} format string to be applied to the
394right argument, and returns the string resulting from this formatting
395operation.
396
397The right argument should be a tuple with one item for each argument
398required by the format string; if the string requires a single
Fred Drakeea003fc1999-04-05 21:59:15 +0000399argument, the right argument may also be a single non-tuple
400object.\footnote{A tuple object in this case should be a singleton.}
Fred Drake64e3b431998-07-24 13:56:11 +0000401The following format characters are understood:
402\code{\%}, \code{c}, \code{s}, \code{i}, \code{d}, \code{u}, \code{o},
403\code{x}, \code{X}, \code{e}, \code{E}, \code{f}, \code{g}, \code{G}.
404Width and precision may be a \code{*} to specify that an integer argument
405specifies the actual width or precision. The flag characters
Fred Drake6d20caa1999-04-21 18:17:11 +0000406\code{-}, \code{+}, blank, \code{\#} and \code{0} are understood. The
407size specifiers \code{h}, \code{l} or \code{L} may be present but are
408ignored. The \code{\%s} conversion takes any Python object and
409converts it to a string using \code{str()} before formatting it. The
410ANSI features \code{\%p} and \code{\%n} are not supported. Since
411Python strings have an explicit length, \code{\%s} conversions don't
412assume that \code{'\e0'} is the end of the string.
Fred Drake64e3b431998-07-24 13:56:11 +0000413
414For safety reasons, floating point precisions are clipped to 50;
415\code{\%f} conversions for numbers whose absolute value is over 1e25
Fred Drakeea003fc1999-04-05 21:59:15 +0000416are replaced by \code{\%g} conversions.\footnote{
417 These numbers are fairly arbitrary. They are intended to
418 avoid printing endless strings of meaningless digits without hampering
419 correct use and without having to know the exact precision of floating
420 point values on a particular machine.}
Fred Drake64e3b431998-07-24 13:56:11 +0000421All other errors raise exceptions.
422
423If the right argument is a dictionary (or any kind of mapping), then
424the formats in the string must have a parenthesized key into that
425dictionary inserted immediately after the \character{\%} character,
426and each format formats the corresponding entry from the mapping.
427For example:
428
429\begin{verbatim}
430>>> count = 2
431>>> language = 'Python'
432>>> print '%(language)s has %(count)03d quote types.' % vars()
433Python has 002 quote types.
434\end{verbatim}
435
436In this case no \code{*} specifiers may occur in a format (since they
437require a sequential parameter list).
438
439Additional string operations are defined in standard module
440\module{string} and in built-in module \module{re}.
441\refstmodindex{string}
Fred Drake66da9d61998-08-07 18:57:18 +0000442\refstmodindex{re}
Fred Drake64e3b431998-07-24 13:56:11 +0000443
Fred Drake9474d861999-02-12 22:05:33 +0000444\subsubsection{Mutable Sequence Types \label{typesseq-mutable}}
Fred Drake64e3b431998-07-24 13:56:11 +0000445
446List objects support additional operations that allow in-place
447modification of the object.
448These operations would be supported by other mutable sequence types
449(when added to the language) as well.
450Strings and tuples are immutable sequence types and such objects cannot
451be modified once created.
452The following operations are defined on mutable sequence types (where
453\var{x} is an arbitrary object):
454\indexiii{mutable}{sequence}{types}
455\indexii{list}{type}
456
457\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
458 \lineiii{\var{s}[\var{i}] = \var{x}}
459 {item \var{i} of \var{s} is replaced by \var{x}}{}
460 \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}}
461 {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{}
462 \lineiii{del \var{s}[\var{i}:\var{j}]}
463 {same as \code{\var{s}[\var{i}:\var{j}] = []}}{}
464 \lineiii{\var{s}.append(\var{x})}
Fred Drake38e5d272000-04-03 20:13:55 +0000465 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{(1)}
Barry Warsawafd974c1998-10-09 16:39:58 +0000466 \lineiii{\var{s}.extend(\var{x})}
Fred Drake38e5d272000-04-03 20:13:55 +0000467 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = \var{x}}}{(2)}
Fred Drake64e3b431998-07-24 13:56:11 +0000468 \lineiii{\var{s}.count(\var{x})}
469 {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{}
470 \lineiii{\var{s}.index(\var{x})}
Fred Drake38e5d272000-04-03 20:13:55 +0000471 {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(3)}
Fred Drake64e3b431998-07-24 13:56:11 +0000472 \lineiii{\var{s}.insert(\var{i}, \var{x})}
473 {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}
474 if \code{\var{i} >= 0}}{}
475 \lineiii{\var{s}.pop(\optional{\var{i}})}
476 {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(4)}
477 \lineiii{\var{s}.remove(\var{x})}
Fred Drake38e5d272000-04-03 20:13:55 +0000478 {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(3)}
Fred Drake64e3b431998-07-24 13:56:11 +0000479 \lineiii{\var{s}.reverse()}
Fred Drake38e5d272000-04-03 20:13:55 +0000480 {reverses the items of \var{s} in place}{(5)}
Fred Drake64e3b431998-07-24 13:56:11 +0000481 \lineiii{\var{s}.sort(\optional{\var{cmpfunc}})}
Fred Drake38e5d272000-04-03 20:13:55 +0000482 {sort the items of \var{s} in place}{(5), (6)}
Fred Drake64e3b431998-07-24 13:56:11 +0000483\end{tableiii}
484\indexiv{operations on}{mutable}{sequence}{types}
485\indexiii{operations on}{sequence}{types}
486\indexiii{operations on}{list}{type}
487\indexii{subscript}{assignment}
488\indexii{slice}{assignment}
489\stindex{del}
Fred Drake9474d861999-02-12 22:05:33 +0000490\withsubitem{(list method)}{
Fred Drake68921df1999-08-09 17:05:12 +0000491 \ttindex{append()}\ttindex{extend()}\ttindex{count()}\ttindex{index()}
492 \ttindex{insert()}\ttindex{pop()}\ttindex{remove()}\ttindex{reverse()}
Fred Drakee8391991998-11-25 17:09:19 +0000493 \ttindex{sort()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000494\noindent
495Notes:
496\begin{description}
Fred Drake38e5d272000-04-03 20:13:55 +0000497\item[(1)] The C implementation of Python has historically accepted
498 multiple parameters and implicitly joined them into a tuple; this
499 will no longer work in Python 1.6. Use of this misfeature has been
500 deprecated since Python 1.4.
501
502\item[(2)] Raises an exception when \var{x} is not a list object. The
503 \method{extend()} method is experimental and not supported by
504 mutable sequence types other than lists.
505
506\item[(3)] Raises \exception{ValueError} when \var{x} is not found in
Fred Drake68921df1999-08-09 17:05:12 +0000507 \var{s}.
508
Fred Drake38e5d272000-04-03 20:13:55 +0000509\item[(4)] The \method{pop()} method is experimental and not supported
510 by other mutable sequence types than lists. The optional argument
511 \var{i} defaults to \code{-1}, so that by default the last item is
512 removed and returned.
513
514\item[(5)] The \method{sort()} and \method{reverse()} methods modify the
515 list in place for economy of space when sorting or reversing a large
516 list. They don't return the sorted or reversed list to remind you
517 of this side effect.
518
519\item[(6)] The \method{sort()} method takes an optional argument
Fred Drake64e3b431998-07-24 13:56:11 +0000520 specifying a comparison function of two arguments (list items) which
Fred Drake68921df1999-08-09 17:05:12 +0000521 should return \code{-1}, \code{0} or \code{1} depending on whether
522 the first argument is considered smaller than, equal to, or larger
523 than the second argument. Note that this slows the sorting process
524 down considerably; e.g. to sort a list in reverse order it is much
525 faster to use calls to the methods \method{sort()} and
526 \method{reverse()} than to use the built-in function
527 \function{sort()} with a comparison function that reverses the
528 ordering of the elements.
Fred Drake64e3b431998-07-24 13:56:11 +0000529\end{description}
530
531
Fred Drake7a2f0661998-09-10 18:25:58 +0000532\subsection{Mapping Types \label{typesmapping}}
Fred Drake38e5d272000-04-03 20:13:55 +0000533\indexii{mapping}{types}
534\indexii{dictionary}{type}
Fred Drake64e3b431998-07-24 13:56:11 +0000535
536A \dfn{mapping} object maps values of one type (the key type) to
537arbitrary objects. Mappings are mutable objects. There is currently
538only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are
539almost arbitrary values. The only types of values not acceptable as
540keys are values containing lists or dictionaries or other mutable
541types that are compared by value rather than by object identity.
542Numeric types used for keys obey the normal rules for numeric
543comparison: if two numbers compare equal (e.g. \code{1} and
544\code{1.0}) then they can be used interchangeably to index the same
545dictionary entry.
546
Fred Drake64e3b431998-07-24 13:56:11 +0000547Dictionaries are created by placing a comma-separated list of
548\code{\var{key}: \var{value}} pairs within braces, for example:
549\code{\{'jack': 4098, 'sjoerd': 4127\}} or
550\code{\{4098: 'jack', 4127: 'sjoerd'\}}.
551
Fred Drake9c5cc141999-06-10 22:37:34 +0000552The following operations are defined on mappings (where \var{a} and
553\var{b} are mappings, \var{k} is a key, and \var{v} and \var{x} are
554arbitrary objects):
Fred Drake64e3b431998-07-24 13:56:11 +0000555\indexiii{operations on}{mapping}{types}
556\indexiii{operations on}{dictionary}{type}
557\stindex{del}
558\bifuncindex{len}
Fred Drake9474d861999-02-12 22:05:33 +0000559\withsubitem{(dictionary method)}{
560 \ttindex{clear()}
561 \ttindex{copy()}
562 \ttindex{has_key()}
563 \ttindex{items()}
564 \ttindex{keys()}
565 \ttindex{update()}
566 \ttindex{values()}
Fred Drakee8391991998-11-25 17:09:19 +0000567 \ttindex{get()}}
Fred Drake9c5cc141999-06-10 22:37:34 +0000568
569\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
570 \lineiii{len(\var{a})}{the number of items in \var{a}}{}
571 \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
572 \lineiii{\var{a}[\var{k}] = \var{x}}
573 {set \code{\var{a}[\var{k}]} to \var{x}}
574 {}
575 \lineiii{del \var{a}[\var{k}]}
576 {remove \code{\var{a}[\var{k}]} from \var{a}}
577 {(1)}
578 \lineiii{\var{a}.clear()}{remove all items from \code{a}}{}
579 \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{}
580 \lineiii{\var{a}.has_key(\var{k})}
581 {\code{1} if \var{a} has a key \var{k}, else \code{0}}
582 {}
583 \lineiii{\var{a}.items()}
584 {a copy of \var{a}'s list of (\var{key}, \var{value}) pairs}
585 {(2)}
586 \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(2)}
587 \lineiii{\var{a}.update(\var{b})}
588 {\code{for k, v in \var{b}.items(): \var{a}[k] = v}}
589 {(3)}
590 \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(2)}
591 \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})}
592 {\code{\var{a}[\var{k}]} if \code{\var{a}.has_key(\var{k})},
593 else \var{x}}
594 {(4)}
595\end{tableiii}
596
Fred Drake64e3b431998-07-24 13:56:11 +0000597\noindent
598Notes:
599\begin{description}
Fred Drake9c5cc141999-06-10 22:37:34 +0000600\item[(1)] Raises a \exception{KeyError} exception if \var{k} is not
601in the map.
Fred Drake64e3b431998-07-24 13:56:11 +0000602
Fred Drake38e5d272000-04-03 20:13:55 +0000603\item[(2)] Keys and values are listed in random order. If
604\method{keys()} and \method{values()} are called with no intervening
605modifications to the dictionary, the two lists will directly
606correspond. This allows the creation of \code{(\var{value},
607\var{key})} pairs using \function{map()}: \samp{pairs = map(None,
608\var{a}.values(), \var{a}.keys())}.
Fred Drake64e3b431998-07-24 13:56:11 +0000609
610\item[(3)] \var{b} must be of the same type as \var{a}.
611
612\item[(4)] Never raises an exception if \var{k} is not in the map,
Fred Drake38e5d272000-04-03 20:13:55 +0000613instead it returns \var{x}. \var{x} is optional; when \var{x} is not
Fred Drake9c5cc141999-06-10 22:37:34 +0000614provided and \var{k} is not in the map, \code{None} is returned.
Fred Drake64e3b431998-07-24 13:56:11 +0000615\end{description}
616
617
Fred Drake7a2f0661998-09-10 18:25:58 +0000618\subsection{Other Built-in Types \label{typesother}}
Fred Drake64e3b431998-07-24 13:56:11 +0000619
620The interpreter supports several other kinds of objects.
621Most of these support only one or two operations.
622
Fred Drake4e7c2051999-02-19 15:30:25 +0000623
Fred Drake9474d861999-02-12 22:05:33 +0000624\subsubsection{Modules \label{typesmodules}}
Fred Drake64e3b431998-07-24 13:56:11 +0000625
626The only special operation on a module is attribute access:
627\code{\var{m}.\var{name}}, where \var{m} is a module and \var{name}
628accesses a name defined in \var{m}'s symbol table. Module attributes
Fred Drake84538cd1998-11-30 21:51:25 +0000629can be assigned to. (Note that the \keyword{import} statement is not,
Fred Draked0421dd1998-08-24 17:57:20 +0000630strictly speaking, an operation on a module object; \code{import
Fred Drake64e3b431998-07-24 13:56:11 +0000631\var{foo}} does not require a module object named \var{foo} to exist,
632rather it requires an (external) \emph{definition} for a module named
633\var{foo} somewhere.)
634
Fred Drake84538cd1998-11-30 21:51:25 +0000635A special member of every module is \member{__dict__}.
Fred Drake64e3b431998-07-24 13:56:11 +0000636This is the dictionary containing the module's symbol table.
637Modifying this dictionary will actually change the module's symbol
Fred Drake84538cd1998-11-30 21:51:25 +0000638table, but direct assignment to the \member{__dict__} attribute is not
Fred Drake64e3b431998-07-24 13:56:11 +0000639possible (i.e., you can write \code{\var{m}.__dict__['a'] = 1}, which
640defines \code{\var{m}.a} to be \code{1}, but you can't write
641\code{\var{m}.__dict__ = \{\}}.
642
Fred Drake4e7c2051999-02-19 15:30:25 +0000643Modules built into the interpreter are written like this:
644\code{<module 'sys' (built-in)>}. If loaded from a file, they are
645written as \code{<module 'os' from '/usr/local/lib/python1.5/os.pyc'>}.
646
Fred Drake64e3b431998-07-24 13:56:11 +0000647
Fred Drake9474d861999-02-12 22:05:33 +0000648\subsubsection{Classes and Class Instances \label{typesobjects}}
Fred Drake64e3b431998-07-24 13:56:11 +0000649\nodename{Classes and Instances}
650
Fred Drake38e5d272000-04-03 20:13:55 +0000651See chapters 3 and 7 of the \citetitle[../ref/ref.html]{Python
Fred Drake37f15741999-11-10 16:21:37 +0000652Reference Manual} for these.
Fred Drake64e3b431998-07-24 13:56:11 +0000653
Fred Drake4e7c2051999-02-19 15:30:25 +0000654
Fred Drake9474d861999-02-12 22:05:33 +0000655\subsubsection{Functions \label{typesfunctions}}
Fred Drake64e3b431998-07-24 13:56:11 +0000656
657Function objects are created by function definitions. The only
658operation on a function object is to call it:
659\code{\var{func}(\var{argument-list})}.
660
661There are really two flavors of function objects: built-in functions
662and user-defined functions. Both support the same operation (to call
663the function), but the implementation is different, hence the
664different object types.
665
666The implementation adds two special read-only attributes:
667\code{\var{f}.func_code} is a function's \dfn{code
668object}\obindex{code} (see below) and \code{\var{f}.func_globals} is
669the dictionary used as the function's global name space (this is the
670same as \code{\var{m}.__dict__} where \var{m} is the module in which
671the function \var{f} was defined).
672
673
Fred Drake9474d861999-02-12 22:05:33 +0000674\subsubsection{Methods \label{typesmethods}}
Fred Drake64e3b431998-07-24 13:56:11 +0000675\obindex{method}
676
677Methods are functions that are called using the attribute notation.
Fred Drake84538cd1998-11-30 21:51:25 +0000678There are two flavors: built-in methods (such as \method{append()} on
Fred Drake64e3b431998-07-24 13:56:11 +0000679lists) and class instance methods. Built-in methods are described
680with the types that support them.
681
682The implementation adds two special read-only attributes to class
Fred Draked0421dd1998-08-24 17:57:20 +0000683instance methods: \code{\var{m}.im_self} is the object on which the
684method operates, and \code{\var{m}.im_func} is the function
685implementing the method. Calling \code{\var{m}(\var{arg-1},
Fred Drake84538cd1998-11-30 21:51:25 +0000686\var{arg-2}, \textrm{\ldots}, \var{arg-n})} is completely equivalent to
Fred Draked0421dd1998-08-24 17:57:20 +0000687calling \code{\var{m}.im_func(\var{m}.im_self, \var{arg-1},
Fred Drake84538cd1998-11-30 21:51:25 +0000688\var{arg-2}, \textrm{\ldots}, \var{arg-n})}.
Fred Drake64e3b431998-07-24 13:56:11 +0000689
Fred Drake37f15741999-11-10 16:21:37 +0000690See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
691information.
Fred Drake64e3b431998-07-24 13:56:11 +0000692
Fred Drake7a2f0661998-09-10 18:25:58 +0000693
694\subsubsection{Code Objects \label{bltin-code-objects}}
Fred Drake64e3b431998-07-24 13:56:11 +0000695\obindex{code}
696
697Code objects are used by the implementation to represent
698``pseudo-compiled'' executable Python code such as a function body.
699They differ from function objects because they don't contain a
700reference to their global execution environment. Code objects are
Fred Drake84538cd1998-11-30 21:51:25 +0000701returned by the built-in \function{compile()} function and can be
702extracted from function objects through their \member{func_code}
Fred Drake64e3b431998-07-24 13:56:11 +0000703attribute.
704\bifuncindex{compile}
Fred Drakee8391991998-11-25 17:09:19 +0000705\withsubitem{(function object attribute)}{\ttindex{func_code}}
Fred Drake64e3b431998-07-24 13:56:11 +0000706
707A code object can be executed or evaluated by passing it (instead of a
Fred Drake84538cd1998-11-30 21:51:25 +0000708source string) to the \keyword{exec} statement or the built-in
709\function{eval()} function.
Fred Drake64e3b431998-07-24 13:56:11 +0000710\stindex{exec}
711\bifuncindex{eval}
712
Fred Drake37f15741999-11-10 16:21:37 +0000713See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
714information.
Fred Drake64e3b431998-07-24 13:56:11 +0000715
Fred Drake7a2f0661998-09-10 18:25:58 +0000716
717\subsubsection{Type Objects \label{bltin-type-objects}}
Fred Drake64e3b431998-07-24 13:56:11 +0000718
719Type objects represent the various object types. An object's type is
Fred Drake84538cd1998-11-30 21:51:25 +0000720accessed by the built-in function \function{type()}. There are no special
721operations on types. The standard module \module{types} defines names
Fred Drake64e3b431998-07-24 13:56:11 +0000722for all standard built-in types.
723\bifuncindex{type}
724\refstmodindex{types}
725
726Types are written like this: \code{<type 'int'>}.
727
Fred Drake7a2f0661998-09-10 18:25:58 +0000728
729\subsubsection{The Null Object \label{bltin-null-object}}
Fred Drake64e3b431998-07-24 13:56:11 +0000730
731This object is returned by functions that don't explicitly return a
732value. It supports no special operations. There is exactly one null
733object, named \code{None} (a built-in name).
734
735It is written as \code{None}.
736
Fred Drake7a2f0661998-09-10 18:25:58 +0000737
738\subsubsection{The Ellipsis Object \label{bltin-ellipsis-object}}
Guido van Rossumb193c951998-07-24 15:02:02 +0000739
Fred Drake37f15741999-11-10 16:21:37 +0000740This object is used by extended slice notation (see the
741\citetitle[../ref/ref.html]{Python Reference Manual}). It supports no
742special operations. There is exactly one ellipsis object, named
743\constant{Ellipsis} (a built-in name).
Guido van Rossumb193c951998-07-24 15:02:02 +0000744
745It is written as \code{Ellipsis}.
746
Fred Drakec3fcd6f1999-04-21 13:58:17 +0000747\subsubsection{File Objects\obindex{file}
748 \label{bltin-file-objects}}
Fred Drake64e3b431998-07-24 13:56:11 +0000749
Fred Drakec3fcd6f1999-04-21 13:58:17 +0000750File objects are implemented using \C{}'s \code{stdio}
751package and can be created with the built-in function
Fred Drake38e5d272000-04-03 20:13:55 +0000752\function{open()}\bifuncindex{open} described in section
Fred Drake130072d1998-10-28 20:08:35 +0000753\ref{built-in-funcs}, ``Built-in Functions.'' They are also returned
754by some other built-in functions and methods, e.g.,
755\function{posix.popen()} and \function{posix.fdopen()} and the
756\method{makefile()} method of socket objects.
Fred Drake64e3b431998-07-24 13:56:11 +0000757\refbimodindex{posix}
758\refbimodindex{socket}
759
760When a file operation fails for an I/O-related reason, the exception
Fred Drake84538cd1998-11-30 21:51:25 +0000761\exception{IOError} is raised. This includes situations where the
762operation is not defined for some reason, like \method{seek()} on a tty
Fred Drake64e3b431998-07-24 13:56:11 +0000763device or writing a file opened for reading.
764
765Files have the following methods:
766
767
768\begin{methoddesc}[file]{close}{}
769 Close the file. A closed file cannot be read or written anymore.
770\end{methoddesc}
771
772\begin{methoddesc}[file]{flush}{}
Fred Drake84538cd1998-11-30 21:51:25 +0000773 Flush the internal buffer, like \code{stdio}'s \cfunction{fflush()}.
Fred Drake64e3b431998-07-24 13:56:11 +0000774\end{methoddesc}
775
776\begin{methoddesc}[file]{isatty}{}
777 Return \code{1} if the file is connected to a tty(-like) device, else
778 \code{0}.
779\end{methoddesc}
780
781\begin{methoddesc}[file]{fileno}{}
782Return the integer ``file descriptor'' that is used by the underlying
783implementation to request I/O operations from the operating system.
784This can be useful for other, lower level interfaces that use file
Fred Drake84538cd1998-11-30 21:51:25 +0000785descriptors, e.g. module \module{fcntl} or \function{os.read()} and friends.
Fred Drake64e3b431998-07-24 13:56:11 +0000786\refbimodindex{fcntl}
787\end{methoddesc}
788
789\begin{methoddesc}[file]{read}{\optional{size}}
790 Read at most \var{size} bytes from the file (less if the read hits
Fred Drakef4cbada1999-04-14 14:31:53 +0000791 \EOF{} before obtaining \var{size} bytes). If the \var{size}
792 argument is negative or omitted, read all data until \EOF{} is
793 reached. The bytes are returned as a string object. An empty
794 string is returned when \EOF{} is encountered immediately. (For
795 certain files, like ttys, it makes sense to continue reading after
796 an \EOF{} is hit.) Note that this method may call the underlying
797 C function \cfunction{fread()} more than once in an effort to
798 acquire as close to \var{size} bytes as possible.
Fred Drake64e3b431998-07-24 13:56:11 +0000799\end{methoddesc}
800
801\begin{methoddesc}[file]{readline}{\optional{size}}
802 Read one entire line from the file. A trailing newline character is
Fred Drakeea003fc1999-04-05 21:59:15 +0000803 kept in the string\footnote{
804 The advantage of leaving the newline on is that an empty string
Fred Drake64e3b431998-07-24 13:56:11 +0000805 can be returned to mean \EOF{} without being ambiguous. Another
806 advantage is that (in cases where it might matter, e.g. if you
807 want to make an exact copy of a file while scanning its lines)
808 you can tell whether the last line of a file ended in a newline
809 or not (yes this happens!).}
810 (but may be absent when a file ends with an
811 incomplete line). If the \var{size} argument is present and
812 non-negative, it is a maximum byte count (including the trailing
813 newline) and an incomplete line may be returned.
814 An empty string is returned when \EOF{} is hit
Fred Drake38e5d272000-04-03 20:13:55 +0000815 immediately. Note: Unlike \code{stdio}'s \cfunction{fgets()}, the returned
Fred Drake64e3b431998-07-24 13:56:11 +0000816 string contains null characters (\code{'\e 0'}) if they occurred in the
817 input.
818\end{methoddesc}
819
820\begin{methoddesc}[file]{readlines}{\optional{sizehint}}
821 Read until \EOF{} using \method{readline()} and return a list containing
822 the lines thus read. If the optional \var{sizehint} argument is
823 present, instead of reading up to \EOF{}, whole lines totalling
824 approximately \var{sizehint} bytes (possibly after rounding up to an
825 internal buffer size) are read.
826\end{methoddesc}
827
828\begin{methoddesc}[file]{seek}{offset\optional{, whence}}
829 Set the file's current position, like \code{stdio}'s \cfunction{fseek()}.
830 The \var{whence} argument is optional and defaults to \code{0}
831 (absolute file positioning); other values are \code{1} (seek
832 relative to the current position) and \code{2} (seek relative to the
833 file's end). There is no return value.
834\end{methoddesc}
835
836\begin{methoddesc}[file]{tell}{}
837 Return the file's current position, like \code{stdio}'s
838 \cfunction{ftell()}.
839\end{methoddesc}
840
841\begin{methoddesc}[file]{truncate}{\optional{size}}
842Truncate the file's size. If the optional size argument present, the
843file is truncated to (at most) that size. The size defaults to the
844current position. Availability of this function depends on the
Fred Drake38e5d272000-04-03 20:13:55 +0000845operating system version (for example, not all \UNIX{} versions support this
Fred Drake64e3b431998-07-24 13:56:11 +0000846operation).
847\end{methoddesc}
848
849\begin{methoddesc}[file]{write}{str}
Fred Drake38e5d272000-04-03 20:13:55 +0000850Write a string to the file. There is no return value. Note: Due to
Fred Drake64e3b431998-07-24 13:56:11 +0000851buffering, the string may not actually show up in the file until
852the \method{flush()} or \method{close()} method is called.
853\end{methoddesc}
854
855\begin{methoddesc}[file]{writelines}{list}
856Write a list of strings to the file. There is no return value.
857(The name is intended to match \method{readlines()};
858\method{writelines()} does not add line separators.)
859\end{methoddesc}
860
861
862File objects also offer the following attributes:
863
864\begin{memberdesc}[file]{closed}
865Boolean indicating the current state of the file object. This is a
866read-only attribute; the \method{close()} method changes the value.
867\end{memberdesc}
868
869\begin{memberdesc}[file]{mode}
870The I/O mode for the file. If the file was created using the
871\function{open()} built-in function, this will be the value of the
872\var{mode} parameter. This is a read-only attribute.
873\end{memberdesc}
874
875\begin{memberdesc}[file]{name}
876If the file object was created using \function{open()}, the name of
877the file. Otherwise, some string that indicates the source of the
878file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only
879attribute.
880\end{memberdesc}
881
882\begin{memberdesc}[file]{softspace}
883Boolean that indicates whether a space character needs to be printed
884before another value when using the \keyword{print} statement.
885Classes that are trying to simulate a file object should also have a
886writable \member{softspace} attribute, which should be initialized to
887zero. This will be automatic for classes implemented in Python; types
888implemented in \C{} will have to provide a writable \member{softspace}
889attribute.
890\end{memberdesc}
891
Fred Drake9474d861999-02-12 22:05:33 +0000892\subsubsection{Internal Objects \label{typesinternal}}
Fred Drake64e3b431998-07-24 13:56:11 +0000893
Fred Drake37f15741999-11-10 16:21:37 +0000894See the \citetitle[../ref/ref.html]{Python Reference Manual} for this
895information. It describes code objects, stack frame objects,
896traceback objects, and slice objects.
Fred Drake64e3b431998-07-24 13:56:11 +0000897
898
Fred Drake7a2f0661998-09-10 18:25:58 +0000899\subsection{Special Attributes \label{specialattrs}}
Fred Drake64e3b431998-07-24 13:56:11 +0000900
901The implementation adds a few special read-only attributes to several
902object types, where they are relevant:
903
Fred Drake7a2f0661998-09-10 18:25:58 +0000904\begin{memberdescni}{__dict__}
905A dictionary of some sort used to store an
906object's (writable) attributes.
907\end{memberdescni}
Fred Drake64e3b431998-07-24 13:56:11 +0000908
Fred Drake7a2f0661998-09-10 18:25:58 +0000909\begin{memberdescni}{__methods__}
910List of the methods of many built-in object types,
Fred Drake64e3b431998-07-24 13:56:11 +0000911e.g., \code{[].__methods__} yields
Fred Drake7a2f0661998-09-10 18:25:58 +0000912\code{['append', 'count', 'index', 'insert', 'pop', 'remove',
913'reverse', 'sort']}.
914\end{memberdescni}
Fred Drake64e3b431998-07-24 13:56:11 +0000915
Fred Drake7a2f0661998-09-10 18:25:58 +0000916\begin{memberdescni}{__members__}
917Similar to \member{__methods__}, but lists data attributes.
918\end{memberdescni}
Fred Drake64e3b431998-07-24 13:56:11 +0000919
Fred Drake7a2f0661998-09-10 18:25:58 +0000920\begin{memberdescni}{__class__}
921The class to which a class instance belongs.
922\end{memberdescni}
Fred Drake64e3b431998-07-24 13:56:11 +0000923
Fred Drake7a2f0661998-09-10 18:25:58 +0000924\begin{memberdescni}{__bases__}
925The tuple of base classes of a class object.
926\end{memberdescni}