blob: b612a0b53c209f02242cd3ad21e914bed9f3dd37 [file] [log] [blame]
Fred Drake64e3b431998-07-24 13:56:11 +00001\section{Built-in Types}
2\label{types}
3
4The following sections describe the standard types that are built into
5the interpreter. These are the numeric types, sequence types, and
6several others, including types themselves. There is no explicit
7Boolean type; use integers instead.
8\indexii{built-in}{types}
9\indexii{Boolean}{type}
10
11Some operations are supported by several object types; in particular,
12all objects can be compared, tested for truth value, and converted to
13a string (with the \code{`{\rm \ldots}`} notation). The latter conversion is
14implicitly used when an object is written by the \code{print} statement.
15\stindex{print}
16
17
18\subsection{Truth Value Testing}
19\label{truth}
20
21Any object can be tested for truth value, for use in an \code{if} or
22\code{while} condition or as operand of the Boolean operations below.
23The following values are considered false:
24\stindex{if}
25\stindex{while}
26\indexii{truth}{value}
27\indexii{Boolean}{operations}
28\index{false}
29
30\setindexsubitem{(Built-in object)}
31\begin{itemize}
32
33\item \code{None}
34 \ttindex{None}
35
36\item zero of any numeric type, e.g., \code{0}, \code{0L}, \code{0.0}.
37
38\item any empty sequence, e.g., \code{''}, \code{()}, \code{[]}.
39
40\item any empty mapping, e.g., \code{\{\}}.
41
42\item instances of user-defined classes, if the class defines a
43 \code{__nonzero__()} or \code{__len__()} method, when that
44 method returns zero.
45
46\end{itemize}
47
48All other values are considered true --- so objects of many types are
49always true.
50\index{true}
51
52Operations and built-in functions that have a Boolean result always
53return \code{0} for false and \code{1} for true, unless otherwise
54stated. (Important exception: the Boolean operations
55\samp{or}\opindex{or} and \samp{and}\opindex{and} always return one of
56their operands.)
57
58
59\subsection{Boolean Operations}
60\label{boolean}
61
62These are the Boolean operations, ordered by ascending priority:
63\indexii{Boolean}{operations}
64
65\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
66 \lineiii{\var{x} or \var{y}}{if \var{x} is false, then \var{y}, else \var{x}}{(1)}
67 \lineiii{\var{x} and \var{y}}{if \var{x} is false, then \var{x}, else \var{y}}{(1)}
68 \hline
69 \lineiii{not \var{x}}{if \var{x} is false, then \code{1}, else \code{0}}{(2)}
70\end{tableiii}
71\opindex{and}
72\opindex{or}
73\opindex{not}
74
75\noindent
76Notes:
77
78\begin{description}
79
80\item[(1)]
81These only evaluate their second argument if needed for their outcome.
82
83\item[(2)]
84\samp{not} has a lower priority than non-Boolean operators, so e.g.
85\code{not a == b} is interpreted as \code{not(a == b)}, and
86\code{a == not b} is a syntax error.
87
88\end{description}
89
90
91\subsection{Comparisons}
92\label{comparisons}
93
94Comparison operations are supported by all objects. They all have the
95same priority (which is higher than that of the Boolean operations).
96Comparisons can be chained arbitrarily, e.g. \code{x < y <= z} is
97equivalent to \code{x < y and y <= z}, except that \code{y} is
98evaluated only once (but in both cases \code{z} is not evaluated at
99all when \code{x < y} is found to be false).
100\indexii{chaining}{comparisons}
101
102This table summarizes the comparison operations:
103
104\begin{tableiii}{c|l|c}{code}{Operation}{Meaning}{Notes}
105 \lineiii{<}{strictly less than}{}
106 \lineiii{<=}{less than or equal}{}
107 \lineiii{>}{strictly greater than}{}
108 \lineiii{>=}{greater than or equal}{}
109 \lineiii{==}{equal}{}
110 \lineiii{<>}{not equal}{(1)}
111 \lineiii{!=}{not equal}{(1)}
112 \lineiii{is}{object identity}{}
113 \lineiii{is not}{negated object identity}{}
114\end{tableiii}
115\indexii{operator}{comparison}
116\opindex{==} % XXX *All* others have funny characters < ! >
117\opindex{is}
118\opindex{is not}
119
120\noindent
121Notes:
122
123\begin{description}
124
125\item[(1)]
126\code{<>} and \code{!=} are alternate spellings for the same operator.
127(I couldn't choose between \ABC{} and \C{}! :-)
128\index{ABC language@\ABC{} language}
129\index{language!ABC@\ABC{}}
130\indexii{C@\C{}}{language}
131
132\end{description}
133
134Objects of different types, except different numeric types, never
135compare equal; such objects are ordered consistently but arbitrarily
136(so that sorting a heterogeneous array yields a consistent result).
137Furthermore, some types (e.g., windows) support only a degenerate
138notion of comparison where any two objects of that type are unequal.
139Again, such objects are ordered arbitrarily but consistently.
140\indexii{types}{numeric}
141\indexii{objects}{comparing}
142
143(Implementation note: objects of different types except numbers are
144ordered by their type names; objects of the same types that don't
145support proper comparison are ordered by their address.)
146
147Two more operations with the same syntactic priority, \samp{in} and
148\samp{not in}, are supported only by sequence types (below).
149\opindex{in}
150\opindex{not in}
151
152
153\subsection{Numeric Types}
154\label{typesnumeric}
155
156There are four numeric types: \dfn{plain integers}, \dfn{long integers},
157\dfn{floating point numbers}, and \dfn{complex numbers}.
158Plain integers (also just called \dfn{integers})
159are implemented using \code{long} in \C{}, which gives them at least 32
160bits of precision. Long integers have unlimited precision. Floating
161point numbers are implemented using \code{double} in \C{}. All bets on
162their precision are off unless you happen to know the machine you are
163working with.
164\indexii{numeric}{types}
165\indexii{integer}{types}
166\indexii{integer}{type}
167\indexiii{long}{integer}{type}
168\indexii{floating point}{type}
169\indexii{complex number}{type}
170\indexii{C@\C{}}{language}
171
172Complex numbers have a real and imaginary part, which are both
173implemented using \code{double} in \C{}. To extract these parts from
174a complex number \var{z}, use \code{\var{z}.real} and \code{\var{z}.imag}.
175
176Numbers are created by numeric literals or as the result of built-in
177functions and operators. Unadorned integer literals (including hex
178and octal numbers) yield plain integers. Integer literals with an \samp{L}
179or \samp{l} suffix yield long integers
180(\samp{L} is preferred because \samp{1l} looks too much like eleven!).
181Numeric literals containing a decimal point or an exponent sign yield
182floating point numbers. Appending \samp{j} or \samp{J} to a numeric
183literal yields a complex number.
184\indexii{numeric}{literals}
185\indexii{integer}{literals}
186\indexiii{long}{integer}{literals}
187\indexii{floating point}{literals}
188\indexii{complex number}{literals}
189\indexii{hexadecimal}{literals}
190\indexii{octal}{literals}
191
192Python fully supports mixed arithmetic: when a binary arithmetic
193operator has operands of different numeric types, the operand with the
194``smaller'' type is converted to that of the other, where plain
195integer is smaller than long integer is smaller than floating point is
196smaller than complex.
197Comparisons between numbers of mixed type use the same rule.%
198\footnote{As a consequence, the list \code{[1, 2]} is considered equal
199 to \code{[1.0, 2.0]}, and similar for tuples.}
200The functions \code{int()}, \code{long()}, \code{float()},
201and \code{complex()} can be used
202to coerce numbers to a specific type.
203\index{arithmetic}
204\bifuncindex{int}
205\bifuncindex{long}
206\bifuncindex{float}
207\bifuncindex{complex}
208
209All numeric types support the following operations, sorted by
210ascending priority (operations in the same box have the same
211priority; all numeric operations have a higher priority than
212comparison operations):
213
214\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
215 \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{}
216 \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{}
217 \hline
218 \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{}
219 \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(1)}
220 \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{}
221 \hline
222 \lineiii{-\var{x}}{\var{x} negated}{}
223 \lineiii{+\var{x}}{\var{x} unchanged}{}
224 \hline
225 \lineiii{abs(\var{x})}{absolute value or magnitude of \var{x}}{}
226 \lineiii{int(\var{x})}{\var{x} converted to integer}{(2)}
227 \lineiii{long(\var{x})}{\var{x} converted to long integer}{(2)}
228 \lineiii{float(\var{x})}{\var{x} converted to floating point}{}
229 \lineiii{complex(\var{re},\var{im})}{a complex number with real part \var{re}, imaginary part \var{im}. \var{im} defaults to zero.}{}
230 \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)}
231 \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{}
232 \lineiii{\var{x} ** \var{y}}{\var{x} to the power \var{y}}{}
233\end{tableiii}
234\indexiii{operations on}{numeric}{types}
235
236\noindent
237Notes:
238\begin{description}
239
240\item[(1)]
241For (plain or long) integer division, the result is an integer.
242The result is always rounded towards minus infinity: 1/2 is 0,
243(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0.
244\indexii{integer}{division}
245\indexiii{long}{integer}{division}
246
247\item[(2)]
248Conversion from floating point to (long or plain) integer may round or
249truncate as in \C{}; see functions \code{floor()} and \code{ceil()} in
250module \code{math} for well-defined conversions.
251\bifuncindex{floor}
252\bifuncindex{ceil}
253\indexii{numeric}{conversions}
254\refbimodindex{math}
255\indexii{C@\C{}}{language}
256
257\item[(3)]
258See the section on built-in functions for an exact definition.
259
260\end{description}
261% XXXJH exceptions: overflow (when? what operations?) zerodivision
262
263\subsubsection{Bit-string Operations on Integer Types}
264\nodename{Bit-string Operations}
265
266Plain and long integer types support additional operations that make
267sense only for bit-strings. Negative numbers are treated as their 2's
268complement value (for long integers, this assumes a sufficiently large
269number of bits that no overflow occurs during the operation).
270
271The priorities of the binary bit-wise operations are all lower than
272the numeric operations and higher than the comparisons; the unary
273operation \samp{\~} has the same priority as the other unary numeric
274operations (\samp{+} and \samp{-}).
275
276This table lists the bit-string operations sorted in ascending
277priority (operations in the same box have the same priority):
278
279\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
280 \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{}
281 \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{}
282 \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{}
283 \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{(1), (2)}
284 \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{(1), (3)}
285 \hline
286 \lineiii{\~\var{x}}{the bits of \var{x} inverted}{}
287\end{tableiii}
288\indexiii{operations on}{integer}{types}
289\indexii{bit-string}{operations}
290\indexii{shifting}{operations}
291\indexii{masking}{operations}
292
293\noindent
294Notes:
295\begin{description}
296\item[(1)] Negative shift counts are illegal and cause a
297\exception{ValueError} to be raised.
298\item[(2)] A left shift by \var{n} bits is equivalent to
299multiplication by \code{pow(2, \var{n})} without overflow check.
300\item[(3)] A right shift by \var{n} bits is equivalent to
301division by \code{pow(2, \var{n})} without overflow check.
302\end{description}
303
304
305\subsection{Sequence Types}
306\label{typesseq}
307
308There are three sequence types: strings, lists and tuples.
309
310Strings literals are written in single or double quotes:
311\code{'xyzzy'}, \code{"frobozz"}. See Chapter 2 of the \emph{Python
312Reference Manual} for more about string literals. Lists are
313constructed with square brackets, separating items with commas:
314\code{[a, b, c]}. Tuples are constructed by the comma operator (not
315within square brackets), with or without enclosing parentheses, but an
316empty tuple must have the enclosing parentheses, e.g.,
317\code{a, b, c} or \code{()}. A single item tuple must have a trailing
318comma, e.g., \code{(d,)}.
319\indexii{sequence}{types}
320\indexii{string}{type}
321\indexii{tuple}{type}
322\indexii{list}{type}
323
324Sequence types support the following operations. The \samp{in} and
325\samp{not in} operations have the same priorities as the comparison
326operations. The \samp{+} and \samp{*} operations have the same
327priority as the corresponding numeric operations.\footnote{They must
328have since the parser can't tell the type of the operands.}
329
330This table lists the sequence operations sorted in ascending priority
331(operations in the same box have the same priority). In the table,
332\var{s} and \var{t} are sequences of the same type; \var{n}, \var{i}
333and \var{j} are integers:
334
335\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
336 \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{}
337 \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is
338equal to \var{x}, else \code{1}}{}
339 \hline
340 \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
341 \lineiii{\var{s} * \var{n}{\rm ,} \var{n} * \var{s}}{\var{n} copies of \var{s} concatenated}{(3)}
342 \hline
343 \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(1)}
344 \lineiii{\var{s}[\var{i}:\var{j}]}{slice of \var{s} from \var{i} to \var{j}}{(1), (2)}
345 \hline
346 \lineiii{len(\var{s})}{length of \var{s}}{}
347 \lineiii{min(\var{s})}{smallest item of \var{s}}{}
348 \lineiii{max(\var{s})}{largest item of \var{s}}{}
349\end{tableiii}
350\indexiii{operations on}{sequence}{types}
351\bifuncindex{len}
352\bifuncindex{min}
353\bifuncindex{max}
354\indexii{concatenation}{operation}
355\indexii{repetition}{operation}
356\indexii{subscript}{operation}
357\indexii{slice}{operation}
358\opindex{in}
359\opindex{not in}
360
361\noindent
362Notes:
363
364\begin{description}
365
366\item[(1)] If \var{i} or \var{j} is negative, the index is relative to
367 the end of the string, i.e., \code{len(\var{s}) + \var{i}} or
368 \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is
369 still \code{0}.
370
371\item[(2)] The slice of \var{s} from \var{i} to \var{j} is defined as
372 the sequence of items with index \var{k} such that \code{\var{i} <=
373 \var{k} < \var{j}}. If \var{i} or \var{j} is greater than
374 \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted,
375 use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If
376 \var{i} is greater than or equal to \var{j}, the slice is empty.
377
378\item[(3)] Values of \var{n} less than \code{0} are treated as
379 \code{0} (which yields an empty sequence of the same type as
380 \var{s}).
381
382\end{description}
383
384\subsubsection{More String Operations}
385
386String objects have one unique built-in operation: the \code{\%}
387operator (modulo) with a string left argument interprets this string
388as a \C{} \cfunction{sprintf()} format string to be applied to the
389right argument, and returns the string resulting from this formatting
390operation.
391
392The right argument should be a tuple with one item for each argument
393required by the format string; if the string requires a single
394argument, the right argument may also be a single non-tuple object.%
395\footnote{A tuple object in this case should be a singleton.}
396The following format characters are understood:
397\code{\%}, \code{c}, \code{s}, \code{i}, \code{d}, \code{u}, \code{o},
398\code{x}, \code{X}, \code{e}, \code{E}, \code{f}, \code{g}, \code{G}.
399Width and precision may be a \code{*} to specify that an integer argument
400specifies the actual width or precision. The flag characters
401\code{-}, \code{+}, blank, \code{\#} and \code{0} are understood. The
402size specifiers \code{h}, \code{l} or \code{L} may be
403present but are ignored. The \code{\%s} conversion takes any Python
404object and converts it to a string using \code{str()} before
405formatting it. The ANSI features \code{\%p} and \code{\%n}
406are not supported. Since Python strings have an explicit length,
407\code{\%s} conversions don't assume that \code{'\e0'} is the end of
408the string.
409
410For safety reasons, floating point precisions are clipped to 50;
411\code{\%f} conversions for numbers whose absolute value is over 1e25
412are replaced by \code{\%g} conversions.%
413\footnote{These numbers are fairly arbitrary. They are intended to
414avoid printing endless strings of meaningless digits without hampering
415correct use and without having to know the exact precision of floating
416point values on a particular machine.}
417All other errors raise exceptions.
418
419If the right argument is a dictionary (or any kind of mapping), then
420the formats in the string must have a parenthesized key into that
421dictionary inserted immediately after the \character{\%} character,
422and each format formats the corresponding entry from the mapping.
423For example:
424
425\begin{verbatim}
426>>> count = 2
427>>> language = 'Python'
428>>> print '%(language)s has %(count)03d quote types.' % vars()
429Python has 002 quote types.
430\end{verbatim}
431
432In this case no \code{*} specifiers may occur in a format (since they
433require a sequential parameter list).
434
435Additional string operations are defined in standard module
436\module{string} and in built-in module \module{re}.
437\refstmodindex{string}
438\refbimodindex{re}
439
440\subsubsection{Mutable Sequence Types}
441
442List objects support additional operations that allow in-place
443modification of the object.
444These operations would be supported by other mutable sequence types
445(when added to the language) as well.
446Strings and tuples are immutable sequence types and such objects cannot
447be modified once created.
448The following operations are defined on mutable sequence types (where
449\var{x} is an arbitrary object):
450\indexiii{mutable}{sequence}{types}
451\indexii{list}{type}
452
453\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
454 \lineiii{\var{s}[\var{i}] = \var{x}}
455 {item \var{i} of \var{s} is replaced by \var{x}}{}
456 \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}}
457 {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{}
458 \lineiii{del \var{s}[\var{i}:\var{j}]}
459 {same as \code{\var{s}[\var{i}:\var{j}] = []}}{}
460 \lineiii{\var{s}.append(\var{x})}
461 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{}
462 \lineiii{\var{s}.count(\var{x})}
463 {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{}
464 \lineiii{\var{s}.index(\var{x})}
465 {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(1)}
466 \lineiii{\var{s}.insert(\var{i}, \var{x})}
467 {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}
468 if \code{\var{i} >= 0}}{}
469 \lineiii{\var{s}.pop(\optional{\var{i}})}
470 {same as \code{\var{x} = \var{s}[\var{i}]; del \var{s}[\var{i}]; return \var{x}}}{(4)}
471 \lineiii{\var{s}.remove(\var{x})}
472 {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(1)}
473 \lineiii{\var{s}.reverse()}
474 {reverses the items of \var{s} in place}{(3)}
475 \lineiii{\var{s}.sort(\optional{\var{cmpfunc}})}
476 {sort the items of \var{s} in place}{(2), (3)}
477\end{tableiii}
478\indexiv{operations on}{mutable}{sequence}{types}
479\indexiii{operations on}{sequence}{types}
480\indexiii{operations on}{list}{type}
481\indexii{subscript}{assignment}
482\indexii{slice}{assignment}
483\stindex{del}
484\setindexsubitem{(list method)}
485\ttindex{append}
486\ttindex{count}
487\ttindex{index}
488\ttindex{insert}
489\ttindex{pop}
490\ttindex{remove}
491\ttindex{reverse}
492\ttindex{sort}
493
494\noindent
495Notes:
496\begin{description}
497\item[(1)] Raises an exception when \var{x} is not found in \var{s}.
498
499\item[(2)] The \code{sort()} method takes an optional argument
500 specifying a comparison function of two arguments (list items) which
501 should return \code{-1}, \code{0} or \code{1} depending on whether the
502 first argument is considered smaller than, equal to, or larger than the
503 second argument. Note that this slows the sorting process down
504 considerably; e.g. to sort a list in reverse order it is much faster
505 to use calls to \code{sort()} and \code{reverse()} than to use
506 \code{sort()} with a comparison function that reverses the ordering of
507 the elements.
508
509\item[(3)] The \code{sort()} and \code{reverse()} methods modify the
510list in place for economy of space when sorting or reversing a large
511list. They don't return the sorted or reversed list to remind you of
512this side effect.
513
514\item[(4)] The \method{pop()} method is experimental and not supported
515by other mutable sequence types than lists.
516The optional argument \var{i} defaults to \code{-1}, so that
517by default the last item is removed and returned.
518
519\end{description}
520
521
522\subsection{Mapping Types}
523\label{typesmapping}
524
525A \dfn{mapping} object maps values of one type (the key type) to
526arbitrary objects. Mappings are mutable objects. There is currently
527only one standard mapping type, the \dfn{dictionary}. A dictionary's keys are
528almost arbitrary values. The only types of values not acceptable as
529keys are values containing lists or dictionaries or other mutable
530types that are compared by value rather than by object identity.
531Numeric types used for keys obey the normal rules for numeric
532comparison: if two numbers compare equal (e.g. \code{1} and
533\code{1.0}) then they can be used interchangeably to index the same
534dictionary entry.
535
536\indexii{mapping}{types}
537\indexii{dictionary}{type}
538
539Dictionaries are created by placing a comma-separated list of
540\code{\var{key}: \var{value}} pairs within braces, for example:
541\code{\{'jack': 4098, 'sjoerd': 4127\}} or
542\code{\{4098: 'jack', 4127: 'sjoerd'\}}.
543
544The following operations are defined on mappings (where \var{a} is a
545mapping, \var{k} is a key and \var{x} is an arbitrary object):
546
547\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
548 \lineiii{len(\var{a})}{the number of items in \var{a}}{}
549 \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
550 \lineiii{\var{a}[\var{k}] = \var{x}}{set \code{\var{a}[\var{k}]} to \var{x}}{}
551 \lineiii{del \var{a}[\var{k}]}{remove \code{\var{a}[\var{k}]} from \var{a}}{(1)}
552 \lineiii{\var{a}.clear()}{remove all items from \code{a}}{}
553 \lineiii{\var{a}.copy()}{a (shallow) copy of \code{a}}{}
554 \lineiii{\var{a}.has_key(\var{k})}{\code{1} if \var{a} has a key \var{k}, else \code{0}}{}
555 \lineiii{\var{a}.items()}{a copy of \var{a}'s list of (key, item) pairs}{(2)}
556 \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(2)}
557 \lineiii{\var{a}.update(\var{b})}{\code{for k, v in \var{b}.items(): \var{a}[k] = v}}{(3)}
558 \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(2)}
559 \lineiii{\var{a}.get(\var{k}\optional{, \var{f}})}{the item of \var{a} with key \var{k}}{(4)}
560\end{tableiii}
561\indexiii{operations on}{mapping}{types}
562\indexiii{operations on}{dictionary}{type}
563\stindex{del}
564\bifuncindex{len}
565\setindexsubitem{(dictionary method)}
566\ttindex{keys}
567\ttindex{has_key}
568
569\noindent
570Notes:
571\begin{description}
572\item[(1)] Raises an exception if \var{k} is not in the map.
573
574\item[(2)] Keys and values are listed in random order.
575
576\item[(3)] \var{b} must be of the same type as \var{a}.
577
578\item[(4)] Never raises an exception if \var{k} is not in the map,
579instead it returns \var{f}. \var{f} is optional, when not provided
580and \var{k} is not in the map, \code{None} is returned.
581\end{description}
582
583
584\subsection{Other Built-in Types}
585\label{typesother}
586
587The interpreter supports several other kinds of objects.
588Most of these support only one or two operations.
589
590\subsubsection{Modules}
591
592The only special operation on a module is attribute access:
593\code{\var{m}.\var{name}}, where \var{m} is a module and \var{name}
594accesses a name defined in \var{m}'s symbol table. Module attributes
595can be assigned to. (Note that the \code{import} statement is not,
596strictly spoking, an operation on a module object; \code{import
597\var{foo}} does not require a module object named \var{foo} to exist,
598rather it requires an (external) \emph{definition} for a module named
599\var{foo} somewhere.)
600
601A special member of every module is \code{__dict__}.
602This is the dictionary containing the module's symbol table.
603Modifying this dictionary will actually change the module's symbol
604table, but direct assignment to the \code{__dict__} attribute is not
605possible (i.e., you can write \code{\var{m}.__dict__['a'] = 1}, which
606defines \code{\var{m}.a} to be \code{1}, but you can't write
607\code{\var{m}.__dict__ = \{\}}.
608
609Modules are written like this: \code{<module 'sys'>}.
610
611\subsubsection{Classes and Class Instances}
612\nodename{Classes and Instances}
613
614See Chapters 3 and 7 of the \emph{Python Reference Manual} for these.
615
616\subsubsection{Functions}
617
618Function objects are created by function definitions. The only
619operation on a function object is to call it:
620\code{\var{func}(\var{argument-list})}.
621
622There are really two flavors of function objects: built-in functions
623and user-defined functions. Both support the same operation (to call
624the function), but the implementation is different, hence the
625different object types.
626
627The implementation adds two special read-only attributes:
628\code{\var{f}.func_code} is a function's \dfn{code
629object}\obindex{code} (see below) and \code{\var{f}.func_globals} is
630the dictionary used as the function's global name space (this is the
631same as \code{\var{m}.__dict__} where \var{m} is the module in which
632the function \var{f} was defined).
633
634
635\subsubsection{Methods}
636\obindex{method}
637
638Methods are functions that are called using the attribute notation.
639There are two flavors: built-in methods (such as \code{append()} on
640lists) and class instance methods. Built-in methods are described
641with the types that support them.
642
643The implementation adds two special read-only attributes to class
644instance methods: \code{\var{m}.im_self} is the object whose method this
645is, and \code{\var{m}.im_func} is the function implementing the method.
646Calling \code{\var{m}(\var{arg-1}, \var{arg-2}, {\rm \ldots},
647\var{arg-n})} is completely equivalent to calling
648\code{\var{m}.im_func(\var{m}.im_self, \var{arg-1}, \var{arg-2}, {\rm
649\ldots}, \var{arg-n})}.
650
651See the \emph{Python Reference Manual} for more information.
652
653\subsubsection{Code Objects}
654\obindex{code}
655
656Code objects are used by the implementation to represent
657``pseudo-compiled'' executable Python code such as a function body.
658They differ from function objects because they don't contain a
659reference to their global execution environment. Code objects are
660returned by the built-in \code{compile()} function and can be
661extracted from function objects through their \code{func_code}
662attribute.
663\bifuncindex{compile}
664\ttindex{func_code}
665
666A code object can be executed or evaluated by passing it (instead of a
667source string) to the \code{exec} statement or the built-in
668\code{eval()} function.
669\stindex{exec}
670\bifuncindex{eval}
671
672See the \emph{Python Reference Manual} for more information.
673
674\subsubsection{Type Objects}
675\label{bltin-type-objects}
676
677Type objects represent the various object types. An object's type is
678accessed by the built-in function \code{type()}. There are no special
679operations on types. The standard module \code{types} defines names
680for all standard built-in types.
681\bifuncindex{type}
682\refstmodindex{types}
683
684Types are written like this: \code{<type 'int'>}.
685
686\subsubsection{The Null Object}
687\label{bltin-null-object}
688
689This object is returned by functions that don't explicitly return a
690value. It supports no special operations. There is exactly one null
691object, named \code{None} (a built-in name).
692
693It is written as \code{None}.
694
695\subsubsection{File Objects}
696\label{bltin-file-objects}
697
698File objects are implemented using \C{}'s \code{stdio} package and can be
699created with the built-in function \code{open()} described under
700Built-in Functions below. They are also returned by some other
701built-in functions and methods, e.g.\ \code{posix.popen()} and
702\code{posix.fdopen()} and the \code{makefile()} method of socket
703objects.
704\bifuncindex{open}
705\refbimodindex{posix}
706\refbimodindex{socket}
707
708When a file operation fails for an I/O-related reason, the exception
709\code{IOError} is raised. This includes situations where the
710operation is not defined for some reason, like \code{seek()} on a tty
711device or writing a file opened for reading.
712
713Files have the following methods:
714
715
716\begin{methoddesc}[file]{close}{}
717 Close the file. A closed file cannot be read or written anymore.
718\end{methoddesc}
719
720\begin{methoddesc}[file]{flush}{}
721 Flush the internal buffer, like \code{stdio}'s \code{fflush()}.
722\end{methoddesc}
723
724\begin{methoddesc}[file]{isatty}{}
725 Return \code{1} if the file is connected to a tty(-like) device, else
726 \code{0}.
727\end{methoddesc}
728
729\begin{methoddesc}[file]{fileno}{}
730Return the integer ``file descriptor'' that is used by the underlying
731implementation to request I/O operations from the operating system.
732This can be useful for other, lower level interfaces that use file
733descriptors, e.g. module \code{fcntl} or \code{os.read()} and friends.
734\refbimodindex{fcntl}
735\end{methoddesc}
736
737\begin{methoddesc}[file]{read}{\optional{size}}
738 Read at most \var{size} bytes from the file (less if the read hits
739 \EOF{} or no more data is immediately available on a pipe, tty or
740 similar device). If the \var{size} argument is negative or omitted,
741 read all data until \EOF{} is reached. The bytes are returned as a string
742 object. An empty string is returned when \EOF{} is encountered
743 immediately. (For certain files, like ttys, it makes sense to
744 continue reading after an \EOF{} is hit.)
745\end{methoddesc}
746
747\begin{methoddesc}[file]{readline}{\optional{size}}
748 Read one entire line from the file. A trailing newline character is
749 kept in the string%
750\footnote{The advantage of leaving the newline on is that an empty string
751 can be returned to mean \EOF{} without being ambiguous. Another
752 advantage is that (in cases where it might matter, e.g. if you
753 want to make an exact copy of a file while scanning its lines)
754 you can tell whether the last line of a file ended in a newline
755 or not (yes this happens!).}
756 (but may be absent when a file ends with an
757 incomplete line). If the \var{size} argument is present and
758 non-negative, it is a maximum byte count (including the trailing
759 newline) and an incomplete line may be returned.
760 An empty string is returned when \EOF{} is hit
761 immediately. Note: unlike \code{stdio}'s \cfunction{fgets()}, the returned
762 string contains null characters (\code{'\e 0'}) if they occurred in the
763 input.
764\end{methoddesc}
765
766\begin{methoddesc}[file]{readlines}{\optional{sizehint}}
767 Read until \EOF{} using \method{readline()} and return a list containing
768 the lines thus read. If the optional \var{sizehint} argument is
769 present, instead of reading up to \EOF{}, whole lines totalling
770 approximately \var{sizehint} bytes (possibly after rounding up to an
771 internal buffer size) are read.
772\end{methoddesc}
773
774\begin{methoddesc}[file]{seek}{offset\optional{, whence}}
775 Set the file's current position, like \code{stdio}'s \cfunction{fseek()}.
776 The \var{whence} argument is optional and defaults to \code{0}
777 (absolute file positioning); other values are \code{1} (seek
778 relative to the current position) and \code{2} (seek relative to the
779 file's end). There is no return value.
780\end{methoddesc}
781
782\begin{methoddesc}[file]{tell}{}
783 Return the file's current position, like \code{stdio}'s
784 \cfunction{ftell()}.
785\end{methoddesc}
786
787\begin{methoddesc}[file]{truncate}{\optional{size}}
788Truncate the file's size. If the optional size argument present, the
789file is truncated to (at most) that size. The size defaults to the
790current position. Availability of this function depends on the
791operating system version (e.g., not all \UNIX{} versions support this
792operation).
793\end{methoddesc}
794
795\begin{methoddesc}[file]{write}{str}
796Write a string to the file. There is no return value. Note: due to
797buffering, the string may not actually show up in the file until
798the \method{flush()} or \method{close()} method is called.
799\end{methoddesc}
800
801\begin{methoddesc}[file]{writelines}{list}
802Write a list of strings to the file. There is no return value.
803(The name is intended to match \method{readlines()};
804\method{writelines()} does not add line separators.)
805\end{methoddesc}
806
807
808File objects also offer the following attributes:
809
810\begin{memberdesc}[file]{closed}
811Boolean indicating the current state of the file object. This is a
812read-only attribute; the \method{close()} method changes the value.
813\end{memberdesc}
814
815\begin{memberdesc}[file]{mode}
816The I/O mode for the file. If the file was created using the
817\function{open()} built-in function, this will be the value of the
818\var{mode} parameter. This is a read-only attribute.
819\end{memberdesc}
820
821\begin{memberdesc}[file]{name}
822If the file object was created using \function{open()}, the name of
823the file. Otherwise, some string that indicates the source of the
824file object, of the form \samp{<\mbox{\ldots}>}. This is a read-only
825attribute.
826\end{memberdesc}
827
828\begin{memberdesc}[file]{softspace}
829Boolean that indicates whether a space character needs to be printed
830before another value when using the \keyword{print} statement.
831Classes that are trying to simulate a file object should also have a
832writable \member{softspace} attribute, which should be initialized to
833zero. This will be automatic for classes implemented in Python; types
834implemented in \C{} will have to provide a writable \member{softspace}
835attribute.
836\end{memberdesc}
837
838\subsubsection{Internal Objects}
839
840See the \emph{Python Reference Manual} for this information. It
841describes code objects, stack frame objects, traceback objects, and
842slice objects.
843
844
845\subsection{Special Attributes}
846\label{specialattrs}
847
848The implementation adds a few special read-only attributes to several
849object types, where they are relevant:
850
851\begin{itemize}
852
853\item
854\code{\var{x}.__dict__} is a dictionary of some sort used to store an
855object's (writable) attributes;
856
857\item
858\code{\var{x}.__methods__} lists the methods of many built-in object types,
859e.g., \code{[].__methods__} yields
860\code{['append', 'count', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']};
861
862\item
863\code{\var{x}.__members__} lists data attributes;
864
865\item
866\code{\var{x}.__class__} is the class to which a class instance belongs;
867
868\item
869\code{\var{x}.__bases__} is the tuple of base classes of a class object.
870
871\end{itemize}