blob: b5dadaf17aeaf5e7e0db5c50a5fb5270d155eab1 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Types}
2
3The following sections describe the standard types that are built into
4the interpreter. These are the numeric types, sequence types, and
5several others, including types themselves. There is no explicit
6Boolean type; use integers instead.
7\indexii{built-in}{types}
8\indexii{Boolean}{type}
9
10Some operations are supported by several object types; in particular,
11all objects can be compared, tested for truth value, and converted to
12a string (with the \code{`{\rm \ldots}`} notation). The latter conversion is
13implicitly used when an object is written by the \code{print} statement.
14\stindex{print}
15
16\subsection{Truth Value Testing}
17
18Any object can be tested for truth value, for use in an \code{if} or
19\code{while} condition or as operand of the Boolean operations below.
20The following values are false:
21\stindex{if}
22\stindex{while}
23\indexii{truth}{value}
24\indexii{Boolean}{operations}
25\index{false}
26
27\begin{itemize}
28\renewcommand{\indexsubitem}{(Built-in object)}
29
30\item \code{None}
31 \ttindex{None}
32
33\item zero of any numeric type, e.g., \code{0}, \code{0L}, \code{0.0}.
34
35\item any empty sequence, e.g., \code{''}, \code{()}, \code{[]}.
36
37\item any empty mapping, e.g., \code{\{\}}.
38
39\end{itemize}
40
41\emph{All} other values are true --- so objects of many types are
42always true.
43\index{true}
44
45\subsection{Boolean Operations}
46
47These are the Boolean operations:
48\indexii{Boolean}{operations}
49
50\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
51 \lineiii{\var{x} or \var{y}}{if \var{x} is false, then \var{y}, else \var{x}}{(1)}
52 \lineiii{\var{x} and \var{y}}{if \var{x} is false, then \var{x}, else \var{y}}{(1)}
53 \lineiii{not \var{x}}{if \var{x} is false, then \code{1}, else \code{0}}{}
54\end{tableiii}
55\opindex{and}
56\opindex{or}
57\opindex{not}
58
59\noindent
60Notes:
61
62\begin{description}
63
64\item[(1)]
65These only evaluate their second argument if needed for their outcome.
66
67\end{description}
68
69\subsection{Comparisons}
70
71Comparison operations are supported by all objects:
72
73\begin{tableiii}{|c|l|c|}{code}{Operation}{Meaning}{Notes}
74 \lineiii{<}{strictly less than}{}
75 \lineiii{<=}{less than or equal}{}
76 \lineiii{>}{strictly greater than}{}
77 \lineiii{>=}{greater than or equal}{}
78 \lineiii{==}{equal}{}
79 \lineiii{<>}{not equal}{(1)}
80 \lineiii{!=}{not equal}{(1)}
81 \lineiii{is}{object identity}{}
82 \lineiii{is not}{negated object identity}{}
83\end{tableiii}
84\indexii{operator}{comparison}
85\opindex{==} % XXX *All* others have funny characters < ! >
86\opindex{is}
87\opindex{is not}
88
89\noindent
90Notes:
91
92\begin{description}
93
94\item[(1)]
95\code{<>} and \code{!=} are alternate spellings for the same operator.
96(I couldn't choose between \ABC{} and \C{}! :-)
97\indexii{\ABC{}}{language}
98\indexii{\C{}}{language}
99
100\end{description}
101
102Objects of different types, except different numeric types, never
103compare equal; such objects are ordered consistently but arbitrarily
104(so that sorting a heterogeneous array yields a consistent result).
105Furthermore, some types (e.g., windows) support only a degenerate
106notion of comparison where any two objects of that type are unequal.
107Again, such objects are ordered arbitrarily but consistently.
108\indexii{types}{numeric}
109\indexii{objects}{comparing}
110
111(Implementation note: objects of different types except numbers are
112ordered by their type names; objects of the same types that don't
113support proper comparison are ordered by their address.)
114
115Two more operations with the same syntactic priority, \code{in} and
116\code{not in}, are supported only by sequence types (below).
117\opindex{in}
118\opindex{not in}
119
120\subsection{Numeric Types}
121
122There are three numeric types: \dfn{plain integers}, \dfn{long integers}, and
123\dfn{floating point numbers}. Plain integers (also just called \dfn{integers})
124are implemented using \code{long} in \C{}, which gives them at least 32
125bits of precision. Long integers have unlimited precision. Floating
126point numbers are implemented using \code{double} in \C{}. All bets on
127their precision are off unless you happen to know the machine you are
128working with.
129\indexii{numeric}{types}
130\indexii{integer}{types}
131\indexii{integer}{type}
132\indexiii{long}{integer}{type}
133\indexii{floating point}{type}
134\indexii{\C{}}{language}
135
136Numbers are created by numeric literals or as the result of built-in
137functions and operators. Unadorned integer literals (including hex
138and octal numbers) yield plain integers. Integer literals with an \samp{L}
139or \samp{l} suffix yield long integers
140(\samp{L} is preferred because \code{1l} looks too much like eleven!).
141Numeric literals containing a decimal point or an exponent sign yield
142floating point numbers.
143\indexii{numeric}{literals}
144\indexii{integer}{literals}
145\indexiii{long}{integer}{literals}
146\indexii{floating point}{literals}
147\indexii{hexadecimal}{literals}
148\indexii{octal}{literals}
149
150Python fully supports mixed arithmetic: when a binary arithmetic
151operator has operands of different numeric types, the operand with the
152``smaller'' type is converted to that of the other, where plain
153integer is smaller than long integer is smaller than floating point.
154Comparisons between numbers of mixed type use the same rule.%
155\footnote{As a consequence, the list \code{[1, 2]} is considered equal
156 to \code{[1.0, 2.0]}, and similar for tuples.}
157The functions \code{int()}, \code{long()} and \code{float()} can be used
158to coerce numbers to a specific type.
159\index{arithmetic}
160\bifuncindex{int}
161\bifuncindex{long}
162\bifuncindex{float}
163
164All numeric types support the following operations:
165
166\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
167 \lineiii{abs(\var{x})}{absolute value of \var{x}}{}
168 \lineiii{int(\var{x})}{\var{x} converted to integer}{(1)}
169 \lineiii{long(\var{x})}{\var{x} converted to long integer}{(1)}
170 \lineiii{float(\var{x})}{\var{x} converted to floating point}{}
171 \lineiii{-\var{x}}{\var{x} negated}{}
172 \lineiii{+\var{x}}{\var{x} unchanged}{}
173 \lineiii{\var{x} + \var{y}}{sum of \var{x} and \var{y}}{}
174 \lineiii{\var{x} - \var{y}}{difference of \var{x} and \var{y}}{}
175 \lineiii{\var{x} * \var{y}}{product of \var{x} and \var{y}}{}
176 \lineiii{\var{x} / \var{y}}{quotient of \var{x} and \var{y}}{(2)}
177 \lineiii{\var{x} \%{} \var{y}}{remainder of \code{\var{x} / \var{y}}}{}
178 \lineiii{divmod(\var{x}, \var{y})}{the pair \code{(\var{x} / \var{y}, \var{x} \%{} \var{y})}}{(3)}
179 \lineiii{pow(\var{x}, \var{y})}{\var{x} to the power \var{y}}{}
180\end{tableiii}
181\indexiii{operations on}{numeric}{types}
182
183\noindent
184Notes:
185\begin{description}
186\item[(1)]
187Conversion from floating point to (long or plain) integer may round or
188% XXXJH xref here
189truncate as in \C{}; see functions \code{floor} and \code{ceil} in module
190\code{math} for well-defined conversions.
191\indexii{numeric}{conversions}
192\ttindex{math}
193\indexii{\C{}}{language}
194
195\item[(2)]
196For (plain or long) integer division, the result is an integer; it
197always truncates towards zero.
198% XXXJH integer division is better defined nowadays
199\indexii{integer}{division}
200\indexiii{long}{integer}{division}
201
202\item[(3)]
203See the section on built-in functions for an exact definition.
204
205\end{description}
206% XXXJH exceptions: overflow (when? what operations?) zerodivision
207
208\subsubsection{Bit-string Operations on Integer Types.}
209
210Plain and long integer types support additional operations that make
211sense only for bit-strings. Negative numbers are treated as their 2's
212complement value:
213
214\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
215 \lineiii{\~\var{x}}{the bits of \var{x} inverted}{}
216 \lineiii{\var{x} \^{} \var{y}}{bitwise \dfn{exclusive or} of \var{x} and \var{y}}{}
217 \lineiii{\var{x} \&{} \var{y}}{bitwise \dfn{and} of \var{x} and \var{y}}{}
218 \lineiii{\var{x} | \var{y}}{bitwise \dfn{or} of \var{x} and \var{y}}{}
219 \lineiii{\var{x} << \var{n}}{\var{x} shifted left by \var{n} bits}{}
220 \lineiii{\var{x} >> \var{n}}{\var{x} shifted right by \var{n} bits}{}
221\end{tableiii}
222% XXXJH what's `left'? `right'? maybe better use lsb or msb or something
223\indexiii{operations on}{integer}{types}
224\indexii{bit-string}{operations}
225\indexii{shifting}{operations}
226\indexii{masking}{operations}
227
228\subsection{Sequence Types}
229
230There are three sequence types: strings, lists and tuples.
231Strings literals are written in single quotes: \code{'xyzzy'}.
232Lists are constructed with square brackets,
233separating items with commas:
234\code{[a, b, c]}.
235Tuples are constructed by the comma operator
236(not within square brackets), with or without enclosing parentheses,
237but an empty tuple must have the enclosing parentheses, e.g.,
238\code{a, b, c} or \code{()}. A single item tuple must have a trailing comma,
239e.g., \code{(d,)}.
240\indexii{sequence}{types}
241\indexii{string}{type}
242\indexii{tuple}{type}
243\indexii{list}{type}
244
245Sequence types support the following operations (\var{s} and \var{t} are
246sequences of the same type; \var{n}, \var{i} and \var{j} are integers):
247
248\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
249 \lineiii{len(\var{s})}{length of \var{s}}{}
250 \lineiii{min(\var{s})}{smallest item of \var{s}}{}
251 \lineiii{max(\var{s})}{largest item of \var{s}}{}
252 \lineiii{\var{x} in \var{s}}{\code{1} if an item of \var{s} is equal to \var{x}, else \code{0}}{}
253 \lineiii{\var{x} not in \var{s}}{\code{0} if an item of \var{s} is equal to \var{x}, else \code{1}}{}
254 \lineiii{\var{s} + \var{t}}{the concatenation of \var{s} and \var{t}}{}
255 \lineiii{\var{s} * \var{n}{\rm ,} \var{n} * \var{s}}{\var{n} copies of \var{s} concatenated}{}
256 \lineiii{\var{s}[\var{i}]}{\var{i}'th item of \var{s}, origin 0}{(1)}
257 \lineiii{\var{s}[\var{i}:\var{j}]}{slice of \var{s} from \var{i} to \var{j}}{(1), (2)}
258\end{tableiii}
259\indexiii{operations on}{sequence}{types}
260\bifuncindex{len}
261\bifuncindex{min}
262\bifuncindex{max}
263\indexii{concatenation}{operation}
264\indexii{repetition}{operation}
265\indexii{subscript}{operation}
266\indexii{slice}{operation}
267\opindex{in}
268\opindex{not in}
269
270\noindent
271Notes:
272
273% XXXJH all TeX-math expressions replaced by python-syntax expressions
274\begin{description}
275
276\item[(1)] If \var{i} or \var{j} is negative, the index is relative to
277 the end of the string, i.e., \code{len(\var{s}) + \var{i}} or
278 \code{len(\var{s}) + \var{j}} is substituted. But note that \code{-0} is
279 still \code{0}.
280
281\item[(2)] The slice of \var{s} from \var{i} to \var{j} is defined as
282 the sequence of items with index \var{k} such that \code{\var{i} <=
283 \var{k} < \var{j}}. If \var{i} or \var{j} is greater than
284 \code{len(\var{s})}, use \code{len(\var{s})}. If \var{i} is omitted,
285 use \code{0}. If \var{j} is omitted, use \code{len(\var{s})}. If
286 \var{i} is greater than or equal to \var{j}, the slice is empty.
287
288\end{description}
289
290\subsubsection{More String Operations.}
291
292String objects have one unique built-in operation: the \code{\%}
293operator (modulo) with a string left argument interprets this string
294as a C sprintf format string to be applied to the right argument, and
295returns the string resulting from this formatting operation.
296
Guido van Rossum8b605eb1994-06-23 12:14:07 +0000297The right argument should be a tuple with one item for each argument
298required by the format string; if the string requires a single
299argument, the right argument may also be a single non-tuple object.%
300\footnote{A tuple object in this case should be a singleton.}
301The following format characters are understood:
302\%, c, s, i, d, u, o, x, X, e, E, f, g, G.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000303Width and precision may be a * to specify that an integer argument
304specifies the actual width or precision. The flag characters -, +,
305blank, \# and 0 are understood. The size specifiers h, l or L may be
Guido van Rossum17383111994-04-21 10:32:28 +0000306present but are ignored. The \code{\%s} conversion takes any Python
307object and converts it to a string using \code{str()} before
308formatting it. The ANSI features \code{\%p} and \code{\%n}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000309are not supported. Since Python strings have an explicit length,
310\code{\%s} conversions don't assume that \code{'\\0'} is the end of
311the string.
312
Guido van Rossume6ef0321994-05-09 14:54:24 +0000313For safety reasons, floating point precisions are clipped to 50;
314\code{\%f} conversions for numbers whose absolute value is over 1e25
315are replaced by \code{\%g} conversions.%
316\footnote{These numbers are fairly arbitrary. They are intended to
317avoid printing endless strings of meaningless digits without hampering
318correct use and without having to know the exact precision of floating
319point values on a particular machine.}
320All other errors raise exceptions.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000321
Guido van Rossum17383111994-04-21 10:32:28 +0000322If the right argument is a dictionary (or any kind of mapping), then
323the formats in the string must have a parenthesized key into that
324dictionary inserted immediately after the \code{\%} character, and
325each format formats the corresponding entry from the mapping. E.g.
326\begin{verbatim}
327 >>> count = 2
328 >>> language = 'Python'
329 >>> print '%(language)s has %(count)03d quote types.' % vars()
330 Python has 002 quote types.
331 >>>
332\end{verbatim}
333In this case no * specifiers may occur in a format.
334
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000335Additional string operations are defined in standard module
336\code{string} and in built-in module \code{regex}.
337\index{string}
338\index{regex}
339
340\subsubsection{Mutable Sequence Types.}
341
342List objects support additional operations that allow in-place
343modification of the object.
344These operations would be supported by other mutable sequence types
345(when added to the language) as well.
346Strings and tuples are immutable sequence types and such objects cannot
347be modified once created.
348The following operations are defined on mutable sequence types (where
349\var{x} is an arbitrary object):
350\indexiii{mutable}{sequence}{types}
351\indexii{list}{type}
352
353\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
354 \lineiii{\var{s}[\var{i}] = \var{x}}
355 {item \var{i} of \var{s} is replaced by \var{x}}{}
356 \lineiii{\var{s}[\var{i}:\var{j}] = \var{t}}
357 {slice of \var{s} from \var{i} to \var{j} is replaced by \var{t}}{}
358 \lineiii{del \var{s}[\var{i}:\var{j}]}
359 {same as \code{\var{s}[\var{i}:\var{j}] = []}}{}
360 \lineiii{\var{s}.append(\var{x})}
Guido van Rossume6ef0321994-05-09 14:54:24 +0000361 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000362 \lineiii{\var{s}.count(\var{x})}
363 {return number of \var{i}'s for which \code{\var{s}[\var{i}] == \var{x}}}{}
364 \lineiii{\var{s}.index(\var{x})}
365 {return smallest \var{i} such that \code{\var{s}[\var{i}] == \var{x}}}{(1)}
366 \lineiii{\var{s}.insert(\var{i}, \var{x})}
367 {same as \code{\var{s}[\var{i}:\var{i}] = [\var{x}]}}{}
368 \lineiii{\var{s}.remove(\var{x})}
369 {same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(1)}
370 \lineiii{\var{s}.reverse()}
371 {reverses the items of \var{s} in place}{}
372 \lineiii{\var{s}.sort()}
373 {permutes the items of \var{s} to satisfy
374 \code{\var{s}[\var{i}] <= \var{s}[\var{j}]},
375 for \code{\var{i} < \var{j}}}{(2)}
376\end{tableiii}
377\indexiv{operations on}{mutable}{sequence}{types}
378\indexiii{operations on}{sequence}{types}
379\indexiii{operations on}{list}{type}
380\indexii{subscript}{assignment}
381\indexii{slice}{assignment}
382\stindex{del}
383\renewcommand{\indexsubitem}{(list method)}
384\ttindex{append}
385\ttindex{count}
386\ttindex{index}
387\ttindex{insert}
388\ttindex{remove}
389\ttindex{reverse}
390\ttindex{sort}
391
392\noindent
393Notes:
394\begin{description}
395\item[(1)] Raises an exception when \var{x} is not found in \var{s}.
396
397\item[(2)] The \code{sort()} method takes an optional argument
398 specifying a comparison function of two arguments (list items) which
399 should return \code{-1}, \code{0} or \code{1} depending on whether the
400 first argument is considered smaller than, equal to, or larger than the
401 second argument. Note that this slows the sorting process down
402 considerably; e.g. to sort an array in reverse order it is much faster
403 to use calls to \code{sort()} and \code{reverse()} than to use
404 \code{sort()} with a comparison function that reverses the ordering of
405 the elements.
406\end{description}
407
408\subsection{Mapping Types}
409
410A \dfn{mapping} object maps values of one type (the key type) to
411arbitrary objects. Mappings are mutable objects. There is currently
412only one mapping type, the \dfn{dictionary}. A dictionary's keys are
413almost arbitrary values. The only types of values not acceptable as
414keys are values containing lists or dictionaries or other mutable
415types that are compared by value rather than by object identity.
416Numeric types used for keys obey the normal rules for numeric
417comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
418can be used interchangeably to index the same dictionary entry.
419
420\indexii{mapping}{types}
421\indexii{dictionary}{type}
422
423Dictionaries are created by placing a comma-separated list of
424\code{\var{key}: \var{value}} pairs within braces, for example:
425\code{\{'jack': 4098, 'sjoerd: 4127\}} or
426\code{\{4098: 'jack', 4127: 'sjoerd\}}.
427
428The following operations are defined on mappings (where \var{a} is a
429mapping, \var{k} is a key and \var{x} is an arbitrary object):
430
431\begin{tableiii}{|c|l|c|}{code}{Operation}{Result}{Notes}
432 \lineiii{len(\var{a})}{the number of items in \var{a}}{}
433 \lineiii{\var{a}[\var{k}]}{the item of \var{a} with key \var{k}}{(1)}
434 \lineiii{\var{a}[\var{k}] = \var{x}}{set \code{\var{a}[\var{k}]} to \var{x}}{}
435 \lineiii{del \var{a}[\var{k}]}{remove \code{\var{a}[\var{k}]} from \var{a}}{(1)}
436 \lineiii{\var{a}.items()}{a copy of \var{a}'s list of (key, item) pairs}{(2)}
437 \lineiii{\var{a}.keys()}{a copy of \var{a}'s list of keys}{(2)}
438 \lineiii{\var{a}.values()}{a copy of \var{a}'s list of values}{(2)}
439 \lineiii{\var{a}.has_key(\var{k})}{\code{1} if \var{a} has a key \var{k}, else \code{0}}{}
440\end{tableiii}
441\indexiii{operations on}{mapping}{types}
442\indexiii{operations on}{dictionary}{type}
443\stindex{del}
444\bifuncindex{len}
445\renewcommand{\indexsubitem}{(dictionary method)}
446\ttindex{keys}
447\ttindex{has_key}
448
449% XXXJH some lines above, you talk about `true', elsewhere you
450% explicitely states \code{0} or \code{1}.
451\noindent
452Notes:
453\begin{description}
454\item[(1)] Raises an exception if \var{k} is not in the map.
455
456\item[(2)] Keys and values are listed in random order, but at any
457moment the ordering of the \code{keys()}, \code{values()} and
458\code{items()} lists is the consistent with each other.
459\end{description}
460
461\subsection{Other Built-in Types}
462
463The interpreter supports several other kinds of objects.
464Most of these support only one or two operations.
465
466\subsubsection{Modules.}
467
468The only special operation on a module is attribute access:
469\code{\var{m}.\var{name}}, where \var{m} is a module and \var{name} accesses
470a name defined in \var{m}'s symbol table. Module attributes can be
471assigned to. (Note that the \code{import} statement is not, strictly
472spoken, an operation on a module object; \code{import \var{foo}} does not
473require a module object named \var{foo} to exist, rather it requires
474an (external) \emph{definition} for a module named \var{foo}
475somewhere.)
476
477A special member of every module is \code{__dict__}.
478This is the dictionary containing the module's symbol table.
479Modifying this dictionary will actually change the module's symbol
480table, but direct assignment to the \code{__dict__} attribute is not
481possible (i.e., you can write \code{\var{m}.__dict__['a'] = 1}, which
482defines \code{\var{m}.a} to be \code{1}, but you can't write \code{\var{m}.__dict__ = \{\}}.
483
484Modules are written like this: \code{<module 'sys'>}.
485
486\subsubsection{Classes and Class Instances.}
487% XXXJH cross ref here
488(See the Python Reference Manual for these.)
489
490\subsubsection{Functions.}
491
492Function objects are created by function definitions. The only
493operation on a function object is to call it:
494\code{\var{func}(\var{argument-list})}.
495
496There are really two flavors of function objects: built-in functions
497and user-defined functions. Both support the same operation (to call
498the function), but the implementation is different, hence the
499different object types.
500
501The implementation adds two special read-only attributes:
502\code{\var{f}.func_code} is a function's \dfn{code object} (see below) and
503\code{\var{f}.func_globals} is the dictionary used as the function's
504global name space (this is the same as \code{\var{m}.__dict__} where
505\var{m} is the module in which the function \var{f} was defined).
506
507\subsubsection{Methods.}
508
509Methods are functions that are called using the attribute notation.
510There are two flavors: built-in methods (such as \code{append()} on
511lists) and class instance methods. Built-in methods are described
512with the types that support them.
513
514The implementation adds two special read-only attributes to class
515instance methods: \code{\var{m}.im_self} is the object whose method this
516is, and \code{\var{m}.im_func} is the function implementing the method.
517Calling \code{\var{m}(\var{arg-1}, \var{arg-2}, {\rm \ldots},
518\var{arg-n})} is completely equivalent to calling
519\code{\var{m}.im_func(\var{m}.im_self, \var{arg-1}, \var{arg-2}, {\rm
520\ldots}, \var{arg-n})}.
521
522(See the Python Reference Manual for more info.)
523
524\subsubsection{Type Objects.}
525
526Type objects represent the various object types. An object's type is
527% XXXJH xref here
528accessed by the built-in function \code{type()}. There are no special
529operations on types.
530
531Types are written like this: \code{<type 'int'>}.
532
533\subsubsection{The Null Object.}
534
535This object is returned by functions that don't explicitly return a
536value. It supports no special operations. There is exactly one null
537object, named \code{None} (a built-in name).
538
539It is written as \code{None}.
540
541\subsubsection{File Objects.}
542
543File objects are implemented using \C{}'s \code{stdio} package and can be
544% XXXJH xref here
545created with the built-in function \code{open()} described under
546Built-in Functions below.
547
548When a file operation fails for an I/O-related reason, the exception
549\code{IOError} is raised. This includes situations where the
550operation is not defined for some reason, like \code{seek()} on a tty
551device or writing a file opened for reading.
552
553Files have the following methods:
554
555
556\renewcommand{\indexsubitem}{(file method)}
557
558\begin{funcdesc}{close}{}
559 Close the file. A closed file cannot be read or written anymore.
560\end{funcdesc}
561
562\begin{funcdesc}{flush}{}
563 Flush the internal buffer, like \code{stdio}'s \code{fflush()}.
564\end{funcdesc}
565
566\begin{funcdesc}{isatty}{}
567 Return \code{1} if the file is connected to a tty(-like) device, else
568 \code{0}.
569\end{funcdesc}
570
571\begin{funcdesc}{read}{size}
572 Read at most \var{size} bytes from the file (less if the read hits
573 \EOF{} or no more data is immediately available on a pipe, tty or
574 similar device). If the \var{size} argument is omitted, read all
575 data until \EOF{} is reached. The bytes are returned as a string
576 object. An empty string is returned when \EOF{} is encountered
577 immediately. (For certain files, like ttys, it makes sense to
578 continue reading after an \EOF{} is hit.)
579\end{funcdesc}
580
581\begin{funcdesc}{readline}{}
582 Read one entire line from the file. A trailing newline character is
583 kept in the string (but may be absent when a file ends with an
584 incomplete line). An empty string is returned when \EOF{} is hit
585 immediately. Note: unlike \code{stdio}'s \code{fgets()}, the returned
586 string contains null characters (\code{'\e 0'}) if they occurred in the
587 input.
588\end{funcdesc}
589
590\begin{funcdesc}{readlines}{}
591 Read until \EOF{} using \code{readline()} and return a list containing
592 the lines thus read.
593\end{funcdesc}
594
595\begin{funcdesc}{seek}{offset\, whence}
596 Set the file's current position, like \code{stdio}'s \code{fseek()}.
597 The \var{whence} argument is optional and defaults to \code{0}
598 (absolute file positioning); other values are \code{1} (seek
599 relative to the current position) and \code{2} (seek relative to the
600 file's end). There is no return value.
601\end{funcdesc}
602
603\begin{funcdesc}{tell}{}
604 Return the file's current position, like \code{stdio}'s \code{ftell()}.
605\end{funcdesc}
606
607\begin{funcdesc}{write}{str}
608 Write a string to the file. There is no return value.
609\end{funcdesc}
610
Guido van Rossum8b605eb1994-06-23 12:14:07 +0000611\begin{funcdesc}{writelines}{list}
612Write a list of strings to the file. There is no return value.
613(The name is intended to match \code{readlines}; \code{writelines}
614does not add line separators.)
615\end{funcdesc}
616
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000617\subsubsection{Internal Objects.}
618
619(See the Python Reference Manual for these.)
620
621\subsection{Special Attributes}
622
623The implementation adds a few special read-only attributes to several
624object types, where they are relevant:
625
626\begin{itemize}
627
628\item
629\code{\var{x}.__dict__} is a dictionary of some sort used to store an
630object's (writable) attributes;
631
632\item
633\code{\var{x}.__methods__} lists the methods of many built-in object types,
634e.g., \code{[].__methods__} is
635% XXXJH results in?, yields?, written down as an example
636\code{['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']};
637
638\item
639\code{\var{x}.__members__} lists data attributes;
640
641\item
642\code{\var{x}.__class__} is the class to which a class instance belongs;
643
644\item
645\code{\var{x}.__bases__} is the tuple of base classes of a class object.
646
647\end{itemize}