blob: f9e6566fc1e72cf9b9a111242749e49d4e4c5c87 [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 Drake64e3b431998-07-24 13:56:11 +000020
21
Steve Holden1e4519f2002-06-14 09:16:40 +000022\subsection{Truth Value Testing} \label{truth}
Fred Drake64e3b431998-07-24 13:56:11 +000023
Fred Drake84538cd1998-11-30 21:51:25 +000024Any object can be tested for truth value, for use in an \keyword{if} or
25\keyword{while} condition or as operand of the Boolean operations below.
Fred Drake64e3b431998-07-24 13:56:11 +000026The following values are considered false:
27\stindex{if}
28\stindex{while}
29\indexii{truth}{value}
30\indexii{Boolean}{operations}
31\index{false}
32
Fred Drake64e3b431998-07-24 13:56:11 +000033\begin{itemize}
34
35\item \code{None}
Fred Drake442c7c72002-08-07 15:40:15 +000036 \withsubitem{(Built-in object)}{\ttindex{None}}
Fred Drake64e3b431998-07-24 13:56:11 +000037
Guido van Rossum77f6a652002-04-03 22:41:51 +000038\item \code{False}
Fred Drake442c7c72002-08-07 15:40:15 +000039 \withsubitem{(Built-in object)}{\ttindex{False}}
Guido van Rossum77f6a652002-04-03 22:41:51 +000040
Fred Drake38e5d272000-04-03 20:13:55 +000041\item zero of any numeric type, for example, \code{0}, \code{0L},
42 \code{0.0}, \code{0j}.
Fred Drake64e3b431998-07-24 13:56:11 +000043
Fred Drake38e5d272000-04-03 20:13:55 +000044\item any empty sequence, for example, \code{''}, \code{()}, \code{[]}.
Fred Drake64e3b431998-07-24 13:56:11 +000045
Fred Drake38e5d272000-04-03 20:13:55 +000046\item any empty mapping, for example, \code{\{\}}.
Fred Drake64e3b431998-07-24 13:56:11 +000047
48\item instances of user-defined classes, if the class defines a
Fred Drake442c7c72002-08-07 15:40:15 +000049 \method{__nonzero__()} or \method{__len__()} method, when that
50 method returns the integer zero or \class{bool} value
51 \code{False}.\footnote{Additional
Fred Drake3e59f722002-07-12 17:15:10 +000052information on these special methods may be found in the
53\citetitle[../ref/ref.html]{Python Reference Manual}.}
Fred Drake64e3b431998-07-24 13:56:11 +000054
55\end{itemize}
56
57All other values are considered true --- so objects of many types are
58always true.
59\index{true}
60
61Operations and built-in functions that have a Boolean result always
Guido van Rossum77f6a652002-04-03 22:41:51 +000062return \code{0} or \code{False} for false and \code{1} or \code{True}
63for true, unless otherwise stated. (Important exception: the Boolean
64operations \samp{or}\opindex{or} and \samp{and}\opindex{and} always
65return one of their operands.)
66\index{False}
67\index{True}
Fred Drake64e3b431998-07-24 13:56:11 +000068
Fred Drake7a2f0661998-09-10 18:25:58 +000069\subsection{Boolean Operations \label{boolean}}
Fred Drake64e3b431998-07-24 13:56:11 +000070
71These are the Boolean operations, ordered by ascending priority:
72\indexii{Boolean}{operations}
73
74\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake8c071d42001-01-26 20:48:35 +000075 \lineiii{\var{x} or \var{y}}
76 {if \var{x} is false, then \var{y}, else \var{x}}{(1)}
77 \lineiii{\var{x} and \var{y}}
78 {if \var{x} is false, then \var{x}, else \var{y}}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +000079 \hline
Fred Drake8c071d42001-01-26 20:48:35 +000080 \lineiii{not \var{x}}
Guido van Rossum77f6a652002-04-03 22:41:51 +000081 {if \var{x} is false, then \code{True}, else \code{False}}{(2)}
Fred Drake64e3b431998-07-24 13:56:11 +000082\end{tableiii}
83\opindex{and}
84\opindex{or}
85\opindex{not}
86
87\noindent
88Notes:
89
90\begin{description}
91
92\item[(1)]
93These only evaluate their second argument if needed for their outcome.
94
95\item[(2)]
Fred Drake38e5d272000-04-03 20:13:55 +000096\samp{not} has a lower priority than non-Boolean operators, so
97\code{not \var{a} == \var{b}} is interpreted as \code{not (\var{a} ==
98\var{b})}, and \code{\var{a} == not \var{b}} is a syntax error.
Fred Drake64e3b431998-07-24 13:56:11 +000099
100\end{description}
101
102
Fred Drake7a2f0661998-09-10 18:25:58 +0000103\subsection{Comparisons \label{comparisons}}
Fred Drake64e3b431998-07-24 13:56:11 +0000104
105Comparison operations are supported by all objects. They all have the
106same priority (which is higher than that of the Boolean operations).
Fred Drake38e5d272000-04-03 20:13:55 +0000107Comparisons can be chained arbitrarily; for example, \code{\var{x} <
108\var{y} <= \var{z}} is equivalent to \code{\var{x} < \var{y} and
109\var{y} <= \var{z}}, except that \var{y} is evaluated only once (but
110in both cases \var{z} is not evaluated at all when \code{\var{x} <
111\var{y}} is found to be false).
Fred Drake64e3b431998-07-24 13:56:11 +0000112\indexii{chaining}{comparisons}
113
114This table summarizes the comparison operations:
115
116\begin{tableiii}{c|l|c}{code}{Operation}{Meaning}{Notes}
117 \lineiii{<}{strictly less than}{}
118 \lineiii{<=}{less than or equal}{}
119 \lineiii{>}{strictly greater than}{}
120 \lineiii{>=}{greater than or equal}{}
121 \lineiii{==}{equal}{}
Fred Drake64e3b431998-07-24 13:56:11 +0000122 \lineiii{!=}{not equal}{(1)}
Fred Drake512bb722000-08-18 03:12:38 +0000123 \lineiii{<>}{not equal}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000124 \lineiii{is}{object identity}{}
125 \lineiii{is not}{negated object identity}{}
126\end{tableiii}
127\indexii{operator}{comparison}
128\opindex{==} % XXX *All* others have funny characters < ! >
129\opindex{is}
130\opindex{is not}
131
132\noindent
133Notes:
134
135\begin{description}
136
137\item[(1)]
138\code{<>} and \code{!=} are alternate spellings for the same operator.
Fred Drake38e5d272000-04-03 20:13:55 +0000139\code{!=} is the preferred spelling; \code{<>} is obsolescent.
Fred Drake64e3b431998-07-24 13:56:11 +0000140
141\end{description}
142
143Objects of different types, except different numeric types, never
144compare equal; such objects are ordered consistently but arbitrarily
145(so that sorting a heterogeneous array yields a consistent result).
Fred Drake38e5d272000-04-03 20:13:55 +0000146Furthermore, some types (for example, file objects) support only a
147degenerate notion of comparison where any two objects of that type are
148unequal. Again, such objects are ordered arbitrarily but
Steve Holden1e4519f2002-06-14 09:16:40 +0000149consistently. The \code{<}, \code{<=}, \code{>} and \code{>=}
150operators will raise a \exception{TypeError} exception when any operand
151is a complex number.
Fred Drake38e5d272000-04-03 20:13:55 +0000152\indexii{object}{numeric}
Fred Drake64e3b431998-07-24 13:56:11 +0000153\indexii{objects}{comparing}
154
Fred Drake38e5d272000-04-03 20:13:55 +0000155Instances of a class normally compare as non-equal unless the class
156\withsubitem{(instance method)}{\ttindex{__cmp__()}}
Fred Drake66571cc2000-09-09 03:30:34 +0000157defines the \method{__cmp__()} method. Refer to the
158\citetitle[../ref/customization.html]{Python Reference Manual} for
159information on the use of this method to effect object comparisons.
Fred Drake64e3b431998-07-24 13:56:11 +0000160
Fred Drake38e5d272000-04-03 20:13:55 +0000161\strong{Implementation note:} Objects of different types except
162numbers are ordered by their type names; objects of the same types
163that don't support proper comparison are ordered by their address.
164
165Two more operations with the same syntactic priority,
166\samp{in}\opindex{in} and \samp{not in}\opindex{not in}, are supported
167only by sequence types (below).
Fred Drake64e3b431998-07-24 13:56:11 +0000168
169
Fred Drake7a2f0661998-09-10 18:25:58 +0000170\subsection{Numeric Types \label{typesnumeric}}
Fred Drake64e3b431998-07-24 13:56:11 +0000171
Guido van Rossum77f6a652002-04-03 22:41:51 +0000172There are four distinct numeric types: \dfn{plain integers},
173\dfn{long integers},
Fred Drake64e3b431998-07-24 13:56:11 +0000174\dfn{floating point numbers}, and \dfn{complex numbers}.
Guido van Rossum77f6a652002-04-03 22:41:51 +0000175In addition, Booleans are a subtype of plain integers.
Fred Drake64e3b431998-07-24 13:56:11 +0000176Plain integers (also just called \dfn{integers})
Fred Drake38e5d272000-04-03 20:13:55 +0000177are implemented using \ctype{long} in C, which gives them at least 32
Fred Drake64e3b431998-07-24 13:56:11 +0000178bits of precision. Long integers have unlimited precision. Floating
Fred Drake38e5d272000-04-03 20:13:55 +0000179point numbers are implemented using \ctype{double} in C. All bets on
Fred Drake64e3b431998-07-24 13:56:11 +0000180their precision are off unless you happen to know the machine you are
181working with.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000182\obindex{numeric}
Guido van Rossum77f6a652002-04-03 22:41:51 +0000183\obindex{Boolean}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000184\obindex{integer}
185\obindex{long integer}
186\obindex{floating point}
187\obindex{complex number}
Fred Drake38e5d272000-04-03 20:13:55 +0000188\indexii{C}{language}
Fred Drake64e3b431998-07-24 13:56:11 +0000189
Steve Holden1e4519f2002-06-14 09:16:40 +0000190Complex numbers have a real and imaginary part, which are each
Fred Drake38e5d272000-04-03 20:13:55 +0000191implemented using \ctype{double} in C. To extract these parts from
Tim Peters8f01b682002-03-12 03:04:44 +0000192a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}.
Fred Drake64e3b431998-07-24 13:56:11 +0000193
194Numbers are created by numeric literals or as the result of built-in
195functions and operators. Unadorned integer literals (including hex
Steve Holden1e4519f2002-06-14 09:16:40 +0000196and octal numbers) yield plain integers unless the value they denote
197is too large to be represented as a plain integer, in which case
198they yield a long integer. Integer literals with an
Fred Drake38e5d272000-04-03 20:13:55 +0000199\character{L} or \character{l} suffix yield long integers
200(\character{L} is preferred because \samp{1l} looks too much like
201eleven!). Numeric literals containing a decimal point or an exponent
202sign yield floating point numbers. Appending \character{j} or
Steve Holden1e4519f2002-06-14 09:16:40 +0000203\character{J} to a numeric literal yields a complex number with a
204zero real part. A complex numeric literal is the sum of a real and
205an imaginary part.
Fred Drake64e3b431998-07-24 13:56:11 +0000206\indexii{numeric}{literals}
207\indexii{integer}{literals}
208\indexiii{long}{integer}{literals}
209\indexii{floating point}{literals}
210\indexii{complex number}{literals}
211\indexii{hexadecimal}{literals}
212\indexii{octal}{literals}
213
214Python fully supports mixed arithmetic: when a binary arithmetic
215operator has operands of different numeric types, the operand with the
Steve Holden1e4519f2002-06-14 09:16:40 +0000216``narrower'' type is widened to that of the other, where plain
217integer is narrower than long integer is narrower than floating point is
218narrower than complex.
Fred Drakeea003fc1999-04-05 21:59:15 +0000219Comparisons between numbers of mixed type use the same rule.\footnote{
220 As a consequence, the list \code{[1, 2]} is considered equal
Steve Holden1e4519f2002-06-14 09:16:40 +0000221 to \code{[1.0, 2.0]}, and similarly for tuples.
222} The constructors \function{int()}, \function{long()}, \function{float()},
Fred Drake84538cd1998-11-30 21:51:25 +0000223and \function{complex()} can be used
Steve Holden1e4519f2002-06-14 09:16:40 +0000224to produce numbers of a specific type.
Fred Drake64e3b431998-07-24 13:56:11 +0000225\index{arithmetic}
226\bifuncindex{int}
227\bifuncindex{long}
228\bifuncindex{float}
229\bifuncindex{complex}
230
Michael W. Hudson9c206152003-03-05 14:42:09 +0000231All numeric types (except complex) support the following operations,
232sorted by ascending priority (operations in the same box have the same
Fred Drake64e3b431998-07-24 13:56:11 +0000233priority; all numeric operations have a higher priority than
234comparison operations):
235
236\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
237 \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{}
238 \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{}
239 \hline
240 \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{}
241 \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(1)}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000242 \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000243 \hline
244 \lineiii{-\var{x}}{\var{x} negated}{}
245 \lineiii{+\var{x}}{\var{x} unchanged}{}
246 \hline
247 \lineiii{abs(\var{x})}{absolute value or magnitude of \var{x}}{}
248 \lineiii{int(\var{x})}{\var{x} converted to integer}{(2)}
249 \lineiii{long(\var{x})}{\var{x} converted to long integer}{(2)}
250 \lineiii{float(\var{x})}{\var{x} converted to floating point}{}
251 \lineiii{complex(\var{re},\var{im})}{a complex number with real part \var{re}, imaginary part \var{im}. \var{im} defaults to zero.}{}
Fred Drake26b698f1999-02-12 18:27:31 +0000252 \lineiii{\var{c}.conjugate()}{conjugate of the complex number \var{c}}{}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000253 \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 +0000254 \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{}
255 \lineiii{\var{x} ** \var{y}}{\var{x} to the power \var{y}}{}
256\end{tableiii}
257\indexiii{operations on}{numeric}{types}
Fred Drake26b698f1999-02-12 18:27:31 +0000258\withsubitem{(complex number method)}{\ttindex{conjugate()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000259
260\noindent
261Notes:
262\begin{description}
263
264\item[(1)]
265For (plain or long) integer division, the result is an integer.
Tim Peters8f01b682002-03-12 03:04:44 +0000266The result is always rounded towards minus infinity: 1/2 is 0,
Fred Drake38e5d272000-04-03 20:13:55 +0000267(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result
268is a long integer if either operand is a long integer, regardless of
269the numeric value.
Fred Drake64e3b431998-07-24 13:56:11 +0000270\indexii{integer}{division}
271\indexiii{long}{integer}{division}
272
273\item[(2)]
274Conversion from floating point to (long or plain) integer may round or
Fred Drake4de96c22000-08-12 03:36:23 +0000275truncate as in C; see functions \function{floor()} and
276\function{ceil()} in the \refmodule{math}\refbimodindex{math} module
277for well-defined conversions.
Fred Drake9474d861999-02-12 22:05:33 +0000278\withsubitem{(in module math)}{\ttindex{floor()}\ttindex{ceil()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000279\indexii{numeric}{conversions}
Fred Drake4de96c22000-08-12 03:36:23 +0000280\indexii{C}{language}
Fred Drake64e3b431998-07-24 13:56:11 +0000281
282\item[(3)]
Fred Drake38e5d272000-04-03 20:13:55 +0000283See section \ref{built-in-funcs}, ``Built-in Functions,'' for a full
284description.
Fred Drake64e3b431998-07-24 13:56:11 +0000285
Michael W. Hudson9c206152003-03-05 14:42:09 +0000286\item[(4)]
287Complex floor division operator, modulo operator, and \function{divmod()}.
288
289\deprecated{2.3}{Instead convert to float using \function{abs()}
290if appropriate.}
291
Fred Drake64e3b431998-07-24 13:56:11 +0000292\end{description}
293% XXXJH exceptions: overflow (when? what operations?) zerodivision
294
Fred Drake4e7c2051999-02-19 15:30:25 +0000295\subsubsection{Bit-string Operations on Integer Types \label{bitstring-ops}}
Fred Drake64e3b431998-07-24 13:56:11 +0000296\nodename{Bit-string Operations}
297
298Plain and long integer types support additional operations that make
299sense only for bit-strings. Negative numbers are treated as their 2's
300complement value (for long integers, this assumes a sufficiently large
301number of bits that no overflow occurs during the operation).
302
303The priorities of the binary bit-wise operations are all lower than
304the numeric operations and higher than the comparisons; the unary
305operation \samp{\~} has the same priority as the other unary numeric
306operations (\samp{+} and \samp{-}).
307
308This table lists the bit-string operations sorted in ascending
309priority (operations in the same box have the same priority):
310
311\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
312 \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{}
313 \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{}
314 \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{}
315 \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{(1), (2)}
316 \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{(1), (3)}
317 \hline
318 \lineiii{\~\var{x}}{the bits of \var{x} inverted}{}
319\end{tableiii}
320\indexiii{operations on}{integer}{types}
321\indexii{bit-string}{operations}
322\indexii{shifting}{operations}
323\indexii{masking}{operations}
324
325\noindent
326Notes:
327\begin{description}
328\item[(1)] Negative shift counts are illegal and cause a
329\exception{ValueError} to be raised.
330\item[(2)] A left shift by \var{n} bits is equivalent to
331multiplication by \code{pow(2, \var{n})} without overflow check.
332\item[(3)] A right shift by \var{n} bits is equivalent to
333division by \code{pow(2, \var{n})} without overflow check.
334\end{description}
335
336
Fred Drake93656e72001-05-02 20:18:03 +0000337\subsection{Iterator Types \label{typeiter}}
338
Fred Drakef42cc452001-05-03 04:39:10 +0000339\versionadded{2.2}
Fred Drake93656e72001-05-02 20:18:03 +0000340\index{iterator protocol}
341\index{protocol!iterator}
342\index{sequence!iteration}
343\index{container!iteration over}
344
345Python supports a concept of iteration over containers. This is
346implemented using two distinct methods; these are used to allow
347user-defined classes to support iteration. Sequences, described below
348in more detail, always support the iteration methods.
349
350One method needs to be defined for container objects to provide
351iteration support:
352
353\begin{methoddesc}[container]{__iter__}{}
Greg Ward54f65092001-07-26 21:01:21 +0000354 Return an iterator object. The object is required to support the
Fred Drake93656e72001-05-02 20:18:03 +0000355 iterator protocol described below. If a container supports
356 different types of iteration, additional methods can be provided to
357 specifically request iterators for those iteration types. (An
358 example of an object supporting multiple forms of iteration would be
359 a tree structure which supports both breadth-first and depth-first
360 traversal.) This method corresponds to the \member{tp_iter} slot of
361 the type structure for Python objects in the Python/C API.
362\end{methoddesc}
363
364The iterator objects themselves are required to support the following
365two methods, which together form the \dfn{iterator protocol}:
366
367\begin{methoddesc}[iterator]{__iter__}{}
368 Return the iterator object itself. This is required to allow both
369 containers and iterators to be used with the \keyword{for} and
370 \keyword{in} statements. This method corresponds to the
371 \member{tp_iter} slot of the type structure for Python objects in
372 the Python/C API.
373\end{methoddesc}
374
Fred Drakef42cc452001-05-03 04:39:10 +0000375\begin{methoddesc}[iterator]{next}{}
Fred Drake93656e72001-05-02 20:18:03 +0000376 Return the next item from the container. If there are no further
377 items, raise the \exception{StopIteration} exception. This method
378 corresponds to the \member{tp_iternext} slot of the type structure
379 for Python objects in the Python/C API.
380\end{methoddesc}
381
382Python defines several iterator objects to support iteration over
383general and specific sequence types, dictionaries, and other more
384specialized forms. The specific types are not important beyond their
385implementation of the iterator protocol.
386
Guido van Rossum9534e142002-07-16 19:53:39 +0000387The intention of the protocol is that once an iterator's
388\method{next()} method raises \exception{StopIteration}, it will
389continue to do so on subsequent calls. Implementations that
390do not obey this property are deemed broken. (This constraint
391was added in Python 2.3; in Python 2.2, various iterators are
392broken according to this rule.)
393
Fred Drake93656e72001-05-02 20:18:03 +0000394
Fred Drake7a2f0661998-09-10 18:25:58 +0000395\subsection{Sequence Types \label{typesseq}}
Fred Drake64e3b431998-07-24 13:56:11 +0000396
Fred Drake107b9672000-08-14 15:37:59 +0000397There are six sequence types: strings, Unicode strings, lists,
Fred Drake512bb722000-08-18 03:12:38 +0000398tuples, buffers, and xrange objects.
Fred Drake64e3b431998-07-24 13:56:11 +0000399
Steve Holden1e4519f2002-06-14 09:16:40 +0000400String literals are written in single or double quotes:
Fred Drake38e5d272000-04-03 20:13:55 +0000401\code{'xyzzy'}, \code{"frobozz"}. See chapter 2 of the
Fred Drake4de96c22000-08-12 03:36:23 +0000402\citetitle[../ref/strings.html]{Python Reference Manual} for more about
403string literals. Unicode strings are much like strings, but are
404specified in the syntax using a preceeding \character{u} character:
405\code{u'abc'}, \code{u"def"}. Lists are constructed with square brackets,
Fred Drake37f15741999-11-10 16:21:37 +0000406separating items with commas: \code{[a, b, c]}. Tuples are
407constructed by the comma operator (not within square brackets), with
408or without enclosing parentheses, but an empty tuple must have the
409enclosing parentheses, e.g., \code{a, b, c} or \code{()}. A single
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000410item tuple must have a trailing comma, e.g., \code{(d,)}.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000411\obindex{sequence}
412\obindex{string}
413\obindex{Unicode}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000414\obindex{tuple}
415\obindex{list}
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000416
417Buffer objects are not directly supported by Python syntax, but can be
418created by calling the builtin function
Fred Drake36c2bd82002-09-24 15:32:04 +0000419\function{buffer()}.\bifuncindex{buffer} They don't support
Steve Holden1e4519f2002-06-14 09:16:40 +0000420concatenation or repetition.
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000421\obindex{buffer}
422
423Xrange objects are similar to buffers in that there is no specific
Steve Holden1e4519f2002-06-14 09:16:40 +0000424syntax to create them, but they are created using the \function{xrange()}
425function.\bifuncindex{xrange} They don't support slicing,
426concatenation or repetition, and using \code{in}, \code{not in},
427\function{min()} or \function{max()} on them is inefficient.
Fred Drake0b4e25d2000-10-04 04:21:19 +0000428\obindex{xrange}
Fred Drake64e3b431998-07-24 13:56:11 +0000429
Guido van Rossum5fe2c132001-07-05 15:27:19 +0000430Most sequence types support the following operations. The \samp{in} and
Fred Drake64e3b431998-07-24 13:56:11 +0000431\samp{not in} operations have the same priorities as the comparison
432operations. The \samp{+} and \samp{*} operations have the same
433priority as the corresponding numeric operations.\footnote{They must
434have since the parser can't tell the type of the operands.}
435
436This table lists the sequence operations sorted in ascending priority
437(operations in the same box have the same priority). In the table,
438\var{s} and \var{t} are sequences of the same type; \var{n}, \var{i}
439and \var{j} are integers:
440
441\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Barry Warsaw817918c2002-08-06 16:58:21 +0000442 \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 +0000443 \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is
Barry Warsaw817918c2002-08-06 16:58:21 +0000444equal to \var{x}, else \code{1}}{(1)}
Fred Drake64e3b431998-07-24 13:56:11 +0000445 \hline
446 \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
Barry Warsaw817918c2002-08-06 16:58:21 +0000447 \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 +0000448 \hline
Barry Warsaw817918c2002-08-06 16:58:21 +0000449 \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(3)}
450 \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 +0000451 \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 +0000452 \hline
453 \lineiii{len(\var{s})}{length of \var{s}}{}
454 \lineiii{min(\var{s})}{smallest item of \var{s}}{}
455 \lineiii{max(\var{s})}{largest item of \var{s}}{}
456\end{tableiii}
457\indexiii{operations on}{sequence}{types}
458\bifuncindex{len}
459\bifuncindex{min}
460\bifuncindex{max}
461\indexii{concatenation}{operation}
462\indexii{repetition}{operation}
463\indexii{subscript}{operation}
464\indexii{slice}{operation}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000465\indexii{extended slice}{operation}
Fred Drake64e3b431998-07-24 13:56:11 +0000466\opindex{in}
467\opindex{not in}
468
469\noindent
470Notes:
471
472\begin{description}
Barry Warsaw817918c2002-08-06 16:58:21 +0000473\item[(1)] When \var{s} is a string or Unicode string object the
474\code{in} and \code{not in} operations act like a substring test. In
475Python versions before 2.3, \var{x} had to be a string of length 1.
476In Python 2.3 and beyond, \var{x} may be a string of any length.
477
478\item[(2)] Values of \var{n} less than \code{0} are treated as
Fred Drake38e5d272000-04-03 20:13:55 +0000479 \code{0} (which yields an empty sequence of the same type as
Fred Draked800cff2001-08-28 14:56:05 +0000480 \var{s}). Note also that the copies are shallow; nested structures
481 are not copied. This often haunts new Python programmers; consider:
482
483\begin{verbatim}
484>>> lists = [[]] * 3
485>>> lists
486[[], [], []]
487>>> lists[0].append(3)
488>>> lists
489[[3], [3], [3]]
490\end{verbatim}
491
492 What has happened is that \code{lists} is a list containing three
493 copies of the list \code{[[]]} (a one-element list containing an
494 empty list), but the contained list is shared by each copy. You can
495 create a list of different lists this way:
496
497\begin{verbatim}
498>>> lists = [[] for i in range(3)]
499>>> lists[0].append(3)
500>>> lists[1].append(5)
501>>> lists[2].append(7)
502>>> lists
503[[3], [5], [7]]
504\end{verbatim}
Fred Drake38e5d272000-04-03 20:13:55 +0000505
Barry Warsaw817918c2002-08-06 16:58:21 +0000506\item[(3)] If \var{i} or \var{j} is negative, the index is relative to
Fred Drake907e76b2001-07-06 20:30:11 +0000507 the end of the string: \code{len(\var{s}) + \var{i}} or
Fred Drake64e3b431998-07-24 13:56:11 +0000508 \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is
509 still \code{0}.
Tim Peters8f01b682002-03-12 03:04:44 +0000510
Barry Warsaw817918c2002-08-06 16:58:21 +0000511\item[(4)] The slice of \var{s} from \var{i} to \var{j} is defined as
Fred Drake64e3b431998-07-24 13:56:11 +0000512 the sequence of items with index \var{k} such that \code{\var{i} <=
513 \var{k} < \var{j}}. If \var{i} or \var{j} is greater than
514 \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted,
515 use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If
516 \var{i} is greater than or equal to \var{j}, the slice is empty.
Michael W. Hudson9c206152003-03-05 14:42:09 +0000517
518\item[(5)] The slice of \var{s} from \var{i} to \var{j} with step
519 \var{k} is defined as the sequence of items with index
520 \code{\var{x} = \var{i} + \var{n}*\var{k}} such that \code{0}
521 \code{<=} \var{n} \code{<} \code{abs(i-j)}. If \var{i} or \var{j}
522 is greater than \code{len(\var{s})}, use \code{len(\var{s})}. If
523 \var{i} or \var{j} are ommitted then they become ``end'' values
524 (which end depends on the sign of \var{k}).
525
Fred Drake64e3b431998-07-24 13:56:11 +0000526\end{description}
527
Fred Drake9474d861999-02-12 22:05:33 +0000528
Fred Drake4de96c22000-08-12 03:36:23 +0000529\subsubsection{String Methods \label{string-methods}}
530
531These are the string methods which both 8-bit strings and Unicode
532objects support:
533
534\begin{methoddesc}[string]{capitalize}{}
535Return a copy of the string with only its first character capitalized.
536\end{methoddesc}
537
538\begin{methoddesc}[string]{center}{width}
539Return centered in a string of length \var{width}. Padding is done
540using spaces.
541\end{methoddesc}
542
543\begin{methoddesc}[string]{count}{sub\optional{, start\optional{, end}}}
544Return the number of occurrences of substring \var{sub} in string
545S\code{[\var{start}:\var{end}]}. Optional arguments \var{start} and
546\var{end} are interpreted as in slice notation.
547\end{methoddesc}
548
Fred Drake6048ce92001-12-10 16:43:08 +0000549\begin{methoddesc}[string]{decode}{\optional{encoding\optional{, errors}}}
550Decodes the string using the codec registered for \var{encoding}.
551\var{encoding} defaults to the default string encoding. \var{errors}
552may be given to set a different error handling scheme. The default is
553\code{'strict'}, meaning that encoding errors raise
554\exception{ValueError}. Other possible values are \code{'ignore'} and
555\code{replace'}.
556\versionadded{2.2}
557\end{methoddesc}
558
Fred Drake4de96c22000-08-12 03:36:23 +0000559\begin{methoddesc}[string]{encode}{\optional{encoding\optional{,errors}}}
560Return an encoded version of the string. Default encoding is the current
561default string encoding. \var{errors} may be given to set a different
562error handling scheme. The default for \var{errors} is
563\code{'strict'}, meaning that encoding errors raise a
564\exception{ValueError}. Other possible values are \code{'ignore'} and
565\code{'replace'}.
Fred Drake1dba66c2000-10-25 21:03:55 +0000566\versionadded{2.0}
Fred Drake4de96c22000-08-12 03:36:23 +0000567\end{methoddesc}
568
569\begin{methoddesc}[string]{endswith}{suffix\optional{, start\optional{, end}}}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000570Return \code{True} if the string ends with the specified \var{suffix},
571otherwise return \code{False}. With optional \var{start}, test beginning at
Fred Drake4de96c22000-08-12 03:36:23 +0000572that position. With optional \var{end}, stop comparing at that position.
573\end{methoddesc}
574
575\begin{methoddesc}[string]{expandtabs}{\optional{tabsize}}
576Return a copy of the string where all tab characters are expanded
577using spaces. If \var{tabsize} is not given, a tab size of \code{8}
578characters is assumed.
579\end{methoddesc}
580
581\begin{methoddesc}[string]{find}{sub\optional{, start\optional{, end}}}
582Return the lowest index in the string where substring \var{sub} is
583found, such that \var{sub} is contained in the range [\var{start},
584\var{end}). Optional arguments \var{start} and \var{end} are
585interpreted as in slice notation. Return \code{-1} if \var{sub} is
586not found.
587\end{methoddesc}
588
589\begin{methoddesc}[string]{index}{sub\optional{, start\optional{, end}}}
590Like \method{find()}, but raise \exception{ValueError} when the
591substring is not found.
592\end{methoddesc}
593
594\begin{methoddesc}[string]{isalnum}{}
595Return true if all characters in the string are alphanumeric and there
596is at least one character, false otherwise.
597\end{methoddesc}
598
599\begin{methoddesc}[string]{isalpha}{}
600Return true if all characters in the string are alphabetic and there
601is at least one character, false otherwise.
602\end{methoddesc}
603
604\begin{methoddesc}[string]{isdigit}{}
605Return true if there are only digit characters, false otherwise.
606\end{methoddesc}
607
608\begin{methoddesc}[string]{islower}{}
609Return true if all cased characters in the string are lowercase and
610there is at least one cased character, false otherwise.
611\end{methoddesc}
612
613\begin{methoddesc}[string]{isspace}{}
614Return true if there are only whitespace characters in the string and
615the string is not empty, false otherwise.
616\end{methoddesc}
617
618\begin{methoddesc}[string]{istitle}{}
Fred Drake907e76b2001-07-06 20:30:11 +0000619Return true if the string is a titlecased string: uppercase
Fred Drake4de96c22000-08-12 03:36:23 +0000620characters may only follow uncased characters and lowercase characters
621only cased ones. Return false otherwise.
622\end{methoddesc}
623
624\begin{methoddesc}[string]{isupper}{}
625Return true if all cased characters in the string are uppercase and
626there is at least one cased character, false otherwise.
627\end{methoddesc}
628
629\begin{methoddesc}[string]{join}{seq}
630Return a string which is the concatenation of the strings in the
631sequence \var{seq}. The separator between elements is the string
632providing this method.
633\end{methoddesc}
634
635\begin{methoddesc}[string]{ljust}{width}
636Return the string left justified in a string of length \var{width}.
637Padding is done using spaces. The original string is returned if
638\var{width} is less than \code{len(\var{s})}.
639\end{methoddesc}
640
641\begin{methoddesc}[string]{lower}{}
642Return a copy of the string converted to lowercase.
643\end{methoddesc}
644
Fred Drake8b1c47b2002-04-13 02:43:39 +0000645\begin{methoddesc}[string]{lstrip}{\optional{chars}}
646Return a copy of the string with leading characters removed. If
647\var{chars} is omitted or \code{None}, whitespace characters are
648removed. If given and not \code{None}, \var{chars} must be a string;
649the characters in the string will be stripped from the beginning of
650the string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000651\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000652\end{methoddesc}
653
654\begin{methoddesc}[string]{replace}{old, new\optional{, maxsplit}}
655Return a copy of the string with all occurrences of substring
656\var{old} replaced by \var{new}. If the optional argument
657\var{maxsplit} is given, only the first \var{maxsplit} occurrences are
658replaced.
659\end{methoddesc}
660
661\begin{methoddesc}[string]{rfind}{sub \optional{,start \optional{,end}}}
662Return the highest index in the string where substring \var{sub} is
663found, such that \var{sub} is contained within s[start,end]. Optional
664arguments \var{start} and \var{end} are interpreted as in slice
665notation. Return \code{-1} on failure.
666\end{methoddesc}
667
668\begin{methoddesc}[string]{rindex}{sub\optional{, start\optional{, end}}}
669Like \method{rfind()} but raises \exception{ValueError} when the
670substring \var{sub} is not found.
671\end{methoddesc}
672
673\begin{methoddesc}[string]{rjust}{width}
674Return the string right justified in a string of length \var{width}.
675Padding is done using spaces. The original string is returned if
676\var{width} is less than \code{len(\var{s})}.
677\end{methoddesc}
678
Fred Drake8b1c47b2002-04-13 02:43:39 +0000679\begin{methoddesc}[string]{rstrip}{\optional{chars}}
680Return a copy of the string with trailing characters removed. If
681\var{chars} is omitted or \code{None}, whitespace characters are
682removed. If given and not \code{None}, \var{chars} must be a string;
683the characters in the string will be stripped from the end of the
684string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000685\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000686\end{methoddesc}
687
688\begin{methoddesc}[string]{split}{\optional{sep \optional{,maxsplit}}}
689Return a list of the words in the string, using \var{sep} as the
690delimiter string. If \var{maxsplit} is given, at most \var{maxsplit}
691splits are done. If \var{sep} is not specified or \code{None}, any
692whitespace string is a separator.
693\end{methoddesc}
694
695\begin{methoddesc}[string]{splitlines}{\optional{keepends}}
696Return a list of the lines in the string, breaking at line
697boundaries. Line breaks are not included in the resulting list unless
698\var{keepends} is given and true.
699\end{methoddesc}
700
Fred Drake8b1c47b2002-04-13 02:43:39 +0000701\begin{methoddesc}[string]{startswith}{prefix\optional{,
702 start\optional{, end}}}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000703Return \code{True} if string starts with the \var{prefix}, otherwise
704return \code{False}. With optional \var{start}, test string beginning at
Fred Drake4de96c22000-08-12 03:36:23 +0000705that position. With optional \var{end}, stop comparing string at that
706position.
707\end{methoddesc}
708
Fred Drake8b1c47b2002-04-13 02:43:39 +0000709\begin{methoddesc}[string]{strip}{\optional{chars}}
710Return a copy of the string with leading and trailing characters
711removed. If \var{chars} is omitted or \code{None}, whitespace
712characters are removed. If given and not \code{None}, \var{chars}
713must be a string; the characters in the string will be stripped from
714the both ends of the string this method is called on.
Fred Drake91718012002-11-16 00:41:55 +0000715\versionchanged[Support for the \var{chars} argument]{2.2.2}
Fred Drake4de96c22000-08-12 03:36:23 +0000716\end{methoddesc}
717
718\begin{methoddesc}[string]{swapcase}{}
719Return a copy of the string with uppercase characters converted to
720lowercase and vice versa.
721\end{methoddesc}
722
723\begin{methoddesc}[string]{title}{}
Fred Drake907e76b2001-07-06 20:30:11 +0000724Return a titlecased version of the string: words start with uppercase
Fred Drake4de96c22000-08-12 03:36:23 +0000725characters, all remaining cased characters are lowercase.
726\end{methoddesc}
727
728\begin{methoddesc}[string]{translate}{table\optional{, deletechars}}
729Return a copy of the string where all characters occurring in the
730optional argument \var{deletechars} are removed, and the remaining
731characters have been mapped through the given translation table, which
732must be a string of length 256.
733\end{methoddesc}
734
735\begin{methoddesc}[string]{upper}{}
736Return a copy of the string converted to uppercase.
737\end{methoddesc}
738
Walter Dörwald068325e2002-04-15 13:36:47 +0000739\begin{methoddesc}[string]{zfill}{width}
740Return the numeric string left filled with zeros in a string
741of length \var{width}. The original string is returned if
742\var{width} is less than \code{len(\var{s})}.
Fred Drakee55bec22002-11-16 00:44:00 +0000743\versionadded{2.2.2}
Walter Dörwald068325e2002-04-15 13:36:47 +0000744\end{methoddesc}
745
Fred Drake4de96c22000-08-12 03:36:23 +0000746
747\subsubsection{String Formatting Operations \label{typesseq-strings}}
Fred Drake64e3b431998-07-24 13:56:11 +0000748
Fred Drakeb38784e2001-12-03 22:15:56 +0000749\index{formatting, string (\%{})}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000750\index{interpolation, string (\%{})}
Fred Drake66d32b12000-09-14 17:57:42 +0000751\index{string!formatting}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000752\index{string!interpolation}
Fred Drake66d32b12000-09-14 17:57:42 +0000753\index{printf-style formatting}
754\index{sprintf-style formatting}
Fred Drakeb38784e2001-12-03 22:15:56 +0000755\index{\protect\%{} formatting}
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000756\index{\protect\%{} interpolation}
Fred Drake66d32b12000-09-14 17:57:42 +0000757
Fred Drake8c071d42001-01-26 20:48:35 +0000758String and Unicode objects have one unique built-in operation: the
Fred Drakeab2dc1d2001-12-26 20:06:40 +0000759\code{\%} operator (modulo). This is also known as the string
760\emph{formatting} or \emph{interpolation} operator. Given
761\code{\var{format} \% \var{values}} (where \var{format} is a string or
762Unicode object), \code{\%} conversion specifications in \var{format}
763are replaced with zero or more elements of \var{values}. The effect
764is similar to the using \cfunction{sprintf()} in the C language. If
765\var{format} is a Unicode object, or if any of the objects being
766converted using the \code{\%s} conversion are Unicode objects, the
Steve Holden1e4519f2002-06-14 09:16:40 +0000767result will also be a Unicode object.
Fred Drake64e3b431998-07-24 13:56:11 +0000768
Fred Drake8c071d42001-01-26 20:48:35 +0000769If \var{format} requires a single argument, \var{values} may be a
Steve Holden1e4519f2002-06-14 09:16:40 +0000770single non-tuple object. \footnote{To format only a tuple you
771should therefore provide a singleton tuple whose only element
772is the tuple to be formatted.} Otherwise, \var{values} must be a tuple with
Fred Drake8c071d42001-01-26 20:48:35 +0000773exactly the number of items specified by the format string, or a
774single mapping object (for example, a dictionary).
Fred Drake64e3b431998-07-24 13:56:11 +0000775
Fred Drake8c071d42001-01-26 20:48:35 +0000776A conversion specifier contains two or more characters and has the
777following components, which must occur in this order:
778
779\begin{enumerate}
780 \item The \character{\%} character, which marks the start of the
781 specifier.
Steve Holden1e4519f2002-06-14 09:16:40 +0000782 \item Mapping key (optional), consisting of a parenthesised sequence
783 of characters (for example, \code{(somename)}).
Fred Drake8c071d42001-01-26 20:48:35 +0000784 \item Conversion flags (optional), which affect the result of some
785 conversion types.
786 \item Minimum field width (optional). If specified as an
787 \character{*} (asterisk), the actual width is read from the
788 next element of the tuple in \var{values}, and the object to
789 convert comes after the minimum field width and optional
790 precision.
791 \item Precision (optional), given as a \character{.} (dot) followed
792 by the precision. If specified as \character{*} (an
793 asterisk), the actual width is read from the next element of
794 the tuple in \var{values}, and the value to convert comes after
795 the precision.
796 \item Length modifier (optional).
797 \item Conversion type.
798\end{enumerate}
Fred Drake64e3b431998-07-24 13:56:11 +0000799
Steve Holden1e4519f2002-06-14 09:16:40 +0000800When the right argument is a dictionary (or other mapping type), then
801the formats in the string \emph{must} include a parenthesised mapping key into
Fred Drake8c071d42001-01-26 20:48:35 +0000802that dictionary inserted immediately after the \character{\%}
Steve Holden1e4519f2002-06-14 09:16:40 +0000803character. The mapping key selects the value to be formatted from the
Fred Drake8c071d42001-01-26 20:48:35 +0000804mapping. For example:
Fred Drake64e3b431998-07-24 13:56:11 +0000805
806\begin{verbatim}
Steve Holden1e4519f2002-06-14 09:16:40 +0000807>>> print '%(language)s has %(#)03d quote types.' % \
808 {'language': "Python", "#": 2}
Fred Drake64e3b431998-07-24 13:56:11 +0000809Python has 002 quote types.
810\end{verbatim}
811
812In this case no \code{*} specifiers may occur in a format (since they
813require a sequential parameter list).
814
Fred Drake8c071d42001-01-26 20:48:35 +0000815The conversion flag characters are:
816
817\begin{tableii}{c|l}{character}{Flag}{Meaning}
818 \lineii{\#}{The value conversion will use the ``alternate form''
819 (where defined below).}
Neal Norwitzf927f142003-02-17 18:57:06 +0000820 \lineii{0}{The conversion will be zero padded for numeric values.}
Fred Drake8c071d42001-01-26 20:48:35 +0000821 \lineii{-}{The converted value is left adjusted (overrides
Fred Drakef5968262002-10-25 16:55:51 +0000822 the \character{0} conversion if both are given).}
Fred Drake8c071d42001-01-26 20:48:35 +0000823 \lineii{{~}}{(a space) A blank should be left before a positive number
824 (or empty string) produced by a signed conversion.}
825 \lineii{+}{A sign character (\character{+} or \character{-}) will
826 precede the conversion (overrides a "space" flag).}
827\end{tableii}
828
829The length modifier may be \code{h}, \code{l}, and \code{L} may be
830present, but are ignored as they are not necessary for Python.
831
832The conversion types are:
833
Fred Drakef5968262002-10-25 16:55:51 +0000834\begin{tableiii}{c|l|c}{character}{Conversion}{Meaning}{Notes}
835 \lineiii{d}{Signed integer decimal.}{}
836 \lineiii{i}{Signed integer decimal.}{}
837 \lineiii{o}{Unsigned octal.}{(1)}
838 \lineiii{u}{Unsigned decimal.}{}
839 \lineiii{x}{Unsigned hexidecimal (lowercase).}{(2)}
840 \lineiii{X}{Unsigned hexidecimal (uppercase).}{(2)}
841 \lineiii{e}{Floating point exponential format (lowercase).}{}
842 \lineiii{E}{Floating point exponential format (uppercase).}{}
843 \lineiii{f}{Floating point decimal format.}{}
844 \lineiii{F}{Floating point decimal format.}{}
845 \lineiii{g}{Same as \character{e} if exponent is greater than -4 or
846 less than precision, \character{f} otherwise.}{}
847 \lineiii{G}{Same as \character{E} if exponent is greater than -4 or
848 less than precision, \character{F} otherwise.}{}
849 \lineiii{c}{Single character (accepts integer or single character
850 string).}{}
851 \lineiii{r}{String (converts any python object using
852 \function{repr()}).}{(3)}
853 \lineiii{s}{String (converts any python object using
Raymond Hettinger2bd15682003-01-13 04:29:19 +0000854 \function{str()}).}{(4)}
Fred Drakef5968262002-10-25 16:55:51 +0000855 \lineiii{\%}{No argument is converted, results in a \character{\%}
856 character in the result.}{}
857\end{tableiii}
858
859\noindent
860Notes:
861\begin{description}
862 \item[(1)]
863 The alternate form causes a leading zero (\character{0}) to be
864 inserted between left-hand padding and the formatting of the
865 number if the leading character of the result is not already a
866 zero.
867 \item[(2)]
868 The alternate form causes a leading \code{'0x'} or \code{'0X'}
869 (depending on whether the \character{x} or \character{X} format
870 was used) to be inserted between left-hand padding and the
871 formatting of the number if the leading character of the result is
872 not already a zero.
873 \item[(3)]
874 The \code{\%r} conversion was added in Python 2.0.
Raymond Hettinger2bd15682003-01-13 04:29:19 +0000875 \item[(4)]
876 If the object or format provided is a \class{unicode} string,
877 the resulting string will also be \class{unicode}.
Fred Drakef5968262002-10-25 16:55:51 +0000878\end{description}
Fred Drake8c071d42001-01-26 20:48:35 +0000879
880% XXX Examples?
881
Fred Drake8c071d42001-01-26 20:48:35 +0000882Since Python strings have an explicit length, \code{\%s} conversions
883do not assume that \code{'\e0'} is the end of the string.
884
885For safety reasons, floating point precisions are clipped to 50;
886\code{\%f} conversions for numbers whose absolute value is over 1e25
887are replaced by \code{\%g} conversions.\footnote{
888 These numbers are fairly arbitrary. They are intended to
889 avoid printing endless strings of meaningless digits without hampering
890 correct use and without having to know the exact precision of floating
891 point values on a particular machine.
892} All other errors raise exceptions.
893
Fred Drake14f5c5f2001-12-03 18:33:13 +0000894Additional string operations are defined in standard modules
895\refmodule{string}\refstmodindex{string} and
Tim Peters8f01b682002-03-12 03:04:44 +0000896\refmodule{re}.\refstmodindex{re}
Fred Drake64e3b431998-07-24 13:56:11 +0000897
Fred Drake107b9672000-08-14 15:37:59 +0000898
Fred Drake512bb722000-08-18 03:12:38 +0000899\subsubsection{XRange Type \label{typesseq-xrange}}
Fred Drake107b9672000-08-14 15:37:59 +0000900
Fred Drake0b4e25d2000-10-04 04:21:19 +0000901The xrange\obindex{xrange} type is an immutable sequence which is
Fred Drake512bb722000-08-18 03:12:38 +0000902commonly used for looping. The advantage of the xrange type is that an
903xrange object will always take the same amount of memory, no matter the
Fred Drake107b9672000-08-14 15:37:59 +0000904size of the range it represents. There are no consistent performance
905advantages.
906
Raymond Hettingerd2bef822002-12-11 07:14:03 +0000907XRange objects have very little behavior: they only support indexing,
908iteration, and the \function{len()} function.
Fred Drake107b9672000-08-14 15:37:59 +0000909
910
Fred Drake9474d861999-02-12 22:05:33 +0000911\subsubsection{Mutable Sequence Types \label{typesseq-mutable}}
Fred Drake64e3b431998-07-24 13:56:11 +0000912
913List objects support additional operations that allow in-place
914modification of the object.
Steve Holden1e4519f2002-06-14 09:16:40 +0000915Other mutable sequence types (when added to the language) should
916also support these operations.
917Strings and tuples are immutable sequence types: such objects cannot
Fred Drake64e3b431998-07-24 13:56:11 +0000918be modified once created.
919The following operations are defined on mutable sequence types (where
920\var{x} is an arbitrary object):
921\indexiii{mutable}{sequence}{types}
Fred Drake0b4e25d2000-10-04 04:21:19 +0000922\obindex{list}
Fred Drake64e3b431998-07-24 13:56:11 +0000923
924\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
925 \lineiii{\var{s}[\var{i}] = \var{x}}
926 {item \var{i} of \var{s} is replaced by \var{x}}{}
927 \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}}
928 {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{}
929 \lineiii{del \var{s}[\var{i}:\var{j}]}
930 {same as \code{\var{s}[\var{i}:\var{j}] = []}}{}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000931 \lineiii{\var{s}[\var{i}:\var{j}:\var{k}] = \var{t}}
932 {the elements of \code{\var{s}[\var{i}:\var{j}:\var{k}]} are replaced by those of \var{t}}{(1)}
933 \lineiii{del \var{s}[\var{i}:\var{j}:\var{k}]}
934 {removes the elements of \code{\var{s}[\var{i}:\var{j}:\var{k}]} from the list}{}
Fred Drake64e3b431998-07-24 13:56:11 +0000935 \lineiii{\var{s}.append(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000936 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{(2)}
Barry Warsawafd974c1998-10-09 16:39:58 +0000937 \lineiii{\var{s}.extend(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000938 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = \var{x}}}{(3)}
Fred Drake64e3b431998-07-24 13:56:11 +0000939 \lineiii{\var{s}.count(\var{x})}
940 {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{}
941 \lineiii{\var{s}.index(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000942 {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000943 \lineiii{\var{s}.insert(\var{i}, \var{x})}
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000944 {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}}{(5)}
Fred Drake64e3b431998-07-24 13:56:11 +0000945 \lineiii{\var{s}.pop(\optional{\var{i}})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000946 {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 +0000947 \lineiii{\var{s}.remove(\var{x})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000948 {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(4)}
Fred Drake64e3b431998-07-24 13:56:11 +0000949 \lineiii{\var{s}.reverse()}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000950 {reverses the items of \var{s} in place}{(7)}
Skip Montanaro4abd5f02003-01-02 20:51:08 +0000951 \lineiii{\var{s}.sort(\optional{\var{cmpfunc=None}})}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000952 {sort the items of \var{s} in place}{(7), (8), (9), (10)}
Fred Drake64e3b431998-07-24 13:56:11 +0000953\end{tableiii}
954\indexiv{operations on}{mutable}{sequence}{types}
955\indexiii{operations on}{sequence}{types}
956\indexiii{operations on}{list}{type}
957\indexii{subscript}{assignment}
958\indexii{slice}{assignment}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000959\indexii{extended slice}{assignment}
Fred Drake64e3b431998-07-24 13:56:11 +0000960\stindex{del}
Fred Drake9474d861999-02-12 22:05:33 +0000961\withsubitem{(list method)}{
Fred Drake68921df1999-08-09 17:05:12 +0000962 \ttindex{append()}\ttindex{extend()}\ttindex{count()}\ttindex{index()}
963 \ttindex{insert()}\ttindex{pop()}\ttindex{remove()}\ttindex{reverse()}
Fred Drakee8391991998-11-25 17:09:19 +0000964 \ttindex{sort()}}
Fred Drake64e3b431998-07-24 13:56:11 +0000965\noindent
966Notes:
967\begin{description}
Michael W. Hudson9c206152003-03-05 14:42:09 +0000968\item[(1)] \var{t} must have the same length as the slice it is
969 replacing.
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000970
Michael W. Hudson9c206152003-03-05 14:42:09 +0000971\item[(2)] The C implementation of Python has historically accepted
972 multiple parameters and implicitly joined them into a tuple; this
973 no longer works in Python 2.0. Use of this misfeature has been
974 deprecated since Python 1.4.
Fred Drake38e5d272000-04-03 20:13:55 +0000975
Michael W. Hudson9c206152003-03-05 14:42:09 +0000976\item[(3)] Raises an exception when \var{x} is not a list object. The
977 \method{extend()} method is experimental and not supported by
978 mutable sequence types other than lists.
979
980\item[(4)] Raises \exception{ValueError} when \var{x} is not found in
Fred Drake68921df1999-08-09 17:05:12 +0000981 \var{s}.
982
Michael W. Hudson9c206152003-03-05 14:42:09 +0000983\item[(5)] When a negative index is passed as the first parameter to
Guido van Rossum3a3cca52003-04-14 20:58:14 +0000984 the \method{insert()} method, the list length is added, as for slice
985 indices. If it is still negative, it is truncated to zero, as for
986 slice indices. \versionchanged[Previously, all negative indices
987 were truncated to zero]{2.3}
Fred Drakeef428a22001-10-26 18:57:14 +0000988
Michael W. Hudson9c206152003-03-05 14:42:09 +0000989\item[(6)] The \method{pop()} method is only supported by the list and
Fred Drakefbd3b452000-07-31 23:42:23 +0000990 array types. The optional argument \var{i} defaults to \code{-1},
991 so that by default the last item is removed and returned.
Fred Drake38e5d272000-04-03 20:13:55 +0000992
Michael W. Hudson9c206152003-03-05 14:42:09 +0000993\item[(7)] The \method{sort()} and \method{reverse()} methods modify the
Fred Drake38e5d272000-04-03 20:13:55 +0000994 list in place for economy of space when sorting or reversing a large
Skip Montanaro41d7d582001-07-25 16:18:19 +0000995 list. To remind you that they operate by side effect, they don't return
996 the sorted or reversed list.
Fred Drake38e5d272000-04-03 20:13:55 +0000997
Michael W. Hudson9c206152003-03-05 14:42:09 +0000998\item[(8)] The \method{sort()} method takes an optional argument
Fred Drake64e3b431998-07-24 13:56:11 +0000999 specifying a comparison function of two arguments (list items) which
Tim Peters599db7d2001-09-29 01:08:19 +00001000 should return a negative, zero or positive number depending on whether
Fred Drake68921df1999-08-09 17:05:12 +00001001 the first argument is considered smaller than, equal to, or larger
1002 than the second argument. Note that this slows the sorting process
Fred Drake4cee2202003-03-20 22:17:59 +00001003 down considerably; for example to sort a list in reverse order it is much
1004 faster to call \method{sort()} followed by \method{reverse()}
1005 than to use \method{sort()} with a comparison function that
Skip Montanaro4abd5f02003-01-02 20:51:08 +00001006 reverses the ordering of the elements. Passing \constant{None} as the
1007 comparison function is semantically equivalent to calling
1008 \method{sort()} with no comparison function.
Fred Drake4cee2202003-03-20 22:17:59 +00001009 \versionchanged[Support for \code{None} as an equivalent to omitting
1010 \var{cmpfunc} was added]{2.3}
1011
1012 As an example of using the \var{cmpfunc} argument to the
1013 \method{sort()} method, consider sorting a list of sequences by the
1014 second element of that list:
1015
1016\begin{verbatim}
1017def mycmp(a, b):
1018 return cmp(a[1], b[1])
1019
1020mylist.sort(mycmp)
1021\end{verbatim}
1022
1023 A more time-efficient approach for reasonably-sized data structures can
1024 often be used:
1025
1026\begin{verbatim}
1027tmplist = [(x[1], x) for x in mylist]
1028tmplist.sort()
1029mylist = [x for (key, x) in tmplist]
1030\end{verbatim}
Tim Peters74824582002-08-01 03:10:45 +00001031
Michael W. Hudson9c206152003-03-05 14:42:09 +00001032\item[(9)] Whether the \method{sort()} method is stable is not defined by
Tim Peters74824582002-08-01 03:10:45 +00001033 the language (a sort is stable if it guarantees not to change the
1034 relative order of elements that compare equal). In the C
1035 implementation of Python, sorts were stable only by accident through
1036 Python 2.2. The C implementation of Python 2.3 introduced a stable
1037 \method{sort()} method, but code that intends to be portable across
1038 implementations and versions must not rely on stability.
Tim Petersb9099c32002-11-12 22:08:10 +00001039
Michael W. Hudson9c206152003-03-05 14:42:09 +00001040\item[(10)] While a list is being sorted, the effect of attempting to
Tim Petersb9099c32002-11-12 22:08:10 +00001041 mutate, or even inspect, the list is undefined. The C implementation
1042 of Python 2.3 makes the list appear empty for the duration, and raises
1043 \exception{ValueError} if it can detect that the list has been
1044 mutated during a sort.
Fred Drake64e3b431998-07-24 13:56:11 +00001045\end{description}
1046
1047
Fred Drake7a2f0661998-09-10 18:25:58 +00001048\subsection{Mapping Types \label{typesmapping}}
Fred Drake0b4e25d2000-10-04 04:21:19 +00001049\obindex{mapping}
1050\obindex{dictionary}
Fred Drake64e3b431998-07-24 13:56:11 +00001051
Steve Holden1e4519f2002-06-14 09:16:40 +00001052A \dfn{mapping} object maps immutable values to
Fred Drake64e3b431998-07-24 13:56:11 +00001053arbitrary objects. Mappings are mutable objects. There is currently
1054only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are
Steve Holden1e4519f2002-06-14 09:16:40 +00001055almost arbitrary values. Only values containing lists, dictionaries
1056or other mutable types (that are compared by value rather than by
1057object identity) may not be used as keys.
Fred Drake64e3b431998-07-24 13:56:11 +00001058Numeric types used for keys obey the normal rules for numeric
1059comparison: if two numbers compare equal (e.g. \code{1} and
1060\code{1.0}) then they can be used interchangeably to index the same
1061dictionary entry.
1062
Fred Drake64e3b431998-07-24 13:56:11 +00001063Dictionaries are created by placing a comma-separated list of
1064\code{\var{key}: \var{value}} pairs within braces, for example:
1065\code{\{'jack': 4098, 'sjoerd': 4127\}} or
1066\code{\{4098: 'jack', 4127: 'sjoerd'\}}.
1067
Fred Drake9c5cc141999-06-10 22:37:34 +00001068The following operations are defined on mappings (where \var{a} and
1069\var{b} are mappings, \var{k} is a key, and \var{v} and \var{x} are
1070arbitrary objects):
Fred Drake64e3b431998-07-24 13:56:11 +00001071\indexiii{operations on}{mapping}{types}
1072\indexiii{operations on}{dictionary}{type}
1073\stindex{del}
1074\bifuncindex{len}
Fred Drake9474d861999-02-12 22:05:33 +00001075\withsubitem{(dictionary method)}{
1076 \ttindex{clear()}
1077 \ttindex{copy()}
1078 \ttindex{has_key()}
1079 \ttindex{items()}
1080 \ttindex{keys()}
1081 \ttindex{update()}
1082 \ttindex{values()}
Michael W. Hudson9c206152003-03-05 14:42:09 +00001083 \ttindex{get()}
1084 \ttindex{setdefault()}
1085 \ttindex{pop()}
1086 \ttindex{popitem()}
1087 \ttindex{iteritems()}
1088 \ttindex{iterkeys)}
1089 \ttindex{itervalues()}}
Fred Drake9c5cc141999-06-10 22:37:34 +00001090
1091\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
1092 \lineiii{len(\var{a})}{the number of items in \var{a}}{}
1093 \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
Fred Drake1e75e172000-07-31 16:34:46 +00001094 \lineiii{\var{a}[\var{k}] = \var{v}}
1095 {set \code{\var{a}[\var{k}]} to \var{v}}
Fred Drake9c5cc141999-06-10 22:37:34 +00001096 {}
1097 \lineiii{del \var{a}[\var{k}]}
1098 {remove \code{\var{a}[\var{k}]} from \var{a}}
1099 {(1)}
1100 \lineiii{\var{a}.clear()}{remove all items from \code{a}}{}
1101 \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001102 \lineiii{\var{a}.has_key(\var{k})}
Fred Drake9c5cc141999-06-10 22:37:34 +00001103 {\code{1} if \var{a} has a key \var{k}, else \code{0}}
1104 {}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001105 \lineiii{\var{k} \code{in} \var{a}}
1106 {Equivalent to \var{a}.has_key(\var{k})}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001107 {(2)}
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001108 \lineiii{\var{k} not in \var{a}}
Guido van Rossum8b3d6ca2001-04-23 13:22:59 +00001109 {Equivalent to \code{not} \var{a}.has_key(\var{k})}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001110 {(2)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001111 \lineiii{\var{a}.items()}
1112 {a copy of \var{a}'s list of (\var{key}, \var{value}) pairs}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001113 {(3)}
Fred Drake4a6c5c52001-06-12 03:31:56 +00001114 \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001115 \lineiii{\var{a}.update(\var{b})}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001116 {\code{for \var{k} in \var{b}.keys(): \var{a}[\var{k}] = \var{b}[\var{k}]}}
Barry Warsawe9218a12001-06-26 20:32:59 +00001117 {}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001118 \lineiii{\var{a}.fromkeys(\var{seq}\optional{, \var{value}})}
1119 {Creates a new dictionary with keys from \var{seq} and values set to \var{value}}
1120 {(7)}
Fred Drake4a6c5c52001-06-12 03:31:56 +00001121 \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001122 \lineiii{\var{a}.get(\var{k}\optional{, \var{x}})}
Fred Drake4cacec52001-04-21 05:56:06 +00001123 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
Fred Drake9c5cc141999-06-10 22:37:34 +00001124 else \var{x}}
Barry Warsawe9218a12001-06-26 20:32:59 +00001125 {(4)}
Guido van Rossum8141cf52000-08-08 16:15:49 +00001126 \lineiii{\var{a}.setdefault(\var{k}\optional{, \var{x}})}
Fred Drake4cacec52001-04-21 05:56:06 +00001127 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
Guido van Rossum8141cf52000-08-08 16:15:49 +00001128 else \var{x} (also setting it)}
Barry Warsawe9218a12001-06-26 20:32:59 +00001129 {(5)}
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001130 \lineiii{\var{a}.pop(\var{k}\optional{, \var{x}})}
1131 {\code{\var{a}[\var{k}]} if \code{\var{k} in \var{a}},
1132 else \var{x} (and remove k)}
1133 {(8)}
Guido van Rossumff63f202000-12-12 22:03:47 +00001134 \lineiii{\var{a}.popitem()}
1135 {remove and return an arbitrary (\var{key}, \var{value}) pair}
Barry Warsawe9218a12001-06-26 20:32:59 +00001136 {(6)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001137 \lineiii{\var{a}.iteritems()}
1138 {return an iterator over (\var{key}, \var{value}) pairs}
Fred Drake01777832002-08-19 21:58:58 +00001139 {(2), (3)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001140 \lineiii{\var{a}.iterkeys()}
1141 {return an iterator over the mapping's keys}
Fred Drake01777832002-08-19 21:58:58 +00001142 {(2), (3)}
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001143 \lineiii{\var{a}.itervalues()}
1144 {return an iterator over the mapping's values}
Fred Drake01777832002-08-19 21:58:58 +00001145 {(2), (3)}
Fred Drake9c5cc141999-06-10 22:37:34 +00001146\end{tableiii}
1147
Fred Drake64e3b431998-07-24 13:56:11 +00001148\noindent
1149Notes:
1150\begin{description}
Fred Drake9c5cc141999-06-10 22:37:34 +00001151\item[(1)] Raises a \exception{KeyError} exception if \var{k} is not
1152in the map.
Fred Drake64e3b431998-07-24 13:56:11 +00001153
Fred Drakec6d8f8d2001-05-25 04:24:37 +00001154\item[(2)] \versionadded{2.2}
1155
1156\item[(3)] Keys and values are listed in random order. If
Fred Drake01777832002-08-19 21:58:58 +00001157\method{items()}, \method{keys()}, \method{values()},
1158\method{iteritems()}, \method{iterkeys()}, and \method{itervalues()}
1159are called with no intervening modifications to the dictionary, the
1160lists will directly correspond. This allows the creation of
1161\code{(\var{value}, \var{key})} pairs using \function{zip()}:
1162\samp{pairs = zip(\var{a}.values(), \var{a}.keys())}. The same
1163relationship holds for the \method{iterkeys()} and
1164\method{itervalues()} methods: \samp{pairs = zip(\var{a}.itervalues(),
1165\var{a}.iterkeys())} provides the same value for \code{pairs}.
1166Another way to create the same list is \samp{pairs = [(v, k) for (k,
1167v) in \var{a}.iteritems()]}.
Fred Drake64e3b431998-07-24 13:56:11 +00001168
Barry Warsawe9218a12001-06-26 20:32:59 +00001169\item[(4)] Never raises an exception if \var{k} is not in the map,
Fred Drake38e5d272000-04-03 20:13:55 +00001170instead it returns \var{x}. \var{x} is optional; when \var{x} is not
Fred Drake9c5cc141999-06-10 22:37:34 +00001171provided and \var{k} is not in the map, \code{None} is returned.
Guido van Rossum8141cf52000-08-08 16:15:49 +00001172
Barry Warsawe9218a12001-06-26 20:32:59 +00001173\item[(5)] \function{setdefault()} is like \function{get()}, except
Guido van Rossum8141cf52000-08-08 16:15:49 +00001174that if \var{k} is missing, \var{x} is both returned and inserted into
1175the dictionary as the value of \var{k}.
Guido van Rossumff63f202000-12-12 22:03:47 +00001176
Barry Warsawe9218a12001-06-26 20:32:59 +00001177\item[(6)] \function{popitem()} is useful to destructively iterate
Guido van Rossumff63f202000-12-12 22:03:47 +00001178over a dictionary, as often used in set algorithms.
Fred Drake64e3b431998-07-24 13:56:11 +00001179
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001180\item[(7)] \function{fromkeys()} is a class method that returns a
1181new dictionary. \var{value} defaults to \code{None}. \versionadded{2.3}
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001182
1183\item[(8)] \function{pop()} raises a \exception{KeyError} when no default
1184value is given and the key is not found. \versionadded{2.3}
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001185\end{description}
1186
Fred Drake64e3b431998-07-24 13:56:11 +00001187
Fred Drake99de2182001-10-30 06:23:14 +00001188\subsection{File Objects
1189 \label{bltin-file-objects}}
Fred Drake64e3b431998-07-24 13:56:11 +00001190
Fred Drake99de2182001-10-30 06:23:14 +00001191File objects\obindex{file} are implemented using C's \code{stdio}
1192package and can be created with the built-in constructor
Tim Peters8f01b682002-03-12 03:04:44 +00001193\function{file()}\bifuncindex{file} described in section
Tim Peters003047a2001-10-30 05:54:04 +00001194\ref{built-in-funcs}, ``Built-in Functions.''\footnote{\function{file()}
1195is new in Python 2.2. The older built-in \function{open()} is an
1196alias for \function{file()}.}
Steve Holden1e4519f2002-06-14 09:16:40 +00001197File objects are also returned
Fred Drake907e76b2001-07-06 20:30:11 +00001198by some other built-in functions and methods, such as
Fred Drake4de96c22000-08-12 03:36:23 +00001199\function{os.popen()} and \function{os.fdopen()} and the
Fred Drake130072d1998-10-28 20:08:35 +00001200\method{makefile()} method of socket objects.
Fred Drake4de96c22000-08-12 03:36:23 +00001201\refstmodindex{os}
Fred Drake64e3b431998-07-24 13:56:11 +00001202\refbimodindex{socket}
1203
1204When a file operation fails for an I/O-related reason, the exception
Fred Drake84538cd1998-11-30 21:51:25 +00001205\exception{IOError} is raised. This includes situations where the
1206operation is not defined for some reason, like \method{seek()} on a tty
Fred Drake64e3b431998-07-24 13:56:11 +00001207device or writing a file opened for reading.
1208
1209Files have the following methods:
1210
1211
1212\begin{methoddesc}[file]{close}{}
Steve Holden1e4519f2002-06-14 09:16:40 +00001213 Close the file. A closed file cannot be read or written any more.
Fred Drakea776cea2000-11-06 20:17:37 +00001214 Any operation which requires that the file be open will raise a
1215 \exception{ValueError} after the file has been closed. Calling
Fred Drake752ba392000-09-19 15:18:51 +00001216 \method{close()} more than once is allowed.
Fred Drake64e3b431998-07-24 13:56:11 +00001217\end{methoddesc}
1218
1219\begin{methoddesc}[file]{flush}{}
Fred Drake752ba392000-09-19 15:18:51 +00001220 Flush the internal buffer, like \code{stdio}'s
1221 \cfunction{fflush()}. This may be a no-op on some file-like
1222 objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001223\end{methoddesc}
1224
Fred Drake64e3b431998-07-24 13:56:11 +00001225\begin{methoddesc}[file]{fileno}{}
Fred Drake752ba392000-09-19 15:18:51 +00001226 \index{file descriptor}
1227 \index{descriptor, file}
1228 Return the integer ``file descriptor'' that is used by the
1229 underlying implementation to request I/O operations from the
1230 operating system. This can be useful for other, lower level
Fred Drake907e76b2001-07-06 20:30:11 +00001231 interfaces that use file descriptors, such as the
1232 \refmodule{fcntl}\refbimodindex{fcntl} module or
Fred Drake0aa811c2001-10-20 04:24:09 +00001233 \function{os.read()} and friends. \note{File-like objects
Fred Drake907e76b2001-07-06 20:30:11 +00001234 which do not have a real file descriptor should \emph{not} provide
Fred Drake0aa811c2001-10-20 04:24:09 +00001235 this method!}
Fred Drake64e3b431998-07-24 13:56:11 +00001236\end{methoddesc}
1237
Guido van Rossum0fc01862002-08-06 17:01:28 +00001238\begin{methoddesc}[file]{isatty}{}
1239 Return \code{True} if the file is connected to a tty(-like) device, else
1240 \code{False}. \note{If a file-like object is not associated
1241 with a real file, this method should \emph{not} be implemented.}
1242\end{methoddesc}
1243
1244\begin{methoddesc}[file]{next}{}
1245A file object is its own iterator, i.e. \code{iter(\var{f})} returns
1246\var{f} (unless \var{f} is closed). When a file is used as an
1247iterator, typically in a \keyword{for} loop (for example,
1248\code{for line in f: print line}), the \method{next()} method is
1249called repeatedly. This method returns the next input line, or raises
1250\exception{StopIteration} when \EOF{} is hit. In order to make a
1251\keyword{for} loop the most efficient way of looping over the lines of
1252a file (a very common operation), the \method{next()} method uses a
1253hidden read-ahead buffer. As a consequence of using a read-ahead
1254buffer, combining \method{next()} with other file methods (like
1255\method{readline()}) does not work right. However, using
1256\method{seek()} to reposition the file to an absolute position will
1257flush the read-ahead buffer.
1258\versionadded{2.3}
1259\end{methoddesc}
1260
Fred Drake64e3b431998-07-24 13:56:11 +00001261\begin{methoddesc}[file]{read}{\optional{size}}
1262 Read at most \var{size} bytes from the file (less if the read hits
Fred Drakef4cbada1999-04-14 14:31:53 +00001263 \EOF{} before obtaining \var{size} bytes). If the \var{size}
1264 argument is negative or omitted, read all data until \EOF{} is
1265 reached. The bytes are returned as a string object. An empty
1266 string is returned when \EOF{} is encountered immediately. (For
1267 certain files, like ttys, it makes sense to continue reading after
1268 an \EOF{} is hit.) Note that this method may call the underlying
1269 C function \cfunction{fread()} more than once in an effort to
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001270 acquire as close to \var{size} bytes as possible. Also note that
1271 when in non-blocking mode, less data than what was requested may
1272 be returned, even if no \var{size} parameter was given.
Fred Drake64e3b431998-07-24 13:56:11 +00001273\end{methoddesc}
1274
1275\begin{methoddesc}[file]{readline}{\optional{size}}
1276 Read one entire line from the file. A trailing newline character is
Fred Drakeea003fc1999-04-05 21:59:15 +00001277 kept in the string\footnote{
Steve Holden1e4519f2002-06-14 09:16:40 +00001278 The advantage of leaving the newline on is that
1279 returning an empty string is then an unambiguous \EOF{}
1280 indication. It is also possible (in cases where it might
1281 matter, for example, if you
Tim Peters8f01b682002-03-12 03:04:44 +00001282 want to make an exact copy of a file while scanning its lines)
Steve Holden1e4519f2002-06-14 09:16:40 +00001283 to tell whether the last line of a file ended in a newline
Fred Drake4de96c22000-08-12 03:36:23 +00001284 or not (yes this happens!).
1285 } (but may be absent when a file ends with an
Fred Drake64e3b431998-07-24 13:56:11 +00001286 incomplete line). If the \var{size} argument is present and
1287 non-negative, it is a maximum byte count (including the trailing
1288 newline) and an incomplete line may be returned.
Steve Holden1e4519f2002-06-14 09:16:40 +00001289 An empty string is returned \emph{only} when \EOF{} is encountered
Fred Drake0aa811c2001-10-20 04:24:09 +00001290 immediately. \note{Unlike \code{stdio}'s \cfunction{fgets()}, the
Fred Drake752ba392000-09-19 15:18:51 +00001291 returned string contains null characters (\code{'\e 0'}) if they
Fred Drake0aa811c2001-10-20 04:24:09 +00001292 occurred in the input.}
Fred Drake64e3b431998-07-24 13:56:11 +00001293\end{methoddesc}
1294
1295\begin{methoddesc}[file]{readlines}{\optional{sizehint}}
1296 Read until \EOF{} using \method{readline()} and return a list containing
1297 the lines thus read. If the optional \var{sizehint} argument is
Fred Drakec37b65e2001-11-28 07:26:15 +00001298 present, instead of reading up to \EOF, whole lines totalling
Fred Drake64e3b431998-07-24 13:56:11 +00001299 approximately \var{sizehint} bytes (possibly after rounding up to an
Fred Drake752ba392000-09-19 15:18:51 +00001300 internal buffer size) are read. Objects implementing a file-like
1301 interface may choose to ignore \var{sizehint} if it cannot be
1302 implemented, or cannot be implemented efficiently.
Fred Drake64e3b431998-07-24 13:56:11 +00001303\end{methoddesc}
1304
Guido van Rossum20ab9e92001-01-17 01:18:00 +00001305\begin{methoddesc}[file]{xreadlines}{}
Guido van Rossum0fc01862002-08-06 17:01:28 +00001306 This method returns the same thing as \code{iter(f)}.
Fred Drake82f93c62001-04-22 01:56:51 +00001307 \versionadded{2.1}
Guido van Rossum0fc01862002-08-06 17:01:28 +00001308 \deprecated{2.3}{Use \code{for line in file} instead.}
Guido van Rossum20ab9e92001-01-17 01:18:00 +00001309\end{methoddesc}
1310
Fred Drake64e3b431998-07-24 13:56:11 +00001311\begin{methoddesc}[file]{seek}{offset\optional{, whence}}
1312 Set the file's current position, like \code{stdio}'s \cfunction{fseek()}.
1313 The \var{whence} argument is optional and defaults to \code{0}
1314 (absolute file positioning); other values are \code{1} (seek
1315 relative to the current position) and \code{2} (seek relative to the
Fred Drake19ae7832001-01-04 05:16:39 +00001316 file's end). There is no return value. Note that if the file is
1317 opened for appending (mode \code{'a'} or \code{'a+'}), any
1318 \method{seek()} operations will be undone at the next write. If the
1319 file is only opened for writing in append mode (mode \code{'a'}),
1320 this method is essentially a no-op, but it remains useful for files
1321 opened in append mode with reading enabled (mode \code{'a+'}).
Fred Drake64e3b431998-07-24 13:56:11 +00001322\end{methoddesc}
1323
1324\begin{methoddesc}[file]{tell}{}
1325 Return the file's current position, like \code{stdio}'s
1326 \cfunction{ftell()}.
1327\end{methoddesc}
1328
1329\begin{methoddesc}[file]{truncate}{\optional{size}}
Tim Peters8f01b682002-03-12 03:04:44 +00001330 Truncate the file's size. If the optional \var{size} argument is
Fred Drake752ba392000-09-19 15:18:51 +00001331 present, the file is truncated to (at most) that size. The size
Tim Peters8f01b682002-03-12 03:04:44 +00001332 defaults to the current position. The current file position is
1333 not changed. Note that if a specified size exceeds the file's
1334 current size, the result is platform-dependent: possibilities
1335 include that file may remain unchanged, increase to the specified
1336 size as if zero-filled, or increase to the specified size with
1337 undefined new content.
Tim Petersfb05db22002-03-11 00:24:00 +00001338 Availability: Windows, many \UNIX variants.
Fred Drake64e3b431998-07-24 13:56:11 +00001339\end{methoddesc}
1340
1341\begin{methoddesc}[file]{write}{str}
Fred Drake0aa811c2001-10-20 04:24:09 +00001342 Write a string to the file. There is no return value. Due to
Fred Drake3c48ef72001-01-09 22:47:46 +00001343 buffering, the string may not actually show up in the file until
1344 the \method{flush()} or \method{close()} method is called.
Fred Drake64e3b431998-07-24 13:56:11 +00001345\end{methoddesc}
1346
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001347\begin{methoddesc}[file]{writelines}{sequence}
1348 Write a sequence of strings to the file. The sequence can be any
1349 iterable object producing strings, typically a list of strings.
1350 There is no return value.
Fred Drake3c48ef72001-01-09 22:47:46 +00001351 (The name is intended to match \method{readlines()};
1352 \method{writelines()} does not add line separators.)
1353\end{methoddesc}
1354
Fred Drake64e3b431998-07-24 13:56:11 +00001355
Fred Drake038d2642001-09-22 04:34:48 +00001356Files support the iterator protocol. Each iteration returns the same
1357result as \code{\var{file}.readline()}, and iteration ends when the
1358\method{readline()} method returns an empty string.
1359
1360
Fred Drake752ba392000-09-19 15:18:51 +00001361File objects also offer a number of other interesting attributes.
1362These are not required for file-like objects, but should be
1363implemented if they make sense for the particular object.
Fred Drake64e3b431998-07-24 13:56:11 +00001364
1365\begin{memberdesc}[file]{closed}
Neal Norwitz6b353702002-04-09 18:15:00 +00001366bool indicating the current state of the file object. This is a
Fred Drake64e3b431998-07-24 13:56:11 +00001367read-only attribute; the \method{close()} method changes the value.
Fred Drake752ba392000-09-19 15:18:51 +00001368It may not be available on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001369\end{memberdesc}
1370
1371\begin{memberdesc}[file]{mode}
1372The I/O mode for the file. If the file was created using the
1373\function{open()} built-in function, this will be the value of the
Fred Drake752ba392000-09-19 15:18:51 +00001374\var{mode} parameter. This is a read-only attribute and may not be
1375present on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001376\end{memberdesc}
1377
1378\begin{memberdesc}[file]{name}
1379If the file object was created using \function{open()}, the name of
1380the file. Otherwise, some string that indicates the source of the
1381file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only
Fred Drake752ba392000-09-19 15:18:51 +00001382attribute and may not be present on all file-like objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001383\end{memberdesc}
1384
Michael W. Hudson9c206152003-03-05 14:42:09 +00001385\begin{memberdesc}[file]{newlines}
1386If Python was built with the \code{--with-universal-newlines} option
1387(the default) this read-only attribute exists, and for files opened in
1388universal newline read mode it keeps track of the types of newlines
1389encountered while reading the file. The values it can take are
1390\code{'\e r'}, \code{'\e n'}, \code{'\e r\e n'}, \code{None} (unknown,
1391no newlines read yet) or a tuple containing all the newline
1392types seen, to indicate that multiple
1393newline conventions were encountered. For files not opened in universal
1394newline read mode the value of this attribute will be \code{None}.
1395\end{memberdesc}
1396
Fred Drake64e3b431998-07-24 13:56:11 +00001397\begin{memberdesc}[file]{softspace}
1398Boolean that indicates whether a space character needs to be printed
1399before another value when using the \keyword{print} statement.
1400Classes that are trying to simulate a file object should also have a
1401writable \member{softspace} attribute, which should be initialized to
Fred Drake66571cc2000-09-09 03:30:34 +00001402zero. This will be automatic for most classes implemented in Python
1403(care may be needed for objects that override attribute access); types
1404implemented in C will have to provide a writable
1405\member{softspace} attribute.
Fred Drake0aa811c2001-10-20 04:24:09 +00001406\note{This attribute is not used to control the
Fred Drake51f53df2000-09-20 04:48:20 +00001407\keyword{print} statement, but to allow the implementation of
Fred Drake0aa811c2001-10-20 04:24:09 +00001408\keyword{print} to keep track of its internal state.}
Fred Drake64e3b431998-07-24 13:56:11 +00001409\end{memberdesc}
1410
Fred Drakea776cea2000-11-06 20:17:37 +00001411
Fred Drake99de2182001-10-30 06:23:14 +00001412\subsection{Other Built-in Types \label{typesother}}
1413
1414The interpreter supports several other kinds of objects.
1415Most of these support only one or two operations.
1416
1417
1418\subsubsection{Modules \label{typesmodules}}
1419
1420The only special operation on a module is attribute access:
1421\code{\var{m}.\var{name}}, where \var{m} is a module and \var{name}
1422accesses a name defined in \var{m}'s symbol table. Module attributes
1423can be assigned to. (Note that the \keyword{import} statement is not,
1424strictly speaking, an operation on a module object; \code{import
1425\var{foo}} does not require a module object named \var{foo} to exist,
1426rather it requires an (external) \emph{definition} for a module named
1427\var{foo} somewhere.)
1428
1429A special member of every module is \member{__dict__}.
1430This is the dictionary containing the module's symbol table.
1431Modifying this dictionary will actually change the module's symbol
1432table, but direct assignment to the \member{__dict__} attribute is not
1433possible (you can write \code{\var{m}.__dict__['a'] = 1}, which
1434defines \code{\var{m}.a} to be \code{1}, but you can't write
1435\code{\var{m}.__dict__ = \{\}}.
1436
1437Modules built into the interpreter are written like this:
1438\code{<module 'sys' (built-in)>}. If loaded from a file, they are
1439written as \code{<module 'os' from
1440'/usr/local/lib/python\shortversion/os.pyc'>}.
1441
1442
1443\subsubsection{Classes and Class Instances \label{typesobjects}}
1444\nodename{Classes and Instances}
1445
1446See chapters 3 and 7 of the \citetitle[../ref/ref.html]{Python
1447Reference Manual} for these.
1448
1449
1450\subsubsection{Functions \label{typesfunctions}}
1451
1452Function objects are created by function definitions. The only
1453operation on a function object is to call it:
1454\code{\var{func}(\var{argument-list})}.
1455
1456There are really two flavors of function objects: built-in functions
1457and user-defined functions. Both support the same operation (to call
1458the function), but the implementation is different, hence the
1459different object types.
1460
1461The implementation adds two special read-only attributes:
1462\code{\var{f}.func_code} is a function's \dfn{code
1463object}\obindex{code} (see below) and \code{\var{f}.func_globals} is
1464the dictionary used as the function's global namespace (this is the
1465same as \code{\var{m}.__dict__} where \var{m} is the module in which
1466the function \var{f} was defined).
1467
1468Function objects also support getting and setting arbitrary
1469attributes, which can be used to, e.g. attach metadata to functions.
1470Regular attribute dot-notation is used to get and set such
1471attributes. \emph{Note that the current implementation only supports
1472function attributes on user-defined functions. Function attributes on
1473built-in functions may be supported in the future.}
1474
1475Functions have another special attribute \code{\var{f}.__dict__}
1476(a.k.a. \code{\var{f}.func_dict}) which contains the namespace used to
1477support function attributes. \code{__dict__} and \code{func_dict} can
1478be accessed directly or set to a dictionary object. A function's
1479dictionary cannot be deleted.
1480
1481\subsubsection{Methods \label{typesmethods}}
1482\obindex{method}
1483
1484Methods are functions that are called using the attribute notation.
1485There are two flavors: built-in methods (such as \method{append()} on
1486lists) and class instance methods. Built-in methods are described
1487with the types that support them.
1488
1489The implementation adds two special read-only attributes to class
1490instance methods: \code{\var{m}.im_self} is the object on which the
1491method operates, and \code{\var{m}.im_func} is the function
1492implementing the method. Calling \code{\var{m}(\var{arg-1},
1493\var{arg-2}, \textrm{\ldots}, \var{arg-n})} is completely equivalent to
1494calling \code{\var{m}.im_func(\var{m}.im_self, \var{arg-1},
1495\var{arg-2}, \textrm{\ldots}, \var{arg-n})}.
1496
1497Class instance methods are either \emph{bound} or \emph{unbound},
1498referring to whether the method was accessed through an instance or a
1499class, respectively. When a method is unbound, its \code{im_self}
1500attribute will be \code{None} and if called, an explicit \code{self}
1501object must be passed as the first argument. In this case,
1502\code{self} must be an instance of the unbound method's class (or a
1503subclass of that class), otherwise a \code{TypeError} is raised.
1504
1505Like function objects, methods objects support getting
1506arbitrary attributes. However, since method attributes are actually
1507stored on the underlying function object (\code{meth.im_func}),
1508setting method attributes on either bound or unbound methods is
1509disallowed. Attempting to set a method attribute results in a
1510\code{TypeError} being raised. In order to set a method attribute,
1511you need to explicitly set it on the underlying function object:
1512
1513\begin{verbatim}
1514class C:
1515 def method(self):
1516 pass
1517
1518c = C()
1519c.method.im_func.whoami = 'my name is c'
1520\end{verbatim}
1521
1522See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
1523information.
1524
1525
1526\subsubsection{Code Objects \label{bltin-code-objects}}
1527\obindex{code}
1528
1529Code objects are used by the implementation to represent
1530``pseudo-compiled'' executable Python code such as a function body.
1531They differ from function objects because they don't contain a
1532reference to their global execution environment. Code objects are
1533returned by the built-in \function{compile()} function and can be
1534extracted from function objects through their \member{func_code}
1535attribute.
1536\bifuncindex{compile}
1537\withsubitem{(function object attribute)}{\ttindex{func_code}}
1538
1539A code object can be executed or evaluated by passing it (instead of a
1540source string) to the \keyword{exec} statement or the built-in
1541\function{eval()} function.
1542\stindex{exec}
1543\bifuncindex{eval}
1544
1545See the \citetitle[../ref/ref.html]{Python Reference Manual} for more
1546information.
1547
1548
1549\subsubsection{Type Objects \label{bltin-type-objects}}
1550
1551Type objects represent the various object types. An object's type is
1552accessed by the built-in function \function{type()}. There are no special
1553operations on types. The standard module \module{types} defines names
1554for all standard built-in types.
1555\bifuncindex{type}
1556\refstmodindex{types}
1557
1558Types are written like this: \code{<type 'int'>}.
1559
1560
1561\subsubsection{The Null Object \label{bltin-null-object}}
1562
1563This object is returned by functions that don't explicitly return a
1564value. It supports no special operations. There is exactly one null
1565object, named \code{None} (a built-in name).
1566
1567It is written as \code{None}.
1568
1569
1570\subsubsection{The Ellipsis Object \label{bltin-ellipsis-object}}
1571
1572This object is used by extended slice notation (see the
1573\citetitle[../ref/ref.html]{Python Reference Manual}). It supports no
1574special operations. There is exactly one ellipsis object, named
1575\constant{Ellipsis} (a built-in name).
1576
1577It is written as \code{Ellipsis}.
1578
Guido van Rossum77f6a652002-04-03 22:41:51 +00001579\subsubsection{Boolean Values}
1580
1581Boolean values are the two constant objects \code{False} and
1582\code{True}. They are used to represent truth values (although other
1583values can also be considered false or true). In numeric contexts
1584(for example when used as the argument to an arithmetic operator),
1585they behave like the integers 0 and 1, respectively. The built-in
1586function \function{bool()} can be used to cast any value to a Boolean,
1587if the value can be interpreted as a truth value (see section Truth
1588Value Testing above).
1589
1590They are written as \code{False} and \code{True}, respectively.
1591\index{False}
1592\index{True}
1593\indexii{Boolean}{values}
1594
Fred Drake99de2182001-10-30 06:23:14 +00001595
Fred Drake9474d861999-02-12 22:05:33 +00001596\subsubsection{Internal Objects \label{typesinternal}}
Fred Drake64e3b431998-07-24 13:56:11 +00001597
Fred Drake37f15741999-11-10 16:21:37 +00001598See the \citetitle[../ref/ref.html]{Python Reference Manual} for this
Fred Drake512bb722000-08-18 03:12:38 +00001599information. It describes stack frame objects, traceback objects, and
1600slice objects.
Fred Drake64e3b431998-07-24 13:56:11 +00001601
1602
Fred Drake7a2f0661998-09-10 18:25:58 +00001603\subsection{Special Attributes \label{specialattrs}}
Fred Drake64e3b431998-07-24 13:56:11 +00001604
1605The implementation adds a few special read-only attributes to several
1606object types, where they are relevant:
1607
Fred Drakea776cea2000-11-06 20:17:37 +00001608\begin{memberdesc}[object]{__dict__}
1609A dictionary or other mapping object used to store an
Fred Drake7a2f0661998-09-10 18:25:58 +00001610object's (writable) attributes.
Fred Drakea776cea2000-11-06 20:17:37 +00001611\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001612
Fred Drakea776cea2000-11-06 20:17:37 +00001613\begin{memberdesc}[object]{__methods__}
Fred Drake35705512001-12-03 17:32:27 +00001614\deprecated{2.2}{Use the built-in function \function{dir()} to get a
1615list of an object's attributes. This attribute is no longer available.}
Fred Drakea776cea2000-11-06 20:17:37 +00001616\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001617
Fred Drakea776cea2000-11-06 20:17:37 +00001618\begin{memberdesc}[object]{__members__}
Fred Drake35705512001-12-03 17:32:27 +00001619\deprecated{2.2}{Use the built-in function \function{dir()} to get a
1620list of an object's attributes. This attribute is no longer available.}
Fred Drakea776cea2000-11-06 20:17:37 +00001621\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001622
Fred Drakea776cea2000-11-06 20:17:37 +00001623\begin{memberdesc}[instance]{__class__}
Fred Drake7a2f0661998-09-10 18:25:58 +00001624The class to which a class instance belongs.
Fred Drakea776cea2000-11-06 20:17:37 +00001625\end{memberdesc}
Fred Drake64e3b431998-07-24 13:56:11 +00001626
Fred Drakea776cea2000-11-06 20:17:37 +00001627\begin{memberdesc}[class]{__bases__}
Fred Drake907e76b2001-07-06 20:30:11 +00001628The tuple of base classes of a class object. If there are no base
1629classes, this will be an empty tuple.
Fred Drakea776cea2000-11-06 20:17:37 +00001630\end{memberdesc}