improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Doc / libtypes.tex
blob1d5beb4303587e56c0a8ce0fc6ee5e4c3aa6ee5f
1 \section{Built-in Types}
3 The following sections describe the standard types that are built into
4 the interpreter. These are the numeric types, sequence types, and
5 several others, including types themselves. There is no explicit
6 Boolean type; use integers instead.
7 \indexii{built-in}{types}
8 \indexii{Boolean}{type}
10 Some operations are supported by several object types; in particular,
11 all objects can be compared, tested for truth value, and converted to
12 a string (with the \code{`{\rm \ldots}`} notation). The latter conversion is
13 implicitly used when an object is written by the \code{print} statement.
14 \stindex{print}
16 \subsection{Truth Value Testing}
18 Any 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.
20 The following values are false:
21 \stindex{if}
22 \stindex{while}
23 \indexii{truth}{value}
24 \indexii{Boolean}{operations}
25 \index{false}
27 \begin{itemize}
28 \renewcommand{\indexsubitem}{(Built-in object)}
30 \item \code{None}
31 \ttindex{None}
33 \item zero of any numeric type, e.g., \code{0}, \code{0L}, \code{0.0}.
35 \item any empty sequence, e.g., \code{''}, \code{()}, \code{[]}.
37 \item any empty mapping, e.g., \code{\{\}}.
39 \end{itemize}
41 \emph{All} other values are true --- so objects of many types are
42 always true.
43 \index{true}
45 \subsection{Boolean Operations}
47 These are the Boolean operations:
48 \indexii{Boolean}{operations}
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}
59 \noindent
60 Notes:
62 \begin{description}
64 \item[(1)]
65 These only evaluate their second argument if needed for their outcome.
67 \end{description}
69 \subsection{Comparisons}
71 Comparison operations are supported by all objects:
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}
89 \noindent
90 Notes:
92 \begin{description}
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}
100 \end{description}
102 Objects of different types, except different numeric types, never
103 compare equal; such objects are ordered consistently but arbitrarily
104 (so that sorting a heterogeneous array yields a consistent result).
105 Furthermore, some types (e.g., windows) support only a degenerate
106 notion of comparison where any two objects of that type are unequal.
107 Again, such objects are ordered arbitrarily but consistently.
108 \indexii{types}{numeric}
109 \indexii{objects}{comparing}
111 (Implementation note: objects of different types except numbers are
112 ordered by their type names; objects of the same types that don't
113 support proper comparison are ordered by their address.)
115 Two 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}
120 \subsection{Numeric Types}
122 There are three numeric types: \dfn{plain integers}, \dfn{long integers}, and
123 \dfn{floating point numbers}. Plain integers (also just called \dfn{integers})
124 are implemented using \code{long} in \C{}, which gives them at least 32
125 bits of precision. Long integers have unlimited precision. Floating
126 point numbers are implemented using \code{double} in \C{}. All bets on
127 their precision are off unless you happen to know the machine you are
128 working 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}
136 Numbers are created by numeric literals or as the result of built-in
137 functions and operators. Unadorned integer literals (including hex
138 and octal numbers) yield plain integers. Integer literals with an \samp{L}
139 or \samp{l} suffix yield long integers
140 (\samp{L} is preferred because \code{1l} looks too much like eleven!).
141 Numeric literals containing a decimal point or an exponent sign yield
142 floating 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}
150 Python fully supports mixed arithmetic: when a binary arithmetic
151 operator has operands of different numeric types, the operand with the
152 ``smaller'' type is converted to that of the other, where plain
153 integer is smaller than long integer is smaller than floating point.
154 Comparisons 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.}
157 The functions \code{int()}, \code{long()} and \code{float()} can be used
158 to coerce numbers to a specific type.
159 \index{arithmetic}
160 \bifuncindex{int}
161 \bifuncindex{long}
162 \bifuncindex{float}
164 All numeric types support the following operations:
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}
183 \noindent
184 Notes:
185 \begin{description}
186 \item[(1)]
187 Conversion from floating point to (long or plain) integer may round or
188 % XXXJH xref here
189 truncate 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}
195 \item[(2)]
196 For (plain or long) integer division, the result is an integer; it
197 always truncates towards zero.
198 % XXXJH integer division is better defined nowadays
199 \indexii{integer}{division}
200 \indexiii{long}{integer}{division}
202 \item[(3)]
203 See the section on built-in functions for an exact definition.
205 \end{description}
206 % XXXJH exceptions: overflow (when? what operations?) zerodivision
208 \subsubsection{Bit-string Operations on Integer Types.}
210 Plain and long integer types support additional operations that make
211 sense only for bit-strings. Negative numbers are treated as their 2's
212 complement value:
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}
228 \subsection{Sequence Types}
230 There are three sequence types: strings, lists and tuples.
231 Strings literals are written in single quotes: \code{'xyzzy'}.
232 Lists are constructed with square brackets,
233 separating items with commas:
234 \code{[a, b, c]}.
235 Tuples are constructed by the comma operator
236 (not within square brackets), with or without enclosing parentheses,
237 but 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,
239 e.g., \code{(d,)}.
240 \indexii{sequence}{types}
241 \indexii{string}{type}
242 \indexii{tuple}{type}
243 \indexii{list}{type}
245 Sequence types support the following operations (\var{s} and \var{t} are
246 sequences of the same type; \var{n}, \var{i} and \var{j} are integers):
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}
270 \noindent
271 Notes:
273 % XXXJH all TeX-math expressions replaced by python-syntax expressions
274 \begin{description}
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}.
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.
288 \end{description}
290 \subsubsection{More String Operations.}
292 String objects have one unique built-in operation: the \code{\%}
293 operator (modulo) with a string left argument interprets this string
294 as a C sprintf format string to be applied to the right argument, and
295 returns the string resulting from this formatting operation.
297 The right argument should be a tuple with one item for each argument
298 required by the format string; if the string requires a single
299 argument, the right argument may also be a single non-tuple object.%
300 \footnote{A tuple object in this case should be a singleton.}
301 The following format characters are understood:
302 \%, c, s, i, d, u, o, x, X, e, E, f, g, G.
303 Width and precision may be a * to specify that an integer argument
304 specifies the actual width or precision. The flag characters -, +,
305 blank, \# and 0 are understood. The size specifiers h, l or L may be
306 present but are ignored. The \code{\%s} conversion takes any Python
307 object and converts it to a string using \code{str()} before
308 formatting it. The ANSI features \code{\%p} and \code{\%n}
309 are not supported. Since Python strings have an explicit length,
310 \code{\%s} conversions don't assume that \code{'\\0'} is the end of
311 the string.
313 For safety reasons, floating point precisions are clipped to 50;
314 \code{\%f} conversions for numbers whose absolute value is over 1e25
315 are replaced by \code{\%g} conversions.%
316 \footnote{These numbers are fairly arbitrary. They are intended to
317 avoid printing endless strings of meaningless digits without hampering
318 correct use and without having to know the exact precision of floating
319 point values on a particular machine.}
320 All other errors raise exceptions.
322 If the right argument is a dictionary (or any kind of mapping), then
323 the formats in the string must have a parenthesized key into that
324 dictionary inserted immediately after the \code{\%} character, and
325 each 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}
333 In this case no * specifiers may occur in a format.
335 Additional string operations are defined in standard module
336 \code{string} and in built-in module \code{regex}.
337 \index{string}
338 \index{regex}
340 \subsubsection{Mutable Sequence Types.}
342 List objects support additional operations that allow in-place
343 modification of the object.
344 These operations would be supported by other mutable sequence types
345 (when added to the language) as well.
346 Strings and tuples are immutable sequence types and such objects cannot
347 be modified once created.
348 The 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}
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})}
361 {same as \code{\var{s}[len(\var{s}):len(\var{s})] = [\var{x}]}}{}
362 \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}
392 \noindent
393 Notes:
394 \begin{description}
395 \item[(1)] Raises an exception when \var{x} is not found in \var{s}.
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}
408 \subsection{Mapping Types}
410 A \dfn{mapping} object maps values of one type (the key type) to
411 arbitrary objects. Mappings are mutable objects. There is currently
412 only one mapping type, the \dfn{dictionary}. A dictionary's keys are
413 almost arbitrary values. The only types of values not acceptable as
414 keys are values containing lists or dictionaries or other mutable
415 types that are compared by value rather than by object identity.
416 Numeric types used for keys obey the normal rules for numeric
417 comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
418 can be used interchangeably to index the same dictionary entry.
420 \indexii{mapping}{types}
421 \indexii{dictionary}{type}
423 Dictionaries 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\}}.
428 The following operations are defined on mappings (where \var{a} is a
429 mapping, \var{k} is a key and \var{x} is an arbitrary object):
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}
449 % XXXJH some lines above, you talk about `true', elsewhere you
450 % explicitely states \code{0} or \code{1}.
451 \noindent
452 Notes:
453 \begin{description}
454 \item[(1)] Raises an exception if \var{k} is not in the map.
456 \item[(2)] Keys and values are listed in random order, but at any
457 moment the ordering of the \code{keys()}, \code{values()} and
458 \code{items()} lists is the consistent with each other.
459 \end{description}
461 \subsection{Other Built-in Types}
463 The interpreter supports several other kinds of objects.
464 Most of these support only one or two operations.
466 \subsubsection{Modules.}
468 The 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
470 a name defined in \var{m}'s symbol table. Module attributes can be
471 assigned to. (Note that the \code{import} statement is not, strictly
472 spoken, an operation on a module object; \code{import \var{foo}} does not
473 require a module object named \var{foo} to exist, rather it requires
474 an (external) \emph{definition} for a module named \var{foo}
475 somewhere.)
477 A special member of every module is \code{__dict__}.
478 This is the dictionary containing the module's symbol table.
479 Modifying this dictionary will actually change the module's symbol
480 table, but direct assignment to the \code{__dict__} attribute is not
481 possible (i.e., you can write \code{\var{m}.__dict__['a'] = 1}, which
482 defines \code{\var{m}.a} to be \code{1}, but you can't write \code{\var{m}.__dict__ = \{\}}.
484 Modules are written like this: \code{<module 'sys'>}.
486 \subsubsection{Classes and Class Instances.}
487 % XXXJH cross ref here
488 (See the Python Reference Manual for these.)
490 \subsubsection{Functions.}
492 Function objects are created by function definitions. The only
493 operation on a function object is to call it:
494 \code{\var{func}(\var{argument-list})}.
496 There are really two flavors of function objects: built-in functions
497 and user-defined functions. Both support the same operation (to call
498 the function), but the implementation is different, hence the
499 different object types.
501 The 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
504 global 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).
507 \subsubsection{Methods.}
509 Methods are functions that are called using the attribute notation.
510 There are two flavors: built-in methods (such as \code{append()} on
511 lists) and class instance methods. Built-in methods are described
512 with the types that support them.
514 The implementation adds two special read-only attributes to class
515 instance methods: \code{\var{m}.im_self} is the object whose method this
516 is, and \code{\var{m}.im_func} is the function implementing the method.
517 Calling \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})}.
522 (See the Python Reference Manual for more info.)
524 \subsubsection{Type Objects.}
526 Type objects represent the various object types. An object's type is
527 % XXXJH xref here
528 accessed by the built-in function \code{type()}. There are no special
529 operations on types.
531 Types are written like this: \code{<type 'int'>}.
533 \subsubsection{The Null Object.}
535 This object is returned by functions that don't explicitly return a
536 value. It supports no special operations. There is exactly one null
537 object, named \code{None} (a built-in name).
539 It is written as \code{None}.
541 \subsubsection{File Objects.}
543 File objects are implemented using \C{}'s \code{stdio} package and can be
544 % XXXJH xref here
545 created with the built-in function \code{open()} described under
546 Built-in Functions below.
548 When a file operation fails for an I/O-related reason, the exception
549 \code{IOError} is raised. This includes situations where the
550 operation is not defined for some reason, like \code{seek()} on a tty
551 device or writing a file opened for reading.
553 Files have the following methods:
556 \renewcommand{\indexsubitem}{(file method)}
558 \begin{funcdesc}{close}{}
559 Close the file. A closed file cannot be read or written anymore.
560 \end{funcdesc}
562 \begin{funcdesc}{flush}{}
563 Flush the internal buffer, like \code{stdio}'s \code{fflush()}.
564 \end{funcdesc}
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}
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}
581 \begin{funcdesc}{readline}{}
582 Read one entire line from the file. A trailing newline character is
583 kept in the string%
584 \footnote{The advantage of leaving the newline on is that an empty string
585 can be returned to mean \EOF{} without being ambiguous. Another
586 advantage is that (in cases where it might matter, e.g. if you
587 want to make an exact copy of a file while scanning its lines)
588 you can tell whether the last line of a file ended in a newline
589 or not (yes this happens!).}
590 (but may be absent when a file ends with an
591 incomplete line). An empty string is returned when \EOF{} is hit
592 immediately. Note: unlike \code{stdio}'s \code{fgets()}, the returned
593 string contains null characters (\code{'\e 0'}) if they occurred in the
594 input.
595 \end{funcdesc}
597 \begin{funcdesc}{readlines}{}
598 Read until \EOF{} using \code{readline()} and return a list containing
599 the lines thus read.
600 \end{funcdesc}
602 \begin{funcdesc}{seek}{offset\, whence}
603 Set the file's current position, like \code{stdio}'s \code{fseek()}.
604 The \var{whence} argument is optional and defaults to \code{0}
605 (absolute file positioning); other values are \code{1} (seek
606 relative to the current position) and \code{2} (seek relative to the
607 file's end). There is no return value.
608 \end{funcdesc}
610 \begin{funcdesc}{tell}{}
611 Return the file's current position, like \code{stdio}'s \code{ftell()}.
612 \end{funcdesc}
614 \begin{funcdesc}{write}{str}
615 Write a string to the file. There is no return value.
616 \end{funcdesc}
618 \begin{funcdesc}{writelines}{list}
619 Write a list of strings to the file. There is no return value.
620 (The name is intended to match \code{readlines}; \code{writelines}
621 does not add line separators.)
622 \end{funcdesc}
624 \subsubsection{Internal Objects.}
626 (See the Python Reference Manual for these.)
628 \subsection{Special Attributes}
630 The implementation adds a few special read-only attributes to several
631 object types, where they are relevant:
633 \begin{itemize}
635 \item
636 \code{\var{x}.__dict__} is a dictionary of some sort used to store an
637 object's (writable) attributes;
639 \item
640 \code{\var{x}.__methods__} lists the methods of many built-in object types,
641 e.g., \code{[].__methods__} is
642 % XXXJH results in?, yields?, written down as an example
643 \code{['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']};
645 \item
646 \code{\var{x}.__members__} lists data attributes;
648 \item
649 \code{\var{x}.__class__} is the class to which a class instance belongs;
651 \item
652 \code{\var{x}.__bases__} is the tuple of base classes of a class object.
654 \end{itemize}