blob: 4ca8d69f7292bd2a1586a8f59fedc8233b8026aa [file] [log] [blame]
Fred Drake61c77281998-07-28 19:34:22 +00001\chapter{Data model\label{datamodel}}
Fred Drakef6669171998-05-06 19:52:49 +00002
Fred Drake61c77281998-07-28 19:34:22 +00003\section{Objects, values and types\label{objects}}
Fred Drakef6669171998-05-06 19:52:49 +00004
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
Barry Warsaw92a6ed91998-08-07 16:33:51 +000038allowed to postpone garbage collection or omit it altogether --- it is
39a matter of implementation quality how garbage collection is
Fred Drakef6669171998-05-06 19:52:49 +000040implemented, 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
Fred Drake61c77281998-07-28 19:34:22 +000090\section{The standard type hierarchy\label{types}}
Fred Drakef6669171998-05-06 19:52:49 +000091
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
Fred Drake70da1921998-08-07 16:28:13 +0000423\code{m.im_class} is \code{C}, \code{m.im_func} is \code{f}, and
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000424m\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
Fred Drake70da1921998-08-07 16:28:13 +0000426m\code{.im_class} is \code{C}, \code{m.im_func} is \code{f}, and
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000427\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
Fred Drake61c77281998-07-28 19:34:22 +0000784\section{Special method names\label{specialnames}}
Fred Drakef6669171998-05-06 19:52:49 +0000785
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
Fred Drake61c77281998-07-28 19:34:22 +0000798\subsection{Basic customization\label{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)}]
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000813Called when the instance is about to be destroyed. This is also
814called a destructor\index{destructor}. If a base class
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000815has a \method{__del__()} method, the derived class's \method{__del__()} method
Fred Drakef6669171998-05-06 19:52:49 +0000816must explicitly call it to ensure proper deletion of the base class
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000817part of the instance. Note that it is possible (though not recommended!)
818for the \method{__del__()}
Fred Drakef6669171998-05-06 19:52:49 +0000819method to postpone destruction of the instance by creating a new
820reference to it. It may then be called at a later time when this new
821reference is deleted. It is not guaranteed that
822\method{__del__()} methods are called for objects that still exist when
823the interpreter exits.
Fred Drakef6669171998-05-06 19:52:49 +0000824\ttindex{__del__}
825\stindex{del}
826
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000827\strong{Programmer's note:} ``\code{del x}'' doesn't directly call
828\code{x.__del__()} --- the former decrements the reference count for
829\code{x} by one, and the latter is only called when its reference
830count reaches zero. Some common situations that may prevent the
831reference count of an object to go to zero include: circular
832references between objects (e.g., a doubly-linked list or a tree data
833structure with parent and child pointers); a reference to the object
834on the stack frame of a function that caught an exception (the
835traceback stored in \code{sys.exc_traceback} keeps the stack frame
836alive); or a reference to the object on the stack frame that raised an
837unhandled exception in interactive mode (the traceback stored in
838\code{sys.last_traceback} keeps the stack frame alive). The first
839situation can only be remedied by explicitly breaking the cycles; the
840latter two situations can be resolved by storing None in
841\code{sys.exc_traceback} or \code{sys.last_traceback}.
Fred Drakef6669171998-05-06 19:52:49 +0000842
843\strong{Warning:} due to the precarious circumstances under which
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000844\code{__del__} methods are invoked, exceptions that occur during their
845execution are ignored, and a warning is printed to \code{sys.stderr}
846instead. Also, when \code{__del__} is invoked is response to a module
847being deleted (e.g., when execution of the program is done), other
848globals referenced by the \code{__del__} method may already have been
849deleted. For this reason, \code{__del__} methods should do the
850absolute minimum needed to maintain external invariants. Python 1.5
851guarantees that globals whose name begins with a single underscore are
852deleted from their module before other globals are deleted; if no
853other references to such globals exist, this may help in assuring that
854imported modules are still available at the time when the
855\code{__del__} method is called.
Fred Drakef6669171998-05-06 19:52:49 +0000856
857\item[{\tt __repr__(self)}]
858Called by the \function{repr()} built-in function and by string conversions
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000859(reverse quotes) to compute the ``official'' string representation of
860an object. This should normally look like a valid Python expression
861that can be used to recreate an object with the same value.
862This differs from \code{__repr__} in that it doesn't have to look like
863a valid Python expression: a more convenient or concise representation
864may be used instead.
Fred Drakef6669171998-05-06 19:52:49 +0000865\ttindex{__repr__}
866\bifuncindex{repr}
867\indexii{string}{conversion}
868\indexii{reverse}{quotes}
869\indexii{backward}{quotes}
870\index{back-quotes}
871
872\item[{\tt __str__(self)}]
873Called by the \function{str()} built-in function and by the \keyword{print}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000874statement to compute the ``informal'' string representation of an object.
Fred Drakef6669171998-05-06 19:52:49 +0000875\ttindex{__str__}
876\bifuncindex{str}
877\stindex{print}
878
879\item[{\tt __cmp__(self, other)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000880Called by all comparison operations. Should return a negative integer if
881\code{self < other}, zero if \code{self == other}, a positive integer if
Fred Drakef6669171998-05-06 19:52:49 +0000882\code{self > other}. If no \method{__cmp__()} operation is defined, class
883instances are compared by object identity (``address'').
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000884(Note: the restriction that exceptions are not propagated by
885\code{__cmp__} has been removed in Python 1.5.)
Fred Drakef6669171998-05-06 19:52:49 +0000886\ttindex{__cmp__}
887\bifuncindex{cmp}
888\index{comparisons}
889
890\item[{\tt __hash__(self)}]
891Called for the key object for dictionary operations,
892and by the built-in function
893\function{hash()}\bifuncindex{hash}. Should return a 32-bit integer
894usable as a hash value
895for dictionary operations. The only required property is that objects
896which compare equal have the same hash value; it is advised to somehow
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000897mix together (e.g., using exclusive or) the hash values for the
Fred Drakef6669171998-05-06 19:52:49 +0000898components of the object that also play a part in comparison of
899objects. If a class does not define a \method{__cmp__()} method it should
900not define a \method{__hash__()} operation either; if it defines
901\method{__cmp__()} but not \method{__hash__()} its instances will not be
902usable as dictionary keys. If a class defines mutable objects and
903implements a \method{__cmp__()} method it should not implement
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000904\method{__hash__()}, since the dictionary implementation requires that
905a key's hash value is immutable (if the object's hash value changes, it
906will be in the wrong hash bucket).
Fred Drakef6669171998-05-06 19:52:49 +0000907\obindex{dictionary}
908\ttindex{__cmp__}
909\ttindex{__hash__}
910
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000911\item[__nonzero__(self)]
912Called to implement truth value testing; should return 0 or 1. When
913this method is not defined, \code{__len__} is called, if it is defined
914(see below). If a class defines neither \code{__len__} nor
915\code{__nonzero__}, all its instances are considered true.
Fred Drakef6669171998-05-06 19:52:49 +0000916
917\end{description}
918
919
Fred Drake61c77281998-07-28 19:34:22 +0000920\subsection{Customizing attribute access\label{attribute-access}}
Fred Drakef6669171998-05-06 19:52:49 +0000921
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000922The following methods can be defined to customize the meaning of
923attribute access (use of, assignment to, or deletion of \code{x.name})
924for class instances.
925For performance reasons, these methods are cached in the class object
926at class definition time; therefore, they cannot be changed after the
927class definition is executed.
Fred Drakef6669171998-05-06 19:52:49 +0000928
929\begin{description}
930
931\item[{\tt __getattr__(self, name)}]
932Called when an attribute lookup has not found the attribute in the
933usual places (i.e. it is not an instance attribute nor is it found in
934the class tree for \code{self}). \code{name} is the attribute name.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000935This method should return the (computed) attribute value or raise an
936\code{AttributeError} exception.
Fred Drakef6669171998-05-06 19:52:49 +0000937\ttindex{__getattr__}
938
939Note that if the attribute is found through the normal mechanism,
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000940\code{__getattr__} is not called. (This is an intentional asymmetry between
Fred Drakef6669171998-05-06 19:52:49 +0000941\code{__getattr__} and \code{__setattr__}.)
942This is done both for efficiency reasons and because otherwise
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000943\code{__setattr__} would have no way to access other attributes of the
Fred Drakef6669171998-05-06 19:52:49 +0000944instance.
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000945Note that at least for instance variables, you can fake
946total control by not inserting any values in the instance
947attribute dictionary (but instead inserting them in another object).
Fred Drakef6669171998-05-06 19:52:49 +0000948\ttindex{__setattr__}
949
950\item[{\tt __setattr__(self, name, value)}]
951Called when an attribute assignment is attempted. This is called
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000952instead of the normal mechanism (i.e. store the value in the instance
953dictionary). \code{name} is the attribute name, \code{value} is the
Fred Drakef6669171998-05-06 19:52:49 +0000954value to be assigned to it.
955\ttindex{__setattr__}
956
957If \code{__setattr__} wants to assign to an instance attribute, it
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000958should not simply execute ``\code{self.\var{name} = value}'' --- this would
959cause a recursive call to itself. Instead, it should insert the value in the
960dictionary of instance attributes, e.g.,
961``\code{self.__dict__[name] = value}.''
Fred Drakef6669171998-05-06 19:52:49 +0000962\ttindex{__dict__}
963
964\item[{\tt __delattr__(self, name)}]
965Like \code{__setattr__} but for attribute deletion instead of
966assignment.
967\ttindex{__delattr__}
968
969\end{description}
970
971
Fred Drake61c77281998-07-28 19:34:22 +0000972\subsection{Emulating callable objects\label{callable-types}}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000973
974\begin{description}
975
976\item[{\tt __call__(self, [args...])}]
977Called when the instance is ``called'' as a function; if this method
978is defined, \code{x(arg1, arg2, ...)} is a shorthand for
979\code{x.__call__(arg1, arg2, ...)}.
980\ttindex{__call__}
981\indexii{call}{instance}
982
983\end{description}
984
985
Fred Drake61c77281998-07-28 19:34:22 +0000986\subsection{Emulating sequence and mapping types\label{sequence-types}}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +0000987
988The following methods can be defined to emulate sequence or mapping
989objects. The first set of methods is used either to emulate a
990sequence or to emulate a mapping; the difference is that for a
991sequence, the allowable keys should be the integers \var{k} for which
992\code{0 <= \var{k} < \var{N}} where \var{N} is the length of the
993sequence, and the method \method{__getslice__()} (see below) should be
994defined. It is also recommended that mappings provide methods
995\method{keys()}, \method{values()}, \method{items()},
996\method{has_key()}, \method{get()}, \method{clear()}, \method{copy()},
997 and \method{update()} behaving similar to those for
998Python's standard dictionary objects; mutable sequences should provide
999methods \method{append()}, \method{count()}, \method{index()},
1000\method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()}
1001and \method{sort()}, like Python standard list objects. Finally,
1002sequence types should implement addition (meaning concatenation) and
1003multiplication (meaning repetition) by defining the methods
1004\method{__add__()}, \method{__radd__()}, \method{__mul__()} and
1005\method{__rmul__()} described below; they should not define
1006\method{__coerce__()} or other numerical operators.
1007\ttindex{keys}
1008\ttindex{values}
1009\ttindex{items}
1010\ttindex{has_key}
1011\ttindex{get}
1012\ttindex{clear}
1013\ttindex{copy}
1014\ttindex{update}
1015\ttindex{append}
1016\ttindex{count}
1017\ttindex{index}
1018\ttindex{insert}
1019\ttindex{pop}
1020\ttindex{remove}
1021\ttindex{reverse}
1022\ttindex{sort}
1023\ttindex{__add__}
1024\ttindex{__radd__}
1025\ttindex{__mul__}
1026\ttindex{__rmul__}
1027\ttindex{__coerce__}
Fred Drakef6669171998-05-06 19:52:49 +00001028
1029\begin{description}
1030
1031\item[{\tt __len__(self)}]
1032Called to implement the built-in function \function{len()}. Should return
1033the length of the object, an integer \code{>=} 0. Also, an object
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001034that doesn't define a \method{__nonzero__()} method and
1035whose \method{__len__()} method returns zero is considered to be false in a
Fred Drakef6669171998-05-06 19:52:49 +00001036Boolean context.
1037\ttindex{__len__}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001038\ttindex{__nonzero__}
Fred Drakef6669171998-05-06 19:52:49 +00001039
1040\item[{\tt __getitem__(self, key)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001041Called to implement evaluation of \code{self[key]}.
1042For a sequence types, the accepted keys should be integers. Note that the
1043special interpretation of negative indices (if the class wishes to
Fred Drakef6669171998-05-06 19:52:49 +00001044emulate a sequence type) is up to the \method{__getitem__()} method.
1045\ttindex{__getitem__}
1046
1047\item[{\tt __setitem__(self, key, value)}]
1048Called to implement assignment to \code{self[key]}. Same note as for
1049\method{__getitem__()}.
1050\ttindex{__setitem__}
1051
1052\item[{\tt __delitem__(self, key)}]
1053Called to implement deletion of \code{self[key]}. Same note as for
1054\method{__getitem__()}.
1055\ttindex{__delitem__}
1056
1057\end{description}
1058
1059
Fred Drake61c77281998-07-28 19:34:22 +00001060\subsection{Additional methods for emulation of sequence types%
1061 \label{sequence-methods}}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001062
1063The following methods can be defined to further emulate sequence
1064objects. Immutable sequences methods should only define
1065\method{__getslice__()}; mutable sequences, should define all three
1066three methods.
Fred Drakef6669171998-05-06 19:52:49 +00001067
1068\begin{description}
1069
1070\item[{\tt __getslice__(self, i, j)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001071Called to implement evaluation of \code{self[i:j]}. The returned
1072object should be of the same type as \code{self}. Note that missing
1073\var{i} or \var{j} in the slice expression are replaced by zero or
1074\code{sys.maxint}, respectively, and no further transformations on the
1075indices is performed. The interpretation of negative indices and
1076indices larger than the length of the sequence is up to the method.
Fred Drakef6669171998-05-06 19:52:49 +00001077\ttindex{__getslice__}
1078
1079\item[{\tt __setslice__(self, i, j, sequence)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001080Called to implement assignment to \code{self[i:j]}. Same notes for
1081\var{i} and \var{j} as for \method{__getslice__()}.
Fred Drakef6669171998-05-06 19:52:49 +00001082\ttindex{__setslice__}
1083
1084\item[{\tt __delslice__(self, i, j)}]
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001085Called to implement deletion of \code{self[i:j]}. Same notes for
1086\var{i} and \var{j} as for \method{__getslice__()}.
Fred Drakef6669171998-05-06 19:52:49 +00001087\ttindex{__delslice__}
1088
1089\end{description}
1090
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001091Notice that these methods are only invoked when a single slice with a
1092single colon is used. For slice operations involving extended slice
1093notation, \method{__getitem__()}, \method{__setitem__()}
1094or\method{__delitem__()} is called.
Fred Drakef6669171998-05-06 19:52:49 +00001095
Fred Drake61c77281998-07-28 19:34:22 +00001096\subsection{Emulating numeric types\label{numeric-types}}
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001097
1098The following methods can be defined to emulate numeric objects.
1099Methods corresponding to operations that are not supported by the
1100particular kind of number implemented (e.g., bitwise operations for
1101non-integral numbers) should be left undefined.
Fred Drakef6669171998-05-06 19:52:49 +00001102
1103\begin{description}
1104
1105\item[{\tt __add__(self, other)}]\itemjoin
1106\item[{\tt __sub__(self, other)}]\itemjoin
1107\item[{\tt __mul__(self, other)}]\itemjoin
1108\item[{\tt __div__(self, other)}]\itemjoin
1109\item[{\tt __mod__(self, other)}]\itemjoin
1110\item[{\tt __divmod__(self, other)}]\itemjoin
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001111\item[{\tt __pow__(self, other \optional{, modulo})}]\itemjoin
Fred Drakef6669171998-05-06 19:52:49 +00001112\item[{\tt __lshift__(self, other)}]\itemjoin
1113\item[{\tt __rshift__(self, other)}]\itemjoin
1114\item[{\tt __and__(self, other)}]\itemjoin
1115\item[{\tt __xor__(self, other)}]\itemjoin
1116\item[{\tt __or__(self, other)}]\itembreak
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001117These functions are
1118called to implement the binary arithmetic operations (\code{+},
Fred Drakef6669171998-05-06 19:52:49 +00001119\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()},
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001120\code{**},
Fred Drakef6669171998-05-06 19:52:49 +00001121\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}).
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001122For instance, to evaluate the expression \var{x}\code{+}\var{y}, where
1123\var{x} is an instance of a class that has an \method{__add__()}
1124method, \code{\var{x}.__add__(\var{y})} is called.
1125Note that \function{__pow__()} should be defined to accept an optional
1126third argument if the ternary version of the built-in \function{pow()}
1127function is to be supported.
1128\ttindex{__or__}
1129\ttindex{__xor__}
1130\ttindex{__and__}
1131\ttindex{__rshift__}
1132\ttindex{__lshift__}
1133\ttindex{__pow__}
1134\ttindex{__divmod__}
1135\ttindex{__mod__}
1136\ttindex{__div__}
1137\ttindex{__mul__}
1138\ttindex{__sub__}
1139\ttindex{__add__}
1140
1141\item[{\tt __radd__(self, other)}]\itemjoin
1142\item[{\tt __rsub__(self, other)}]\itemjoin
1143\item[{\tt __rmul__(self, other)}]\itemjoin
1144\item[{\tt __rdiv__(self, other)}]\itemjoin
1145\item[{\tt __rmod__(self, other)}]\itemjoin
1146\item[{\tt __rdivmod__(self, other)}]\itemjoin
1147\item[{\tt __rpow__(self, other)}]\itemjoin
1148\item[{\tt __rlshift__(self, other)}]\itemjoin
1149\item[{\tt __rrshift__(self, other)}]\itemjoin
1150\item[{\tt __rand__(self, other)}]\itemjoin
1151\item[{\tt __rxor__(self, other)}]\itemjoin
1152\item[{\tt __ror__(self, other)}]\itembreak
1153These functions are
1154called to implement the binary arithmetic operations (\code{+},
1155\code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()},
1156\code{**},
1157\code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}) with reversed operands.
1158These functions are only called if the left operand does not support
1159the corresponding operation.
1160For instance, to evaluate the expression \var{x}\code{-}\var{y}, where
1161\var{y} is an instance of a class that has an \method{__rsub__()}
1162method, \code{\var{y}.__rsub__(\var{x})} is called.
1163Note that ternary \function{pow()} will not try calling
1164\method{__rpow__()} (the coercion rules would become too
1165complicated).
Fred Drakef6669171998-05-06 19:52:49 +00001166\ttindex{__or__}
1167\ttindex{__xor__}
1168\ttindex{__and__}
1169\ttindex{__rshift__}
1170\ttindex{__lshift__}
1171\ttindex{__pow__}
1172\ttindex{__divmod__}
1173\ttindex{__mod__}
1174\ttindex{__div__}
1175\ttindex{__mul__}
1176\ttindex{__sub__}
1177\ttindex{__add__}
1178
1179\item[{\tt __neg__(self)}]\itemjoin
1180\item[{\tt __pos__(self)}]\itemjoin
1181\item[{\tt __abs__(self)}]\itemjoin
1182\item[{\tt __invert__(self)}]\itembreak
1183Called to implement the unary arithmetic operations (\code{-}, \code{+},
1184\function{abs()} and \code{~}).
1185\ttindex{__invert__}
1186\ttindex{__abs__}
1187\ttindex{__pos__}
1188\ttindex{__neg__}
1189
Fred Drakef6669171998-05-06 19:52:49 +00001190\item[{\tt __int__(self)}]\itemjoin
1191\item[{\tt __long__(self)}]\itemjoin
1192\item[{\tt __float__(self)}]\itembreak
1193Called to implement the built-in functions \function{int()}, \function{long()}
1194and \function{float()}. Should return a value of the appropriate type.
1195\ttindex{__float__}
1196\ttindex{__long__}
1197\ttindex{__int__}
1198
1199\item[{\tt __oct__(self)}]\itemjoin
1200\item[{\tt __hex__(self)}]\itembreak
1201Called to implement the built-in functions \function{oct()} and
1202\function{hex()}. Should return a string value.
1203\ttindex{__hex__}
1204\ttindex{__oct__}
1205
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001206\item[{\tt __coerce__(self, other)}]
1207Called to implement ``mixed-mode'' numeric arithmetic. Should either
1208return a 2-tuple containing \code{self} and \code{other} converted to
1209a common numeric type, or \code{None} if conversion is possible. When
1210the common type would be the type of \code{other}, it is sufficient to
1211return \code{None}, since the interpreter will also ask the other
1212object to attempt a coercion (but sometimes, if the implementation of
1213the other type cannot be changed, it is useful to do the conversion to
1214the other type here).
1215\ttindex{__coerce__}
1216
1217\strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the
1218following steps are taken (where \method{__op__()} and
1219\method{__rop__()} are the method names corresponding to \var{op},
Guido van Rossum7c0240f1998-07-24 15:36:43 +00001220e.g., if var{op} is `\code{+}', \method{__add__()} and
Guido van Rossum83b2f8a1998-07-23 17:12:46 +00001221\method{__radd__()} are used). If an exception occurs at any point,
1222the evaluation is abandoned and exception handling takes over.
1223
1224\begin{itemize}
1225
1226\item[0.] If \var{x} is a string object and op is the modulo operator (\%),
1227the string formatting operation is invoked and the remaining steps are
1228skipped.
1229
1230\item[1.] If \var{x} is a class instance:
1231
1232 \begin{itemize}
1233
1234 \item[1a.] If \var{x} has a \method{__coerce__()} method:
1235 replace \var{x} and \var{y} with the 2-tuple returned by
1236 \code{\var{x}.__coerce__(\var{y})}; skip to step 2 if the
1237 coercion returns \code{None}.
1238
1239 \item[1b.] If neither \var{x} nor \var{y} is a class instance
1240 after coercion, go to step 3.
1241
1242 \item[1c.] If \var{x} has a method \method{__op__()}, return
1243 \code{\var{x}.__op__(\var{y})}; otherwise, restore \var{x} and
1244 \var{y} to their value before step 1a.
1245
1246 \end{itemize}
1247
1248\item[2.] If \var{y} is a class instance:
1249
1250 \begin{itemize}
1251
1252 \item[2a.] If \var{y} has a \method{__coerce__()} method:
1253 replace \var{y} and \var{x} with the 2-tuple returned by
1254 \code{\var{y}.__coerce__(\var{x})}; skip to step 3 if the
1255 coercion returns \code{None}.
1256
1257 \item[2b.] If neither \var{x} nor \var{y} is a class instance
1258 after coercion, go to step 3.
1259
1260 \item[2b.] If \var{y} has a method \method{__rop__()}, return
1261 \code{\var{y}.__rop__(\var{x})}; otherwise, restore \var{x}
1262 and \var{y} to their value before step 2a.
1263
1264 \end{itemize}
1265
1266\item[3.] We only get here if neither \var{x} nor \var{y} is a class
1267instance.
1268
1269 \begin{itemize}
1270
1271 \item[3a.] If op is `\code{+}' and \var{x} is a sequence,
1272 sequence concatenation is invoked.
1273
1274 \item[3b.] If op is `\code{*}' and one operand is a sequence
1275 and the other an integer, sequence repetition is invoked.
1276
1277 \item[3c.] Otherwise, both operands must be numbers; they are
1278 coerced to a common type if possible, and the numeric
1279 operation is invoked for that type.
1280
1281 \end{itemize}
1282
1283\end{itemize}
1284
Fred Drakef6669171998-05-06 19:52:49 +00001285\end{description}