This commit was manufactured by cvs2svn to create tag 'cnrisync'.
[python/dscho.git] / Doc / ref / ref3.tex
blob60af2dc94a0eeb63cb1654e639358018307c27b1
1 \chapter{Data model}
3 \section{Objects, values and types}
5 {\em Objects} are Python's abstraction for data. All data in a Python
6 program is represented by objects or by relations between objects.
7 (In a sense, and in conformance to Von Neumann's model of a
8 ``stored program computer'', code is also represented by objects.)
9 \index{object}
10 \index{data}
12 Every object has an identity, a type and a value. An object's {\em
13 identity} never changes once it has been created; you may think of it
14 as the object's address in memory. An object's {\em type} is also
15 unchangeable. It determines the operations that an object supports
16 (e.g. ``does it have a length?'') and also defines the possible
17 values for objects of that type. The {\em value} of some objects can
18 change. Objects whose value can change are said to be {\em mutable};
19 objects whose value is unchangeable once they are created are called
20 {\em immutable}. The type determines an object's (im)mutability.
21 \index{identity of an object}
22 \index{value of an object}
23 \index{type of an object}
24 \index{mutable object}
25 \index{immutable object}
27 Objects are never explicitly destroyed; however, when they become
28 unreachable they may be garbage-collected. An implementation is
29 allowed to delay garbage collection or omit it altogether --- it is a
30 matter of implementation quality how garbage collection is
31 implemented, as long as no objects are collected that are still
32 reachable. (Implementation note: the current implementation uses a
33 reference-counting scheme which collects most objects as soon as they
34 become unreachable, but never collects garbage containing circular
35 references.)
36 \index{garbage collection}
37 \index{reference counting}
38 \index{unreachable object}
40 Note that the use of the implementation's tracing or debugging
41 facilities may keep objects alive that would normally be collectable.
43 Some objects contain references to ``external'' resources such as open
44 files or windows. It is understood that these resources are freed
45 when the object is garbage-collected, but since garbage collection is
46 not guaranteed to happen, such objects also provide an explicit way to
47 release the external resource, usually a \verb@close@ method.
48 Programs are strongly recommended to always explicitly close such
49 objects.
51 Some objects contain references to other objects; these are called
52 {\em containers}. Examples of containers are tuples, lists and
53 dictionaries. The references are part of a container's value. In
54 most cases, when we talk about the value of a container, we imply the
55 values, not the identities of the contained objects; however, when we
56 talk about the (im)mutability of a container, only the identities of
57 the immediately contained objects are implied. (So, if an immutable
58 container contains a reference to a mutable object, its value changes
59 if that mutable object is changed.)
60 \index{container}
62 Types affect almost all aspects of objects' lives. Even the meaning
63 of object identity is affected in some sense: for immutable types,
64 operations that compute new values may actually return a reference to
65 any existing object with the same type and value, while for mutable
66 objects this is not allowed. E.g. after
68 \begin{verbatim}
69 a = 1; b = 1; c = []; d = []
70 \end{verbatim}
72 \verb@a@ and \verb@b@ may or may not refer to the same object with the
73 value one, depending on the implementation, but \verb@c@ and \verb@d@
74 are guaranteed to refer to two different, unique, newly created empty
75 lists.
77 \section{The standard type hierarchy} \label{types}
79 Below is a list of the types that are built into Python. Extension
80 modules written in C can define additional types. Future versions of
81 Python may add types to the type hierarchy (e.g. rational or complex
82 numbers, efficiently stored arrays of integers, etc.).
83 \index{type}
84 \indexii{data}{type}
85 \indexii{type}{hierarchy}
86 \indexii{extension}{module}
87 \index{C}
89 Some of the type descriptions below contain a paragraph listing
90 `special attributes'. These are attributes that provide access to the
91 implementation and are not intended for general use. Their definition
92 may change in the future. There are also some `generic' special
93 attributes, not listed with the individual objects: \verb@__methods__@
94 is a list of the method names of a built-in object, if it has any;
95 \verb@__members__@ is a list of the data attribute names of a built-in
96 object, if it has any.
97 \index{attribute}
98 \indexii{special}{attribute}
99 \indexiii{generic}{special}{attribute}
100 \ttindex{__methods__}
101 \ttindex{__members__}
103 \begin{description}
105 \item[None]
106 This type has a single value. There is a single object with this value.
107 This object is accessed through the built-in name \verb@None@.
108 It is returned from functions that don't explicitly return an object.
109 \ttindex{None}
110 \obindex{None@{\tt None}}
112 \item[Numbers]
113 These are created by numeric literals and returned as results by
114 arithmetic operators and arithmetic built-in functions. Numeric
115 objects are immutable; once created their value never changes. Python
116 numbers are of course strongly related to mathematical numbers, but
117 subject to the limitations of numerical representation in computers.
118 \obindex{number}
119 \obindex{numeric}
121 Python distinguishes between integers and floating point numbers:
123 \begin{description}
124 \item[Integers]
125 These represent elements from the mathematical set of whole numbers.
126 \obindex{integer}
128 There are two types of integers:
130 \begin{description}
132 \item[Plain integers]
133 These represent numbers in the range -2147483648 through 2147483647.
134 (The range may be larger on machines with a larger natural word
135 size, but not smaller.)
136 When the result of an operation falls outside this range, the
137 exception \verb@OverflowError@ is raised.
138 For the purpose of shift and mask operations, integers are assumed to
139 have a binary, 2's complement notation using 32 or more bits, and
140 hiding no bits from the user (i.e., all 4294967296 different bit
141 patterns correspond to different values).
142 \obindex{plain integer}
144 \item[Long integers]
145 These represent numbers in an unlimited range, subject to available
146 (virtual) memory only. For the purpose of shift and mask operations,
147 a binary representation is assumed, and negative numbers are
148 represented in a variant of 2's complement which gives the illusion of
149 an infinite string of sign bits extending to the left.
150 \obindex{long integer}
152 \end{description} % Integers
154 The rules for integer representation are intended to give the most
155 meaningful interpretation of shift and mask operations involving
156 negative integers and the least surprises when switching between the
157 plain and long integer domains. For any operation except left shift,
158 if it yields a result in the plain integer domain without causing
159 overflow, it will yield the same result in the long integer domain or
160 when using mixed operands.
161 \indexii{integer}{representation}
163 \item[Floating point numbers]
164 These represent machine-level double precision floating point numbers.
165 You are at the mercy of the underlying machine architecture and
166 C implementation for the accepted range and handling of overflow.
167 \obindex{floating point}
168 \indexii{floating point}{number}
169 \index{C}
171 \end{description} % Numbers
173 \item[Sequences]
174 These represent finite ordered sets indexed by natural numbers.
175 The built-in function \verb@len()@ returns the number of elements
176 of a sequence. When this number is \var{n}, the index set contains
177 the numbers 0, 1, \ldots, \var{n}-1. Element \var{i} of sequence
178 \var{a} is selected by \code{\var{a}[\var{i}]}.
179 \obindex{seqence}
180 \bifuncindex{len}
181 \index{index operation}
182 \index{item selection}
183 \index{subscription}
185 Sequences also support slicing: \verb@a[i:j]@ selects all elements
186 with index \var{k} such that \var{i} \code{<=} \var{k} \code{<}
187 \var{j}. When used as an expression, a slice is a sequence of the
188 same type --- this implies that the index set is renumbered so that it
189 starts at 0 again.
190 \index{slicing}
192 Sequences are distinguished according to their mutability:
194 \begin{description}
196 \item[Immutable sequences]
197 An object of an immutable sequence type cannot change once it is
198 created. (If the object contains references to other objects,
199 these other objects may be mutable and may be changed; however
200 the collection of objects directly referenced by an immutable object
201 cannot change.)
202 \obindex{immutable sequence}
203 \obindex{immutable}
205 The following types are immutable sequences:
207 \begin{description}
209 \item[Strings]
210 The elements of a string are characters. There is no separate
211 character type; a character is represented by a string of one element.
212 Characters represent (at least) 8-bit bytes. The built-in
213 functions \verb@chr()@ and \verb@ord()@ convert between characters
214 and nonnegative integers representing the byte values.
215 Bytes with the values 0-127 represent the corresponding \ASCII{} values.
216 The string data type is also used to represent arrays of bytes, e.g.
217 to hold data read from a file.
218 \obindex{string}
219 \index{character}
220 \index{byte}
221 \index{ASCII}
222 \bifuncindex{chr}
223 \bifuncindex{ord}
225 (On systems whose native character set is not \ASCII{}, strings may use
226 EBCDIC in their internal representation, provided the functions
227 \verb@chr()@ and \verb@ord()@ implement a mapping between \ASCII{} and
228 EBCDIC, and string comparison preserves the \ASCII{} order.
229 Or perhaps someone can propose a better rule?)
230 \index{ASCII}
231 \index{EBCDIC}
232 \index{character set}
233 \indexii{string}{comparison}
234 \bifuncindex{chr}
235 \bifuncindex{ord}
237 \item[Tuples]
238 The elements of a tuple are arbitrary Python objects.
239 Tuples of two or more elements are formed by comma-separated lists
240 of expressions. A tuple of one element (a `singleton') can be formed
241 by affixing a comma to an expression (an expression by itself does
242 not create a tuple, since parentheses must be usable for grouping of
243 expressions). An empty tuple can be formed by enclosing `nothing' in
244 parentheses.
245 \obindex{tuple}
246 \indexii{singleton}{tuple}
247 \indexii{empty}{tuple}
249 \end{description} % Immutable sequences
251 \item[Mutable sequences]
252 Mutable sequences can be changed after they are created. The
253 subscription and slicing notations can be used as the target of
254 assignment and \verb@del@ (delete) statements.
255 \obindex{mutable sequece}
256 \obindex{mutable}
257 \indexii{assignment}{statement}
258 \index{delete}
259 \stindex{del}
260 \index{subscription}
261 \index{slicing}
263 There is currently a single mutable sequence type:
265 \begin{description}
267 \item[Lists]
268 The elements of a list are arbitrary Python objects. Lists are formed
269 by placing a comma-separated list of expressions in square brackets.
270 (Note that there are no special cases needed to form lists of length 0
271 or 1.)
272 \obindex{list}
274 \end{description} % Mutable sequences
276 \end{description} % Sequences
278 \item[Mapping types]
279 These represent finite sets of objects indexed by arbitrary index sets.
280 The subscript notation \verb@a[k]@ selects the element indexed
281 by \verb@k@ from the mapping \verb@a@; this can be used in
282 expressions and as the target of assignments or \verb@del@ statements.
283 The built-in function \verb@len()@ returns the number of elements
284 in a mapping.
285 \bifuncindex{len}
286 \index{subscription}
287 \obindex{mapping}
289 There is currently a single mapping type:
291 \begin{description}
293 \item[Dictionaries]
294 These represent finite sets of objects indexed by almost arbitrary
295 values. The only types of values not acceptable as keys are values
296 containing lists or dictionaries or other mutable types that are
297 compared by value rather than by object identity --- the reason being
298 that the implementation requires that a key's hash value be constant.
299 Numeric types used for keys obey the normal rules for numeric
300 comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
301 can be used interchangeably to index the same dictionary entry.
303 Dictionaries are mutable; they are created by the \verb@{...}@
304 notation (see section \ref{dict}).
305 \obindex{dictionary}
306 \obindex{mutable}
308 \end{description} % Mapping types
310 \item[Callable types]
311 These are the types to which the function call (invocation) operation,
312 written as \verb@function(argument, argument, ...)@, can be applied:
313 \indexii{function}{call}
314 \index{invocation}
315 \indexii{function}{argument}
316 \obindex{callable}
318 \begin{description}
320 \item[User-defined functions]
321 A user-defined function object is created by a function definition
322 (see section \ref{function}). It should be called with an argument
323 list containing the same number of items as the function's formal
324 parameter list.
325 \indexii{user-defined}{function}
326 \obindex{function}
327 \obindex{user-defined function}
329 Special read-only attributes: \verb@func_code@ is the code object
330 representing the compiled function body, and \verb@func_globals@ is (a
331 reference to) the dictionary that holds the function's global
332 variables --- it implements the global name space of the module in
333 which the function was defined.
334 \ttindex{func_code}
335 \ttindex{func_globals}
336 \indexii{global}{name space}
338 \item[User-defined methods]
339 A user-defined method (a.k.a. {\em object closure}) is a pair of a
340 class instance object and a user-defined function. It should be
341 called with an argument list containing one item less than the number
342 of items in the function's formal parameter list. When called, the
343 class instance becomes the first argument, and the call arguments are
344 shifted one to the right.
345 \obindex{method}
346 \obindex{user-defined method}
347 \indexii{user-defined}{method}
348 \index{object closure}
350 Special read-only attributes: \verb@im_self@ is the class instance
351 object, \verb@im_func@ is the function object.
352 \ttindex{im_func}
353 \ttindex{im_self}
355 \item[Built-in functions]
356 A built-in function object is a wrapper around a C function. Examples
357 of built-in functions are \verb@len@ and \verb@math.sin@. There
358 are no special attributes. The number and type of the arguments are
359 determined by the C function.
360 \obindex{built-in function}
361 \obindex{function}
362 \index{C}
364 \item[Built-in methods]
365 This is really a different disguise of a built-in function, this time
366 containing an object passed to the C function as an implicit extra
367 argument. An example of a built-in method is \verb@list.append@ if
368 \verb@list@ is a list object.
369 \obindex{built-in method}
370 \obindex{method}
371 \indexii{built-in}{method}
373 \item[Classes]
374 Class objects are described below. When a class object is called as a
375 function, a new class instance (also described below) is created and
376 returned. This implies a call to the class's \verb@__init__@ method
377 if it has one. Any arguments are passed on to the \verb@__init__@
378 method --- if there is no \verb@__init__@ method, the class must be called
379 without arguments.
380 \ttindex{__init__}
381 \obindex{class}
382 \obindex{class instance}
383 \obindex{instance}
384 \indexii{class object}{call}
386 \end{description}
388 \item[Modules]
389 Modules are imported by the \verb@import@ statement (see section
390 \ref{import}). A module object is a container for a module's name
391 space, which is a dictionary (the same dictionary as referenced by the
392 \verb@func_globals@ attribute of functions defined in the module).
393 Module attribute references are translated to lookups in this
394 dictionary. A module object does not contain the code object used to
395 initialize the module (since it isn't needed once the initialization
396 is done).
397 \stindex{import}
398 \obindex{module}
400 Attribute assignment update the module's name space dictionary.
402 Special read-only attributes: \verb@__dict__@ yields the module's name
403 space as a dictionary object; \verb@__name__@ yields the module's name
404 as a string object.
405 \ttindex{__dict__}
406 \ttindex{__name__}
407 \indexii{module}{name space}
409 \item[Classes]
410 Class objects are created by class definitions (see section
411 \ref{class}). A class is a container for a dictionary containing the
412 class's name space. Class attribute references are translated to
413 lookups in this dictionary. When an attribute name is not found
414 there, the attribute search continues in the base classes. The search
415 is depth-first, left-to-right in the order of their occurrence in the
416 base class list.
417 \obindex{class}
418 \obindex{class instance}
419 \obindex{instance}
420 \indexii{class object}{call}
421 \index{container}
422 \obindex{dictionary}
423 \indexii{class}{attribute}
425 Class attribute assignments update the class's dictionary, never the
426 dictionary of a base class.
427 \indexiii{class}{attribute}{assignment}
429 A class can be called as a function to yield a class instance (see
430 above).
431 \indexii{class object}{call}
433 Special read-only attributes: \verb@__dict__@ yields the dictionary
434 containing the class's name space; \verb@__bases__@ yields a tuple
435 (possibly empty or a singleton) containing the base classes, in the
436 order of their occurrence in the base class list.
437 \ttindex{__dict__}
438 \ttindex{__bases__}
440 \item[Class instances]
441 A class instance is created by calling a class object as a
442 function. A class instance has a dictionary in which
443 attribute references are searched. When an attribute is not found
444 there, and the instance's class has an attribute by that name, and
445 that class attribute is a user-defined function (and in no other
446 cases), the instance attribute reference yields a user-defined method
447 object (see above) constructed from the instance and the function.
448 \obindex{class instance}
449 \obindex{instance}
450 \indexii{class}{instance}
451 \indexii{class instance}{attribute}
453 Attribute assignments update the instance's dictionary.
454 \indexiii{class instance}{attribute}{assignment}
456 Class instances can pretend to be numbers, sequences, or mappings if
457 they have methods with certain special names. These are described in
458 section \ref{specialnames}.
459 \obindex{number}
460 \obindex{sequence}
461 \obindex{mapping}
463 Special read-only attributes: \verb@__dict__@ yields the attribute
464 dictionary; \verb@__class__@ yields the instance's class.
465 \ttindex{__dict__}
466 \ttindex{__class__}
468 \item[Files]
469 A file object represents an open file. (It is a wrapper around a C
470 {\tt stdio} file pointer.) File objects are created by the
471 \verb@open()@ built-in function, and also by \verb@posix.popen()@ and
472 the \verb@makefile@ method of socket objects. \verb@sys.stdin@,
473 \verb@sys.stdout@ and \verb@sys.stderr@ are file objects corresponding
474 to the interpreter's standard input, output and error streams.
475 See the Python Library Reference for methods of file objects and other
476 details.
477 \obindex{file}
478 \index{C}
479 \index{stdio}
480 \bifuncindex{open}
481 \bifuncindex{popen}
482 \bifuncindex{makefile}
483 \ttindex{stdin}
484 \ttindex{stdout}
485 \ttindex{stderr}
486 \ttindex{sys.stdin}
487 \ttindex{sys.stdout}
488 \ttindex{sys.stderr}
490 \item[Internal types]
491 A few types used internally by the interpreter are exposed to the user.
492 Their definition may change with future versions of the interpreter,
493 but they are mentioned here for completeness.
494 \index{internal type}
496 \begin{description}
498 \item[Code objects]
499 Code objects represent ``pseudo-compiled'' executable Python code.
500 The difference between a code
501 object and a function object is that the function object contains an
502 explicit reference to the function's context (the module in which it
503 was defined) while a code object contains no context.
504 \obindex{code}
506 Special read-only attributes: \verb@co_code@ is a string representing
507 the sequence of instructions; \verb@co_consts@ is a list of literals
508 used by the code; \verb@co_names@ is a list of names (strings) used by
509 the code; \verb@co_filename@ is the filename from which the code was
510 compiled. (To find out the line numbers, you would have to decode the
511 instructions; the standard library module \verb@dis@ contains an
512 example of how to do this.)
513 \ttindex{co_code}
514 \ttindex{co_consts}
515 \ttindex{co_names}
516 \ttindex{co_filename}
518 \item[Frame objects]
519 Frame objects represent execution frames. They may occur in traceback
520 objects (see below).
521 \obindex{frame}
523 Special read-only attributes: \verb@f_back@ is to the previous
524 stack frame (towards the caller), or \verb@None@ if this is the bottom
525 stack frame; \verb@f_code@ is the code object being executed in this
526 frame; \verb@f_globals@ is the dictionary used to look up global
527 variables; \verb@f_locals@ is used for local variables;
528 \verb@f_lineno@ gives the line number and \verb@f_lasti@ gives the
529 precise instruction (this is an index into the instruction string of
530 the code object).
531 \ttindex{f_back}
532 \ttindex{f_code}
533 \ttindex{f_globals}
534 \ttindex{f_locals}
535 \ttindex{f_lineno}
536 \ttindex{f_lasti}
538 \item[Traceback objects] \label{traceback}
539 Traceback objects represent a stack trace of an exception. A
540 traceback object is created when an exception occurs. When the search
541 for an exception handler unwinds the execution stack, at each unwound
542 level a traceback object is inserted in front of the current
543 traceback. When an exception handler is entered
544 (see also section \ref{try}), the stack trace is
545 made available to the program as \verb@sys.exc_traceback@. When the
546 program contains no suitable handler, the stack trace is written
547 (nicely formatted) to the standard error stream; if the interpreter is
548 interactive, it is also made available to the user as
549 \verb@sys.last_traceback@.
550 \obindex{traceback}
551 \indexii{stack}{trace}
552 \indexii{exception}{handler}
553 \indexii{execution}{stack}
554 \ttindex{exc_traceback}
555 \ttindex{last_traceback}
556 \ttindex{sys.exc_traceback}
557 \ttindex{sys.last_traceback}
559 Special read-only attributes: \verb@tb_next@ is the next level in the
560 stack trace (towards the frame where the exception occurred), or
561 \verb@None@ if there is no next level; \verb@tb_frame@ points to the
562 execution frame of the current level; \verb@tb_lineno@ gives the line
563 number where the exception occurred; \verb@tb_lasti@ indicates the
564 precise instruction. The line number and last instruction in the
565 traceback may differ from the line number of its frame object if the
566 exception occurred in a \verb@try@ statement with no matching
567 \verb@except@ clause or with a \verb@finally@ clause.
568 \ttindex{tb_next}
569 \ttindex{tb_frame}
570 \ttindex{tb_lineno}
571 \ttindex{tb_lasti}
572 \stindex{try}
574 \end{description} % Internal types
576 \end{description} % Types
579 \section{Special method names} \label{specialnames}
581 A class can implement certain operations that are invoked by special
582 syntax (such as subscription or arithmetic operations) by defining
583 methods with special names. For instance, if a class defines a
584 method named \verb@__getitem__@, and \verb@x@ is an instance of this
585 class, then \verb@x[i]@ is equivalent to \verb@x.__getitem__(i)@.
586 (The reverse is not true --- if \verb@x@ is a list object,
587 \verb@x.__getitem__(i)@ is not equivalent to \verb@x[i]@.)
588 \ttindex{__getitem__}
590 Except for \verb@__repr__@, \verb@__str__@ and \verb@__cmp__@,
591 attempts to execute an
592 operation raise an exception when no appropriate method is defined.
593 For \verb@__repr__@, the default is to return a string describing the
594 object's class and address.
595 For \verb@__cmp__@, the default is to compare instances based on their
596 address.
597 For \verb@__str__@, the default is to use \verb@__repr__@.
598 \ttindex{__repr__}
599 \ttindex{__str__}
600 \ttindex{__cmp__}
603 \subsection{Special methods for any type}
605 \begin{description}
607 \item[{\tt __init__(self, args...)}]
608 Called when the instance is created. The arguments are those passed
609 to the class constructor expression. If a base class has an
610 \code{__init__} method the derived class's \code{__init__} method must
611 explicitly call it to ensure proper initialization of the base class
612 part of the instance.
613 \ttindex{__init__}
614 \indexii{class}{constructor}
617 \item[{\tt __del__(self)}]
618 Called when the instance is about to be destroyed. If a base class
619 has an \code{__del__} method the derived class's \code{__del__} method
620 must explicitly call it to ensure proper deletion of the base class
621 part of the instance. Note that it is possible for the \code{__del__}
622 method to postpone destruction of the instance by creating a new
623 reference to it. It may then be called at a later time when this new
624 reference is deleted. It is not guaranteed that
625 \code{__del__} methods are called for objects that still exist when
626 the interpreter exits.
627 \ttindex{__del__}
628 \stindex{del}
630 Note that \code{del x} doesn't directly call \code{x.__del__} --- the
631 former decrements the reference count for \code{x} by one, but
632 \code{x.__del__} is only called when its reference count reaches zero.
634 \item[{\tt __repr__(self)}]
635 Called by the \verb@repr()@ built-in function and by string conversions
636 (reverse or backward quotes) to compute the string representation of an object.
637 \ttindex{__repr__}
638 \bifuncindex{repr}
639 \indexii{string}{conversion}
640 \indexii{reverse}{quotes}
641 \indexii{backward}{quotes}
642 \index{back-quotes}
644 \item[{\tt __str__(self)}]
645 Called by the \verb@str()@ built-in function and by the \verb@print@
646 statement compute the string representation of an object.
647 \ttindex{__str__}
648 \bifuncindex{str}
649 \stindex{print}
651 \item[{\tt __cmp__(self, other)}]
652 Called by all comparison operations. Should return -1 if
653 \verb@self < other@, 0 if \verb@self == other@, +1 if
654 \verb@self > other@. If no \code{__cmp__} operation is defined, class
655 instances are compared by object identity (``address'').
656 (Implementation note: due to limitations in the interpreter,
657 exceptions raised by comparisons are ignored, and the objects will be
658 considered equal in this case.)
659 \ttindex{__cmp__}
660 \bifuncindex{cmp}
661 \index{comparisons}
663 \item[{\tt __hash__(self)}]
664 Called for the key object for dictionary operations,
665 and by the built-in function
666 \code{hash()}. Should return a 32-bit integer usable as a hash value
667 for dictionary operations. The only required property is that objects
668 which compare equal have the same hash value; it is advised to somehow
669 mix together (e.g. using exclusive or) the hash values for the
670 components of the object that also play a part in comparison of
671 objects. If a class does not define a \code{__cmp__} method it should
672 not define a \code{__hash__} operation either; if it defines
673 \code{__cmp__} but not \code{__hash__} its instances will not be
674 usable as dictionary keys. If a class defines mutable objects and
675 implements a \code{__cmp__} method it should not implement
676 \code{__hash__}, since the dictionary implementation assumes that a
677 key's hash value is a constant.
678 \obindex{dictionary}
679 \ttindex{__cmp__}
680 \ttindex{__hash__}
681 \bifuncindex{hash}
683 \item[{\tt __call__(self, *args)}]
684 Called when the instance is ``called'' as a function.
685 \ttindex{__call__}
686 \indexii{call}{instance}
688 \end{description}
691 \subsection{Special methods for attribute access}
693 The following methods can be used to change the meaning of attribute
694 access for class instances.
696 \begin{description}
698 \item[{\tt __getattr__(self, name)}]
699 Called when an attribute lookup has not found the attribute in the
700 usual places (i.e. it is not an instance attribute nor is it found in
701 the class tree for \code{self}). \code{name} is the attribute name.
702 \ttindex{__getattr__}
704 Note that if the attribute is found through the normal mechanism,
705 \code{__getattr__} is not called. (This is an asymmetry between
706 \code{__getattr__} and \code{__setattr__}.)
707 This is done both for efficiency reasons and because otherwise
708 \code{__getattr__} would have no way to access other attributes of the
709 instance.
710 Note that at least for instance variables, \code{__getattr__} can fake
711 total control by simply not inserting any values in the instance
712 attribute dictionary.
713 \ttindex{__setattr__}
715 \item[{\tt __setattr__(self, name, value)}]
716 Called when an attribute assignment is attempted. This is called
717 instead of the normal mechanism (i.e. store the value as an instance
718 attribute). \code{name} is the attribute name, \code{value} is the
719 value to be assigned to it.
720 \ttindex{__setattr__}
722 If \code{__setattr__} wants to assign to an instance attribute, it
723 should not simply execute \code{self.\var{name} = value} --- this would
724 cause a recursive call. Instead, it should insert the value in the
725 dictionary of instance attributes, e.g. \code{self.__dict__[name] =
726 value}.
727 \ttindex{__dict__}
729 \item[{\tt __delattr__(self, name)}]
730 Like \code{__setattr__} but for attribute deletion instead of
731 assignment.
732 \ttindex{__delattr__}
734 \end{description}
737 \subsection{Special methods for sequence and mapping types}
739 \begin{description}
741 \item[{\tt __len__(self)}]
742 Called to implement the built-in function \verb@len()@. Should return
743 the length of the object, an integer \verb@>=@ 0. Also, an object
744 whose \verb@__len__()@ method returns 0 is considered to be false in a
745 Boolean context.
746 \ttindex{__len__}
748 \item[{\tt __getitem__(self, key)}]
749 Called to implement evaluation of \verb@self[key]@. Note that the
750 special interpretation of negative keys (if the class wishes to
751 emulate a sequence type) is up to the \verb@__getitem__@ method.
752 \ttindex{__getitem__}
754 \item[{\tt __setitem__(self, key, value)}]
755 Called to implement assignment to \verb@self[key]@. Same note as for
756 \verb@__getitem__@.
757 \ttindex{__setitem__}
759 \item[{\tt __delitem__(self, key)}]
760 Called to implement deletion of \verb@self[key]@. Same note as for
761 \verb@__getitem__@.
762 \ttindex{__delitem__}
764 \end{description}
767 \subsection{Special methods for sequence types}
769 \begin{description}
771 \item[{\tt __getslice__(self, i, j)}]
772 Called to implement evaluation of \verb@self[i:j]@. Note that missing
773 \verb@i@ or \verb@j@ are replaced by 0 or \verb@len(self)@,
774 respectively, and \verb@len(self)@ has been added (once) to originally
775 negative \verb@i@ or \verb@j@ by the time this function is called
776 (unlike for \verb@__getitem__@).
777 \ttindex{__getslice__}
779 \item[{\tt __setslice__(self, i, j, sequence)}]
780 Called to implement assignment to \verb@self[i:j]@. Same notes as for
781 \verb@__getslice__@.
782 \ttindex{__setslice__}
784 \item[{\tt __delslice__(self, i, j)}]
785 Called to implement deletion of \verb@self[i:j]@. Same notes as for
786 \verb@__getslice__@.
787 \ttindex{__delslice__}
789 \end{description}
792 \subsection{Special methods for numeric types}
794 \begin{description}
796 \item[{\tt __add__(self, other)}]\itemjoin
797 \item[{\tt __sub__(self, other)}]\itemjoin
798 \item[{\tt __mul__(self, other)}]\itemjoin
799 \item[{\tt __div__(self, other)}]\itemjoin
800 \item[{\tt __mod__(self, other)}]\itemjoin
801 \item[{\tt __divmod__(self, other)}]\itemjoin
802 \item[{\tt __pow__(self, other)}]\itemjoin
803 \item[{\tt __lshift__(self, other)}]\itemjoin
804 \item[{\tt __rshift__(self, other)}]\itemjoin
805 \item[{\tt __and__(self, other)}]\itemjoin
806 \item[{\tt __xor__(self, other)}]\itemjoin
807 \item[{\tt __or__(self, other)}]\itembreak
808 Called to implement the binary arithmetic operations (\verb@+@,
809 \verb@-@, \verb@*@, \verb@/@, \verb@%@, \verb@divmod()@, \verb@pow()@,
810 \verb@<<@, \verb@>>@, \verb@&@, \verb@^@, \verb@|@).
811 \ttindex{__or__}
812 \ttindex{__xor__}
813 \ttindex{__and__}
814 \ttindex{__rshift__}
815 \ttindex{__lshift__}
816 \ttindex{__pow__}
817 \ttindex{__divmod__}
818 \ttindex{__mod__}
819 \ttindex{__div__}
820 \ttindex{__mul__}
821 \ttindex{__sub__}
822 \ttindex{__add__}
824 \item[{\tt __neg__(self)}]\itemjoin
825 \item[{\tt __pos__(self)}]\itemjoin
826 \item[{\tt __abs__(self)}]\itemjoin
827 \item[{\tt __invert__(self)}]\itembreak
828 Called to implement the unary arithmetic operations (\verb@-@, \verb@+@,
829 \verb@abs()@ and \verb@~@).
830 \ttindex{__invert__}
831 \ttindex{__abs__}
832 \ttindex{__pos__}
833 \ttindex{__neg__}
835 \item[{\tt __nonzero__(self)}]
836 Called to implement boolean testing; should return 0 or 1. An
837 alternative name for this method is \verb@__len__@.
838 \ttindex{__nonzero__}
840 \item[{\tt __coerce__(self, other)}]
841 Called to implement ``mixed-mode'' numeric arithmetic. Should either
842 return a tuple containing self and other converted to a common numeric
843 type, or None if no way of conversion is known. When the common type
844 would be the type of other, it is sufficient to return None, since the
845 interpreter will also ask the other object to attempt a coercion (but
846 sometimes, if the implementation of the other type cannot be changed,
847 it is useful to do the conversion to the other type here).
848 \ttindex{__coerce__}
850 Note that this method is not called to coerce the arguments to \verb@+@
851 and \verb@*@, because these are also used to implement sequence
852 concatenation and repetition, respectively. Also note that, for the
853 same reason, in \verb@n*x@, where \verb@n@ is a built-in number and
854 \verb@x@ is an instance, a call to \verb@x.__mul__(n)@ is made.%
855 \footnote{The interpreter should really distinguish between
856 user-defined classes implementing sequences, mappings or numbers, but
857 currently it doesn't --- hence this strange exception.}
858 \ttindex{__mul__}
860 \item[{\tt __int__(self)}]\itemjoin
861 \item[{\tt __long__(self)}]\itemjoin
862 \item[{\tt __float__(self)}]\itembreak
863 Called to implement the built-in functions \verb@int()@, \verb@long()@
864 and \verb@float()@. Should return a value of the appropriate type.
865 \ttindex{__float__}
866 \ttindex{__long__}
867 \ttindex{__int__}
869 \item[{\tt __oct__(self)}]\itemjoin
870 \item[{\tt __hex__(self)}]\itembreak
871 Called to implement the built-in functions \verb@oct()@ and
872 \verb@hex()@. Should return a string value.
873 \ttindex{__hex__}
874 \ttindex{__oct__}
876 \end{description}