blob: 4eb45958597ee2e87619ad37312d1d63e70910df [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
Steve Holden1e4519f2002-06-14 09:16:40 +00004the interpreter. Historically, Python's built-in types have differed
5from user-defined types because it was not possible to use the built-in
6types as the basis for object-oriented inheritance. With the 2.2
7release this situation has started to change, although the intended
8unification of user-defined and built-in types is as yet far from
9complete.
10
11The principal built-in types are numerics, sequences, mappings, files
12classes, instances and exceptions.
Fred Drake64e3b431998-07-24 13:56:11 +000013\indexii{built-in}{types}
Fred Drake64e3b431998-07-24 13:56:11 +000014
15Some operations are supported by several object types; in particular,
16all objects can be compared, tested for truth value, and converted to
Fred Drake84538cd1998-11-30 21:51:25 +000017a string (with the \code{`\textrm{\ldots}`} notation). The latter
18conversion is implicitly used when an object is written by the
19\keyword{print}\stindex{print} statement.
Fred Drake90fc0b32003-04-30 16:44:36 +000020(Information on \ulink{\keyword{print} statement}{../ref/print.html}
21and 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 Drake64e3b431998-07-24 13:56:11 +000024
25
Fred Drake90fc0b32003-04-30 16:44:36 +000026\subsection{Truth Value Testing\label{truth}}
Fred Drake64e3b431998-07-24 13:56:11 +000027
Fred Drake84538cd1998-11-30 21:51:25 +000028Any 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 Drake64e3b431998-07-24 13:56:11 +000030The following values are considered false:
31\stindex{if}
32\stindex{while}
33\indexii{truth}{value}
34\indexii{Boolean}{operations}
35\index{false}
36
Fred Drake64e3b431998-07-24 13:56:11 +000037\begin{itemize}
38
39\item \code{None}
Fred Drake442c7c72002-08-07 15:40:15 +000040 \withsubitem{(Built-in object)}{\ttindex{None}}
Fred Drake64e3b431998-07-24 13:56:11 +000041
Guido van Rossum77f6a652002-04-03 22:41:51 +000042\item \code{False}
Fred Drake442c7c72002-08-07 15:40:15 +000043 \withsubitem{(Built-in object)}{\ttindex{False}}
Guido van Rossum77f6a652002-04-03 22:41:51 +000044
Fred Drake38e5d272000-04-03 20:13:55 +000045\item zero of any numeric type, for example, \code{0}, \code{0L},
46 \code{0.0}, \code{0j}.
Fred Drake64e3b431998-07-24 13:56:11 +000047
Fred Drake38e5d272000-04-03 20:13:55 +000048\item any empty sequence, for example, \code{''}, \code{()}, \code{[]}.
Fred Drake64e3b431998-07-24 13:56:11 +000049
Fred Drake38e5d272000-04-03 20:13:55 +000050\item any empty mapping, for example, \code{\{\}}.
Fred Drake64e3b431998-07-24 13:56:11 +000051
52\item instances of user-defined classes, if the class defines a
Fred Drake442c7c72002-08-07 15:40:15 +000053 \method{__nonzero__()} or \method{__len__()} method, when that
54 method returns the integer zero or \class{bool} value
55 \code{False}.\footnote{Additional
Fred Drake3e59f722002-07-12 17:15:10 +000056information on these special methods may be found in the
57\citetitle[../ref/ref.html]{Python Reference Manual}.}
Fred Drake64e3b431998-07-24 13:56:11 +000058
59\end{itemize}
60
61All other values are considered true --- so objects of many types are
62always true.
63\index{true}
64
65Operations and built-in functions that have a Boolean result always
Guido van Rossum77f6a652002-04-03 22:41:51 +000066return \code{0} or \code{False} for false and \code{1} or \code{True}
67for true, unless otherwise stated. (Important exception: the Boolean
68operations \samp{or}\opindex{or} and \samp{and}\opindex{and} always
69return one of their operands.)
70\index{False}
71\index{True}
Fred Drake64e3b431998-07-24 13:56:11 +000072
Fred Drake7a2f0661998-09-10 18:25:58 +000073\subsection{Boolean Operations \label{boolean}}
Fred Drake64e3b431998-07-24 13:56:11 +000074
75These are the Boolean operations, ordered by ascending priority:
76\indexii{Boolean}{operations}
77
78\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake8c071d42001-01-26 20:48:35 +000079 \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 Drake64e3b431998-07-24 13:56:11 +000083 \hline
Fred Drake8c071d42001-01-26 20:48:35 +000084 \lineiii{not \var{x}}
Guido van Rossum77f6a652002-04-03 22:41:51 +000085 {if \var{x} is false, then \code{True}, else \code{False}}{(2)}
Fred Drake64e3b431998-07-24 13:56:11 +000086\end{tableiii}
87\opindex{and}
88\opindex{or}
89\opindex{not}
90
91\noindent
92Notes:
93
94\begin{description}
95
96\item[(1)]
97These only evaluate their second argument if needed for their outcome.
98
99\item[(2)]
Fred Drake38e5d272000-04-03 20:13:55 +0000100\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 Drake64e3b431998-07-24 13:56:11 +0000103
104\end{description}
105
106
Fred Drake7a2f0661998-09-10 18:25:58 +0000107\subsection{Comparisons \label{comparisons}}
Fred Drake64e3b431998-07-24 13:56:11 +0000108
109Comparison operations are supported by all objects. They all have the
110same priority (which is higher than that of the Boolean operations).
Fred Drake38e5d272000-04-03 20:13:55 +0000111Comparisons 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
114in both cases \var{z} is not evaluated at all when \code{\var{x} <
115\var{y}} is found to be false).
Fred Drake64e3b431998-07-24 13:56:11 +0000116\indexii{chaining}{comparisons}
117
118This 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 Drake64e3b431998-07-24 13:56:11 +0000126 \lineiii{!=}{not equal}{(1)}
Fred Drake512bb722000-08-18 03:12:38 +0000127 \lineiii{<>}{not equal}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000128 \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
137Notes:
138
139\begin{description}
140
141\item[(1)]
142\code{<>} and \code{!=} are alternate spellings for the same operator.
Fred Drake38e5d272000-04-03 20:13:55 +0000143\code{!=} is the preferred spelling; \code{<>} is obsolescent.
Fred Drake64e3b431998-07-24 13:56:11 +0000144
145\end{description}
146
Martin v. Löwis19a5a712003-05-31 08:05:49 +0000147Objects of different types, except different numeric types and different string types, never
Fred Drake64e3b431998-07-24 13:56:11 +0000148compare equal; such objects are ordered consistently but arbitrarily
149(so that sorting a heterogeneous array yields a consistent result).
Fred Drake38e5d272000-04-03 20:13:55 +0000150Furthermore, some types (for example, file objects) support only a
151degenerate notion of comparison where any two objects of that type are
152unequal. Again, such objects are ordered arbitrarily but
Steve Holden1e4519f2002-06-14 09:16:40 +0000153consistently. The \code{<}, \code{<=}, \code{>} and \code{>=}
154operators will raise a \exception{TypeError} exception when any operand
155is a complex number.
Fred Drake38e5d272000-04-03 20:13:55 +0000156\indexii{object}{numeric}
Fred Drake64e3b431998-07-24 13:56:11 +0000157\indexii{objects}{comparing}
158
Fred Drake38e5d272000-04-03 20:13:55 +0000159Instances of a class normally compare as non-equal unless the class
160\withsubitem{(instance method)}{\ttindex{__cmp__()}}
Fred Drake66571cc2000-09-09 03:30:34 +0000161defines the \method{__cmp__()} method. Refer to the
162\citetitle[../ref/customization.html]{Python Reference Manual} for
163information on the use of this method to effect object comparisons.
Fred Drake64e3b431998-07-24 13:56:11 +0000164
Fred Drake38e5d272000-04-03 20:13:55 +0000165\strong{Implementation note:} Objects of different types except
166numbers are ordered by their type names; objects of the same types
167that don't support proper comparison are ordered by their address.
168
169Two more operations with the same syntactic priority,
170\samp{in}\opindex{in} and \samp{not in}\opindex{not in}, are supported
171only by sequence types (below).
Fred Drake64e3b431998-07-24 13:56:11 +0000172
173
Fred Drake7a2f0661998-09-10 18:25:58 +0000174\subsection{Numeric Types \label{typesnumeric}}
Fred Drake64e3b431998-07-24 13:56:11 +0000175
Guido van Rossum77f6a652002-04-03 22:41:51 +0000176There are four distinct numeric types: \dfn{plain integers},
177\dfn{long integers},
Fred Drake64e3b431998-07-24 13:56:11 +0000178\dfn{floating point numbers}, and \dfn{complex numbers}.
Guido van Rossum77f6a652002-04-03 22:41:51 +0000179In addition, Booleans are a subtype of plain integers.
Fred Drake64e3b431998-07-24 13:56:11 +0000180Plain integers (also just called \dfn{integers})
Fred Drake38e5d272000-04-03 20:13:55 +0000181are implemented using \ctype{long} in C, which gives them at least 32
Fred Drake64e3b431998-07-24 13:56:11 +0000182bits of precision. Long integers have unlimited precision. Floating
Fred Drake38e5d272000-04-03 20:13:55 +0000183point numbers are implemented using \ctype{double} in C. All bets on
Fred Drake64e3b431998-07-24 13:56:11 +0000184their precision are off unless you happen to know the machine you are
185working with.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000186\obindex{numeric}
Guido van Rossum77f6a652002-04-03 22:41:51 +0000187\obindex{Boolean}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000188\obindex{integer}
189\obindex{long integer}
190\obindex{floating point}
191\obindex{complex number}
Fred Drake38e5d272000-04-03 20:13:55 +0000192\indexii{C}{language}
Fred Drake64e3b431998-07-24 13:56:11 +0000193
Steve Holden1e4519f2002-06-14 09:16:40 +0000194Complex numbers have a real and imaginary part, which are each
Fred Drake38e5d272000-04-03 20:13:55 +0000195implemented using \ctype{double} in C. To extract these parts from
Tim Peters8f01b682002-03-12 03:04:44 +0000196a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}.
Fred Drake64e3b431998-07-24 13:56:11 +0000197
198Numbers are created by numeric literals or as the result of built-in
199functions and operators. Unadorned integer literals (including hex
Steve Holden1e4519f2002-06-14 09:16:40 +0000200and octal numbers) yield plain integers unless the value they denote
201is too large to be represented as a plain integer, in which case
202they yield a long integer. Integer literals with an
Fred Drake38e5d272000-04-03 20:13:55 +0000203\character{L} or \character{l} suffix yield long integers
204(\character{L} is preferred because \samp{1l} looks too much like
205eleven!). Numeric literals containing a decimal point or an exponent
206sign yield floating point numbers. Appending \character{j} or
Steve Holden1e4519f2002-06-14 09:16:40 +0000207\character{J} to a numeric literal yields a complex number with a
208zero real part. A complex numeric literal is the sum of a real and
209an imaginary part.
Fred Drake64e3b431998-07-24 13:56:11 +0000210\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
218Python fully supports mixed arithmetic: when a binary arithmetic
219operator has operands of different numeric types, the operand with the
Steve Holden1e4519f2002-06-14 09:16:40 +0000220``narrower'' type is widened to that of the other, where plain
221integer is narrower than long integer is narrower than floating point is
222narrower than complex.
Fred Drakeea003fc1999-04-05 21:59:15 +0000223Comparisons between numbers of mixed type use the same rule.\footnote{
224 As a consequence, the list \code{[1, 2]} is considered equal
Steve Holden1e4519f2002-06-14 09:16:40 +0000225 to \code{[1.0, 2.0]}, and similarly for tuples.
226} The constructors \function{int()}, \function{long()}, \function{float()},
Fred Drake84538cd1998-11-30 21:51:25 +0000227and \function{complex()} can be used
Steve Holden1e4519f2002-06-14 09:16:40 +0000228to produce numbers of a specific type.
Fred Drake64e3b431998-07-24 13:56:11 +0000229\index{arithmetic}
230\bifuncindex{int}
231\bifuncindex{long}
232\bifuncindex{float}
233\bifuncindex{complex}
234
Michael W. Hudson9c206152003-03-05 14:42:09 +0000235All numeric types (except complex) support the following operations,
236sorted by ascending priority (operations in the same box have the same
Fred Drake64e3b431998-07-24 13:56:11 +0000237priority; all numeric operations have a higher priority than
238comparison 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. Hudson9c206152003-03-05 14:42:09 +0000246 \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000247 \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 Drake26b698f1999-02-12 18:27:31 +0000256 \lineiii{\var{c}.conjugate()}{conjugate of the complex number \var{c}}{}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000257 \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000258 \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 Drake26b698f1999-02-12 18:27:31 +0000262\withsubitem{(complex number method)}{\ttindex{conjugate()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000263
264\noindent
265Notes:
266\begin{description}
267
268\item[(1)]
269For (plain or long) integer division, the result is an integer.
Tim Peters8f01b682002-03-12 03:04:44 +0000270The result is always rounded towards minus infinity: 1/2 is 0,
Fred Drake38e5d272000-04-03 20:13:55 +0000271(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result
272is a long integer if either operand is a long integer, regardless of
273the numeric value.
Fred Drake64e3b431998-07-24 13:56:11 +0000274\indexii{integer}{division}
275\indexiii{long}{integer}{division}
276
277\item[(2)]
278Conversion from floating point to (long or plain) integer may round or
Fred Drake4de96c22000-08-12 03:36:23 +0000279truncate as in C; see functions \function{floor()} and
280\function{ceil()} in the \refmodule{math}\refbimodindex{math} module
281for well-defined conversions.
Fred Drake9474d861999-02-12 22:05:33 +0000282\withsubitem{(in module math)}{\ttindex{floor()}\ttindex{ceil()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000283\indexii{numeric}{conversions}
Fred Drake4de96c22000-08-12 03:36:23 +0000284\indexii{C}{language}
Fred Drake64e3b431998-07-24 13:56:11 +0000285
286\item[(3)]
Fred Drake38e5d272000-04-03 20:13:55 +0000287See section \ref{built-in-funcs}, ``Built-in Functions,'' for a full
288description.
Fred Drake64e3b431998-07-24 13:56:11 +0000289
Michael W. Hudson9c206152003-03-05 14:42:09 +0000290\item[(4)]
291Complex floor division operator, modulo operator, and \function{divmod()}.
292
293\deprecated{2.3}{Instead convert to float using \function{abs()}
294if appropriate.}
295
Fred Drake64e3b431998-07-24 13:56:11 +0000296\end{description}
297% XXXJH exceptions: overflow (when? what operations?) zerodivision
298
Fred Drake4e7c2051999-02-19 15:30:25 +0000299\subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}}
Fred Drake64e3b431998-07-24 13:56:11 +0000300\nodename{Bit-string Operations}
301
302Plain and long integer types support additional operations that make
303sense only for bit-strings. Negative numbers are treated as their 2's
304complement value (for long integers, this assumes a sufficiently large
305number of bits that no overflow occurs during the operation).
306
307The priorities of the binary bit-wise operations are all lower than
308the numeric operations and higher than the comparisons; the unary
309operation \samp{\~} has the same priority as the other unary numeric
310operations (\samp{+} and \samp{-}).
311
312This table lists the bit-string operations sorted in ascending
313priority (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
330Notes:
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
335multiplication by \code{pow(2, \var{n})} without overflow check.
336\item[(3)] A right shift by \var{n} bits is equivalent to
337division by \code{pow(2, \var{n})} without overflow check.
338\end{description}
339
340
Fred Drake93656e72001-05-02 20:18:03 +0000341\subsection{Iterator Types \label{typeiter}}
342
Fred Drakef42cc452001-05-03 04:39:10 +0000343\versionadded{2.2}
Fred Drake93656e72001-05-02 20:18:03 +0000344\index{iterator protocol}
345\index{protocol!iterator}
346\index{sequence!iteration}
347\index{container!iteration over}
348
349Python supports a concept of iteration over containers. This is
350implemented using two distinct methods; these are used to allow
351user-defined classes to support iteration. Sequences, described below
352in more detail, always support the iteration methods.
353
354One method needs to be defined for container objects to provide
355iteration support:
356
357\begin{methoddesc}[container]{__iter__}{}
Greg Ward54f65092001-07-26 21:01:21 +0000358 Return an iterator object. The object is required to support the
Fred Drake93656e72001-05-02 20:18:03 +0000359 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
368The iterator objects themselves are required to support the following
369two 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 Drakef42cc452001-05-03 04:39:10 +0000379\begin{methoddesc}[iterator]{next}{}
Fred Drake93656e72001-05-02 20:18:03 +0000380 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
386Python defines several iterator objects to support iteration over
387general and specific sequence types, dictionaries, and other more
388specialized forms. The specific types are not important beyond their
389implementation of the iterator protocol.
390
Guido van Rossum9534e142002-07-16 19:53:39 +0000391The intention of the protocol is that once an iterator's
392\method{next()} method raises \exception{StopIteration}, it will
393continue to do so on subsequent calls. Implementations that
394do not obey this property are deemed broken. (This constraint
395was added in Python 2.3; in Python 2.2, various iterators are
396broken according to this rule.)
397
Raymond Hettinger2dd8c422003-06-25 19:03:22 +0000398Python's generators provide a convenient way to implement the
399iterator protocol. If a container object's \method{__iter__()}
400method is implemented as a generator, it will automatically
401return an iterator object (technically, a generator object)
402supplying the \method{__iter__()} and \method{next()} methods.
403
Fred Drake93656e72001-05-02 20:18:03 +0000404
Fred Drake7a2f0661998-09-10 18:25:58 +0000405\subsection{Sequence Types \label{typesseq}}
Fred Drake64e3b431998-07-24 13:56:11 +0000406
Fred Drake107b9672000-08-14 15:37:59 +0000407There are six sequence types: strings, Unicode strings, lists,
Fred Drake512bb722000-08-18 03:12:38 +0000408tuples, buffers, and xrange objects.
Fred Drake64e3b431998-07-24 13:56:11 +0000409
Steve Holden1e4519f2002-06-14 09:16:40 +0000410String literals are written in single or double quotes:
Fred Drake38e5d272000-04-03 20:13:55 +0000411\code{'xyzzy'}, \code{"frobozz"}. See chapter 2 of the
Fred Drake4de96c22000-08-12 03:36:23 +0000412\citetitle[../ref/strings.html]{Python Reference Manual} for more about
413string literals. Unicode strings are much like strings, but are
414specified in the syntax using a preceeding \character{u} character:
415\code{u'abc'}, \code{u"def"}. Lists are constructed with square brackets,
Fred Drake37f15741999-11-10 16:21:37 +0000416separating items with commas: \code{[a, b, c]}. Tuples are
417constructed by the comma operator (not within square brackets), with
418or without enclosing parentheses, but an empty tuple must have the
Raymond Hettingerb67449d2003-09-08 18:52:18 +0000419enclosing parentheses, such as \code{a, b, c} or \code{()}. A single
420item tuple must have a trailing comma, such as \code{(d,)}.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000421\obindex{sequence}
422\obindex{string}
423\obindex{Unicode}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000424\obindex{tuple}
425\obindex{list}
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000426
427Buffer objects are not directly supported by Python syntax, but can be
428created by calling the builtin function
Fred Drake36c2bd82002-09-24 15:32:04 +0000429\function{buffer()}.\bifuncindex{buffer} They don't support
Steve Holden1e4519f2002-06-14 09:16:40 +0000430concatenation or repetition.
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000431\obindex{buffer}
432
433Xrange objects are similar to buffers in that there is no specific
Steve Holden1e4519f2002-06-14 09:16:40 +0000434syntax to create them, but they are created using the \function{xrange()}
435function.\bifuncindex{xrange} They don't support slicing,
436concatenation or repetition, and using \code{in}, \code{not in},
437\function{min()} or \function{max()} on them is inefficient.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000438\obindex{xrange}
Fred Drake64e3b431998-07-24 13:56:11 +0000439
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000440Most sequence types support the following operations. The \samp{in} and
Fred Drake64e3b431998-07-24 13:56:11 +0000441\samp{not in} operations have the same priorities as the comparison
442operations. The \samp{+} and \samp{*} operations have the same
443priority as the corresponding numeric operations.\footnote{They must
444have since the parser can't tell the type of the operands.}
445
446This 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}
449and \var{j} are integers:
450
451\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Barry Warsaw817918c2002-08-06 16:58:21 +0000452 \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000453 \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is
Barry Warsaw817918c2002-08-06 16:58:21 +0000454equal to \var{x}, else \code{1}}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000455 \hline
456 \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
Barry Warsaw817918c2002-08-06 16:58:21 +0000457 \lineiii{\var{s} * \var{n}\textrm{,} \var{n} * \var{s}}{\var{n} shallow copies of \var{s} concatenated}{(2)}
Fred Drake64e3b431998-07-24 13:56:11 +0000458 \hline
Barry Warsaw817918c2002-08-06 16:58:21 +0000459 \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. Hudson9c206152003-03-05 14:42:09 +0000461 \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 Drake64e3b431998-07-24 13:56:11 +0000462 \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. Hudson9c206152003-03-05 14:42:09 +0000475\indexii{extended slice}{operation}
Fred Drake64e3b431998-07-24 13:56:11 +0000476\opindex{in}
477\opindex{not in}
478
479\noindent
480Notes:
481
482\begin{description}
Barry Warsaw817918c2002-08-06 16:58:21 +0000483\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
485Python versions before 2.3, \var{x} had to be a string of length 1.
486In 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 Drake38e5d272000-04-03 20:13:55 +0000489 \code{0} (which yields an empty sequence of the same type as
Fred Draked800cff2001-08-28 14:56:05 +0000490 \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 Drake38e5d272000-04-03 20:13:55 +0000515
Barry Warsaw817918c2002-08-06 16:58:21 +0000516\item[(3)] If \var{i} or \var{j} is negative, the index is relative to
Fred Drake907e76b2001-07-06 20:30:11 +0000517 the end of the string: \code{len(\var{s}) + \var{i}} or
Fred Drake64e3b431998-07-24 13:56:11 +0000518 \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is
519 still \code{0}.
Tim Peters8f01b682002-03-12 03:04:44 +0000520
Barry Warsaw817918c2002-08-06 16:58:21 +0000521\item[(4)] The slice of \var{s} from \var{i} to \var{j} is defined as
Fred Drake64e3b431998-07-24 13:56:11 +0000522 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. Hudson9c206152003-03-05 14:42:09 +0000527
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 Hettinger81702002003-08-30 23:31:31 +0000533 \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. Hudson9c206152003-03-05 14:42:09 +0000536
Fred Drake64e3b431998-07-24 13:56:11 +0000537\end{description}
538
Fred Drake9474d861999-02-12 22:05:33 +0000539
Fred Drake4de96c22000-08-12 03:36:23 +0000540\subsubsection{String Methods \label{string-methods}}
541
542These are the string methods which both 8-bit strings and Unicode
543objects support:
544
545\begin{methoddesc}[string]{capitalize}{}
546Return a copy of the string with only its first character capitalized.
547\end{methoddesc}
548
549\begin{methoddesc}[string]{center}{width}
550Return centered in a string of length \var{width}. Padding is done
551using spaces.
552\end{methoddesc}
553
554\begin{methoddesc}[string]{count}{sub\optional{, start\optional{, end}}}
555Return the number of occurrences of substring \var{sub} in string
556S\code{[\var{start}:\var{end}]}. Optional arguments \var{start} and
557\var{end} are interpreted as in slice notation.
558\end{methoddesc}
559
Fred Drake6048ce92001-12-10 16:43:08 +0000560\begin{methoddesc}[string]{decode}{\optional{encoding\optional{, errors}}}
561Decodes the string using the codec registered for \var{encoding}.
562\var{encoding} defaults to the default string encoding. \var{errors}
563may be given to set a different error handling scheme. The default is
564\code{'strict'}, meaning that encoding errors raise
565\exception{ValueError}. Other possible values are \code{'ignore'} and
Fred Draked22bb652003-10-22 02:56:40 +0000566\code{'replace'}.
Fred Drake6048ce92001-12-10 16:43:08 +0000567\versionadded{2.2}
568\end{methoddesc}
569
Fred Drake4de96c22000-08-12 03:36:23 +0000570\begin{methoddesc}[string]{encode}{\optional{encoding\optional{,errors}}}
571Return an encoded version of the string. Default encoding is the current
572default string encoding. \var{errors} may be given to set a different
573error handling scheme. The default for \var{errors} is
574\code{'strict'}, meaning that encoding errors raise a
575\exception{ValueError}. Other possible values are \code{'ignore'} and
576\code{'replace'}.
Fred Drake1dba66c2000-10-25 21:03:55 +0000577\versionadded{2.0}
Fred Drake4de96c22000-08-12 03:36:23 +0000578\end{methoddesc}
579
580\begin{methoddesc}[string]{endswith}{suffix\optional{, start\optional{, end}}}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000581Return \code{True} if the string ends with the specified \var{suffix},
582otherwise return \code{False}. With optional \var{start}, test beginning at
Fred Drake4de96c22000-08-12 03:36:23 +0000583that position. With optional \var{end}, stop comparing at that position.
584\end{methoddesc}
585
586\begin{methoddesc}[string]{expandtabs}{\optional{tabsize}}
587Return a copy of the string where all tab characters are expanded
588using spaces. If \var{tabsize} is not given, a tab size of \code{8}
589characters is assumed.
590\end{methoddesc}
591
592\begin{methoddesc}[string]{find}{sub\optional{, start\optional{, end}}}
593Return the lowest index in the string where substring \var{sub} is
594found, such that \var{sub} is contained in the range [\var{start},
595\var{end}). Optional arguments \var{start} and \var{end} are
596interpreted as in slice notation. Return \code{-1} if \var{sub} is
597not found.
598\end{methoddesc}
599
600\begin{methoddesc}[string]{index}{sub\optional{, start\optional{, end}}}
601Like \method{find()}, but raise \exception{ValueError} when the
602substring is not found.
603\end{methoddesc}
604
605\begin{methoddesc}[string]{isalnum}{}
606Return true if all characters in the string are alphanumeric and there
607is at least one character, false otherwise.
608\end{methoddesc}
609
610\begin{methoddesc}[string]{isalpha}{}
611Return true if all characters in the string are alphabetic and there
612is at least one character, false otherwise.
613\end{methoddesc}
614
615\begin{methoddesc}[string]{isdigit}{}
Martin v. Löwis6828e182003-10-18 09:55:08 +0000616Return true if all characters in the string are digits and there
617is at least one character, false otherwise.
Fred Drake4de96c22000-08-12 03:36:23 +0000618\end{methoddesc}
619
620\begin{methoddesc}[string]{islower}{}
621Return true if all cased characters in the string are lowercase and
622there is at least one cased character, false otherwise.
623\end{methoddesc}
624
625\begin{methoddesc}[string]{isspace}{}
626Return true if there are only whitespace characters in the string and
Martin v. Löwis6828e182003-10-18 09:55:08 +0000627there is at least one character, false otherwise.
Fred Drake4de96c22000-08-12 03:36:23 +0000628\end{methoddesc}
629
630\begin{methoddesc}[string]{istitle}{}
Martin v. Löwis6828e182003-10-18 09:55:08 +0000631Return true if the string is a titlecased string and there is at least one
Raymond Hettinger0a9b9da2003-10-29 06:54:43 +0000632character, for example uppercase characters may only follow uncased
Martin v. Löwis6828e182003-10-18 09:55:08 +0000633characters and lowercase characters only cased ones. Return false
634otherwise.
Fred Drake4de96c22000-08-12 03:36:23 +0000635\end{methoddesc}
636
637\begin{methoddesc}[string]{isupper}{}
638Return true if all cased characters in the string are uppercase and
639there is at least one cased character, false otherwise.
640\end{methoddesc}
641
642\begin{methoddesc}[string]{join}{seq}
643Return a string which is the concatenation of the strings in the
644sequence \var{seq}. The separator between elements is the string
645providing this method.
646\end{methoddesc}
647
648\begin{methoddesc}[string]{ljust}{width}
649Return the string left justified in a string of length \var{width}.
650Padding is done using spaces. The original string is returned if
651\var{width} is less than \code{len(\var{s})}.
652\end{methoddesc}
653
654\begin{methoddesc}[string]{lower}{}
655Return a copy of the string converted to lowercase.
656\end{methoddesc}
657
Fred Drake8b1c47b2002-04-13 02:43:39 +0000658\begin{methoddesc}[string]{lstrip}{\optional{chars}}
659Return a copy of the string with leading characters removed. If
660\var{chars} is omitted or \code{None}, whitespace characters are
661removed. If given and not \code{None}, \var{chars} must be a string;
662the characters in the string will be stripped from the beginning of
663the string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000664\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000665\end{methoddesc}
666
Fred Draked22bb652003-10-22 02:56:40 +0000667\begin{methoddesc}[string]{replace}{old, new\optional{, count}}
Fred Drake4de96c22000-08-12 03:36:23 +0000668Return a copy of the string with all occurrences of substring
669\var{old} replaced by \var{new}. If the optional argument
Fred Draked22bb652003-10-22 02:56:40 +0000670\var{count} is given, only the first \var{count} occurrences are
Fred Drake4de96c22000-08-12 03:36:23 +0000671replaced.
672\end{methoddesc}
673
674\begin{methoddesc}[string]{rfind}{sub \optional{,start \optional{,end}}}
675Return the highest index in the string where substring \var{sub} is
676found, such that \var{sub} is contained within s[start,end]. Optional
677arguments \var{start} and \var{end} are interpreted as in slice
678notation. Return \code{-1} on failure.
679\end{methoddesc}
680
681\begin{methoddesc}[string]{rindex}{sub\optional{, start\optional{, end}}}
682Like \method{rfind()} but raises \exception{ValueError} when the
683substring \var{sub} is not found.
684\end{methoddesc}
685
686\begin{methoddesc}[string]{rjust}{width}
687Return the string right justified in a string of length \var{width}.
688Padding is done using spaces. The original string is returned if
689\var{width} is less than \code{len(\var{s})}.
690\end{methoddesc}
691
Fred Drake8b1c47b2002-04-13 02:43:39 +0000692\begin{methoddesc}[string]{rstrip}{\optional{chars}}
693Return a copy of the string with trailing characters removed. If
694\var{chars} is omitted or \code{None}, whitespace characters are
695removed. If given and not \code{None}, \var{chars} must be a string;
696the characters in the string will be stripped from the end of the
697string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000698\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000699\end{methoddesc}
700
701\begin{methoddesc}[string]{split}{\optional{sep \optional{,maxsplit}}}
702Return a list of the words in the string, using \var{sep} as the
703delimiter string. If \var{maxsplit} is given, at most \var{maxsplit}
704splits are done. If \var{sep} is not specified or \code{None}, any
705whitespace string is a separator.
706\end{methoddesc}
707
708\begin{methoddesc}[string]{splitlines}{\optional{keepends}}
709Return a list of the lines in the string, breaking at line
710boundaries. Line breaks are not included in the resulting list unless
711\var{keepends} is given and true.
712\end{methoddesc}
713
Fred Drake8b1c47b2002-04-13 02:43:39 +0000714\begin{methoddesc}[string]{startswith}{prefix\optional{,
715 start\optional{, end}}}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000716Return \code{True} if string starts with the \var{prefix}, otherwise
717return \code{False}. With optional \var{start}, test string beginning at
Fred Drake4de96c22000-08-12 03:36:23 +0000718that position. With optional \var{end}, stop comparing string at that
719position.
720\end{methoddesc}
721
Fred Drake8b1c47b2002-04-13 02:43:39 +0000722\begin{methoddesc}[string]{strip}{\optional{chars}}
723Return a copy of the string with leading and trailing characters
724removed. If \var{chars} is omitted or \code{None}, whitespace
725characters are removed. If given and not \code{None}, \var{chars}
726must be a string; the characters in the string will be stripped from
727the both ends of the string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000728\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000729\end{methoddesc}
730
731\begin{methoddesc}[string]{swapcase}{}
732Return a copy of the string with uppercase characters converted to
733lowercase and vice versa.
734\end{methoddesc}
735
736\begin{methoddesc}[string]{title}{}
Fred Drake907e76b2001-07-06 20:30:11 +0000737Return a titlecased version of the string: words start with uppercase
Fred Drake4de96c22000-08-12 03:36:23 +0000738characters, all remaining cased characters are lowercase.
739\end{methoddesc}
740
741\begin{methoddesc}[string]{translate}{table\optional{, deletechars}}
742Return a copy of the string where all characters occurring in the
743optional argument \var{deletechars} are removed, and the remaining
744characters have been mapped through the given translation table, which
745must be a string of length 256.
Raymond Hettinger46f681c2003-07-16 05:11:27 +0000746
747For Unicode objects, the \method{translate()} method does not
748accept the optional \var{deletechars} argument. Instead, it
749returns a copy of the \var{s} where all characters have been mapped
750through the given translation table which must be a mapping of
751Unicode ordinals to Unicode ordinals, Unicode strings or \code{None}.
752Unmapped characters are left untouched. Characters mapped to \code{None}
753are deleted. Note, a more flexible approach is to create a custom
754character mapping codec using the \refmodule{codecs} module (see
755\module{encodings.cp1251} for an example).
Fred Drake4de96c22000-08-12 03:36:23 +0000756\end{methoddesc}
757
758\begin{methoddesc}[string]{upper}{}
759Return a copy of the string converted to uppercase.
760\end{methoddesc}
761
Walter Dörwald068325e2002-04-15 13:36:47 +0000762\begin{methoddesc}[string]{zfill}{width}
763Return the numeric string left filled with zeros in a string
764of length \var{width}. The original string is returned if
765\var{width} is less than \code{len(\var{s})}.
Fred Drakee55bec22002-11-16 00:44:00 +0000766\versionadded{2.2.2}
Walter Dörwald068325e2002-04-15 13:36:47 +0000767\end{methoddesc}
768
Fred Drake4de96c22000-08-12 03:36:23 +0000769
770\subsubsection{String Formatting Operations \label{typesseq-strings}}
Fred Drake64e3b431998-07-24 13:56:11 +0000771
Fred Drakeb38784e2001-12-03 22:15:56 +0000772\index{formatting, string (\%{})}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000773\index{interpolation, string (\%{})}
Fred Drake66d32b12000-09-14 17:57:42 +0000774\index{string!formatting}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000775\index{string!interpolation}
Fred Drake66d32b12000-09-14 17:57:42 +0000776\index{printf-style formatting}
777\index{sprintf-style formatting}
Fred Drakeb38784e2001-12-03 22:15:56 +0000778\index{\protect\%{} formatting}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000779\index{\protect\%{} interpolation}
Fred Drake66d32b12000-09-14 17:57:42 +0000780
Fred Drake8c071d42001-01-26 20:48:35 +0000781String and Unicode objects have one unique built-in operation: the
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000782\code{\%} operator (modulo). This is also known as the string
783\emph{formatting} or \emph{interpolation} operator. Given
784\code{\var{format} \% \var{values}} (where \var{format} is a string or
785Unicode object), \code{\%} conversion specifications in \var{format}
786are replaced with zero or more elements of \var{values}. The effect
787is similar to the using \cfunction{sprintf()} in the C language. If
788\var{format} is a Unicode object, or if any of the objects being
789converted using the \code{\%s} conversion are Unicode objects, the
Steve Holden1e4519f2002-06-14 09:16:40 +0000790result will also be a Unicode object.
Fred Drake64e3b431998-07-24 13:56:11 +0000791
Fred Drake8c071d42001-01-26 20:48:35 +0000792If \var{format} requires a single argument, \var{values} may be a
Steve Holden1e4519f2002-06-14 09:16:40 +0000793single non-tuple object. \footnote{To format only a tuple you
794should therefore provide a singleton tuple whose only element
795is the tuple to be formatted.} Otherwise, \var{values} must be a tuple with
Fred Drake8c071d42001-01-26 20:48:35 +0000796exactly the number of items specified by the format string, or a
797single mapping object (for example, a dictionary).
Fred Drake64e3b431998-07-24 13:56:11 +0000798
Fred Drake8c071d42001-01-26 20:48:35 +0000799A conversion specifier contains two or more characters and has the
800following components, which must occur in this order:
801
802\begin{enumerate}
803 \item The \character{\%} character, which marks the start of the
804 specifier.
Steve Holden1e4519f2002-06-14 09:16:40 +0000805 \item Mapping key (optional), consisting of a parenthesised sequence
806 of characters (for example, \code{(somename)}).
Fred Drake8c071d42001-01-26 20:48:35 +0000807 \item Conversion flags (optional), which affect the result of some
808 conversion types.
809 \item Minimum field width (optional). If specified as an
810 \character{*} (asterisk), the actual width is read from the
811 next element of the tuple in \var{values}, and the object to
812 convert comes after the minimum field width and optional
813 precision.
814 \item Precision (optional), given as a \character{.} (dot) followed
815 by the precision. If specified as \character{*} (an
816 asterisk), the actual width is read from the next element of
817 the tuple in \var{values}, and the value to convert comes after
818 the precision.
819 \item Length modifier (optional).
820 \item Conversion type.
821\end{enumerate}
Fred Drake64e3b431998-07-24 13:56:11 +0000822
Steve Holden1e4519f2002-06-14 09:16:40 +0000823When the right argument is a dictionary (or other mapping type), then
824the formats in the string \emph{must} include a parenthesised mapping key into
Fred Drake8c071d42001-01-26 20:48:35 +0000825that dictionary inserted immediately after the \character{\%}
Steve Holden1e4519f2002-06-14 09:16:40 +0000826character. The mapping key selects the value to be formatted from the
Fred Drake8c071d42001-01-26 20:48:35 +0000827mapping. For example:
Fred Drake64e3b431998-07-24 13:56:11 +0000828
829\begin{verbatim}
Steve Holden1e4519f2002-06-14 09:16:40 +0000830>>> print '%(language)s has %(#)03d quote types.' % \
831 {'language': "Python", "#": 2}
Fred Drake64e3b431998-07-24 13:56:11 +0000832Python has 002 quote types.
833\end{verbatim}
834
835In this case no \code{*} specifiers may occur in a format (since they
836require a sequential parameter list).
837
Fred Drake8c071d42001-01-26 20:48:35 +0000838The conversion flag characters are:
839
840\begin{tableii}{c|l}{character}{Flag}{Meaning}
841 \lineii{\#}{The value conversion will use the ``alternate form''
842 (where defined below).}
Neal Norwitzf927f142003-02-17 18:57:06 +0000843 \lineii{0}{The conversion will be zero padded for numeric values.}
Fred Drake8c071d42001-01-26 20:48:35 +0000844 \lineii{-}{The converted value is left adjusted (overrides
Fred Drakef5968262002-10-25 16:55:51 +0000845 the \character{0} conversion if both are given).}
Fred Drake8c071d42001-01-26 20:48:35 +0000846 \lineii{{~}}{(a space) A blank should be left before a positive number
847 (or empty string) produced by a signed conversion.}
848 \lineii{+}{A sign character (\character{+} or \character{-}) will
849 precede the conversion (overrides a "space" flag).}
850\end{tableii}
851
852The length modifier may be \code{h}, \code{l}, and \code{L} may be
853present, but are ignored as they are not necessary for Python.
854
855The conversion types are:
856
Fred Drakef5968262002-10-25 16:55:51 +0000857\begin{tableiii}{c|l|c}{character}{Conversion}{Meaning}{Notes}
858 \lineiii{d}{Signed integer decimal.}{}
859 \lineiii{i}{Signed integer decimal.}{}
860 \lineiii{o}{Unsigned octal.}{(1)}
861 \lineiii{u}{Unsigned decimal.}{}
862 \lineiii{x}{Unsigned hexidecimal (lowercase).}{(2)}
863 \lineiii{X}{Unsigned hexidecimal (uppercase).}{(2)}
864 \lineiii{e}{Floating point exponential format (lowercase).}{}
865 \lineiii{E}{Floating point exponential format (uppercase).}{}
866 \lineiii{f}{Floating point decimal format.}{}
867 \lineiii{F}{Floating point decimal format.}{}
868 \lineiii{g}{Same as \character{e} if exponent is greater than -4 or
869 less than precision, \character{f} otherwise.}{}
870 \lineiii{G}{Same as \character{E} if exponent is greater than -4 or
871 less than precision, \character{F} otherwise.}{}
872 \lineiii{c}{Single character (accepts integer or single character
873 string).}{}
874 \lineiii{r}{String (converts any python object using
875 \function{repr()}).}{(3)}
876 \lineiii{s}{String (converts any python object using
Raymond Hettinger2bd15682003-01-13 04:29:19 +0000877 \function{str()}).}{(4)}
Fred Drakef5968262002-10-25 16:55:51 +0000878 \lineiii{\%}{No argument is converted, results in a \character{\%}
879 character in the result.}{}
880\end{tableiii}
881
882\noindent
883Notes:
884\begin{description}
885 \item[(1)]
886 The alternate form causes a leading zero (\character{0}) to be
887 inserted between left-hand padding and the formatting of the
888 number if the leading character of the result is not already a
889 zero.
890 \item[(2)]
891 The alternate form causes a leading \code{'0x'} or \code{'0X'}
892 (depending on whether the \character{x} or \character{X} format
893 was used) to be inserted between left-hand padding and the
894 formatting of the number if the leading character of the result is
895 not already a zero.
896 \item[(3)]
897 The \code{\%r} conversion was added in Python 2.0.
Raymond Hettinger2bd15682003-01-13 04:29:19 +0000898 \item[(4)]
899 If the object or format provided is a \class{unicode} string,
900 the resulting string will also be \class{unicode}.
Fred Drakef5968262002-10-25 16:55:51 +0000901\end{description}
Fred Drake8c071d42001-01-26 20:48:35 +0000902
903% XXX Examples?
904
Fred Drake8c071d42001-01-26 20:48:35 +0000905Since Python strings have an explicit length, \code{\%s} conversions
906do not assume that \code{'\e0'} is the end of the string.
907
908For safety reasons, floating point precisions are clipped to 50;
909\code{\%f} conversions for numbers whose absolute value is over 1e25
910are replaced by \code{\%g} conversions.\footnote{
911 These numbers are fairly arbitrary. They are intended to
912 avoid printing endless strings of meaningless digits without hampering
913 correct use and without having to know the exact precision of floating
914 point values on a particular machine.
915} All other errors raise exceptions.
916
Fred Drake14f5c5f2001-12-03 18:33:13 +0000917Additional string operations are defined in standard modules
918\refmodule{string}\refstmodindex{string} and
Tim Peters8f01b682002-03-12 03:04:44 +0000919\refmodule{re}.\refstmodindex{re}
Fred Drake64e3b431998-07-24 13:56:11 +0000920
Fred Drake107b9672000-08-14 15:37:59 +0000921
Fred Drake512bb722000-08-18 03:12:38 +0000922\subsubsection{XRange Type \label{typesseq-xrange}}
Fred Drake107b9672000-08-14 15:37:59 +0000923
Fred Drake0b4e25d2000-10-04 04:21:19 +0000924The xrange\obindex{xrange} type is an immutable sequence which is
Fred Drake512bb722000-08-18 03:12:38 +0000925commonly used for looping. The advantage of the xrange type is that an
926xrange object will always take the same amount of memory, no matter the
Fred Drake107b9672000-08-14 15:37:59 +0000927size of the range it represents. There are no consistent performance
928advantages.
929
Raymond Hettingerd2bef822002-12-11 07:14:03 +0000930XRange objects have very little behavior: they only support indexing,
931iteration, and the \function{len()} function.
Fred Drake107b9672000-08-14 15:37:59 +0000932
933
Fred Drake9474d861999-02-12 22:05:33 +0000934\subsubsection{Mutable Sequence Types \label{typesseq-mutable}}
Fred Drake64e3b431998-07-24 13:56:11 +0000935
936List objects support additional operations that allow in-place
937modification of the object.
Steve Holden1e4519f2002-06-14 09:16:40 +0000938Other mutable sequence types (when added to the language) should
939also support these operations.
940Strings and tuples are immutable sequence types: such objects cannot
Fred Drake64e3b431998-07-24 13:56:11 +0000941be modified once created.
942The following operations are defined on mutable sequence types (where
943\var{x} is an arbitrary object):
944\indexiii{mutable}{sequence}{types}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000945\obindex{list}
Fred Drake64e3b431998-07-24 13:56:11 +0000946
947\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
948 \lineiii{\var{s}[\var{i}] = \var{x}}
949 {item \var{i} of \var{s} is replaced by \var{x}}{}
950 \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}}
951 {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{}
952 \lineiii{del \var{s}[\var{i}:\var{j}]}
953 {same as \code{\var{s}[\var{i}:\var{j}] = []}}{}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000954 \lineiii{\var{s}[\var{i}:\var{j}:\var{k}] = \var{t}}
955 {the elements of \code{\var{s}[\var{i}:\var{j}:\var{k}]} are replaced by those of \var{t}}{(1)}
956 \lineiii{del \var{s}[\var{i}:\var{j}:\var{k}]}
957 {removes the elements of \code{\var{s}[\var{i}:\var{j}:\var{k}]} from the list}{}
Fred Drake64e3b431998-07-24 13:56:11 +0000958 \lineiii{\var{s}.append(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000959 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{(2)}
Barry Warsawafd974c1998-10-09 16:39:58 +0000960 \lineiii{\var{s}.extend(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000961 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = \var{x}}}{(3)}
Fred Drake64e3b431998-07-24 13:56:11 +0000962 \lineiii{\var{s}.count(\var{x})}
963 {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{}
Walter Dörwald93719b52003-06-17 16:19:56 +0000964 \lineiii{\var{s}.index(\var{x}\optional{, \var{i}\optional{, \var{j}}})}
965 {return smallest \var{k} such that \code{\var{s}[\var{k}] == \var{x}} and
966 \code{\var{i} <= \var{k} < \var{j}}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000967 \lineiii{\var{s}.insert(\var{i}, \var{x})}
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000968 {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}}{(5)}
Fred Drake64e3b431998-07-24 13:56:11 +0000969 \lineiii{\var{s}.pop(\optional{\var{i}})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000970 {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(6)}
Fred Drake64e3b431998-07-24 13:56:11 +0000971 \lineiii{\var{s}.remove(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000972 {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000973 \lineiii{\var{s}.reverse()}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000974 {reverses the items of \var{s} in place}{(7)}
Raymond Hettinger42b1ba32003-10-16 03:41:09 +0000975 \lineiii{\var{s}.sort(\optional{\var{cmp}=None\optional{, \var{key}=None
976 \optional{, \var{reverse}=False}}})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000977 {sort the items of \var{s} in place}{(7), (8), (9), (10)}
Raymond Hettinger0a9b9da2003-10-29 06:54:43 +0000978 \lineiii{\var{s}.sorted(\var{iterable}\optional{, \var{cmp}=None\optional{, \var{key}=None
979 \optional{, \var{reverse}=False}}})}
980 {return a new sorted list from the items in \var{iterable}}{(8), (9), (11)}
Fred Drake64e3b431998-07-24 13:56:11 +0000981\end{tableiii}
982\indexiv{operations on}{mutable}{sequence}{types}
983\indexiii{operations on}{sequence}{types}
984\indexiii{operations on}{list}{type}
985\indexii{subscript}{assignment}
986\indexii{slice}{assignment}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000987\indexii{extended slice}{assignment}
Fred Drake64e3b431998-07-24 13:56:11 +0000988\stindex{del}
Fred Drake9474d861999-02-12 22:05:33 +0000989\withsubitem{(list method)}{
Fred Drake68921df1999-08-09 17:05:12 +0000990 \ttindex{append()}\ttindex{extend()}\ttindex{count()}\ttindex{index()}
991 \ttindex{insert()}\ttindex{pop()}\ttindex{remove()}\ttindex{reverse()}
Fred Drakee8391991998-11-25 17:09:19 +0000992 \ttindex{sort()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000993\noindent
994Notes:
995\begin{description}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000996\item[(1)] \var{t} must have the same length as the slice it is
997 replacing.
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000998
Michael W. Hudson9c206152003-03-05 14:42:09 +0000999\item[(2)] The C implementation of Python has historically accepted
1000 multiple parameters and implicitly joined them into a tuple; this
1001 no longer works in Python 2.0. Use of this misfeature has been
1002 deprecated since Python 1.4.
Fred Drake38e5d272000-04-03 20:13:55 +00001003
Michael W. Hudson9c206152003-03-05 14:42:09 +00001004\item[(3)] Raises an exception when \var{x} is not a list object. The
1005 \method{extend()} method is experimental and not supported by
1006 mutable sequence types other than lists.
1007
1008\item[(4)] Raises \exception{ValueError} when \var{x} is not found in
Walter Dörwald93719b52003-06-17 16:19:56 +00001009 \var{s}. When a negative index is passed as the second or third parameter
1010 to the \method{index()} method, the list length is added, as for slice
1011 indices. If it is still negative, it is truncated to zero, as for
1012 slice indices. \versionchanged[Previously, \method{index()} didn't
1013 have arguments for specifying start and stop positions]{2.3}
Fred Drake68921df1999-08-09 17:05:12 +00001014
Michael W. Hudson9c206152003-03-05 14:42:09 +00001015\item[(5)] When a negative index is passed as the first parameter to
Guido van Rossum3a3cca52003-04-14 20:58:14 +00001016 the \method{insert()} method, the list length is added, as for slice
1017 indices. If it is still negative, it is truncated to zero, as for
1018 slice indices. \versionchanged[Previously, all negative indices
1019 were truncated to zero]{2.3}
Fred Drakeef428a22001-10-26 18:57:14 +00001020
Michael W. Hudson9c206152003-03-05 14:42:09 +00001021\item[(6)] The \method{pop()} method is only supported by the list and
Fred Drakefbd3b452000-07-31 23:42:23 +00001022 array types. The optional argument \var{i} defaults to \code{-1},
1023 so that by default the last item is removed and returned.
Fred Drake38e5d272000-04-03 20:13:55 +00001024
Michael W. Hudson9c206152003-03-05 14:42:09 +00001025\item[(7)] The \method{sort()} and \method{reverse()} methods modify the
Fred Drake38e5d272000-04-03 20:13:55 +00001026 list in place for economy of space when sorting or reversing a large
Skip Montanaro41d7d582001-07-25 16:18:19 +00001027 list. To remind you that they operate by side effect, they don't return
1028 the sorted or reversed list.
Fred Drake38e5d272000-04-03 20:13:55 +00001029
Raymond Hettinger0a9b9da2003-10-29 06:54:43 +00001030\item[(8)] The \method{sort()} and \method{sorted()} methods take optional
Raymond Hettinger9885c932003-10-30 06:08:32 +00001031 arguments for controlling the comparisons.
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001032
1033 \var{cmp} specifies a custom comparison function of two arguments
1034 (list items) which should return a negative, zero or positive number
1035 depending on whether the first argument is considered smaller than,
1036 equal to, or larger than the second argument:
1037 \samp{\var{cmp}=\keyword{lambda} \var{x},\var{y}:
1038 \function{cmp}(x.lower(), y.lower())}
1039
1040 \var{key} specifies a function of one argument that is used to
1041 extract a comparison key from each list element:
1042 \samp{\var{cmp}=\function{str.lower}}
1043
1044 \var{reverse} is a boolean value. If set to \code{True}, then the
1045 list elements are sorted as if each comparison were reversed.
1046
1047 In general, the \var{key} and \var{reverse} conversion processes are
1048 much faster than specifying an equivalent \var{cmp} function. This is
1049 because \var{cmp} is called multiple times for each list element while
Fred Drake5b6150e2003-10-21 17:04:21 +00001050 \var{key} and \var{reverse} touch each element only once.
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001051
Fred Drake4cee2202003-03-20 22:17:59 +00001052 \versionchanged[Support for \code{None} as an equivalent to omitting
1053 \var{cmpfunc} was added]{2.3}
1054
Fred Drake5b6150e2003-10-21 17:04:21 +00001055 \versionchanged[Support for \var{key} and \var{reverse} was added]{2.4}
Fred Drake4cee2202003-03-20 22:17:59 +00001056
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001057\item[(9)] Starting with Python 2.3, the \method{sort()} method is
Raymond Hettinger0a9b9da2003-10-29 06:54:43 +00001058 guaranteed to be stable. Starting with Python 2.4, the \method{sorted()}
1059 method is also guaranteed to be stable. A sort is stable if it does not
Raymond Hettinger42b1ba32003-10-16 03:41:09 +00001060 change the relative order of elements that compare equal --- this is
1061 helpful for sorting in multiple passes (for example, sort by
1062 department, then by salary grade).
Tim Petersb9099c32002-11-12 22:08:10 +00001063
Michael W. Hudson9c206152003-03-05 14:42:09 +00001064\item[(10)] While a list is being sorted, the effect of attempting to
Tim Petersb9099c32002-11-12 22:08:10 +00001065 mutate, or even inspect, the list is undefined. The C implementation
1066 of Python 2.3 makes the list appear empty for the duration, and raises
1067 \exception{ValueError} if it can detect that the list has been
1068 mutated during a sort.
Raymond Hettinger0a9b9da2003-10-29 06:54:43 +00001069
1070\item[(11)] \method{sorted()} is a class method that returns a new list.
1071 \versionadded{2.4}
Fred Drake64e3b431998-07-24 13:56:11 +00001072\end{description}
1073
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001074\subsection{Set Types \label{types-set}}
1075\obindex{set}
1076
1077A \dfn{set} object is an unordered collection of immutable values.
1078Common uses include membership testing, removing duplicates from a sequence,
1079and computing mathematical operations such as intersection, union, difference,
1080and symmetric difference.
1081\versionadded{2.4}
1082
1083Like other collections, sets support \code{\var{x} in \var{set}},
1084\code{len(\var{set})}, and \code{for \var{x} in \var{set}}. Being an
1085unordered collection, sets do not record element position or order of
1086insertion. Accordingly, sets do not support indexing, slicing, or
1087other sequence-like behavior.
1088
1089There are currently two builtin set types, \class{set} and \class{frozenset}.
1090The \class{set} type is mutable --- the contents can be changed using methods
1091like \method{add()} and \method{remove()}. Since it is mutable, it has no
1092hash value and cannot be used as either a dictionary key or as an element of
1093another set. The \class{frozenset} type is immutable and hashable --- its
1094contents cannot be altered after is created; however, it can be used as
1095a dictionary key or as an element of another set.
1096
1097Instances of \class{set} and \class{frozenset} provide the following operations:
1098
1099\begin{tableiii}{c|c|l}{code}{Operation}{Equivalent}{Result}
1100 \lineiii{len(\var{s})}{}{cardinality of set \var{s}}
1101
1102 \hline
1103 \lineiii{\var{x} in \var{s}}{}
1104 {test \var{x} for membership in \var{s}}
1105 \lineiii{\var{x} not in \var{s}}{}
1106 {test \var{x} for non-membership in \var{s}}
1107 \lineiii{\var{s}.issubset(\var{t})}{\code{\var{s} <= \var{t}}}
1108 {test whether every element in \var{s} is in \var{t}}
1109 \lineiii{\var{s}.issuperset(\var{t})}{\code{\var{s} >= \var{t}}}
1110 {test whether every element in \var{t} is in \var{s}}
1111
1112 \hline
1113 \lineiii{\var{s}.union(\var{t})}{\var{s} | \var{t}}
1114 {new set with elements from both \var{s} and \var{t}}
1115 \lineiii{\var{s}.intersection(\var{t})}{\var{s} \&\ \var{t}}
1116 {new set with elements common to \var{s} and \var{t}}
1117 \lineiii{\var{s}.difference(\var{t})}{\var{s} - \var{t}}
1118 {new set with elements in \var{s} but not in \var{t}}
1119 \lineiii{\var{s}.symmetric_difference(\var{t})}{\var{s} \^\ \var{t}}
1120 {new set with elements in either \var{s} or \var{t} but not both}
1121 \lineiii{\var{s}.copy()}{}
1122 {new set with a shallow copy of \var{s}}
1123\end{tableiii}
1124
1125Note, the non-operator versions of \method{union()}, \method{intersection()},
1126\method{difference()}, and \method{symmetric_difference()},
1127\method{issubset()}, and \method{issuperset()} methods will accept any
1128iterable as an argument. In contrast, their operator based counterparts
1129require their arguments to be sets. This precludes error-prone constructions
1130like \code{set('abc') \&\ 'cbs'} in favor of the more readable
1131\code{set('abc').intersection('cbs')}.
1132
1133Both \class{set} and \class{frozenset} support set to set comparisons.
1134Two sets are equal if and only if every element of each set is contained in
1135the other (each is a subset of the other).
1136A set is less than another set if and only if the first set is a proper
1137subset of the second set (is a subset, but is not equal).
1138A set is greater than another set if and only if the first set is a proper
1139superset of the second set (is a superset, but is not equal).
1140
1141The subset and equality comparisons do not generalize to a complete
1142ordering function. For example, any two disjoint sets are not equal and
1143are not subsets of each other, so \emph{all} of the following return
1144\code{False}: \code{\var{a}<\var{b}}, \code{\var{a}==\var{b}}, or
1145\code{\var{a}>\var{b}}.
1146Accordingly, sets do not implement the \method{__cmp__} method.
1147
1148Since sets only define partial ordering (subset relationships), the output
1149of the \method{list.sort()} method is undefined for lists of sets.
1150
1151For convenience in implementing sets of sets, the \method{__contains__()},
1152\method{remove()}, and \method{discard()} methods automatically match
1153instances of the \class{set} class their \class{frozenset} counterparts
1154inside a set. For example, \code{set('abc') in set([frozenset('abc')])}
1155returns \code{True}.
1156
1157The following table lists operations available for \class{set}
1158that do not apply to immutable instances of \class{frozenset}:
1159
1160\begin{tableiii}{c|c|l}{code}{Operation}{Equivalent}{Result}
1161 \lineiii{\var{s}.update(\var{t})}
1162 {\var{s} |= \var{t}}
1163 {return set \var{s} with elements added from \var{t}}
1164 \lineiii{\var{s}.intersection_update(\var{t})}
1165 {\var{s} \&= \var{t}}
1166 {return set \var{s} keeping only elements also found in \var{t}}
1167 \lineiii{\var{s}.difference_update(\var{t})}
1168 {\var{s} -= \var{t}}
1169 {return set \var{s} after removing elements found in \var{t}}
1170 \lineiii{\var{s}.symmetric_difference_update(\var{t})}
1171 {\var{s} \textasciicircum= \var{t}}
1172 {return set \var{s} with elements from \var{s} or \var{t}
1173 but not both}
1174
1175 \hline
1176 \lineiii{\var{s}.add(\var{x})}{}
1177 {add element \var{x} to set \var{s}}
1178 \lineiii{\var{s}.remove(\var{x})}{}
1179 {remove \var{x} from set \var{s}; raises KeyError if not present}
1180 \lineiii{\var{s}.discard(\var{x})}{}
1181 {removes \var{x} from set \var{s} if present}
1182 \lineiii{\var{s}.pop()}{}
1183 {remove and return an arbitrary element from \var{s}; raises
1184 \exception{KeyError} if empty}
1185 \lineiii{\var{s}.clear()}{}
1186 {remove all elements from set \var{s}}
1187\end{tableiii}
1188
1189Note, the non-operator versions of the \method{update()},
1190\method{intersection_update()}, \method{difference_update()}, and
1191\method{symmetric_difference_update()} methods will accept any iterable
1192as an argument.
1193
Fred Drake64e3b431998-07-24 13:56:11 +00001194
Fred Drake7a2f0661998-09-10 18:25:58 +00001195\subsection{Mapping Types \label{typesmapping}}
Fred Drake0b4e25d2000-10-04 04:21:19 +00001196\obindex{mapping}
1197\obindex{dictionary}
Fred Drake64e3b431998-07-24 13:56:11 +00001198
Steve Holden1e4519f2002-06-14 09:16:40 +00001199A \dfn{mapping} object maps immutable values to
Fred Drake64e3b431998-07-24 13:56:11 +00001200arbitrary objects. Mappings are mutable objects. There is currently
1201only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are
Steve Holden1e4519f2002-06-14 09:16:40 +00001202almost arbitrary values. Only values containing lists, dictionaries
1203or other mutable types (that are compared by value rather than by
1204object identity) may not be used as keys.
Fred Drake64e3b431998-07-24 13:56:11 +00001205Numeric types used for keys obey the normal rules for numeric
Raymond Hettinger74c8e552003-09-12 00:02:37 +00001206comparison: if two numbers compare equal (such as \code{1} and
Fred Drake64e3b431998-07-24 13:56:11 +00001207\code{1.0}) then they can be used interchangeably to index the same
1208dictionary entry.
1209
Fred Drake64e3b431998-07-24 13:56:11 +00001210Dictionaries are created by placing a comma-separated list of
1211\code{\var{key}: \var{value}} pairs within braces, for example:
1212\code{\{'jack': 4098, 'sjoerd': 4127\}} or
1213\code{\{4098: 'jack', 4127: 'sjoerd'\}}.
1214
Fred Drake9c5cc141999-06-10 22:37:34 +00001215The following operations are defined on mappings (where \var{a} and
1216\var{b} are mappings, \var{k} is a key, and \var{v} and \var{x} are
1217arbitrary objects):
Fred Drake64e3b431998-07-24 13:56:11 +00001218\indexiii{operations on}{mapping}{types}
1219\indexiii{operations on}{dictionary}{type}
1220\stindex{del}
1221\bifuncindex{len}
Fred Drake9474d861999-02-12 22:05:33 +00001222\withsubitem{(dictionary method)}{
1223 \ttindex{clear()}
1224 \ttindex{copy()}
1225 \ttindex{has_key()}
Raymond Hettinger74c8e552003-09-12 00:02:37 +00001226 \ttindex{fromkeys()}
Fred Drake9474d861999-02-12 22:05:33 +00001227 \ttindex{items()}
1228 \ttindex{keys()}
1229 \ttindex{update()}
1230 \ttindex{values()}
Michael W. Hudson9c206152003-03-05 14:42:09 +00001231 \ttindex{get()}
1232 \ttindex{setdefault()}
1233 \ttindex{pop()}
1234 \ttindex{popitem()}
1235 \ttindex{iteritems()}
Raymond Hettinger0dfd7a92003-05-10 07:40:56 +00001236 \ttindex{iterkeys()}
Michael W. Hudson9c206152003-03-05 14:42:09 +00001237 \ttindex{itervalues()}}
Fred Drake9c5cc141999-06-10 22:37:34 +00001238
1239\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
1240 \lineiii{len(\var{a})}{the number of items in \var{a}}{}
1241 \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
Fred Drake1e75e172000-07-31 16:34:46 +00001242 \lineiii{\var{a}[\var{k}] = \var{v}}
1243 {set \code{\var{a}[\var{k}]} to \var{v}}
Fred Drake9c5cc141999-06-10 22:37:34 +00001244 {}
1245 \lineiii{del \var{a}[\var{k}]}
1246 {remove \code{\var{a}[\var{k}]} from \var{a}}
1247 {(1)}
1248 \lineiii{\var{a}.clear()}{remove all items from \code{a}}{}
1249 \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001250 \lineiii{\var{a}.has_key(\var{k})}
Raymond Hettinger6e13bcc2003-08-08 11:07:59 +00001251 {\code{True} if \var{a} has a key \var{k}, else \code{False}}
Fred Drake9c5cc141999-06-10 22:37:34 +00001252 {}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001253 \lineiii{\var{k} \code{in} \var{a}}
1254 {Equivalent to \var{a}.has_key(\var{k})}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001255 {(2)}
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001256 \lineiii{\var{k} not in \var{a}}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001257 {Equivalent to \code{not} \var{a}.has_key(\var{k})}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001258 {(2)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001259 \lineiii{\var{a}.items()}
1260 {a copy of \var{a}'s list of (\var{key}, \var{value}) pairs}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001261 {(3)}
Fred Drake4a6c5c52001-06-12 03:31:56 +00001262 \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001263 \lineiii{\var{a}.update(\var{b})}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001264 {\code{for \var{k} in \var{b}.keys(): \var{a}[\var{k}] = \var{b}[\var{k}]}}
Barry Warsawe9218a12001-06-26 20:32:59 +00001265 {}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001266 \lineiii{\var{a}.fromkeys(\var{seq}\optional{, \var{value}})}
1267 {Creates a new dictionary with keys from \var{seq} and values set to \var{value}}
1268 {(7)}
Fred Drake4a6c5c52001-06-12 03:31:56 +00001269 \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001270 \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})}
Fred Drake4cacec52001-04-21 05:56:06 +00001271 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
Fred Drake9c5cc141999-06-10 22:37:34 +00001272 else \var{x}}
Barry Warsawe9218a12001-06-26 20:32:59 +00001273 {(4)}
Guido van Rossum8141cf52000-08-08 16:15:49 +00001274 \lineiii{\var{a}.setdefault(\var{k}\optional{, \var{x}})}
Fred Drake4cacec52001-04-21 05:56:06 +00001275 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
Guido van Rossum8141cf52000-08-08 16:15:49 +00001276 else \var{x} (also setting it)}
Barry Warsawe9218a12001-06-26 20:32:59 +00001277 {(5)}
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001278 \lineiii{\var{a}.pop(\var{k}\optional{, \var{x}})}
1279 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
1280 else \var{x} (and remove k)}
1281 {(8)}
Guido van Rossumff63f202000-12-12 22:03:47 +00001282 \lineiii{\var{a}.popitem()}
1283 {remove and return an arbitrary (\var{key}, \var{value}) pair}
Barry Warsawe9218a12001-06-26 20:32:59 +00001284 {(6)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001285 \lineiii{\var{a}.iteritems()}
1286 {return an iterator over (\var{key}, \var{value}) pairs}
Fred Drake01777832002-08-19 21:58:58 +00001287 {(2), (3)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001288 \lineiii{\var{a}.iterkeys()}
1289 {return an iterator over the mapping's keys}
Fred Drake01777832002-08-19 21:58:58 +00001290 {(2), (3)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001291 \lineiii{\var{a}.itervalues()}
1292 {return an iterator over the mapping's values}
Fred Drake01777832002-08-19 21:58:58 +00001293 {(2), (3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001294\end{tableiii}
1295
Fred Drake64e3b431998-07-24 13:56:11 +00001296\noindent
1297Notes:
1298\begin{description}
Fred Drake9c5cc141999-06-10 22:37:34 +00001299\item[(1)] Raises a \exception{KeyError} exception if \var{k} is not
1300in the map.
Fred Drake64e3b431998-07-24 13:56:11 +00001301
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001302\item[(2)] \versionadded{2.2}
1303
1304\item[(3)] Keys and values are listed in random order. If
Fred Drake01777832002-08-19 21:58:58 +00001305\method{items()}, \method{keys()}, \method{values()},
1306\method{iteritems()}, \method{iterkeys()}, and \method{itervalues()}
1307are called with no intervening modifications to the dictionary, the
1308lists will directly correspond. This allows the creation of
1309\code{(\var{value}, \var{key})} pairs using \function{zip()}:
1310\samp{pairs = zip(\var{a}.values(), \var{a}.keys())}. The same
1311relationship holds for the \method{iterkeys()} and
1312\method{itervalues()} methods: \samp{pairs = zip(\var{a}.itervalues(),
1313\var{a}.iterkeys())} provides the same value for \code{pairs}.
1314Another way to create the same list is \samp{pairs = [(v, k) for (k,
1315v) in \var{a}.iteritems()]}.
Fred Drake64e3b431998-07-24 13:56:11 +00001316
Barry Warsawe9218a12001-06-26 20:32:59 +00001317\item[(4)] Never raises an exception if \var{k} is not in the map,
Fred Drake38e5d272000-04-03 20:13:55 +00001318instead it returns \var{x}. \var{x} is optional; when \var{x} is not
Fred Drake9c5cc141999-06-10 22:37:34 +00001319provided and \var{k} is not in the map, \code{None} is returned.
Guido van Rossum8141cf52000-08-08 16:15:49 +00001320
Barry Warsawe9218a12001-06-26 20:32:59 +00001321\item[(5)] \function{setdefault()} is like \function{get()}, except
Guido van Rossum8141cf52000-08-08 16:15:49 +00001322that if \var{k} is missing, \var{x} is both returned and inserted into
1323the dictionary as the value of \var{k}.
Guido van Rossumff63f202000-12-12 22:03:47 +00001324
Barry Warsawe9218a12001-06-26 20:32:59 +00001325\item[(6)] \function{popitem()} is useful to destructively iterate
Guido van Rossumff63f202000-12-12 22:03:47 +00001326over a dictionary, as often used in set algorithms.
Fred Drake64e3b431998-07-24 13:56:11 +00001327
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001328\item[(7)] \function{fromkeys()} is a class method that returns a
1329new dictionary. \var{value} defaults to \code{None}. \versionadded{2.3}
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001330
1331\item[(8)] \function{pop()} raises a \exception{KeyError} when no default
1332value is given and the key is not found. \versionadded{2.3}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001333\end{description}
1334
Fred Drake64e3b431998-07-24 13:56:11 +00001335
Fred Drake99de2182001-10-30 06:23:14 +00001336\subsection{File Objects
1337 \label{bltin-file-objects}}
Fred Drake64e3b431998-07-24 13:56:11 +00001338
Fred Drake99de2182001-10-30 06:23:14 +00001339File objects\obindex{file} are implemented using C's \code{stdio}
1340package and can be created with the built-in constructor
Tim Peters8f01b682002-03-12 03:04:44 +00001341\function{file()}\bifuncindex{file} described in section
Tim Peters003047a2001-10-30 05:54:04 +00001342\ref{built-in-funcs}, ``Built-in Functions.''\footnote{\function{file()}
1343is new in Python 2.2. The older built-in \function{open()} is an
1344alias for \function{file()}.}
Steve Holden1e4519f2002-06-14 09:16:40 +00001345File objects are also returned
Fred Drake907e76b2001-07-06 20:30:11 +00001346by some other built-in functions and methods, such as
Fred Drake4de96c22000-08-12 03:36:23 +00001347\function{os.popen()} and \function{os.fdopen()} and the
Fred Drake130072d1998-10-28 20:08:35 +00001348\method{makefile()} method of socket objects.
Fred Drake4de96c22000-08-12 03:36:23 +00001349\refstmodindex{os}
Fred Drake64e3b431998-07-24 13:56:11 +00001350\refbimodindex{socket}
1351
1352When a file operation fails for an I/O-related reason, the exception
Fred Drake84538cd1998-11-30 21:51:25 +00001353\exception{IOError} is raised. This includes situations where the
1354operation is not defined for some reason, like \method{seek()} on a tty
Fred Drake64e3b431998-07-24 13:56:11 +00001355device or writing a file opened for reading.
1356
1357Files have the following methods:
1358
1359
1360\begin{methoddesc}[file]{close}{}
Steve Holden1e4519f2002-06-14 09:16:40 +00001361 Close the file. A closed file cannot be read or written any more.
Fred Drakea776cea2000-11-06 20:17:37 +00001362 Any operation which requires that the file be open will raise a
1363 \exception{ValueError} after the file has been closed. Calling
Fred Drake752ba392000-09-19 15:18:51 +00001364 \method{close()} more than once is allowed.
Fred Drake64e3b431998-07-24 13:56:11 +00001365\end{methoddesc}
1366
1367\begin{methoddesc}[file]{flush}{}
Fred Drake752ba392000-09-19 15:18:51 +00001368 Flush the internal buffer, like \code{stdio}'s
1369 \cfunction{fflush()}. This may be a no-op on some file-like
1370 objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001371\end{methoddesc}
1372
Fred Drake64e3b431998-07-24 13:56:11 +00001373\begin{methoddesc}[file]{fileno}{}
Fred Drake752ba392000-09-19 15:18:51 +00001374 \index{file descriptor}
1375 \index{descriptor, file}
1376 Return the integer ``file descriptor'' that is used by the
1377 underlying implementation to request I/O operations from the
1378 operating system. This can be useful for other, lower level
Fred Drake907e76b2001-07-06 20:30:11 +00001379 interfaces that use file descriptors, such as the
1380 \refmodule{fcntl}\refbimodindex{fcntl} module or
Fred Drake0aa811c2001-10-20 04:24:09 +00001381 \function{os.read()} and friends. \note{File-like objects
Fred Drake907e76b2001-07-06 20:30:11 +00001382 which do not have a real file descriptor should \emph{not} provide
Fred Drake0aa811c2001-10-20 04:24:09 +00001383 this method!}
Fred Drake64e3b431998-07-24 13:56:11 +00001384\end{methoddesc}
1385
Guido van Rossum0fc01862002-08-06 17:01:28 +00001386\begin{methoddesc}[file]{isatty}{}
1387 Return \code{True} if the file is connected to a tty(-like) device, else
1388 \code{False}. \note{If a file-like object is not associated
1389 with a real file, this method should \emph{not} be implemented.}
1390\end{methoddesc}
1391
1392\begin{methoddesc}[file]{next}{}
Raymond Hettinger74c8e552003-09-12 00:02:37 +00001393A file object is its own iterator, for example \code{iter(\var{f})} returns
Guido van Rossum0fc01862002-08-06 17:01:28 +00001394\var{f} (unless \var{f} is closed). When a file is used as an
1395iterator, typically in a \keyword{for} loop (for example,
1396\code{for line in f: print line}), the \method{next()} method is
1397called repeatedly. This method returns the next input line, or raises
1398\exception{StopIteration} when \EOF{} is hit. In order to make a
1399\keyword{for} loop the most efficient way of looping over the lines of
1400a file (a very common operation), the \method{next()} method uses a
1401hidden read-ahead buffer. As a consequence of using a read-ahead
1402buffer, combining \method{next()} with other file methods (like
1403\method{readline()}) does not work right. However, using
1404\method{seek()} to reposition the file to an absolute position will
1405flush the read-ahead buffer.
1406\versionadded{2.3}
1407\end{methoddesc}
1408
Fred Drake64e3b431998-07-24 13:56:11 +00001409\begin{methoddesc}[file]{read}{\optional{size}}
1410 Read at most \var{size} bytes from the file (less if the read hits
Fred Drakef4cbada1999-04-14 14:31:53 +00001411 \EOF{} before obtaining \var{size} bytes). If the \var{size}
1412 argument is negative or omitted, read all data until \EOF{} is
1413 reached. The bytes are returned as a string object. An empty
1414 string is returned when \EOF{} is encountered immediately. (For
1415 certain files, like ttys, it makes sense to continue reading after
1416 an \EOF{} is hit.) Note that this method may call the underlying
1417 C function \cfunction{fread()} more than once in an effort to
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001418 acquire as close to \var{size} bytes as possible. Also note that
1419 when in non-blocking mode, less data than what was requested may
1420 be returned, even if no \var{size} parameter was given.
Fred Drake64e3b431998-07-24 13:56:11 +00001421\end{methoddesc}
1422
1423\begin{methoddesc}[file]{readline}{\optional{size}}
1424 Read one entire line from the file. A trailing newline character is
Fred Drakeea003fc1999-04-05 21:59:15 +00001425 kept in the string\footnote{
Steve Holden1e4519f2002-06-14 09:16:40 +00001426 The advantage of leaving the newline on is that
1427 returning an empty string is then an unambiguous \EOF{}
1428 indication. It is also possible (in cases where it might
1429 matter, for example, if you
Tim Peters8f01b682002-03-12 03:04:44 +00001430 want to make an exact copy of a file while scanning its lines)
Steve Holden1e4519f2002-06-14 09:16:40 +00001431 to tell whether the last line of a file ended in a newline
Fred Drake4de96c22000-08-12 03:36:23 +00001432 or not (yes this happens!).
1433 } (but may be absent when a file ends with an
Fred Drake64e3b431998-07-24 13:56:11 +00001434 incomplete line). If the \var{size} argument is present and
1435 non-negative, it is a maximum byte count (including the trailing
1436 newline) and an incomplete line may be returned.
Steve Holden1e4519f2002-06-14 09:16:40 +00001437 An empty string is returned \emph{only} when \EOF{} is encountered
Fred Drake0aa811c2001-10-20 04:24:09 +00001438 immediately. \note{Unlike \code{stdio}'s \cfunction{fgets()}, the
Fred Drake752ba392000-09-19 15:18:51 +00001439 returned string contains null characters (\code{'\e 0'}) if they
Fred Drake0aa811c2001-10-20 04:24:09 +00001440 occurred in the input.}
Fred Drake64e3b431998-07-24 13:56:11 +00001441\end{methoddesc}
1442
1443\begin{methoddesc}[file]{readlines}{\optional{sizehint}}
1444 Read until \EOF{} using \method{readline()} and return a list containing
1445 the lines thus read. If the optional \var{sizehint} argument is
Fred Drakec37b65e2001-11-28 07:26:15 +00001446 present, instead of reading up to \EOF, whole lines totalling
Fred Drake64e3b431998-07-24 13:56:11 +00001447 approximately \var{sizehint} bytes (possibly after rounding up to an
Fred Drake752ba392000-09-19 15:18:51 +00001448 internal buffer size) are read. Objects implementing a file-like
1449 interface may choose to ignore \var{sizehint} if it cannot be
1450 implemented, or cannot be implemented efficiently.
Fred Drake64e3b431998-07-24 13:56:11 +00001451\end{methoddesc}
1452
Guido van Rossum20ab9e92001-01-17 01:18:00 +00001453\begin{methoddesc}[file]{xreadlines}{}
Guido van Rossum0fc01862002-08-06 17:01:28 +00001454 This method returns the same thing as \code{iter(f)}.
Fred Drake82f93c62001-04-22 01:56:51 +00001455 \versionadded{2.1}
Guido van Rossum0fc01862002-08-06 17:01:28 +00001456 \deprecated{2.3}{Use \code{for line in file} instead.}
Guido van Rossum20ab9e92001-01-17 01:18:00 +00001457\end{methoddesc}
1458
Fred Drake64e3b431998-07-24 13:56:11 +00001459\begin{methoddesc}[file]{seek}{offset\optional{, whence}}
1460 Set the file's current position, like \code{stdio}'s \cfunction{fseek()}.
1461 The \var{whence} argument is optional and defaults to \code{0}
1462 (absolute file positioning); other values are \code{1} (seek
1463 relative to the current position) and \code{2} (seek relative to the
Fred Drake19ae7832001-01-04 05:16:39 +00001464 file's end). There is no return value. Note that if the file is
1465 opened for appending (mode \code{'a'} or \code{'a+'}), any
1466 \method{seek()} operations will be undone at the next write. If the
1467 file is only opened for writing in append mode (mode \code{'a'}),
1468 this method is essentially a no-op, but it remains useful for files
Martin v. Löwis849a9722003-10-18 09:38:01 +00001469 opened in append mode with reading enabled (mode \code{'a+'}). If the
1470 file is opened in text mode (mode \code{'t'}), only offsets returned
1471 by \method{tell()} are legal. Use of other offsets causes undefined
1472 behavior.
1473
1474 Note that not all file objects are seekable.
Fred Drake64e3b431998-07-24 13:56:11 +00001475\end{methoddesc}
1476
1477\begin{methoddesc}[file]{tell}{}
1478 Return the file's current position, like \code{stdio}'s
1479 \cfunction{ftell()}.
1480\end{methoddesc}
1481
1482\begin{methoddesc}[file]{truncate}{\optional{size}}
Tim Peters8f01b682002-03-12 03:04:44 +00001483 Truncate the file's size. If the optional \var{size} argument is
Fred Drake752ba392000-09-19 15:18:51 +00001484 present, the file is truncated to (at most) that size. The size
Tim Peters8f01b682002-03-12 03:04:44 +00001485 defaults to the current position. The current file position is
1486 not changed. Note that if a specified size exceeds the file's
1487 current size, the result is platform-dependent: possibilities
1488 include that file may remain unchanged, increase to the specified
1489 size as if zero-filled, or increase to the specified size with
1490 undefined new content.
Raymond Hettingerb67449d2003-09-08 18:52:18 +00001491 Availability: Windows, many \UNIX{} variants.
Fred Drake64e3b431998-07-24 13:56:11 +00001492\end{methoddesc}
1493
1494\begin{methoddesc}[file]{write}{str}
Fred Drake0aa811c2001-10-20 04:24:09 +00001495 Write a string to the file. There is no return value. Due to
Fred Drake3c48ef72001-01-09 22:47:46 +00001496 buffering, the string may not actually show up in the file until
1497 the \method{flush()} or \method{close()} method is called.
Fred Drake64e3b431998-07-24 13:56:11 +00001498\end{methoddesc}
1499
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001500\begin{methoddesc}[file]{writelines}{sequence}
1501 Write a sequence of strings to the file. The sequence can be any
1502 iterable object producing strings, typically a list of strings.
1503 There is no return value.
Fred Drake3c48ef72001-01-09 22:47:46 +00001504 (The name is intended to match \method{readlines()};
1505 \method{writelines()} does not add line separators.)
1506\end{methoddesc}
1507
Fred Drake64e3b431998-07-24 13:56:11 +00001508
Fred Drake038d2642001-09-22 04:34:48 +00001509Files support the iterator protocol. Each iteration returns the same
1510result as \code{\var{file}.readline()}, and iteration ends when the
1511\method{readline()} method returns an empty string.
1512
1513
Fred Drake752ba392000-09-19 15:18:51 +00001514File objects also offer a number of other interesting attributes.
1515These are not required for file-like objects, but should be
1516implemented if they make sense for the particular object.
Fred Drake64e3b431998-07-24 13:56:11 +00001517
1518\begin{memberdesc}[file]{closed}
Neal Norwitz6b353702002-04-09 18:15:00 +00001519bool indicating the current state of the file object. This is a
Fred Drake64e3b431998-07-24 13:56:11 +00001520read-only attribute; the \method{close()} method changes the value.
Fred Drake752ba392000-09-19 15:18:51 +00001521It may not be available on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001522\end{memberdesc}
1523
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001524\begin{memberdesc}[file]{encoding}
1525The encoding that this file uses. When Unicode strings are written
1526to a file, they will be converted to byte strings using this encoding.
1527In addition, when the file is connected to a terminal, the attribute
1528gives the encoding that the terminal is likely to use (that
1529information might be incorrect if the user has misconfigured the
1530terminal). The attribute is read-only and may not be present on
1531all file-like objects. It may also be \code{None}, in which case
1532the file uses the system default encoding for converting Unicode
1533strings.
1534
1535\versionadded{2.3}
1536\end{memberdesc}
1537
Fred Drake64e3b431998-07-24 13:56:11 +00001538\begin{memberdesc}[file]{mode}
1539The I/O mode for the file. If the file was created using the
1540\function{open()} built-in function, this will be the value of the
Fred Drake752ba392000-09-19 15:18:51 +00001541\var{mode} parameter. This is a read-only attribute and may not be
1542present on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001543\end{memberdesc}
1544
1545\begin{memberdesc}[file]{name}
1546If the file object was created using \function{open()}, the name of
1547the file. Otherwise, some string that indicates the source of the
1548file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only
Fred Drake752ba392000-09-19 15:18:51 +00001549attribute and may not be present on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001550\end{memberdesc}
1551
Michael W. Hudson9c206152003-03-05 14:42:09 +00001552\begin{memberdesc}[file]{newlines}
1553If Python was built with the \code{--with-universal-newlines} option
1554(the default) this read-only attribute exists, and for files opened in
1555universal newline read mode it keeps track of the types of newlines
1556encountered while reading the file. The values it can take are
1557\code{'\e r'}, \code{'\e n'}, \code{'\e r\e n'}, \code{None} (unknown,
1558no newlines read yet) or a tuple containing all the newline
1559types seen, to indicate that multiple
1560newline conventions were encountered. For files not opened in universal
1561newline read mode the value of this attribute will be \code{None}.
1562\end{memberdesc}
1563
Fred Drake64e3b431998-07-24 13:56:11 +00001564\begin{memberdesc}[file]{softspace}
1565Boolean that indicates whether a space character needs to be printed
1566before another value when using the \keyword{print} statement.
1567Classes that are trying to simulate a file object should also have a
1568writable \member{softspace} attribute, which should be initialized to
Fred Drake66571cc2000-09-09 03:30:34 +00001569zero. This will be automatic for most classes implemented in Python
1570(care may be needed for objects that override attribute access); types
1571implemented in C will have to provide a writable
1572\member{softspace} attribute.
Fred Drake0aa811c2001-10-20 04:24:09 +00001573\note{This attribute is not used to control the
Fred Drake51f53df2000-09-20 04:48:20 +00001574\keyword{print} statement, but to allow the implementation of
Fred Drake0aa811c2001-10-20 04:24:09 +00001575\keyword{print} to keep track of its internal state.}
Fred Drake64e3b431998-07-24 13:56:11 +00001576\end{memberdesc}
1577
Fred Drakea776cea2000-11-06 20:17:37 +00001578
Fred Drake99de2182001-10-30 06:23:14 +00001579\subsection{Other Built-in Types \label{typesother}}
1580
1581The interpreter supports several other kinds of objects.
1582Most of these support only one or two operations.
1583
1584
1585\subsubsection{Modules \label{typesmodules}}
1586
1587The only special operation on a module is attribute access:
1588\code{\var{m}.\var{name}}, where \var{m} is a module and \var{name}
1589accesses a name defined in \var{m}'s symbol table. Module attributes
1590can be assigned to. (Note that the \keyword{import} statement is not,
1591strictly speaking, an operation on a module object; \code{import
1592\var{foo}} does not require a module object named \var{foo} to exist,
1593rather it requires an (external) \emph{definition} for a module named
1594\var{foo} somewhere.)
1595
1596A special member of every module is \member{__dict__}.
1597This is the dictionary containing the module's symbol table.
1598Modifying this dictionary will actually change the module's symbol
1599table, but direct assignment to the \member{__dict__} attribute is not
1600possible (you can write \code{\var{m}.__dict__['a'] = 1}, which
1601defines \code{\var{m}.a} to be \code{1}, but you can't write
Raymond Hettinger0dfd7a92003-05-10 07:40:56 +00001602\code{\var{m}.__dict__ = \{\}}).
Fred Drake99de2182001-10-30 06:23:14 +00001603
1604Modules built into the interpreter are written like this:
1605\code{<module 'sys' (built-in)>}. If loaded from a file, they are
1606written as \code{<module 'os' from
1607'/usr/local/lib/python\shortversion/os.pyc'>}.
1608
1609
1610\subsubsection{Classes and Class Instances \label{typesobjects}}
1611\nodename{Classes and Instances}
1612
1613See chapters 3 and 7 of the \citetitle[../ref/ref.html]{Python
1614Reference Manual} for these.
1615
1616
1617\subsubsection{Functions \label{typesfunctions}}
1618
1619Function objects are created by function definitions. The only
1620operation on a function object is to call it:
1621\code{\var{func}(\var{argument-list})}.
1622
1623There are really two flavors of function objects: built-in functions
1624and user-defined functions. Both support the same operation (to call
1625the function), but the implementation is different, hence the
1626different object types.
1627
1628The implementation adds two special read-only attributes:
1629\code{\var{f}.func_code} is a function's \dfn{code
1630object}\obindex{code} (see below) and \code{\var{f}.func_globals} is
1631the dictionary used as the function's global namespace (this is the
1632same as \code{\var{m}.__dict__} where \var{m} is the module in which
1633the function \var{f} was defined).
1634
1635Function objects also support getting and setting arbitrary
Raymond Hettinger74c8e552003-09-12 00:02:37 +00001636attributes, which can be used, for example, to attach metadata to
1637functions. Regular attribute dot-notation is used to get and set such
Fred Drake99de2182001-10-30 06:23:14 +00001638attributes. \emph{Note that the current implementation only supports
1639function attributes on user-defined functions. Function attributes on
1640built-in functions may be supported in the future.}
1641
1642Functions have another special attribute \code{\var{f}.__dict__}
1643(a.k.a. \code{\var{f}.func_dict}) which contains the namespace used to
1644support function attributes. \code{__dict__} and \code{func_dict} can
1645be accessed directly or set to a dictionary object. A function's
1646dictionary cannot be deleted.
1647
1648\subsubsection{Methods \label{typesmethods}}
1649\obindex{method}
1650
1651Methods are functions that are called using the attribute notation.
1652There are two flavors: built-in methods (such as \method{append()} on
1653lists) and class instance methods. Built-in methods are described
1654with the types that support them.
1655
1656The implementation adds two special read-only attributes to class
1657instance methods: \code{\var{m}.im_self} is the object on which the
1658method operates, and \code{\var{m}.im_func} is the function
1659implementing the method. Calling \code{\var{m}(\var{arg-1},
1660\var{arg-2}, \textrm{\ldots}, \var{arg-n})} is completely equivalent to
1661calling \code{\var{m}.im_func(\var{m}.im_self, \var{arg-1},
1662\var{arg-2}, \textrm{\ldots}, \var{arg-n})}.
1663
1664Class instance methods are either \emph{bound} or \emph{unbound},
1665referring to whether the method was accessed through an instance or a
1666class, respectively. When a method is unbound, its \code{im_self}
1667attribute will be \code{None} and if called, an explicit \code{self}
1668object must be passed as the first argument. In this case,
1669\code{self} must be an instance of the unbound method's class (or a
1670subclass of that class), otherwise a \code{TypeError} is raised.
1671
1672Like function objects, methods objects support getting
1673arbitrary attributes. However, since method attributes are actually
1674stored on the underlying function object (\code{meth.im_func}),
1675setting method attributes on either bound or unbound methods is
1676disallowed. Attempting to set a method attribute results in a
1677\code{TypeError} being raised. In order to set a method attribute,
1678you need to explicitly set it on the underlying function object:
1679
1680\begin{verbatim}
1681class C:
1682 def method(self):
1683 pass
1684
1685c = C()
1686c.method.im_func.whoami = 'my name is c'
1687\end{verbatim}
1688
1689See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
1690information.
1691
1692
1693\subsubsection{Code Objects \label{bltin-code-objects}}
1694\obindex{code}
1695
1696Code objects are used by the implementation to represent
1697``pseudo-compiled'' executable Python code such as a function body.
1698They differ from function objects because they don't contain a
1699reference to their global execution environment. Code objects are
1700returned by the built-in \function{compile()} function and can be
1701extracted from function objects through their \member{func_code}
1702attribute.
1703\bifuncindex{compile}
1704\withsubitem{(function object attribute)}{\ttindex{func_code}}
1705
1706A code object can be executed or evaluated by passing it (instead of a
1707source string) to the \keyword{exec} statement or the built-in
1708\function{eval()} function.
1709\stindex{exec}
1710\bifuncindex{eval}
1711
1712See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
1713information.
1714
1715
1716\subsubsection{Type Objects \label{bltin-type-objects}}
1717
1718Type objects represent the various object types. An object's type is
1719accessed by the built-in function \function{type()}. There are no special
1720operations on types. The standard module \module{types} defines names
1721for all standard built-in types.
1722\bifuncindex{type}
1723\refstmodindex{types}
1724
1725Types are written like this: \code{<type 'int'>}.
1726
1727
1728\subsubsection{The Null Object \label{bltin-null-object}}
1729
1730This object is returned by functions that don't explicitly return a
1731value. It supports no special operations. There is exactly one null
1732object, named \code{None} (a built-in name).
1733
1734It is written as \code{None}.
1735
1736
1737\subsubsection{The Ellipsis Object \label{bltin-ellipsis-object}}
1738
1739This object is used by extended slice notation (see the
1740\citetitle[../ref/ref.html]{Python Reference Manual}). It supports no
1741special operations. There is exactly one ellipsis object, named
1742\constant{Ellipsis} (a built-in name).
1743
1744It is written as \code{Ellipsis}.
1745
Guido van Rossum77f6a652002-04-03 22:41:51 +00001746\subsubsection{Boolean Values}
1747
1748Boolean values are the two constant objects \code{False} and
1749\code{True}. They are used to represent truth values (although other
1750values can also be considered false or true). In numeric contexts
1751(for example when used as the argument to an arithmetic operator),
1752they behave like the integers 0 and 1, respectively. The built-in
1753function \function{bool()} can be used to cast any value to a Boolean,
1754if the value can be interpreted as a truth value (see section Truth
1755Value Testing above).
1756
1757They are written as \code{False} and \code{True}, respectively.
1758\index{False}
1759\index{True}
1760\indexii{Boolean}{values}
1761
Fred Drake99de2182001-10-30 06:23:14 +00001762
Fred Drake9474d861999-02-12 22:05:33 +00001763\subsubsection{Internal Objects \label{typesinternal}}
Fred Drake64e3b431998-07-24 13:56:11 +00001764
Fred Drake37f15741999-11-10 16:21:37 +00001765See the \citetitle[../ref/ref.html]{Python Reference Manual} for this
Fred Drake512bb722000-08-18 03:12:38 +00001766information. It describes stack frame objects, traceback objects, and
1767slice objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001768
1769
Fred Drake7a2f0661998-09-10 18:25:58 +00001770\subsection{Special Attributes \label{specialattrs}}
Fred Drake64e3b431998-07-24 13:56:11 +00001771
1772The implementation adds a few special read-only attributes to several
1773object types, where they are relevant:
1774
Fred Drakea776cea2000-11-06 20:17:37 +00001775\begin{memberdesc}[object]{__dict__}
1776A dictionary or other mapping object used to store an
Fred Drake7a2f0661998-09-10 18:25:58 +00001777object's (writable) attributes.
Fred Drakea776cea2000-11-06 20:17:37 +00001778\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001779
Fred Drakea776cea2000-11-06 20:17:37 +00001780\begin{memberdesc}[object]{__methods__}
Fred Drake35705512001-12-03 17:32:27 +00001781\deprecated{2.2}{Use the built-in function \function{dir()} to get a
1782list of an object's attributes. This attribute is no longer available.}
Fred Drakea776cea2000-11-06 20:17:37 +00001783\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001784
Fred Drakea776cea2000-11-06 20:17:37 +00001785\begin{memberdesc}[object]{__members__}
Fred Drake35705512001-12-03 17:32:27 +00001786\deprecated{2.2}{Use the built-in function \function{dir()} to get a
1787list of an object's attributes. This attribute is no longer available.}
Fred Drakea776cea2000-11-06 20:17:37 +00001788\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001789
Fred Drakea776cea2000-11-06 20:17:37 +00001790\begin{memberdesc}[instance]{__class__}
Fred Drake7a2f0661998-09-10 18:25:58 +00001791The class to which a class instance belongs.
Fred Drakea776cea2000-11-06 20:17:37 +00001792\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001793
Fred Drakea776cea2000-11-06 20:17:37 +00001794\begin{memberdesc}[class]{__bases__}
Fred Drake907e76b2001-07-06 20:30:11 +00001795The tuple of base classes of a class object. If there are no base
1796classes, this will be an empty tuple.
Fred Drakea776cea2000-11-06 20:17:37 +00001797\end{memberdesc}