blob: 4b64e8eea801df76af7ae4e89928565167e301aa [file] [log] [blame]
Fred Drakef6669171998-05-06 19:52:49 +00001\chapter{Data model}
2
3\section{Objects, values and types}
4
5\dfn{Objects} are Python's abstraction for data. All data in a Python
6program is represented by objects or by relations between objects.
7(In a sense, and in conformance to Von Neumann's model of a
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00008``stored program computer,'' code is also represented by objects.)
Fred Drakef6669171998-05-06 19:52:49 +00009\index{object}
10\index{data}
11
12Every object has an identity, a type and a value. An object's
13\emph{identity} never changes once it has been created; you may think
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000014of it as the object's address in memory. The `\code{is}' operator
15compares the identity of two objects; the `\code{id()}' function
16returns an integer representing its identity (currently implemented as
17its address).
18An object's \dfn{type} is
Fred Drakef6669171998-05-06 19:52:49 +000019also unchangeable. It determines the operations that an object
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000020supports (e.g., ``does it have a length?'') and also defines the
21possible values for objects of that type. The `\code{type()}'
22function returns an object's type (which is an object itself).
23The \emph{value} of some
Fred Drakef6669171998-05-06 19:52:49 +000024objects can change. Objects whose value can change are said to be
25\emph{mutable}; objects whose value is unchangeable once they are
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000026created are called \emph{immutable}.
27An object's mutability is determined by its type; for instance,
28numbers, strings and tuples are immutable, while dictionaries and
29lists are mutable.
Fred Drakef6669171998-05-06 19:52:49 +000030\index{identity of an object}
31\index{value of an object}
32\index{type of an object}
33\index{mutable object}
34\index{immutable object}
35
36Objects are never explicitly destroyed; however, when they become
37unreachable they may be garbage-collected. An implementation is
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000038allowed to postpone garbage collection or omit it altogether --- it is a
Fred Drakef6669171998-05-06 19:52:49 +000039matter of implementation quality how garbage collection is
40implemented, as long as no objects are collected that are still
41reachable. (Implementation note: the current implementation uses a
42reference-counting scheme which collects most objects as soon as they
43become unreachable, but never collects garbage containing circular
44references.)
45\index{garbage collection}
46\index{reference counting}
47\index{unreachable object}
48
49Note that the use of the implementation's tracing or debugging
50facilities may keep objects alive that would normally be collectable.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000051Also note that catching an exception with a
52`\code{try}...\code{except}' statement may keep objects alive.
Fred Drakef6669171998-05-06 19:52:49 +000053
54Some objects contain references to ``external'' resources such as open
55files or windows. It is understood that these resources are freed
56when the object is garbage-collected, but since garbage collection is
57not guaranteed to happen, such objects also provide an explicit way to
58release the external resource, usually a \method{close()} method.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000059Programs are strongly recommended to explicitly close such
Fred Drakef6669171998-05-06 19:52:49 +000060objects.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000061The `\code{try}...\code{finally}' statement provides a convenient way
62to do this.
Fred Drakef6669171998-05-06 19:52:49 +000063
64Some objects contain references to other objects; these are called
65\emph{containers}. Examples of containers are tuples, lists and
66dictionaries. The references are part of a container's value. In
67most cases, when we talk about the value of a container, we imply the
68values, not the identities of the contained objects; however, when we
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000069talk about the mutability of a container, only the identities of
70the immediately contained objects are implied. So, if an immutable
71container (like a tuple)
72contains a reference to a mutable object, its value changes
73if that mutable object is changed.
Fred Drakef6669171998-05-06 19:52:49 +000074\index{container}
75
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000076Types affect almost all aspects of object behavior. Even the importance
Fred Drakef6669171998-05-06 19:52:49 +000077of object identity is affected in some sense: for immutable types,
78operations that compute new values may actually return a reference to
79any existing object with the same type and value, while for mutable
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000080objects this is not allowed. E.g., after
81``\code{a = 1; b = 1}'',
Fred Drakef6669171998-05-06 19:52:49 +000082\code{a} and \code{b} may or may not refer to the same object with the
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000083value one, depending on the implementation, but after
84``\code{c = []; d = []}'', \code{c} and \code{d}
Fred Drakef6669171998-05-06 19:52:49 +000085are guaranteed to refer to two different, unique, newly created empty
86lists.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000087(Note that ``\code{c = d = []}'' assigns the same object to both
88\code{c} and \code{d}.)
Fred Drakef6669171998-05-06 19:52:49 +000089
90\section{The standard type hierarchy} \label{types}
91
92Below is a list of the types that are built into Python. Extension
Guido van Rossum83b2f8a1998-07-23 17:12:46 +000093modules written in \C{} can define additional types. Future versions of
94Python may add types to the type hierarchy (e.g., rational
Fred Drakef6669171998-05-06 19:52:49 +000095numbers, efficiently stored arrays of integers, etc.).
96\index{type}
97\indexii{data}{type}
98\indexii{type}{hierarchy}
99\indexii{extension}{module}
100\indexii{C}{language}
101
102Some of the type descriptions below contain a paragraph listing
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000103`special attributes.' These are attributes that provide access to the
Fred Drakef6669171998-05-06 19:52:49 +0000104implementation and are not intended for general use. Their definition
105may change in the future. There are also some `generic' special
106attributes, not listed with the individual objects: \member{__methods__}
107is a list of the method names of a built-in object, if it has any;
108\member{__members__} is a list of the data attribute names of a built-in
109object, if it has any.
110\index{attribute}
111\indexii{special}{attribute}
112\indexiii{generic}{special}{attribute}
113\ttindex{__methods__}
114\ttindex{__members__}
115
116\begin{description}
117
118\item[None]
119This type has a single value. There is a single object with this value.
120This object is accessed through the built-in name \code{None}.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000121It is used to signify the absence of a value in many situations, e.g.,
122it is returned from functions that don't explicitly return anything.
123Its truth value is false.
Fred Drakef6669171998-05-06 19:52:49 +0000124\ttindex{None}
125\obindex{None@{\tt None}}
126
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000127\item[Ellipsis]
128This type has a single value. There is a single object with this value.
129This object is accessed through the built-in name \code{Ellipsis}.
130It is used to indicate the presence of the ``\code{...}'' syntax in a
131slice. Its truth value is true.
132\ttindex{Ellipsis}
133\obindex{Ellipsis@{\tt Ellipsis}}
134
Fred Drakef6669171998-05-06 19:52:49 +0000135\item[Numbers]
136These are created by numeric literals and returned as results by
137arithmetic operators and arithmetic built-in functions. Numeric
138objects are immutable; once created their value never changes. Python
139numbers are of course strongly related to mathematical numbers, but
140subject to the limitations of numerical representation in computers.
141\obindex{number}
142\obindex{numeric}
143
144Python distinguishes between integers and floating point numbers:
145
146\begin{description}
147\item[Integers]
148These represent elements from the mathematical set of whole numbers.
149\obindex{integer}
150
151There are two types of integers:
152
153\begin{description}
154
155\item[Plain integers]
156These represent numbers in the range -2147483648 through 2147483647.
157(The range may be larger on machines with a larger natural word
158size, but not smaller.)
159When the result of an operation falls outside this range, the
160exception \exception{OverflowError} is raised.
161For the purpose of shift and mask operations, integers are assumed to
162have a binary, 2's complement notation using 32 or more bits, and
163hiding no bits from the user (i.e., all 4294967296 different bit
164patterns correspond to different values).
165\obindex{plain integer}
166\withsubitem{(built-in exception)}{\ttindex{OverflowError}}
167
168\item[Long integers]
169These represent numbers in an unlimited range, subject to available
170(virtual) memory only. For the purpose of shift and mask operations,
171a binary representation is assumed, and negative numbers are
172represented in a variant of 2's complement which gives the illusion of
173an infinite string of sign bits extending to the left.
174\obindex{long integer}
175
176\end{description} % Integers
177
178The rules for integer representation are intended to give the most
179meaningful interpretation of shift and mask operations involving
180negative integers and the least surprises when switching between the
181plain and long integer domains. For any operation except left shift,
182if it yields a result in the plain integer domain without causing
183overflow, it will yield the same result in the long integer domain or
184when using mixed operands.
185\indexii{integer}{representation}
186
187\item[Floating point numbers]
188These represent machine-level double precision floating point numbers.
189You are at the mercy of the underlying machine architecture and
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000190\C{} implementation for the accepted range and handling of overflow.
191Python does not support single-precision floating point numbers; the
192savings in CPU and memory usage that are usually the reason for using
193these is dwarfed by the overhead of using objects in Python, so there
194is no reason to complicate the language with two kinds of floating
195point numbers.
Fred Drakef6669171998-05-06 19:52:49 +0000196\obindex{floating point}
197\indexii{floating point}{number}
198\indexii{C}{language}
199
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000200\item[Complex numbers]
201These represent complex numbers as a pair of machine-level double
202precision floating point numbers. The same caveats apply as for
203floating point numbers. The real and imaginary value of a complex
204number \code{z} can be retrieved through the attributes \code{z.real}
205and \code{z.imag}.
206\obindex{complex}
207\indexii{complex}{number}
208
Fred Drakef6669171998-05-06 19:52:49 +0000209\end{description} % Numbers
210
211\item[Sequences]
212These represent finite ordered sets indexed by natural numbers.
213The built-in function \function{len()}\bifuncindex{len} returns the
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000214number of items of a sequence.
215When the lenth of a sequence is \var{n}, the
216index set contains the numbers 0, 1, \ldots, \var{n}-1. Item
Fred Drakef6669171998-05-06 19:52:49 +0000217\var{i} of sequence \var{a} is selected by \code{\var{a}[\var{i}]}.
218\obindex{seqence}
219\index{index operation}
220\index{item selection}
221\index{subscription}
222
223Sequences also support slicing: \code{\var{a}[\var{i}:\var{j}]}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000224selects all items with index \var{k} such that \var{i} \code{<=}
Fred Drakef6669171998-05-06 19:52:49 +0000225\var{k} \code{<} \var{j}. When used as an expression, a slice is a
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000226sequence of the same type. This implies that the index set is
227renumbered so that it starts at 0.
Fred Drakef6669171998-05-06 19:52:49 +0000228\index{slicing}
229
230Sequences are distinguished according to their mutability:
231
232\begin{description}
233%
234\item[Immutable sequences]
235An object of an immutable sequence type cannot change once it is
236created. (If the object contains references to other objects,
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000237these other objects may be mutable and may be changed; however,
Fred Drakef6669171998-05-06 19:52:49 +0000238the collection of objects directly referenced by an immutable object
239cannot change.)
240\obindex{immutable sequence}
241\obindex{immutable}
242
243The following types are immutable sequences:
244
245\begin{description}
246
247\item[Strings]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000248The items of a string are characters. There is no separate
249character type; a character is represented by a string of one item.
Fred Drakef6669171998-05-06 19:52:49 +0000250Characters represent (at least) 8-bit bytes. The built-in
251functions \function{chr()}\bifuncindex{chr} and
252\function{ord()}\bifuncindex{ord} convert between characters and
253nonnegative integers representing the byte values. Bytes with the
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000254values 0-127 usually represent the corresponding \ASCII{} values, but
255the interpretation of values is up to the program. The string
256data type is also used to represent arrays of bytes, e.g., to hold data
Fred Drakef6669171998-05-06 19:52:49 +0000257read from a file.
258\obindex{string}
259\index{character}
260\index{byte}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000261\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000262
263(On systems whose native character set is not \ASCII{}, strings may use
264EBCDIC in their internal representation, provided the functions
265\function{chr()} and \function{ord()} implement a mapping between \ASCII{} and
266EBCDIC, and string comparison preserves the \ASCII{} order.
267Or perhaps someone can propose a better rule?)
Fred Drake5c07d9b1998-05-14 19:37:06 +0000268\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000269\index{EBCDIC}
270\index{character set}
271\indexii{string}{comparison}
272\bifuncindex{chr}
273\bifuncindex{ord}
274
275\item[Tuples]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000276The items of a tuple are arbitrary Python objects.
277Tuples of two or more items are formed by comma-separated lists
278of expressions. A tuple of one item (a `singleton') can be formed
Fred Drakef6669171998-05-06 19:52:49 +0000279by affixing a comma to an expression (an expression by itself does
280not create a tuple, since parentheses must be usable for grouping of
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000281expressions). An empty tuple can be formed by an empty pair of
Fred Drakef6669171998-05-06 19:52:49 +0000282parentheses.
283\obindex{tuple}
284\indexii{singleton}{tuple}
285\indexii{empty}{tuple}
286
287\end{description} % Immutable sequences
288
289\item[Mutable sequences]
290Mutable sequences can be changed after they are created. The
291subscription and slicing notations can be used as the target of
292assignment and \keyword{del} (delete) statements.
293\obindex{mutable sequece}
294\obindex{mutable}
295\indexii{assignment}{statement}
296\index{delete}
297\stindex{del}
298\index{subscription}
299\index{slicing}
300
301There is currently a single mutable sequence type:
302
303\begin{description}
304
305\item[Lists]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000306The items of a list are arbitrary Python objects. Lists are formed
Fred Drakef6669171998-05-06 19:52:49 +0000307by placing a comma-separated list of expressions in square brackets.
308(Note that there are no special cases needed to form lists of length 0
309or 1.)
310\obindex{list}
311
312\end{description} % Mutable sequences
313
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000314The extension module \module{array}\refstmodindex{array} provides an
315additional example of a mutable sequence type.
316
317
Fred Drakef6669171998-05-06 19:52:49 +0000318\end{description} % Sequences
319
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000320\item[Mappings]
Fred Drakef6669171998-05-06 19:52:49 +0000321These represent finite sets of objects indexed by arbitrary index sets.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000322The subscript notation \code{a[k]} selects the item indexed
Fred Drakef6669171998-05-06 19:52:49 +0000323by \code{k} from the mapping \code{a}; this can be used in
324expressions and as the target of assignments or \keyword{del} statements.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000325The built-in function \function{len()} returns the number of items
Fred Drakef6669171998-05-06 19:52:49 +0000326in a mapping.
327\bifuncindex{len}
328\index{subscription}
329\obindex{mapping}
330
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000331There is currently a single intrinsic mapping type:
Fred Drakef6669171998-05-06 19:52:49 +0000332
333\begin{description}
334
335\item[Dictionaries]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000336These represent finite sets of objects indexed by nearly arbitrary
Fred Drakef6669171998-05-06 19:52:49 +0000337values. The only types of values not acceptable as keys are values
338containing lists or dictionaries or other mutable types that are
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000339compared by value rather than by object identity, the reason being
340that the efficient implementation of dictionaries requires a key's
341hash value to remain constant.
Fred Drakef6669171998-05-06 19:52:49 +0000342Numeric types used for keys obey the normal rules for numeric
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000343comparison: if two numbers compare equal (e.g., \code{1} and
Fred Drakef6669171998-05-06 19:52:49 +0000344\code{1.0}) then they can be used interchangeably to index the same
345dictionary entry.
346
347Dictionaries are mutable; they are created by the \code{...}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000348notation (see section \ref{dict}, ``Dictionary Displays'').
Fred Drakef6669171998-05-06 19:52:49 +0000349\obindex{dictionary}
350\obindex{mutable}
351
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000352The extension modules \module{dbm}\refstmodindex{dbm},
353\module{gdbm}\refstmodindex{gdbm}, \module{bsddb}\refstmodindex{bsddb}
354provide additional examples of mapping types.
355
Fred Drakef6669171998-05-06 19:52:49 +0000356\end{description} % Mapping types
357
358\item[Callable types]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000359These are the types to which the function call operation (see section
360\ref{calls}, ``Calls'') can be applied:
Fred Drakef6669171998-05-06 19:52:49 +0000361\indexii{function}{call}
362\index{invocation}
363\indexii{function}{argument}
364\obindex{callable}
365
366\begin{description}
367
368\item[User-defined functions]
369A user-defined function object is created by a function definition
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000370(see section \ref{function}, ``Function definitions''). It should be
371called with an argument
Fred Drakef6669171998-05-06 19:52:49 +0000372list containing the same number of items as the function's formal
373parameter list.
374\indexii{user-defined}{function}
375\obindex{function}
376\obindex{user-defined function}
377
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000378Special read-only attributes: \code{func_doc} or \code{__doc__} is the
379function's documentation string, or None if unavailable;
380\code{func_name} or \code{__name__} is the function's name;
381\code{func_defaults} is a tuple containing default argument values for
382those arguments that have defaults, or \code{None} if no arguments
383have a default value; \code{func_code} is the code object representing
384the compiled function body; \code{func_globals} is (a reference to)
385the dictionary that holds the function's global variables --- it
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000386defines the global namespace of the module in which the function was
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000387defined. Additional information about a function's definition can be
388retrieved from its code object; see the description of internal types
389below.
390\ttindex{func_doc}
391\ttindex{__doc__}
392\ttindex{__name__}
393\ttindex{func_defaults}
Fred Drakef6669171998-05-06 19:52:49 +0000394\ttindex{func_code}
395\ttindex{func_globals}
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000396\indexii{global}{namespace}
Fred Drakef6669171998-05-06 19:52:49 +0000397
398\item[User-defined methods]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000399A user-defined method object combines a class, a class instance (or
400\code{None}) and a user-defined function.
Fred Drakef6669171998-05-06 19:52:49 +0000401\obindex{method}
402\obindex{user-defined method}
403\indexii{user-defined}{method}
Fred Drakef6669171998-05-06 19:52:49 +0000404
405Special read-only attributes: \member{im_self} is the class instance
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000406object, \member{im_func} is the function object;
407\code{im_class} is the class that defined the method (which may be a
408base class of the class of which \code{im_self} is an instance);
409\code{__doc__} is the method's documentation (same as
410\code{im_func.__doc__}); \code{__name__} is the method name (same as
411\code{im_func.__name__}).
412
413User-defined method objects are created in two ways: when getting an
414attribute of a class that is a user-defined function object, or when
415getting an attributes of a class instance that is a user-defined
416function object. In the former case (class attribute), the
417\code{im_self} attribute is \code{None}, and the method object is said
418to be unbound; in the latter case (instance attribute), \code{im_self}
419is the instance, and the method object is said to be bound. For
420instance, when \code{C} is a class which contains a definition for a
421function \code{f}, \code{C.f} does not yield the function object
422\code{f}; rather, it yields an unbound method object \code{m} where
423\code{m.im_class} is \code{C}, \code{m.im_function} is \code{f}, and
424m\code{.im_self} is \code{None}. When \code{x} is a \code{C}
425instance, \code{x.f} yields a bound method object \code{m} where
426m\code{.im_class} is \code{C}, \code{m.im_function} is \code{f}, and
427\code{m.im_self} is \code{x}.
428
429When an unbound user-defined method object is called, the underlying
430function (\code{im_func}) is called, with the restriction that the
431first argument must be an instance of the proper class
432(\code{im_class}) or of a derived class thereof.
433
434When a bound user-defined method object is called, the underlying
435function (\code{im_func}) is called, inserting the class instance
436(\code{im_self}) in front of the argument list. For instance, when
437\code{C} is a class which contains a definition for a function
438\code{f}, and \code{x} is an instance of \code{C}, calling
439\code{x.f(1)} is equivalent to calling \code{C.f(x, 1)}.
440
441Note that the transformation from function object to (unbound or
442bound) method object happens each time the attribute is retrieved from
443the class or instance. In some cases, a fruitful optimization is to
444assign the attribute to a local variable and call that local variable.
445Also notice that this transformation only happens for user-defined
446functions; other callable objects (and all non-callable objects) are
447retrieved without transformation.
448
Fred Drakef6669171998-05-06 19:52:49 +0000449\ttindex{im_func}
450\ttindex{im_self}
451
452\item[Built-in functions]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000453A built-in function object is a wrapper around a \C{} function. Examples
454of built-in functions are \function{len()} and \function{math.sin()}
455(\module{math} is a standard built-in module).
456The number and type of the arguments are
Fred Drakef6669171998-05-06 19:52:49 +0000457determined by the C function.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000458Special read-only attributes: \code{__doc__} is the function's
459documentation string, or \code{None} if unavailable; \code{__name__}
460is the function's name; \code{__self__} is set to \code{None} (but see
461the next item).
Fred Drakef6669171998-05-06 19:52:49 +0000462\obindex{built-in function}
463\obindex{function}
464\indexii{C}{language}
465
466\item[Built-in methods]
467This is really a different disguise of a built-in function, this time
468containing an object passed to the \C{} function as an implicit extra
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000469argument. An example of a built-in method is
470\code{\var{list}.append()}, assuming
Fred Drakef6669171998-05-06 19:52:49 +0000471\var{list} is a list object.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000472In this case, the special read-only attribute \code{__self__} is set
473to the object denoted by \code{list}.
Fred Drakef6669171998-05-06 19:52:49 +0000474\obindex{built-in method}
475\obindex{method}
476\indexii{built-in}{method}
477
478\item[Classes]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000479Class objects are described below. When a class object is called,
480a new class instance (also described below) is created and
Fred Drakef6669171998-05-06 19:52:49 +0000481returned. This implies a call to the class's \method{__init__()} method
482if it has one. Any arguments are passed on to the \method{__init__()}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000483method. If there is no \method{__init__()} method, the class must be called
Fred Drakef6669171998-05-06 19:52:49 +0000484without arguments.
485\ttindex{__init__}
486\obindex{class}
487\obindex{class instance}
488\obindex{instance}
489\indexii{class object}{call}
490
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000491\item[Class instances]
492Class instances are described below. Class instances are callable
493only when the class has a \code{__call__} method; \code{x(arguments)}
494is a shorthand for \code{x.__call__(arguments)}.
495
Fred Drakef6669171998-05-06 19:52:49 +0000496\end{description}
497
498\item[Modules]
499Modules are imported by the \keyword{import} statement (see section
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000500\ref{import}, ``The \keyword{import} statement'').
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000501A module object has a namespace implemented by a dictionary object
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000502(this is the dictionary referenced by the func_globals attribute of
503functions defined in the module). Attribute references are translated
504to lookups in this dictionary, e.g., \code{m.x} is equivalent to
505\code{m.__dict__["x"]}.
506A module object does not contain the code object used to
Fred Drakef6669171998-05-06 19:52:49 +0000507initialize the module (since it isn't needed once the initialization
508is done).
509\stindex{import}
510\obindex{module}
511
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000512Attribute assignment updates the module's namespace dictionary,
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000513e.g., ``\code{m.x = 1}'' is equivalent to ``\code{m.__dict__["x"] = 1}''.
Fred Drakef6669171998-05-06 19:52:49 +0000514
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000515Special read-only attribute: \member{__dict__} is the module's
516namespace as a dictionary object.
Fred Drakef6669171998-05-06 19:52:49 +0000517\ttindex{__dict__}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000518
519Predefined (writable) attributes: \member{__name__}
520is the module's name; \member{__doc__} is the
521module's documentation string, or
522\code{None} if unavailable; \code{__file__} is the pathname of the
523file from which the module was loaded, if it was loaded from a file.
524The \code{__file__} attribute is not present for C{} modules that are
525statically linked into the interpreter; for extension modules loaded
526dynamically from a shared library, it is the pathname of the shared
527library file.
Fred Drakef6669171998-05-06 19:52:49 +0000528\ttindex{__name__}
529\ttindex{__doc__}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000530\ttindex{__file__}
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000531\indexii{module}{namespace}
Fred Drakef6669171998-05-06 19:52:49 +0000532
533\item[Classes]
534Class objects are created by class definitions (see section
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000535\ref{class}, ``Class definitions'').
536A class has a namespace implemented by a dictionary object.
537Class attribute references are translated to
538lookups in this dictionary,
539e.g., ``\code{C.x}'' is translated to ``\code{C.__dict__["x"]}''.
540When the attribute name is not found
Fred Drakef6669171998-05-06 19:52:49 +0000541there, the attribute search continues in the base classes. The search
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000542is depth-first, left-to-right in the order of occurrence in the
Fred Drakef6669171998-05-06 19:52:49 +0000543base class list.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000544When a class attribute reference would yield a user-defined function
545object, it is transformed into an unbound user-defined method object
546(see above). The \code{im_class} attribute of this method object is the
547class in which the function object was found, not necessarily the
548class for which the attribute reference was initiated.
Fred Drakef6669171998-05-06 19:52:49 +0000549\obindex{class}
550\obindex{class instance}
551\obindex{instance}
552\indexii{class object}{call}
553\index{container}
554\obindex{dictionary}
555\indexii{class}{attribute}
556
557Class attribute assignments update the class's dictionary, never the
558dictionary of a base class.
559\indexiii{class}{attribute}{assignment}
560
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000561A class object can be called (see above) to yield a class instance (see
562below).
Fred Drakef6669171998-05-06 19:52:49 +0000563\indexii{class object}{call}
564
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000565Special attributes: \member{__name__} is the class name;
566\member{__module__} is the module name in which the class was defined;
Guido van Rossumdfb658c1998-07-23 17:54:36 +0000567\member{__dict__} is the dictionary containing the class's namespace;
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000568\member{__bases__} is a tuple (possibly empty or a singleton)
569containing the base classes, in the order of their occurrence in the
570base class list; \code{__doc__} is the class's documentation string,
571or None if undefined.
572\ttindex{__name__}
573\ttindex{__module__}
Fred Drakef6669171998-05-06 19:52:49 +0000574\ttindex{__dict__}
575\ttindex{__bases__}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000576\ttindex{__doc__}
Fred Drakef6669171998-05-06 19:52:49 +0000577
578\item[Class instances]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000579A class instance is created by calling a class object (see above).
580A class instance has a namespace implemented as a dictionary which
581is the first place in which
Fred Drakef6669171998-05-06 19:52:49 +0000582attribute references are searched. When an attribute is not found
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000583there, and the instance's class has an attribute by that name,
584the search continues with the class attributes. If a class attribute
585is found that is a user-defined function object (and in no other
586case), it is transformed into an unbound user-defined method object
587(see above). The \code{im_class} attribute of this method object is
588the class in which the function object was found, not necessarily the
589class of the instance for which the attribute reference was initiated.
590If no class attribute is found, and the object's class has a
591\code{__getattr__} method, that is called to satisfy the lookup.
Fred Drakef6669171998-05-06 19:52:49 +0000592\obindex{class instance}
593\obindex{instance}
594\indexii{class}{instance}
595\indexii{class instance}{attribute}
596
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000597Attribute assignments and deletions update the instance's dictionary,
598never a class's dictionary. If the class has a \code{__setattr__} or
599\code{__delattr__} method, this is called instead of updating the
600instance dictionary directly.
Fred Drakef6669171998-05-06 19:52:49 +0000601\indexiii{class instance}{attribute}{assignment}
602
603Class instances can pretend to be numbers, sequences, or mappings if
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000604they have methods with certain special names. See
605section \ref{specialnames}, ``Special method names.''
Fred Drakef6669171998-05-06 19:52:49 +0000606\obindex{number}
607\obindex{sequence}
608\obindex{mapping}
609
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000610Special attributes: \member{__dict__} is the attribute
611dictionary; \member{__class__} is the instance's class.
Fred Drakef6669171998-05-06 19:52:49 +0000612\ttindex{__dict__}
613\ttindex{__class__}
614
615\item[Files]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000616A file object represents an open file. File objects are created by the
617\function{open()} built-in function, and also by
618\function{os.popen()}, \function{os.fdopen()}, and the
619\method{makefile()} method of socket objects (and perhaps by other
620functions or methods provided by extension modules). The objects
621\code{sys.stdin}, \code{sys.stdout} and \code{sys.stderr} are
622initialized to file objects corresponding to the interpreter's
623standard input, output and error streams. See the \emph{Python
624Library Reference} for complete documentation of file objects.
Fred Drakef6669171998-05-06 19:52:49 +0000625\obindex{file}
626\indexii{C}{language}
627\index{stdio}
628\bifuncindex{open}
629\bifuncindex{popen}
630\bifuncindex{makefile}
631\ttindex{stdin}
632\ttindex{stdout}
633\ttindex{stderr}
634\ttindex{sys.stdin}
635\ttindex{sys.stdout}
636\ttindex{sys.stderr}
637
638\item[Internal types]
639A few types used internally by the interpreter are exposed to the user.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000640Their definitions may change with future versions of the interpreter,
Fred Drakef6669171998-05-06 19:52:49 +0000641but they are mentioned here for completeness.
642\index{internal type}
643\index{types, internal}
644
645\begin{description}
646
647\item[Code objects]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000648Code objects represent \emph{byte-compiled} executable Python code, or
649\emph{bytecode}.
Fred Drakef6669171998-05-06 19:52:49 +0000650The difference between a code
651object and a function object is that the function object contains an
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000652explicit reference to the function's globals (the module in which it
653was defined), while a code object contains no context;
654also the default argument values are stored in the function object,
655not in the code object (because they represent values calculated at
656run-time). Unlike function objects, code objects are immutable and
657contain no references (directly or indirectly) to mutable objects.
658\index{bytecode}
Fred Drakef6669171998-05-06 19:52:49 +0000659\obindex{code}
660
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000661Special read-only attributes: \code{co_name}\ttindex{co_name} gives
662the function name; \code{co_argcount}\ttindex{co_argcount}
663is the number of positional arguments (including arguments with
664default values); \code{co_nlocals}\ttindex{co_nlocals} is the number
665of local variables used by the function (including arguments);
666\code{co_varnames}\ttindex{co_varnames} is a tuple containing the
667names of the local variables (starting with the argument names);
668\code{co_code}\ttindex{co_code} is a string representing the sequence
669of bytecode instructions; \code{co_consts}\ttindex{co_consts} is a
670tuple containing the literals used by the bytecode;
671\code{co_names}\ttindex{co_names} is a tuple containing the names used
672by the bytecode; \code{co_filename}\ttindex{co_filename} is the
673filename from which the code was compiled;
674\code{co_firstlineno}\ttindex{co_firstlineno} is the first line number
675of the function; \code{co_lnotab}\ttindex{co_lnotab} is a string
676encoding the mapping from byte code offsets to line numbers (for
677detais see the source code of the interpreter);
678\code{co_stacksize}\ttindex{co_stacksize} is the required stack size
679(including local variables); \code{co_flags}\ttindex{co_flags} is an
680integer encoding a number of flags for the interpreter.
681
682The following flag bits are defined for \code{co_flags}: bit 2 is set
683if the function uses the ``\code{*arguments}'' syntax to accept an
684arbitrary number of positional arguments; bit 3 is set if the function
685uses the ``\code{**keywords}'' syntax to accept arbitrary keyword
686arguments; other bits are used internally or reserved for future use.
687If a code object represents a function, the first item in
688\code{co_consts} is the documentation string of the
689function, or \code{None} if undefined.
Fred Drakef6669171998-05-06 19:52:49 +0000690
691\item[Frame objects]
692Frame objects represent execution frames. They may occur in traceback
693objects (see below).
694\obindex{frame}
695
696Special read-only attributes: \member{f_back} is to the previous
697stack frame (towards the caller), or \code{None} if this is the bottom
698stack frame; \member{f_code} is the code object being executed in this
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000699frame; \member{f_locals} is the dictionary used to look up local
700variables; \member{f_globals} is used for global variables;
701\code{f_builtins} is used for built-in (intrinsic) names;
702\code{f_restricted} is a flag indicating whether the function is
703executing in restricted execution mode;
Fred Drakef6669171998-05-06 19:52:49 +0000704\member{f_lineno} gives the line number and \member{f_lasti} gives the
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000705precise instruction (this is an index into the bytecode string of
Fred Drakef6669171998-05-06 19:52:49 +0000706the code object).
707\ttindex{f_back}
708\ttindex{f_code}
709\ttindex{f_globals}
710\ttindex{f_locals}
711\ttindex{f_lineno}
712\ttindex{f_lasti}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000713\ttindex{f_builtins}
714\ttindex{f_restricted}
715
716Special writable attributes: \code{f_trace}, if not \code{None}, is a
717function called at the start of each source code line (this is used by
718the debugger); \code{f_exc_type}, \code{f_exc_value},
719\code{f_exc_traceback} represent the most recent exception caught in
720this frame.
721\ttindex{f_trace}
722\ttindex{f_exc_type}
723\ttindex{f_exc_value}
724\ttindex{f_exc_traceback}
Fred Drakef6669171998-05-06 19:52:49 +0000725
726\item[Traceback objects] \label{traceback}
727Traceback objects represent a stack trace of an exception. A
728traceback object is created when an exception occurs. When the search
729for an exception handler unwinds the execution stack, at each unwound
730level a traceback object is inserted in front of the current
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000731traceback. When an exception handler is entered, the stack trace is
732made available to the program.
733(See section \ref{try}, ``The \code{try} statement.'')
734It is accessible as \code{sys.exc_traceback}, and also as the third
735item of the tuple returned by \code{sys.exc_info()}. The latter is
736the preferred interface, since it works correctly when the program is
737using multiple threads.
738When the program contains no suitable handler, the stack trace is written
Fred Drakef6669171998-05-06 19:52:49 +0000739(nicely formatted) to the standard error stream; if the interpreter is
740interactive, it is also made available to the user as
741\code{sys.last_traceback}.
742\obindex{traceback}
743\indexii{stack}{trace}
744\indexii{exception}{handler}
745\indexii{execution}{stack}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000746\ttindex{exc_info}
Fred Drakef6669171998-05-06 19:52:49 +0000747\ttindex{exc_traceback}
748\ttindex{last_traceback}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000749\ttindex{sys.exc_info}
Fred Drakef6669171998-05-06 19:52:49 +0000750\ttindex{sys.exc_traceback}
751\ttindex{sys.last_traceback}
752
753Special read-only attributes: \member{tb_next} is the next level in the
754stack trace (towards the frame where the exception occurred), or
755\code{None} if there is no next level; \member{tb_frame} points to the
756execution frame of the current level; \member{tb_lineno} gives the line
757number where the exception occurred; \member{tb_lasti} indicates the
758precise instruction. The line number and last instruction in the
759traceback may differ from the line number of its frame object if the
760exception occurred in a \keyword{try} statement with no matching
761except clause or with a finally clause.
762\ttindex{tb_next}
763\ttindex{tb_frame}
764\ttindex{tb_lineno}
765\ttindex{tb_lasti}
766\stindex{try}
767
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000768\item[Slice objects]
769Slice objects are used to represent slices when \emph{extended slice
770syntax} is used. This is a slice using two colons, or multiple slices
771or ellipses separated by commas, e.g., \code{a[i:j:step]}, \code{a[i:j,
772k:l]}, or \code{a[..., i:j])}. They are also created by the built-in
773\function{slice()} function.
774
775Special read-only attributes: \code{start} is the lowerbound;
776\code{stop} is the upperbound; \code{step} is the step value; each is
777\code{None} if omitted. These attributes can have any type.
778
Fred Drakef6669171998-05-06 19:52:49 +0000779\end{description} % Internal types
780
781\end{description} % Types
782
783
784\section{Special method names} \label{specialnames}
785
786A class can implement certain operations that are invoked by special
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000787syntax (such as arithmetic operations or subscripting and slicing) by defining
Fred Drakef6669171998-05-06 19:52:49 +0000788methods with special names. For instance, if a class defines a
789method named \method{__getitem__()}, and \code{x} is an instance of this
790class, then \code{x[i]} is equivalent to \code{x.__getitem__(i)}.
791(The reverse is not true --- if \code{x} is a list object,
792\code{x.__getitem__(i)} is not equivalent to \code{x[i]}.)
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000793Except where mentioned, attempts to execute an
794operation raise an exception when no appropriate method is defined.
Fred Drakef6669171998-05-06 19:52:49 +0000795\ttindex{__getitem__}
796
Fred Drakef6669171998-05-06 19:52:49 +0000797
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000798\subsection{Basic customization}
Fred Drakef6669171998-05-06 19:52:49 +0000799
800\begin{description}
801
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000802\item[{\tt __init__(self, [args...])}]
Fred Drakef6669171998-05-06 19:52:49 +0000803Called when the instance is created. The arguments are those passed
804to the class constructor expression. If a base class has an
805\code{__init__} method the derived class's \code{__init__} method must
806explicitly call it to ensure proper initialization of the base class
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000807part of the instance, e.g., ``\code{BaseClass.__init__(self, [args...])}''.
Fred Drakef6669171998-05-06 19:52:49 +0000808\ttindex{__init__}
809\indexii{class}{constructor}
810
811
812\item[{\tt __del__(self)}]
813Called when the instance is about to be destroyed. If a base class
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000814has a \method{__del__()} method, the derived class's \method{__del__()} method
Fred Drakef6669171998-05-06 19:52:49 +0000815must explicitly call it to ensure proper deletion of the base class
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000816part of the instance. Note that it is possible (though not recommended!)
817for the \method{__del__()}
Fred Drakef6669171998-05-06 19:52:49 +0000818method to postpone destruction of the instance by creating a new
819reference to it. It may then be called at a later time when this new
820reference is deleted. It is not guaranteed that
821\method{__del__()} methods are called for objects that still exist when
822the interpreter exits.
Fred Drakef6669171998-05-06 19:52:49 +0000823\ttindex{__del__}
824\stindex{del}
825
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000826\strong{Programmer's note:} ``\code{del x}'' doesn't directly call
827\code{x.__del__()} --- the former decrements the reference count for
828\code{x} by one, and the latter is only called when its reference
829count reaches zero. Some common situations that may prevent the
830reference count of an object to go to zero include: circular
831references between objects (e.g., a doubly-linked list or a tree data
832structure with parent and child pointers); a reference to the object
833on the stack frame of a function that caught an exception (the
834traceback stored in \code{sys.exc_traceback} keeps the stack frame
835alive); or a reference to the object on the stack frame that raised an
836unhandled exception in interactive mode (the traceback stored in
837\code{sys.last_traceback} keeps the stack frame alive). The first
838situation can only be remedied by explicitly breaking the cycles; the
839latter two situations can be resolved by storing None in
840\code{sys.exc_traceback} or \code{sys.last_traceback}.
Fred Drakef6669171998-05-06 19:52:49 +0000841
842\strong{Warning:} due to the precarious circumstances under which
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000843\code{__del__} methods are invoked, exceptions that occur during their
844execution are ignored, and a warning is printed to \code{sys.stderr}
845instead. Also, when \code{__del__} is invoked is response to a module
846being deleted (e.g., when execution of the program is done), other
847globals referenced by the \code{__del__} method may already have been
848deleted. For this reason, \code{__del__} methods should do the
849absolute minimum needed to maintain external invariants. Python 1.5
850guarantees that globals whose name begins with a single underscore are
851deleted from their module before other globals are deleted; if no
852other references to such globals exist, this may help in assuring that
853imported modules are still available at the time when the
854\code{__del__} method is called.
Fred Drakef6669171998-05-06 19:52:49 +0000855
856\item[{\tt __repr__(self)}]
857Called by the \function{repr()} built-in function and by string conversions
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000858(reverse quotes) to compute the ``official'' string representation of
859an object. This should normally look like a valid Python expression
860that can be used to recreate an object with the same value.
861This differs from \code{__repr__} in that it doesn't have to look like
862a valid Python expression: a more convenient or concise representation
863may be used instead.
Fred Drakef6669171998-05-06 19:52:49 +0000864\ttindex{__repr__}
865\bifuncindex{repr}
866\indexii{string}{conversion}
867\indexii{reverse}{quotes}
868\indexii{backward}{quotes}
869\index{back-quotes}
870
871\item[{\tt __str__(self)}]
872Called by the \function{str()} built-in function and by the \keyword{print}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000873statement to compute the ``informal'' string representation of an object.
Fred Drakef6669171998-05-06 19:52:49 +0000874\ttindex{__str__}
875\bifuncindex{str}
876\stindex{print}
877
878\item[{\tt __cmp__(self, other)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000879Called by all comparison operations. Should return a negative integer if
880\code{self < other}, zero if \code{self == other}, a positive integer if
Fred Drakef6669171998-05-06 19:52:49 +0000881\code{self > other}. If no \method{__cmp__()} operation is defined, class
882instances are compared by object identity (``address'').
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000883(Note: the restriction that exceptions are not propagated by
884\code{__cmp__} has been removed in Python 1.5.)
Fred Drakef6669171998-05-06 19:52:49 +0000885\ttindex{__cmp__}
886\bifuncindex{cmp}
887\index{comparisons}
888
889\item[{\tt __hash__(self)}]
890Called for the key object for dictionary operations,
891and by the built-in function
892\function{hash()}\bifuncindex{hash}. Should return a 32-bit integer
893usable as a hash value
894for dictionary operations. The only required property is that objects
895which compare equal have the same hash value; it is advised to somehow
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000896mix together (e.g., using exclusive or) the hash values for the
Fred Drakef6669171998-05-06 19:52:49 +0000897components of the object that also play a part in comparison of
898objects. If a class does not define a \method{__cmp__()} method it should
899not define a \method{__hash__()} operation either; if it defines
900\method{__cmp__()} but not \method{__hash__()} its instances will not be
901usable as dictionary keys. If a class defines mutable objects and
902implements a \method{__cmp__()} method it should not implement
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000903\method{__hash__()}, since the dictionary implementation requires that
904a key's hash value is immutable (if the object's hash value changes, it
905will be in the wrong hash bucket).
Fred Drakef6669171998-05-06 19:52:49 +0000906\obindex{dictionary}
907\ttindex{__cmp__}
908\ttindex{__hash__}
909
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000910\item[__nonzero__(self)]
911Called to implement truth value testing; should return 0 or 1. When
912this method is not defined, \code{__len__} is called, if it is defined
913(see below). If a class defines neither \code{__len__} nor
914\code{__nonzero__}, all its instances are considered true.
Fred Drakef6669171998-05-06 19:52:49 +0000915
916\end{description}
917
918
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000919\subsection{Customizing attribute access}
Fred Drakef6669171998-05-06 19:52:49 +0000920
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000921The following methods can be defined to customize the meaning of
922attribute access (use of, assignment to, or deletion of \code{x.name})
923for class instances.
924For performance reasons, these methods are cached in the class object
925at class definition time; therefore, they cannot be changed after the
926class definition is executed.
Fred Drakef6669171998-05-06 19:52:49 +0000927
928\begin{description}
929
930\item[{\tt __getattr__(self, name)}]
931Called when an attribute lookup has not found the attribute in the
932usual places (i.e. it is not an instance attribute nor is it found in
933the class tree for \code{self}). \code{name} is the attribute name.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000934This method should return the (computed) attribute value or raise an
935\code{AttributeError} exception.
Fred Drakef6669171998-05-06 19:52:49 +0000936\ttindex{__getattr__}
937
938Note that if the attribute is found through the normal mechanism,
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000939\code{__getattr__} is not called. (This is an intentional asymmetry between
Fred Drakef6669171998-05-06 19:52:49 +0000940\code{__getattr__} and \code{__setattr__}.)
941This is done both for efficiency reasons and because otherwise
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000942\code{__setattr__} would have no way to access other attributes of the
Fred Drakef6669171998-05-06 19:52:49 +0000943instance.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000944Note that at least for instance variables, you can fake
945total control by not inserting any values in the instance
946attribute dictionary (but instead inserting them in another object).
Fred Drakef6669171998-05-06 19:52:49 +0000947\ttindex{__setattr__}
948
949\item[{\tt __setattr__(self, name, value)}]
950Called when an attribute assignment is attempted. This is called
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000951instead of the normal mechanism (i.e. store the value in the instance
952dictionary). \code{name} is the attribute name, \code{value} is the
Fred Drakef6669171998-05-06 19:52:49 +0000953value to be assigned to it.
954\ttindex{__setattr__}
955
956If \code{__setattr__} wants to assign to an instance attribute, it
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000957should not simply execute ``\code{self.\var{name} = value}'' --- this would
958cause a recursive call to itself. Instead, it should insert the value in the
959dictionary of instance attributes, e.g.,
960``\code{self.__dict__[name] = value}.''
Fred Drakef6669171998-05-06 19:52:49 +0000961\ttindex{__dict__}
962
963\item[{\tt __delattr__(self, name)}]
964Like \code{__setattr__} but for attribute deletion instead of
965assignment.
966\ttindex{__delattr__}
967
968\end{description}
969
970
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000971\subsection{Emulating callable objects}
972
973\begin{description}
974
975\item[{\tt __call__(self, [args...])}]
976Called when the instance is ``called'' as a function; if this method
977is defined, \code{x(arg1, arg2, ...)} is a shorthand for
978\code{x.__call__(arg1, arg2, ...)}.
979\ttindex{__call__}
980\indexii{call}{instance}
981
982\end{description}
983
984
985\subsection{Emulating sequence and mapping types}
986
987The following methods can be defined to emulate sequence or mapping
988objects. The first set of methods is used either to emulate a
989sequence or to emulate a mapping; the difference is that for a
990sequence, the allowable keys should be the integers \var{k} for which
991\code{0 <= \var{k} < \var{N}} where \var{N} is the length of the
992sequence, and the method \method{__getslice__()} (see below) should be
993defined. It is also recommended that mappings provide methods
994\method{keys()}, \method{values()}, \method{items()},
995\method{has_key()}, \method{get()}, \method{clear()}, \method{copy()},
996 and \method{update()} behaving similar to those for
997Python's standard dictionary objects; mutable sequences should provide
998methods \method{append()}, \method{count()}, \method{index()},
999\method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()}
1000and \method{sort()}, like Python standard list objects. Finally,
1001sequence types should implement addition (meaning concatenation) and
1002multiplication (meaning repetition) by defining the methods
1003\method{__add__()}, \method{__radd__()}, \method{__mul__()} and
1004\method{__rmul__()} described below; they should not define
1005\method{__coerce__()} or other numerical operators.
1006\ttindex{keys}
1007\ttindex{values}
1008\ttindex{items}
1009\ttindex{has_key}
1010\ttindex{get}
1011\ttindex{clear}
1012\ttindex{copy}
1013\ttindex{update}
1014\ttindex{append}
1015\ttindex{count}
1016\ttindex{index}
1017\ttindex{insert}
1018\ttindex{pop}
1019\ttindex{remove}
1020\ttindex{reverse}
1021\ttindex{sort}
1022\ttindex{__add__}
1023\ttindex{__radd__}
1024\ttindex{__mul__}
1025\ttindex{__rmul__}
1026\ttindex{__coerce__}
Fred Drakef6669171998-05-06 19:52:49 +00001027
1028\begin{description}
1029
1030\item[{\tt __len__(self)}]
1031Called to implement the built-in function \function{len()}. Should return
1032the length of the object, an integer \code{>=} 0. Also, an object
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001033that doesn't define a \method{__nonzero__()} method and
1034whose \method{__len__()} method returns zero is considered to be false in a
Fred Drakef6669171998-05-06 19:52:49 +00001035Boolean context.
1036\ttindex{__len__}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001037\ttindex{__nonzero__}
Fred Drakef6669171998-05-06 19:52:49 +00001038
1039\item[{\tt __getitem__(self, key)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001040Called to implement evaluation of \code{self[key]}.
1041For a sequence types, the accepted keys should be integers. Note that the
1042special interpretation of negative indices (if the class wishes to
Fred Drakef6669171998-05-06 19:52:49 +00001043emulate a sequence type) is up to the \method{__getitem__()} method.
1044\ttindex{__getitem__}
1045
1046\item[{\tt __setitem__(self, key, value)}]
1047Called to implement assignment to \code{self[key]}. Same note as for
1048\method{__getitem__()}.
1049\ttindex{__setitem__}
1050
1051\item[{\tt __delitem__(self, key)}]
1052Called to implement deletion of \code{self[key]}. Same note as for
1053\method{__getitem__()}.
1054\ttindex{__delitem__}
1055
1056\end{description}
1057
1058
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001059\subsection{Additional methods for emulation of sequence types}
1060
1061The following methods can be defined to further emulate sequence
1062objects. Immutable sequences methods should only define
1063\method{__getslice__()}; mutable sequences, should define all three
1064three methods.
Fred Drakef6669171998-05-06 19:52:49 +00001065
1066\begin{description}
1067
1068\item[{\tt __getslice__(self, i, j)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001069Called to implement evaluation of \code{self[i:j]}. The returned
1070object should be of the same type as \code{self}. Note that missing
1071\var{i} or \var{j} in the slice expression are replaced by zero or
1072\code{sys.maxint}, respectively, and no further transformations on the
1073indices is performed. The interpretation of negative indices and
1074indices larger than the length of the sequence is up to the method.
Fred Drakef6669171998-05-06 19:52:49 +00001075\ttindex{__getslice__}
1076
1077\item[{\tt __setslice__(self, i, j, sequence)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001078Called to implement assignment to \code{self[i:j]}. Same notes for
1079\var{i} and \var{j} as for \method{__getslice__()}.
Fred Drakef6669171998-05-06 19:52:49 +00001080\ttindex{__setslice__}
1081
1082\item[{\tt __delslice__(self, i, j)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001083Called to implement deletion of \code{self[i:j]}. Same notes for
1084\var{i} and \var{j} as for \method{__getslice__()}.
Fred Drakef6669171998-05-06 19:52:49 +00001085\ttindex{__delslice__}
1086
1087\end{description}
1088
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001089Notice that these methods are only invoked when a single slice with a
1090single colon is used. For slice operations involving extended slice
1091notation, \method{__getitem__()}, \method{__setitem__()}
1092or\method{__delitem__()} is called.
Fred Drakef6669171998-05-06 19:52:49 +00001093
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001094\subsection{Emulating numeric types}
1095
1096The following methods can be defined to emulate numeric objects.
1097Methods corresponding to operations that are not supported by the
1098particular kind of number implemented (e.g., bitwise operations for
1099non-integral numbers) should be left undefined.
Fred Drakef6669171998-05-06 19:52:49 +00001100
1101\begin{description}
1102
1103\item[{\tt __add__(self, other)}]\itemjoin
1104\item[{\tt __sub__(self, other)}]\itemjoin
1105\item[{\tt __mul__(self, other)}]\itemjoin
1106\item[{\tt __div__(self, other)}]\itemjoin
1107\item[{\tt __mod__(self, other)}]\itemjoin
1108\item[{\tt __divmod__(self, other)}]\itemjoin
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001109\item[{\tt __pow__(self, other \optional{, modulo})}]\itemjoin
Fred Drakef6669171998-05-06 19:52:49 +00001110\item[{\tt __lshift__(self, other)}]\itemjoin
1111\item[{\tt __rshift__(self, other)}]\itemjoin
1112\item[{\tt __and__(self, other)}]\itemjoin
1113\item[{\tt __xor__(self, other)}]\itemjoin
1114\item[{\tt __or__(self, other)}]\itembreak
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001115These functions are
1116called to implement the binary arithmetic operations (\code{+},
Fred Drakef6669171998-05-06 19:52:49 +00001117\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()},
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001118\code{**},
Fred Drakef6669171998-05-06 19:52:49 +00001119\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}).
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001120For instance, to evaluate the expression \var{x}\code{+}\var{y}, where
1121\var{x} is an instance of a class that has an \method{__add__()}
1122method, \code{\var{x}.__add__(\var{y})} is called.
1123Note that \function{__pow__()} should be defined to accept an optional
1124third argument if the ternary version of the built-in \function{pow()}
1125function is to be supported.
1126\ttindex{__or__}
1127\ttindex{__xor__}
1128\ttindex{__and__}
1129\ttindex{__rshift__}
1130\ttindex{__lshift__}
1131\ttindex{__pow__}
1132\ttindex{__divmod__}
1133\ttindex{__mod__}
1134\ttindex{__div__}
1135\ttindex{__mul__}
1136\ttindex{__sub__}
1137\ttindex{__add__}
1138
1139\item[{\tt __radd__(self, other)}]\itemjoin
1140\item[{\tt __rsub__(self, other)}]\itemjoin
1141\item[{\tt __rmul__(self, other)}]\itemjoin
1142\item[{\tt __rdiv__(self, other)}]\itemjoin
1143\item[{\tt __rmod__(self, other)}]\itemjoin
1144\item[{\tt __rdivmod__(self, other)}]\itemjoin
1145\item[{\tt __rpow__(self, other)}]\itemjoin
1146\item[{\tt __rlshift__(self, other)}]\itemjoin
1147\item[{\tt __rrshift__(self, other)}]\itemjoin
1148\item[{\tt __rand__(self, other)}]\itemjoin
1149\item[{\tt __rxor__(self, other)}]\itemjoin
1150\item[{\tt __ror__(self, other)}]\itembreak
1151These functions are
1152called to implement the binary arithmetic operations (\code{+},
1153\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()},
1154\code{**},
1155\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}) with reversed operands.
1156These functions are only called if the left operand does not support
1157the corresponding operation.
1158For instance, to evaluate the expression \var{x}\code{-}\var{y}, where
1159\var{y} is an instance of a class that has an \method{__rsub__()}
1160method, \code{\var{y}.__rsub__(\var{x})} is called.
1161Note that ternary \function{pow()} will not try calling
1162\method{__rpow__()} (the coercion rules would become too
1163complicated).
Fred Drakef6669171998-05-06 19:52:49 +00001164\ttindex{__or__}
1165\ttindex{__xor__}
1166\ttindex{__and__}
1167\ttindex{__rshift__}
1168\ttindex{__lshift__}
1169\ttindex{__pow__}
1170\ttindex{__divmod__}
1171\ttindex{__mod__}
1172\ttindex{__div__}
1173\ttindex{__mul__}
1174\ttindex{__sub__}
1175\ttindex{__add__}
1176
1177\item[{\tt __neg__(self)}]\itemjoin
1178\item[{\tt __pos__(self)}]\itemjoin
1179\item[{\tt __abs__(self)}]\itemjoin
1180\item[{\tt __invert__(self)}]\itembreak
1181Called to implement the unary arithmetic operations (\code{-}, \code{+},
1182\function{abs()} and \code{~}).
1183\ttindex{__invert__}
1184\ttindex{__abs__}
1185\ttindex{__pos__}
1186\ttindex{__neg__}
1187
Fred Drakef6669171998-05-06 19:52:49 +00001188\item[{\tt __int__(self)}]\itemjoin
1189\item[{\tt __long__(self)}]\itemjoin
1190\item[{\tt __float__(self)}]\itembreak
1191Called to implement the built-in functions \function{int()}, \function{long()}
1192and \function{float()}. Should return a value of the appropriate type.
1193\ttindex{__float__}
1194\ttindex{__long__}
1195\ttindex{__int__}
1196
1197\item[{\tt __oct__(self)}]\itemjoin
1198\item[{\tt __hex__(self)}]\itembreak
1199Called to implement the built-in functions \function{oct()} and
1200\function{hex()}. Should return a string value.
1201\ttindex{__hex__}
1202\ttindex{__oct__}
1203
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001204\item[{\tt __coerce__(self, other)}]
1205Called to implement ``mixed-mode'' numeric arithmetic. Should either
1206return a 2-tuple containing \code{self} and \code{other} converted to
1207a common numeric type, or \code{None} if conversion is possible. When
1208the common type would be the type of \code{other}, it is sufficient to
1209return \code{None}, since the interpreter will also ask the other
1210object to attempt a coercion (but sometimes, if the implementation of
1211the other type cannot be changed, it is useful to do the conversion to
1212the other type here).
1213\ttindex{__coerce__}
1214
1215\strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the
1216following steps are taken (where \method{__op__()} and
1217\method{__rop__()} are the method names corresponding to \var{op},
1218e.g. if var{op} is `\code{+}', \method{__add__()} and
1219\method{__radd__()} are used). If an exception occurs at any point,
1220the evaluation is abandoned and exception handling takes over.
1221
1222\begin{itemize}
1223
1224\item[0.] If \var{x} is a string object and op is the modulo operator (\%),
1225the string formatting operation is invoked and the remaining steps are
1226skipped.
1227
1228\item[1.] If \var{x} is a class instance:
1229
1230 \begin{itemize}
1231
1232 \item[1a.] If \var{x} has a \method{__coerce__()} method:
1233 replace \var{x} and \var{y} with the 2-tuple returned by
1234 \code{\var{x}.__coerce__(\var{y})}; skip to step 2 if the
1235 coercion returns \code{None}.
1236
1237 \item[1b.] If neither \var{x} nor \var{y} is a class instance
1238 after coercion, go to step 3.
1239
1240 \item[1c.] If \var{x} has a method \method{__op__()}, return
1241 \code{\var{x}.__op__(\var{y})}; otherwise, restore \var{x} and
1242 \var{y} to their value before step 1a.
1243
1244 \end{itemize}
1245
1246\item[2.] If \var{y} is a class instance:
1247
1248 \begin{itemize}
1249
1250 \item[2a.] If \var{y} has a \method{__coerce__()} method:
1251 replace \var{y} and \var{x} with the 2-tuple returned by
1252 \code{\var{y}.__coerce__(\var{x})}; skip to step 3 if the
1253 coercion returns \code{None}.
1254
1255 \item[2b.] If neither \var{x} nor \var{y} is a class instance
1256 after coercion, go to step 3.
1257
1258 \item[2b.] If \var{y} has a method \method{__rop__()}, return
1259 \code{\var{y}.__rop__(\var{x})}; otherwise, restore \var{x}
1260 and \var{y} to their value before step 2a.
1261
1262 \end{itemize}
1263
1264\item[3.] We only get here if neither \var{x} nor \var{y} is a class
1265instance.
1266
1267 \begin{itemize}
1268
1269 \item[3a.] If op is `\code{+}' and \var{x} is a sequence,
1270 sequence concatenation is invoked.
1271
1272 \item[3b.] If op is `\code{*}' and one operand is a sequence
1273 and the other an integer, sequence repetition is invoked.
1274
1275 \item[3c.] Otherwise, both operands must be numbers; they are
1276 coerced to a common type if possible, and the numeric
1277 operation is invoked for that type.
1278
1279 \end{itemize}
1280
1281\end{itemize}
1282
Fred Drakef6669171998-05-06 19:52:49 +00001283\end{description}