Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1 | \chapter{Data model\label{datamodel}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 3 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 4 | \section{Objects, values and types\label{objects}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 5 | |
| 6 | \dfn{Objects} are Python's abstraction for data. All data in a Python |
| 7 | program is represented by objects or by relations between objects. |
| 8 | (In a sense, and in conformance to Von Neumann's model of a |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 9 | ``stored program computer,'' code is also represented by objects.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 10 | \index{object} |
| 11 | \index{data} |
| 12 | |
| 13 | Every object has an identity, a type and a value. An object's |
| 14 | \emph{identity} never changes once it has been created; you may think |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 15 | of it as the object's address in memory. The `\keyword{is}' operator |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 16 | compares the identity of two objects; the |
| 17 | \function{id()}\bifuncindex{id} function returns an integer |
| 18 | representing its identity (currently implemented as its address). |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 19 | An object's \dfn{type} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 20 | also unchangeable. It determines the operations that an object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 21 | supports (e.g., ``does it have a length?'') and also defines the |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 22 | possible values for objects of that type. The |
| 23 | \function{type()}\bifuncindex{type} function returns an object's type |
| 24 | (which is an object itself). The \emph{value} of some |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 25 | objects can change. Objects whose value can change are said to be |
| 26 | \emph{mutable}; objects whose value is unchangeable once they are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 27 | created are called \emph{immutable}. |
Guido van Rossum | 264bd59 | 1999-02-23 16:40:55 +0000 | [diff] [blame] | 28 | (The value of an immutable container object that contains a reference |
| 29 | to a mutable object can change when the latter's value is changed; |
| 30 | however the container is still considered immutable, because the |
| 31 | collection of objects it contains cannot be changed. So, immutability |
| 32 | is not strictly the same as having an unchangeable value, it is more |
| 33 | subtle.) |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 34 | An object's mutability is determined by its type; for instance, |
| 35 | numbers, strings and tuples are immutable, while dictionaries and |
| 36 | lists are mutable. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 37 | \index{identity of an object} |
| 38 | \index{value of an object} |
| 39 | \index{type of an object} |
| 40 | \index{mutable object} |
| 41 | \index{immutable object} |
| 42 | |
| 43 | Objects are never explicitly destroyed; however, when they become |
| 44 | unreachable they may be garbage-collected. An implementation is |
Barry Warsaw | 92a6ed9 | 1998-08-07 16:33:51 +0000 | [diff] [blame] | 45 | allowed to postpone garbage collection or omit it altogether --- it is |
| 46 | a matter of implementation quality how garbage collection is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 47 | implemented, as long as no objects are collected that are still |
| 48 | reachable. (Implementation note: the current implementation uses a |
Fred Drake | c8e8281 | 2001-01-22 17:46:18 +0000 | [diff] [blame] | 49 | reference-counting scheme with (optional) delayed detection of |
| 50 | cyclicly linked garbage, which collects most objects as soon as they |
| 51 | become unreachable, but is not guaranteed to collect garbage |
| 52 | containing circular references. See the |
| 53 | \citetitle[../lib/module-gc.html]{Python Library Reference} for |
| 54 | information on controlling the collection of cyclic garbage.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 55 | \index{garbage collection} |
| 56 | \index{reference counting} |
| 57 | \index{unreachable object} |
| 58 | |
| 59 | Note that the use of the implementation's tracing or debugging |
| 60 | facilities may keep objects alive that would normally be collectable. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 61 | Also note that catching an exception with a |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 62 | `\keyword{try}...\keyword{except}' statement may keep objects alive. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 63 | |
| 64 | Some objects contain references to ``external'' resources such as open |
| 65 | files or windows. It is understood that these resources are freed |
| 66 | when the object is garbage-collected, but since garbage collection is |
| 67 | not guaranteed to happen, such objects also provide an explicit way to |
| 68 | release the external resource, usually a \method{close()} method. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 69 | Programs are strongly recommended to explicitly close such |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 70 | objects. The `\keyword{try}...\keyword{finally}' statement provides |
| 71 | a convenient way to do this. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 72 | |
| 73 | Some objects contain references to other objects; these are called |
| 74 | \emph{containers}. Examples of containers are tuples, lists and |
| 75 | dictionaries. The references are part of a container's value. In |
| 76 | most cases, when we talk about the value of a container, we imply the |
| 77 | values, not the identities of the contained objects; however, when we |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 78 | talk about the mutability of a container, only the identities of |
| 79 | the immediately contained objects are implied. So, if an immutable |
| 80 | container (like a tuple) |
| 81 | contains a reference to a mutable object, its value changes |
| 82 | if that mutable object is changed. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 83 | \index{container} |
| 84 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 85 | Types affect almost all aspects of object behavior. Even the importance |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 86 | of object identity is affected in some sense: for immutable types, |
| 87 | operations that compute new values may actually return a reference to |
| 88 | any existing object with the same type and value, while for mutable |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 89 | objects this is not allowed. E.g., after |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 90 | \samp{a = 1; b = 1}, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 91 | \code{a} and \code{b} may or may not refer to the same object with the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 92 | value one, depending on the implementation, but after |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 93 | \samp{c = []; d = []}, \code{c} and \code{d} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 94 | are guaranteed to refer to two different, unique, newly created empty |
| 95 | lists. |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 96 | (Note that \samp{c = d = []} assigns the same object to both |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 97 | \code{c} and \code{d}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 98 | |
Fred Drake | 2829f1c | 2001-06-23 05:27:20 +0000 | [diff] [blame] | 99 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 100 | \section{The standard type hierarchy\label{types}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 101 | |
| 102 | Below is a list of the types that are built into Python. Extension |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 103 | modules written in \C{} can define additional types. Future versions of |
| 104 | Python may add types to the type hierarchy (e.g., rational |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 105 | numbers, efficiently stored arrays of integers, etc.). |
| 106 | \index{type} |
| 107 | \indexii{data}{type} |
| 108 | \indexii{type}{hierarchy} |
| 109 | \indexii{extension}{module} |
| 110 | \indexii{C}{language} |
| 111 | |
| 112 | Some of the type descriptions below contain a paragraph listing |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 113 | `special attributes.' These are attributes that provide access to the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 114 | implementation and are not intended for general use. Their definition |
Fred Drake | 3570551 | 2001-12-03 17:32:27 +0000 | [diff] [blame] | 115 | may change in the future. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 116 | \index{attribute} |
| 117 | \indexii{special}{attribute} |
| 118 | \indexiii{generic}{special}{attribute} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 119 | |
| 120 | \begin{description} |
| 121 | |
| 122 | \item[None] |
| 123 | This type has a single value. There is a single object with this value. |
| 124 | This object is accessed through the built-in name \code{None}. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 125 | It is used to signify the absence of a value in many situations, e.g., |
| 126 | it is returned from functions that don't explicitly return anything. |
| 127 | Its truth value is false. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 128 | \ttindex{None} |
Fred Drake | 78eebfd | 1998-11-25 19:09:24 +0000 | [diff] [blame] | 129 | \obindex{None@{\texttt{None}}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 130 | |
Neil Schemenauer | 48c2eb9 | 2001-01-04 01:25:50 +0000 | [diff] [blame] | 131 | \item[NotImplemented] |
| 132 | This type has a single value. There is a single object with this value. |
| 133 | This object is accessed through the built-in name \code{NotImplemented}. |
Guido van Rossum | ab782dd | 2001-01-18 15:17:06 +0000 | [diff] [blame] | 134 | Numeric methods and rich comparison methods may return this value if |
| 135 | they do not implement the operation for the operands provided. (The |
| 136 | interpreter will then try the reflected operation, or some other |
| 137 | fallback, depending on the operator.) Its truth value is true. |
Neil Schemenauer | 48c2eb9 | 2001-01-04 01:25:50 +0000 | [diff] [blame] | 138 | \ttindex{NotImplemented} |
| 139 | \obindex{NotImplemented@{\texttt{NotImplemented}}} |
| 140 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 141 | \item[Ellipsis] |
| 142 | This type has a single value. There is a single object with this value. |
| 143 | This object is accessed through the built-in name \code{Ellipsis}. |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 144 | It is used to indicate the presence of the \samp{...} syntax in a |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 145 | slice. Its truth value is true. |
Fred Drake | c0a02c0 | 2002-04-16 02:03:05 +0000 | [diff] [blame] | 146 | \obindex{Ellipsis} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 147 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 148 | \item[Numbers] |
| 149 | These are created by numeric literals and returned as results by |
| 150 | arithmetic operators and arithmetic built-in functions. Numeric |
| 151 | objects are immutable; once created their value never changes. Python |
| 152 | numbers are of course strongly related to mathematical numbers, but |
| 153 | subject to the limitations of numerical representation in computers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 154 | \obindex{numeric} |
| 155 | |
Fred Drake | b3384d3 | 2001-05-14 16:04:22 +0000 | [diff] [blame] | 156 | Python distinguishes between integers, floating point numbers, and |
| 157 | complex numbers: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 158 | |
| 159 | \begin{description} |
| 160 | \item[Integers] |
| 161 | These represent elements from the mathematical set of whole numbers. |
| 162 | \obindex{integer} |
| 163 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 164 | There are three types of integers: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 165 | |
| 166 | \begin{description} |
| 167 | |
| 168 | \item[Plain integers] |
| 169 | These represent numbers in the range -2147483648 through 2147483647. |
| 170 | (The range may be larger on machines with a larger natural word |
| 171 | size, but not smaller.) |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 172 | When the result of an operation would fall outside this range, the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 173 | exception \exception{OverflowError} is raised. |
| 174 | For the purpose of shift and mask operations, integers are assumed to |
| 175 | have a binary, 2's complement notation using 32 or more bits, and |
| 176 | hiding no bits from the user (i.e., all 4294967296 different bit |
| 177 | patterns correspond to different values). |
| 178 | \obindex{plain integer} |
| 179 | \withsubitem{(built-in exception)}{\ttindex{OverflowError}} |
| 180 | |
| 181 | \item[Long integers] |
| 182 | These represent numbers in an unlimited range, subject to available |
| 183 | (virtual) memory only. For the purpose of shift and mask operations, |
| 184 | a binary representation is assumed, and negative numbers are |
| 185 | represented in a variant of 2's complement which gives the illusion of |
| 186 | an infinite string of sign bits extending to the left. |
| 187 | \obindex{long integer} |
| 188 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 189 | \item[Booleans] |
| 190 | These represent the truth values False and True. The two objects |
| 191 | representing the values False and True are the only Boolean objects. |
| 192 | The Boolean type is a subtype of plain integers, and Boolean values |
| 193 | behave like the values 0 and 1, respectively, in almost all contexts, |
| 194 | the exception being that when converted to a string, the strings |
| 195 | \code{"False"} or \code{"True"} are returned, respectively. |
| 196 | \obindex{Boolean} |
| 197 | \ttindex{False} |
| 198 | \ttindex{True} |
| 199 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 200 | \end{description} % Integers |
| 201 | |
| 202 | The rules for integer representation are intended to give the most |
| 203 | meaningful interpretation of shift and mask operations involving |
| 204 | negative integers and the least surprises when switching between the |
| 205 | plain and long integer domains. For any operation except left shift, |
| 206 | if it yields a result in the plain integer domain without causing |
| 207 | overflow, it will yield the same result in the long integer domain or |
| 208 | when using mixed operands. |
| 209 | \indexii{integer}{representation} |
| 210 | |
| 211 | \item[Floating point numbers] |
| 212 | These represent machine-level double precision floating point numbers. |
| 213 | You are at the mercy of the underlying machine architecture and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 214 | \C{} implementation for the accepted range and handling of overflow. |
| 215 | Python does not support single-precision floating point numbers; the |
Fred Drake | 6e5e1d9 | 2001-07-14 02:12:27 +0000 | [diff] [blame] | 216 | savings in processor and memory usage that are usually the reason for using |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 217 | these is dwarfed by the overhead of using objects in Python, so there |
| 218 | is no reason to complicate the language with two kinds of floating |
| 219 | point numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 220 | \obindex{floating point} |
| 221 | \indexii{floating point}{number} |
| 222 | \indexii{C}{language} |
| 223 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 224 | \item[Complex numbers] |
| 225 | These represent complex numbers as a pair of machine-level double |
| 226 | precision floating point numbers. The same caveats apply as for |
| 227 | floating point numbers. The real and imaginary value of a complex |
| 228 | number \code{z} can be retrieved through the attributes \code{z.real} |
| 229 | and \code{z.imag}. |
| 230 | \obindex{complex} |
| 231 | \indexii{complex}{number} |
| 232 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 233 | \end{description} % Numbers |
| 234 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 235 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 236 | \item[Sequences] |
Fred Drake | 230d17d | 2001-02-22 21:28:04 +0000 | [diff] [blame] | 237 | These represent finite ordered sets indexed by non-negative numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 238 | The built-in function \function{len()}\bifuncindex{len} returns the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 239 | number of items of a sequence. |
Thomas Wouters | f9b526d | 2000-07-16 19:05:38 +0000 | [diff] [blame] | 240 | When the length of a sequence is \var{n}, the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 241 | index set contains the numbers 0, 1, \ldots, \var{n}-1. Item |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 242 | \var{i} of sequence \var{a} is selected by \code{\var{a}[\var{i}]}. |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 243 | \obindex{sequence} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 244 | \index{index operation} |
| 245 | \index{item selection} |
| 246 | \index{subscription} |
| 247 | |
| 248 | Sequences also support slicing: \code{\var{a}[\var{i}:\var{j}]} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 249 | selects all items with index \var{k} such that \var{i} \code{<=} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 250 | \var{k} \code{<} \var{j}. When used as an expression, a slice is a |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 251 | sequence of the same type. This implies that the index set is |
| 252 | renumbered so that it starts at 0. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 253 | \index{slicing} |
| 254 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 255 | Some sequences also support ``extended slicing'' with a third ``step'' |
| 256 | parameter: \code{\var{a}[\var{i}:\var{j}:\var{k}]} selects all items |
| 257 | of \var{a} with index \var{x} where \code{\var{x} = \var{i} + |
| 258 | \var{n}*\var{k}}, \var{n} \code{>=} \code{0} and \var{i} \code{<=} |
| 259 | \var{x} \code{<} \var{j}. |
| 260 | \index{extended slicing} |
| 261 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 262 | Sequences are distinguished according to their mutability: |
| 263 | |
| 264 | \begin{description} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 265 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 266 | \item[Immutable sequences] |
| 267 | An object of an immutable sequence type cannot change once it is |
| 268 | created. (If the object contains references to other objects, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 269 | these other objects may be mutable and may be changed; however, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 270 | the collection of objects directly referenced by an immutable object |
| 271 | cannot change.) |
| 272 | \obindex{immutable sequence} |
| 273 | \obindex{immutable} |
| 274 | |
| 275 | The following types are immutable sequences: |
| 276 | |
| 277 | \begin{description} |
| 278 | |
| 279 | \item[Strings] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 280 | The items of a string are characters. There is no separate |
| 281 | character type; a character is represented by a string of one item. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 282 | Characters represent (at least) 8-bit bytes. The built-in |
| 283 | functions \function{chr()}\bifuncindex{chr} and |
| 284 | \function{ord()}\bifuncindex{ord} convert between characters and |
| 285 | nonnegative integers representing the byte values. Bytes with the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 286 | values 0-127 usually represent the corresponding \ASCII{} values, but |
| 287 | the interpretation of values is up to the program. The string |
| 288 | data type is also used to represent arrays of bytes, e.g., to hold data |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 289 | read from a file. |
| 290 | \obindex{string} |
| 291 | \index{character} |
| 292 | \index{byte} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 293 | \index{ASCII@\ASCII} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 294 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 295 | (On systems whose native character set is not \ASCII, strings may use |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 296 | EBCDIC in their internal representation, provided the functions |
| 297 | \function{chr()} and \function{ord()} implement a mapping between \ASCII{} and |
| 298 | EBCDIC, and string comparison preserves the \ASCII{} order. |
| 299 | Or perhaps someone can propose a better rule?) |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 300 | \index{ASCII@\ASCII} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 301 | \index{EBCDIC} |
| 302 | \index{character set} |
| 303 | \indexii{string}{comparison} |
| 304 | \bifuncindex{chr} |
| 305 | \bifuncindex{ord} |
| 306 | |
Fred Drake | f0aff8e | 2000-04-06 13:57:21 +0000 | [diff] [blame] | 307 | \item[Unicode] |
Fred Drake | 5ec22f2 | 2002-09-24 21:09:13 +0000 | [diff] [blame] | 308 | The items of a Unicode object are Unicode code units. A Unicode code |
| 309 | unit is represented by a Unicode object of one item and can hold |
| 310 | either a 16-bit or 32-bit value representing a Unicode ordinal (the |
| 311 | maximum value for the ordinal is given in \code{sys.maxunicode}, and |
| 312 | depends on how Python is configured at compile time). Surrogate pairs |
| 313 | may be present in the Unicode object, and will be reported as two |
| 314 | separate items. The built-in functions |
Fred Drake | f0aff8e | 2000-04-06 13:57:21 +0000 | [diff] [blame] | 315 | \function{unichr()}\bifuncindex{unichr} and |
Fred Drake | 5ec22f2 | 2002-09-24 21:09:13 +0000 | [diff] [blame] | 316 | \function{ord()}\bifuncindex{ord} convert between code units and |
Fred Drake | f0aff8e | 2000-04-06 13:57:21 +0000 | [diff] [blame] | 317 | nonnegative integers representing the Unicode ordinals as defined in |
| 318 | the Unicode Standard 3.0. Conversion from and to other encodings are |
| 319 | possible through the Unicode method \method{encode} and the built-in |
Fred Drake | 5ec22f2 | 2002-09-24 21:09:13 +0000 | [diff] [blame] | 320 | function \function{unicode()}.\bifuncindex{unicode} |
Fred Drake | f0aff8e | 2000-04-06 13:57:21 +0000 | [diff] [blame] | 321 | \obindex{unicode} |
| 322 | \index{character} |
| 323 | \index{integer} |
Fred Drake | 8b3ce9e | 2000-04-06 14:00:14 +0000 | [diff] [blame] | 324 | \index{Unicode} |
Fred Drake | f0aff8e | 2000-04-06 13:57:21 +0000 | [diff] [blame] | 325 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 326 | \item[Tuples] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 327 | The items of a tuple are arbitrary Python objects. |
| 328 | Tuples of two or more items are formed by comma-separated lists |
| 329 | of expressions. A tuple of one item (a `singleton') can be formed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 330 | by affixing a comma to an expression (an expression by itself does |
| 331 | not create a tuple, since parentheses must be usable for grouping of |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 332 | expressions). An empty tuple can be formed by an empty pair of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 333 | parentheses. |
| 334 | \obindex{tuple} |
| 335 | \indexii{singleton}{tuple} |
| 336 | \indexii{empty}{tuple} |
| 337 | |
| 338 | \end{description} % Immutable sequences |
| 339 | |
| 340 | \item[Mutable sequences] |
| 341 | Mutable sequences can be changed after they are created. The |
| 342 | subscription and slicing notations can be used as the target of |
| 343 | assignment and \keyword{del} (delete) statements. |
Thomas Wouters | f9b526d | 2000-07-16 19:05:38 +0000 | [diff] [blame] | 344 | \obindex{mutable sequence} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 345 | \obindex{mutable} |
| 346 | \indexii{assignment}{statement} |
| 347 | \index{delete} |
| 348 | \stindex{del} |
| 349 | \index{subscription} |
| 350 | \index{slicing} |
| 351 | |
| 352 | There is currently a single mutable sequence type: |
| 353 | |
| 354 | \begin{description} |
| 355 | |
| 356 | \item[Lists] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 357 | The items of a list are arbitrary Python objects. Lists are formed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 358 | by placing a comma-separated list of expressions in square brackets. |
| 359 | (Note that there are no special cases needed to form lists of length 0 |
| 360 | or 1.) |
| 361 | \obindex{list} |
| 362 | |
| 363 | \end{description} % Mutable sequences |
| 364 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 365 | The extension module \module{array}\refstmodindex{array} provides an |
| 366 | additional example of a mutable sequence type. |
| 367 | |
| 368 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 369 | \end{description} % Sequences |
| 370 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 371 | \item[Mappings] |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 372 | These represent finite sets of objects indexed by arbitrary index sets. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 373 | The subscript notation \code{a[k]} selects the item indexed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 374 | by \code{k} from the mapping \code{a}; this can be used in |
| 375 | expressions and as the target of assignments or \keyword{del} statements. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 376 | The built-in function \function{len()} returns the number of items |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 377 | in a mapping. |
| 378 | \bifuncindex{len} |
| 379 | \index{subscription} |
| 380 | \obindex{mapping} |
| 381 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 382 | There is currently a single intrinsic mapping type: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 383 | |
| 384 | \begin{description} |
| 385 | |
| 386 | \item[Dictionaries] |
Fred Drake | 8cdee96 | 1999-02-23 18:50:38 +0000 | [diff] [blame] | 387 | These\obindex{dictionary} represent finite sets of objects indexed by |
| 388 | nearly arbitrary values. The only types of values not acceptable as |
| 389 | keys are values containing lists or dictionaries or other mutable |
| 390 | types that are compared by value rather than by object identity, the |
| 391 | reason being that the efficient implementation of dictionaries |
| 392 | requires a key's hash value to remain constant. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 393 | Numeric types used for keys obey the normal rules for numeric |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 394 | comparison: if two numbers compare equal (e.g., \code{1} and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 395 | \code{1.0}) then they can be used interchangeably to index the same |
| 396 | dictionary entry. |
| 397 | |
Fred Drake | ed5a7ca | 2001-09-10 15:16:08 +0000 | [diff] [blame] | 398 | Dictionaries are mutable; they are created by the |
Fred Drake | 8cdee96 | 1999-02-23 18:50:38 +0000 | [diff] [blame] | 399 | \code{\{...\}} notation (see section \ref{dict}, ``Dictionary |
| 400 | Displays''). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 402 | The extension modules \module{dbm}\refstmodindex{dbm}, |
| 403 | \module{gdbm}\refstmodindex{gdbm}, \module{bsddb}\refstmodindex{bsddb} |
| 404 | provide additional examples of mapping types. |
| 405 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 406 | \end{description} % Mapping types |
| 407 | |
| 408 | \item[Callable types] |
Fred Drake | 8cdee96 | 1999-02-23 18:50:38 +0000 | [diff] [blame] | 409 | These\obindex{callable} are the types to which the function call |
| 410 | operation (see section \ref{calls}, ``Calls'') can be applied: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 411 | \indexii{function}{call} |
| 412 | \index{invocation} |
| 413 | \indexii{function}{argument} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 414 | |
| 415 | \begin{description} |
| 416 | |
| 417 | \item[User-defined functions] |
| 418 | A user-defined function object is created by a function definition |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 419 | (see section \ref{function}, ``Function definitions''). It should be |
| 420 | called with an argument |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 421 | list containing the same number of items as the function's formal |
| 422 | parameter list. |
| 423 | \indexii{user-defined}{function} |
| 424 | \obindex{function} |
| 425 | \obindex{user-defined function} |
| 426 | |
Guido van Rossum | 264bd59 | 1999-02-23 16:40:55 +0000 | [diff] [blame] | 427 | Special attributes: \member{func_doc} or \member{__doc__} is the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 428 | function's documentation string, or None if unavailable; |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 429 | \member{func_name} or \member{__name__} is the function's name; |
| 430 | \member{func_defaults} is a tuple containing default argument values for |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 431 | those arguments that have defaults, or \code{None} if no arguments |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 432 | have a default value; \member{func_code} is the code object representing |
| 433 | the compiled function body; \member{func_globals} is (a reference to) |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 434 | the dictionary that holds the function's global variables --- it |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 435 | defines the global namespace of the module in which the function was |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 436 | defined; \member{func_dict} or \member{__dict__} contains the |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 437 | namespace supporting arbitrary function attributes; |
| 438 | \member{func_closure} is \code{None} or a tuple of cells that contain |
Jeremy Hylton | 26c49b6 | 2002-04-01 17:58:39 +0000 | [diff] [blame] | 439 | bindings for the function's free variables. |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 440 | |
Jeremy Hylton | 26c49b6 | 2002-04-01 17:58:39 +0000 | [diff] [blame] | 441 | Of these, \member{func_code}, \member{func_defaults}, |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 442 | \member{func_doc}/\member{__doc__}, and |
| 443 | \member{func_dict}/\member{__dict__} may be writable; the |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 444 | others can never be changed. Additional information about a |
| 445 | function's definition can be retrieved from its code object; see the |
| 446 | description of internal types below. |
| 447 | |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 448 | \withsubitem{(function attribute)}{ |
| 449 | \ttindex{func_doc} |
| 450 | \ttindex{__doc__} |
| 451 | \ttindex{__name__} |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 452 | \ttindex{__dict__} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 453 | \ttindex{func_defaults} |
Jeremy Hylton | 26c49b6 | 2002-04-01 17:58:39 +0000 | [diff] [blame] | 454 | \ttindex{func_closure} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 455 | \ttindex{func_code} |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 456 | \ttindex{func_globals} |
| 457 | \ttindex{func_dict}} |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 458 | \indexii{global}{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 459 | |
| 460 | \item[User-defined methods] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 461 | A user-defined method object combines a class, a class instance (or |
Fred Drake | 8dd6ffd | 2001-08-02 21:34:53 +0000 | [diff] [blame] | 462 | \code{None}) and any callable object (normally a user-defined |
| 463 | function). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 464 | \obindex{method} |
| 465 | \obindex{user-defined method} |
| 466 | \indexii{user-defined}{method} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 467 | |
| 468 | Special read-only attributes: \member{im_self} is the class instance |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 469 | object, \member{im_func} is the function object; |
Guido van Rossum | b62f0e1 | 2001-12-07 22:03:18 +0000 | [diff] [blame] | 470 | \member{im_class} is the class of \member{im_self} for bound methods, |
| 471 | or the class that asked for the method for unbound methods); |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 472 | \member{__doc__} is the method's documentation (same as |
| 473 | \code{im_func.__doc__}); \member{__name__} is the method name (same as |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 474 | \code{im_func.__name__}). |
Fred Drake | f9d5803 | 2001-12-07 23:13:53 +0000 | [diff] [blame] | 475 | \versionchanged[\member{im_self} used to refer to the class that |
| 476 | defined the method]{2.2} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 477 | \withsubitem{(method attribute)}{ |
| 478 | \ttindex{im_func} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 479 | \ttindex{im_self}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 480 | |
Barry Warsaw | 7a5e80e | 2001-02-27 03:36:30 +0000 | [diff] [blame] | 481 | Methods also support accessing (but not setting) the arbitrary |
| 482 | function attributes on the underlying function object. |
| 483 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 484 | User-defined method objects are created in two ways: when getting an |
| 485 | attribute of a class that is a user-defined function object, or when |
Fred Drake | 35c09f2 | 2000-06-28 20:15:47 +0000 | [diff] [blame] | 486 | getting an attribute of a class instance that is a user-defined |
| 487 | function object defined by the class of the instance. In the former |
| 488 | case (class attribute), the \member{im_self} attribute is \code{None}, |
| 489 | and the method object is said to be unbound; in the latter case |
| 490 | (instance attribute), \method{im_self} is the instance, and the method |
| 491 | object is said to be bound. For |
Guido van Rossum | b62f0e1 | 2001-12-07 22:03:18 +0000 | [diff] [blame] | 492 | instance, when \class{C} is a class which has a method |
| 493 | \method{f()}, \code{C.f} does not yield the function object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 494 | \code{f}; rather, it yields an unbound method object \code{m} where |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 495 | \code{m.im_class} is \class{C}, \code{m.im_func} is \method{f()}, and |
| 496 | \code{m.im_self} is \code{None}. When \code{x} is a \class{C} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 497 | instance, \code{x.f} yields a bound method object \code{m} where |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 498 | \code{m.im_class} is \code{C}, \code{m.im_func} is \method{f()}, and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 499 | \code{m.im_self} is \code{x}. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 500 | \withsubitem{(method attribute)}{ |
Fred Drake | 35c09f2 | 2000-06-28 20:15:47 +0000 | [diff] [blame] | 501 | \ttindex{im_class}\ttindex{im_func}\ttindex{im_self}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 502 | |
| 503 | When an unbound user-defined method object is called, the underlying |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 504 | function (\member{im_func}) is called, with the restriction that the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 505 | first argument must be an instance of the proper class |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 506 | (\member{im_class}) or of a derived class thereof. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 507 | |
| 508 | When a bound user-defined method object is called, the underlying |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 509 | function (\member{im_func}) is called, inserting the class instance |
| 510 | (\member{im_self}) in front of the argument list. For instance, when |
| 511 | \class{C} is a class which contains a definition for a function |
| 512 | \method{f()}, and \code{x} is an instance of \class{C}, calling |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 513 | \code{x.f(1)} is equivalent to calling \code{C.f(x, 1)}. |
| 514 | |
| 515 | Note that the transformation from function object to (unbound or |
| 516 | bound) method object happens each time the attribute is retrieved from |
| 517 | the class or instance. In some cases, a fruitful optimization is to |
| 518 | assign the attribute to a local variable and call that local variable. |
| 519 | Also notice that this transformation only happens for user-defined |
| 520 | functions; other callable objects (and all non-callable objects) are |
Fred Drake | 35c09f2 | 2000-06-28 20:15:47 +0000 | [diff] [blame] | 521 | retrieved without transformation. It is also important to note that |
| 522 | user-defined functions which are attributes of a class instance are |
| 523 | not converted to bound methods; this \emph{only} happens when the |
| 524 | function is an attribute of the class. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 525 | |
Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 526 | \item[Generator functions\index{generator!function}\index{generator!iterator}] |
| 527 | A function or method which uses the \keyword{yield} statement (see |
| 528 | section~\ref{yield}, ``The \keyword{yield} statement'') is called a |
| 529 | \dfn{generator function}. Such a function, when called, always |
| 530 | returns an iterator object which can be used to execute the body of |
| 531 | the function: calling the iterator's \method{next()} method will |
| 532 | cause the function to execute until it provides a value using the |
| 533 | \keyword{yield} statement. When the function executes a |
| 534 | \keyword{return} statement or falls off the end, a |
| 535 | \exception{StopIteration} exception is raised and the iterator will |
| 536 | have reached the end of the set of values to be returned. |
| 537 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 538 | \item[Built-in functions] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 539 | A built-in function object is a wrapper around a \C{} function. Examples |
| 540 | of built-in functions are \function{len()} and \function{math.sin()} |
| 541 | (\module{math} is a standard built-in module). |
| 542 | The number and type of the arguments are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 543 | determined by the C function. |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 544 | Special read-only attributes: \member{__doc__} is the function's |
| 545 | documentation string, or \code{None} if unavailable; \member{__name__} |
| 546 | is the function's name; \member{__self__} is set to \code{None} (but see |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 547 | the next item). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 548 | \obindex{built-in function} |
| 549 | \obindex{function} |
| 550 | \indexii{C}{language} |
| 551 | |
| 552 | \item[Built-in methods] |
| 553 | This is really a different disguise of a built-in function, this time |
| 554 | containing an object passed to the \C{} function as an implicit extra |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 555 | argument. An example of a built-in method is |
| 556 | \code{\var{list}.append()}, assuming |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 557 | \var{list} is a list object. |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 558 | In this case, the special read-only attribute \member{__self__} is set |
Fred Drake | e31e9ce | 2001-12-11 21:10:08 +0000 | [diff] [blame] | 559 | to the object denoted by \var{list}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 560 | \obindex{built-in method} |
| 561 | \obindex{method} |
| 562 | \indexii{built-in}{method} |
| 563 | |
| 564 | \item[Classes] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 565 | Class objects are described below. When a class object is called, |
| 566 | a new class instance (also described below) is created and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 567 | returned. This implies a call to the class's \method{__init__()} method |
| 568 | if it has one. Any arguments are passed on to the \method{__init__()} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 569 | method. If there is no \method{__init__()} method, the class must be called |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 570 | without arguments. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 571 | \withsubitem{(object method)}{\ttindex{__init__()}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 572 | \obindex{class} |
| 573 | \obindex{class instance} |
| 574 | \obindex{instance} |
| 575 | \indexii{class object}{call} |
| 576 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 577 | \item[Class instances] |
| 578 | Class instances are described below. Class instances are callable |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 579 | only when the class has a \method{__call__()} method; \code{x(arguments)} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 580 | is a shorthand for \code{x.__call__(arguments)}. |
| 581 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 582 | \end{description} |
| 583 | |
| 584 | \item[Modules] |
| 585 | Modules are imported by the \keyword{import} statement (see section |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 586 | \ref{import}, ``The \keyword{import} statement''). |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 587 | A module object has a namespace implemented by a dictionary object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 588 | (this is the dictionary referenced by the func_globals attribute of |
| 589 | functions defined in the module). Attribute references are translated |
| 590 | to lookups in this dictionary, e.g., \code{m.x} is equivalent to |
| 591 | \code{m.__dict__["x"]}. |
| 592 | A module object does not contain the code object used to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 593 | initialize the module (since it isn't needed once the initialization |
| 594 | is done). |
| 595 | \stindex{import} |
| 596 | \obindex{module} |
| 597 | |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 598 | Attribute assignment updates the module's namespace dictionary, |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 599 | e.g., \samp{m.x = 1} is equivalent to \samp{m.__dict__["x"] = 1}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 600 | |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 601 | Special read-only attribute: \member{__dict__} is the module's |
| 602 | namespace as a dictionary object. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 603 | \withsubitem{(module attribute)}{\ttindex{__dict__}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 604 | |
| 605 | Predefined (writable) attributes: \member{__name__} |
| 606 | is the module's name; \member{__doc__} is the |
| 607 | module's documentation string, or |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 608 | \code{None} if unavailable; \member{__file__} is the pathname of the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 609 | file from which the module was loaded, if it was loaded from a file. |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 610 | The \member{__file__} attribute is not present for C{} modules that are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 611 | statically linked into the interpreter; for extension modules loaded |
| 612 | dynamically from a shared library, it is the pathname of the shared |
| 613 | library file. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 614 | \withsubitem{(module attribute)}{ |
| 615 | \ttindex{__name__} |
| 616 | \ttindex{__doc__} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 617 | \ttindex{__file__}} |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 618 | \indexii{module}{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 619 | |
| 620 | \item[Classes] |
| 621 | Class objects are created by class definitions (see section |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 622 | \ref{class}, ``Class definitions''). |
| 623 | A class has a namespace implemented by a dictionary object. |
| 624 | Class attribute references are translated to |
| 625 | lookups in this dictionary, |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 626 | e.g., \samp{C.x} is translated to \samp{C.__dict__["x"]}. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 627 | When the attribute name is not found |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 628 | there, the attribute search continues in the base classes. The search |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 629 | is depth-first, left-to-right in the order of occurrence in the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 630 | base class list. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 631 | When a class attribute reference would yield a user-defined function |
| 632 | object, it is transformed into an unbound user-defined method object |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 633 | (see above). The \member{im_class} attribute of this method object is the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 634 | class for which the attribute reference was initiated. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 635 | \obindex{class} |
| 636 | \obindex{class instance} |
| 637 | \obindex{instance} |
| 638 | \indexii{class object}{call} |
| 639 | \index{container} |
| 640 | \obindex{dictionary} |
| 641 | \indexii{class}{attribute} |
| 642 | |
| 643 | Class attribute assignments update the class's dictionary, never the |
| 644 | dictionary of a base class. |
| 645 | \indexiii{class}{attribute}{assignment} |
| 646 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 647 | A class object can be called (see above) to yield a class instance (see |
| 648 | below). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 649 | \indexii{class object}{call} |
| 650 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 651 | Special attributes: \member{__name__} is the class name; |
| 652 | \member{__module__} is the module name in which the class was defined; |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 653 | \member{__dict__} is the dictionary containing the class's namespace; |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 654 | \member{__bases__} is a tuple (possibly empty or a singleton) |
| 655 | containing the base classes, in the order of their occurrence in the |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 656 | base class list; \member{__doc__} is the class's documentation string, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 657 | or None if undefined. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 658 | \withsubitem{(class attribute)}{ |
| 659 | \ttindex{__name__} |
| 660 | \ttindex{__module__} |
| 661 | \ttindex{__dict__} |
| 662 | \ttindex{__bases__} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 663 | \ttindex{__doc__}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 664 | |
| 665 | \item[Class instances] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 666 | A class instance is created by calling a class object (see above). |
| 667 | A class instance has a namespace implemented as a dictionary which |
| 668 | is the first place in which |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 669 | attribute references are searched. When an attribute is not found |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 670 | there, and the instance's class has an attribute by that name, |
| 671 | the search continues with the class attributes. If a class attribute |
| 672 | is found that is a user-defined function object (and in no other |
| 673 | case), it is transformed into an unbound user-defined method object |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 674 | (see above). The \member{im_class} attribute of this method object is |
Guido van Rossum | b62f0e1 | 2001-12-07 22:03:18 +0000 | [diff] [blame] | 675 | the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 676 | class of the instance for which the attribute reference was initiated. |
| 677 | If no class attribute is found, and the object's class has a |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 678 | \method{__getattr__()} method, that is called to satisfy the lookup. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 679 | \obindex{class instance} |
| 680 | \obindex{instance} |
| 681 | \indexii{class}{instance} |
| 682 | \indexii{class instance}{attribute} |
| 683 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 684 | Attribute assignments and deletions update the instance's dictionary, |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 685 | never a class's dictionary. If the class has a \method{__setattr__()} or |
| 686 | \method{__delattr__()} method, this is called instead of updating the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 687 | instance dictionary directly. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 688 | \indexiii{class instance}{attribute}{assignment} |
| 689 | |
| 690 | Class instances can pretend to be numbers, sequences, or mappings if |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 691 | they have methods with certain special names. See |
| 692 | section \ref{specialnames}, ``Special method names.'' |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 693 | \obindex{numeric} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 694 | \obindex{sequence} |
| 695 | \obindex{mapping} |
| 696 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 697 | Special attributes: \member{__dict__} is the attribute |
| 698 | dictionary; \member{__class__} is the instance's class. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 699 | \withsubitem{(instance attribute)}{ |
| 700 | \ttindex{__dict__} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 701 | \ttindex{__class__}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 702 | |
| 703 | \item[Files] |
Fred Drake | e15eb35 | 1999-11-10 16:13:25 +0000 | [diff] [blame] | 704 | A file\obindex{file} object represents an open file. File objects are |
| 705 | created by the \function{open()}\bifuncindex{open} built-in function, |
| 706 | and also by |
| 707 | \withsubitem{(in module os)}{\ttindex{popen()}}\function{os.popen()}, |
| 708 | \function{os.fdopen()}, and the |
| 709 | \method{makefile()}\withsubitem{(socket method)}{\ttindex{makefile()}} |
| 710 | method of socket objects (and perhaps by other functions or methods |
| 711 | provided by extension modules). The objects |
| 712 | \ttindex{sys.stdin}\code{sys.stdin}, |
| 713 | \ttindex{sys.stdout}\code{sys.stdout} and |
| 714 | \ttindex{sys.stderr}\code{sys.stderr} are initialized to file objects |
| 715 | corresponding to the interpreter's standard\index{stdio} input, output |
| 716 | and error streams. See the \citetitle[../lib/lib.html]{Python Library |
| 717 | Reference} for complete documentation of file objects. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 718 | \withsubitem{(in module sys)}{ |
| 719 | \ttindex{stdin} |
| 720 | \ttindex{stdout} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 721 | \ttindex{stderr}} |
Fred Drake | e15eb35 | 1999-11-10 16:13:25 +0000 | [diff] [blame] | 722 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 723 | |
| 724 | \item[Internal types] |
| 725 | A few types used internally by the interpreter are exposed to the user. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 726 | Their definitions may change with future versions of the interpreter, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 727 | but they are mentioned here for completeness. |
| 728 | \index{internal type} |
| 729 | \index{types, internal} |
| 730 | |
| 731 | \begin{description} |
| 732 | |
| 733 | \item[Code objects] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 734 | Code objects represent \emph{byte-compiled} executable Python code, or |
| 735 | \emph{bytecode}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 736 | The difference between a code |
| 737 | object and a function object is that the function object contains an |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 738 | explicit reference to the function's globals (the module in which it |
| 739 | was defined), while a code object contains no context; |
| 740 | also the default argument values are stored in the function object, |
| 741 | not in the code object (because they represent values calculated at |
| 742 | run-time). Unlike function objects, code objects are immutable and |
| 743 | contain no references (directly or indirectly) to mutable objects. |
| 744 | \index{bytecode} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 745 | \obindex{code} |
| 746 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 747 | Special read-only attributes: \member{co_name} gives the function |
| 748 | name; \member{co_argcount} is the number of positional arguments |
| 749 | (including arguments with default values); \member{co_nlocals} is the |
| 750 | number of local variables used by the function (including arguments); |
| 751 | \member{co_varnames} is a tuple containing the names of the local |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 752 | variables (starting with the argument names); \member{co_cellvars} is |
| 753 | a tuple containing the names of local variables that are referenced by |
| 754 | nested functions; \member{co_freevars} is a tuple containing the names |
Jeremy Hylton | 8392f36 | 2002-04-01 18:53:36 +0000 | [diff] [blame] | 755 | of free variables; \member{co_code} is a string representing the |
| 756 | sequence of bytecode instructions; |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 757 | \member{co_consts} is a tuple containing the literals used by the |
| 758 | bytecode; \member{co_names} is a tuple containing the names used by |
| 759 | the bytecode; \member{co_filename} is the filename from which the code |
| 760 | was compiled; \member{co_firstlineno} is the first line number of the |
| 761 | function; \member{co_lnotab} is a string encoding the mapping from |
Thomas Wouters | f9b526d | 2000-07-16 19:05:38 +0000 | [diff] [blame] | 762 | byte code offsets to line numbers (for details see the source code of |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 763 | the interpreter); \member{co_stacksize} is the required stack size |
| 764 | (including local variables); \member{co_flags} is an integer encoding |
| 765 | a number of flags for the interpreter. |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 766 | |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 767 | \withsubitem{(code object attribute)}{ |
| 768 | \ttindex{co_argcount} |
| 769 | \ttindex{co_code} |
| 770 | \ttindex{co_consts} |
| 771 | \ttindex{co_filename} |
| 772 | \ttindex{co_firstlineno} |
| 773 | \ttindex{co_flags} |
| 774 | \ttindex{co_lnotab} |
| 775 | \ttindex{co_name} |
| 776 | \ttindex{co_names} |
| 777 | \ttindex{co_nlocals} |
| 778 | \ttindex{co_stacksize} |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 779 | \ttindex{co_varnames} |
| 780 | \ttindex{co_cellvars} |
| 781 | \ttindex{co_freevars}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 782 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 783 | The following flag bits are defined for \member{co_flags}: bit |
| 784 | \code{0x04} is set if the function uses the \samp{*arguments} syntax |
| 785 | to accept an arbitrary number of positional arguments; bit |
| 786 | \code{0x08} is set if the function uses the \samp{**keywords} syntax |
Jeremy Hylton | 8392f36 | 2002-04-01 18:53:36 +0000 | [diff] [blame] | 787 | to accept arbitrary keyword arguments; bit \code{0x20} is set if the |
| 788 | function is a \obindex{generator}. |
| 789 | |
| 790 | Future feature declarations (\samp{from __future__ import division}) |
| 791 | also use bits in \member{co_flags} to indicate whether a code object |
| 792 | was compiled with a particular feature enabled: bit \code{0x2000} is |
| 793 | set if the function was compiled with future division enabled; bits |
| 794 | \code{0x10} and \code{0x1000} were used in earlier versions of Python. |
| 795 | |
| 796 | Other bits in \member{co_flags} are reserved for internal use. |
| 797 | |
| 798 | If\index{documentation string} a code object represents a function, |
| 799 | the first item in |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 800 | \member{co_consts} is the documentation string of the function, or |
| 801 | \code{None} if undefined. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 802 | |
| 803 | \item[Frame objects] |
| 804 | Frame objects represent execution frames. They may occur in traceback |
| 805 | objects (see below). |
| 806 | \obindex{frame} |
| 807 | |
| 808 | Special read-only attributes: \member{f_back} is to the previous |
| 809 | stack frame (towards the caller), or \code{None} if this is the bottom |
| 810 | stack frame; \member{f_code} is the code object being executed in this |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 811 | frame; \member{f_locals} is the dictionary used to look up local |
| 812 | variables; \member{f_globals} is used for global variables; |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 813 | \member{f_builtins} is used for built-in (intrinsic) names; |
| 814 | \member{f_restricted} is a flag indicating whether the function is |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame^] | 815 | executing in restricted execution mode; \member{f_lasti} gives the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 816 | precise instruction (this is an index into the bytecode string of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 817 | the code object). |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 818 | \withsubitem{(frame attribute)}{ |
| 819 | \ttindex{f_back} |
| 820 | \ttindex{f_code} |
| 821 | \ttindex{f_globals} |
| 822 | \ttindex{f_locals} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 823 | \ttindex{f_lasti} |
| 824 | \ttindex{f_builtins} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 825 | \ttindex{f_restricted}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 826 | |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 827 | Special writable attributes: \member{f_trace}, if not \code{None}, is a |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 828 | function called at the start of each source code line (this is used by |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 829 | the debugger); \member{f_exc_type}, \member{f_exc_value}, |
| 830 | \member{f_exc_traceback} represent the most recent exception caught in |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame^] | 831 | this frame; \member{f_lineno} is the current line number of the frame |
| 832 | --- writing to this from within a trace function jumps to the given line |
| 833 | (only for the bottom-most frame). A debugger can implement a Jump |
| 834 | command (aka Set Next Statement) by writing to f_lineno. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 835 | \withsubitem{(frame attribute)}{ |
| 836 | \ttindex{f_trace} |
| 837 | \ttindex{f_exc_type} |
| 838 | \ttindex{f_exc_value} |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame^] | 839 | \ttindex{f_exc_traceback} |
| 840 | \ttindex{f_lineno}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 841 | |
| 842 | \item[Traceback objects] \label{traceback} |
| 843 | Traceback objects represent a stack trace of an exception. A |
| 844 | traceback object is created when an exception occurs. When the search |
| 845 | for an exception handler unwinds the execution stack, at each unwound |
| 846 | level a traceback object is inserted in front of the current |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 847 | traceback. When an exception handler is entered, the stack trace is |
| 848 | made available to the program. |
| 849 | (See section \ref{try}, ``The \code{try} statement.'') |
| 850 | It is accessible as \code{sys.exc_traceback}, and also as the third |
| 851 | item of the tuple returned by \code{sys.exc_info()}. The latter is |
| 852 | the preferred interface, since it works correctly when the program is |
| 853 | using multiple threads. |
| 854 | When the program contains no suitable handler, the stack trace is written |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 855 | (nicely formatted) to the standard error stream; if the interpreter is |
| 856 | interactive, it is also made available to the user as |
| 857 | \code{sys.last_traceback}. |
| 858 | \obindex{traceback} |
| 859 | \indexii{stack}{trace} |
| 860 | \indexii{exception}{handler} |
| 861 | \indexii{execution}{stack} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 862 | \withsubitem{(in module sys)}{ |
| 863 | \ttindex{exc_info} |
| 864 | \ttindex{exc_traceback} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 865 | \ttindex{last_traceback}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 866 | \ttindex{sys.exc_info} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 867 | \ttindex{sys.exc_traceback} |
| 868 | \ttindex{sys.last_traceback} |
| 869 | |
| 870 | Special read-only attributes: \member{tb_next} is the next level in the |
| 871 | stack trace (towards the frame where the exception occurred), or |
| 872 | \code{None} if there is no next level; \member{tb_frame} points to the |
| 873 | execution frame of the current level; \member{tb_lineno} gives the line |
| 874 | number where the exception occurred; \member{tb_lasti} indicates the |
| 875 | precise instruction. The line number and last instruction in the |
| 876 | traceback may differ from the line number of its frame object if the |
| 877 | exception occurred in a \keyword{try} statement with no matching |
| 878 | except clause or with a finally clause. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 879 | \withsubitem{(traceback attribute)}{ |
| 880 | \ttindex{tb_next} |
| 881 | \ttindex{tb_frame} |
| 882 | \ttindex{tb_lineno} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 883 | \ttindex{tb_lasti}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 884 | \stindex{try} |
| 885 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 886 | \item[Slice objects] |
| 887 | Slice objects are used to represent slices when \emph{extended slice |
| 888 | syntax} is used. This is a slice using two colons, or multiple slices |
| 889 | or ellipses separated by commas, e.g., \code{a[i:j:step]}, \code{a[i:j, |
| 890 | k:l]}, or \code{a[..., i:j])}. They are also created by the built-in |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 891 | \function{slice()}\bifuncindex{slice} function. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 892 | |
Thomas Wouters | f9b526d | 2000-07-16 19:05:38 +0000 | [diff] [blame] | 893 | Special read-only attributes: \member{start} is the lower bound; |
| 894 | \member{stop} is the upper bound; \member{step} is the step value; each is |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 895 | \code{None} if omitted. These attributes can have any type. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 896 | \withsubitem{(slice object attribute)}{ |
| 897 | \ttindex{start} |
| 898 | \ttindex{stop} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 899 | \ttindex{step}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 900 | |
Fred Drake | 5ec22f2 | 2002-09-24 21:09:13 +0000 | [diff] [blame] | 901 | Slice objects support one method: |
| 902 | |
| 903 | \begin{methoddesc}[slice]{indices}{self, length} |
| 904 | This method takes a single integer argument \var{length} and computes |
| 905 | information about the extended slice that the slice object would |
| 906 | describe if applied to a sequence of \var{length} items. It returns a |
| 907 | tuple of three integers; respectively these are the \var{start} and |
| 908 | \var{stop} indices and the \var{step} or stride length of the slice. |
| 909 | Missing or out-of-bounds indices are handled in a manner consistent |
| 910 | with regular slices. |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 911 | \versionadded{2.3} |
Fred Drake | 5ec22f2 | 2002-09-24 21:09:13 +0000 | [diff] [blame] | 912 | \end{methoddesc} |
Michael W. Hudson | f0d777c | 2002-07-19 15:47:06 +0000 | [diff] [blame] | 913 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 914 | \end{description} % Internal types |
| 915 | |
| 916 | \end{description} % Types |
| 917 | |
| 918 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 919 | \section{Special method names\label{specialnames}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 920 | |
| 921 | A class can implement certain operations that are invoked by special |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 922 | syntax (such as arithmetic operations or subscripting and slicing) by |
| 923 | defining methods with special names. For instance, if a class defines |
| 924 | a method named \method{__getitem__()}, and \code{x} is an instance of |
| 925 | this class, then \code{x[i]} is equivalent to |
Raymond Hettinger | 9415309 | 2002-05-12 03:09:25 +0000 | [diff] [blame] | 926 | \code{x.__getitem__(i)}. Except where mentioned, attempts to execute |
| 927 | an operation raise an exception when no appropriate method is defined. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 928 | \withsubitem{(mapping object method)}{\ttindex{__getitem__()}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 929 | |
Fred Drake | 0c47559 | 2000-12-07 04:49:34 +0000 | [diff] [blame] | 930 | When implementing a class that emulates any built-in type, it is |
| 931 | important that the emulation only be implemented to the degree that it |
| 932 | makes sense for the object being modelled. For example, some |
| 933 | sequences may work well with retrieval of individual elements, but |
| 934 | extracting a slice may not make sense. (One example of this is the |
| 935 | \class{NodeList} interface in the W3C's Document Object Model.) |
| 936 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 937 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 938 | \subsection{Basic customization\label{customization}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 939 | |
Fred Drake | 044bb4d | 2001-08-02 15:53:05 +0000 | [diff] [blame] | 940 | \begin{methoddesc}[object]{__init__}{self\optional{, \moreargs}} |
| 941 | Called\indexii{class}{constructor} when the instance is created. The |
| 942 | arguments are those passed to the class constructor expression. If a |
| 943 | base class has an \method{__init__()} method the derived class's |
| 944 | \method{__init__()} method must explicitly call it to ensure proper |
| 945 | initialization of the base class part of the instance; for example: |
| 946 | \samp{BaseClass.__init__(\var{self}, [\var{args}...])}. As a special |
| 947 | contraint on constructors, no value may be returned; doing so will |
| 948 | cause a \exception{TypeError} to be raised at runtime. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 949 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 950 | |
| 951 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 952 | \begin{methoddesc}[object]{__del__}{self} |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 953 | Called when the instance is about to be destroyed. This is also |
| 954 | called a destructor\index{destructor}. If a base class |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 955 | has a \method{__del__()} method, the derived class's \method{__del__()} method |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 956 | must explicitly call it to ensure proper deletion of the base class |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 957 | part of the instance. Note that it is possible (though not recommended!) |
| 958 | for the \method{__del__()} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 959 | method to postpone destruction of the instance by creating a new |
| 960 | reference to it. It may then be called at a later time when this new |
| 961 | reference is deleted. It is not guaranteed that |
| 962 | \method{__del__()} methods are called for objects that still exist when |
| 963 | the interpreter exits. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 964 | \stindex{del} |
| 965 | |
Fred Drake | 591dd8f | 2001-12-14 22:52:41 +0000 | [diff] [blame] | 966 | \begin{notice} |
| 967 | \samp{del x} doesn't directly call |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 968 | \code{x.__del__()} --- the former decrements the reference count for |
| 969 | \code{x} by one, and the latter is only called when its reference |
| 970 | count reaches zero. Some common situations that may prevent the |
| 971 | reference count of an object to go to zero include: circular |
| 972 | references between objects (e.g., a doubly-linked list or a tree data |
| 973 | structure with parent and child pointers); a reference to the object |
| 974 | on the stack frame of a function that caught an exception (the |
| 975 | traceback stored in \code{sys.exc_traceback} keeps the stack frame |
| 976 | alive); or a reference to the object on the stack frame that raised an |
| 977 | unhandled exception in interactive mode (the traceback stored in |
| 978 | \code{sys.last_traceback} keeps the stack frame alive). The first |
| 979 | situation can only be remedied by explicitly breaking the cycles; the |
Fred Drake | 591dd8f | 2001-12-14 22:52:41 +0000 | [diff] [blame] | 980 | latter two situations can be resolved by storing \code{None} in |
| 981 | \code{sys.exc_traceback} or \code{sys.last_traceback}. Circular |
| 982 | references which are garbage are detected when the option cycle |
| 983 | detector is enabled (it's on by default), but can only be cleaned up |
| 984 | if there are no Python-level \method{__del__()} methods involved. |
| 985 | Refer to the documentation for the \ulink{\module{gc} |
| 986 | module}{../lib/module-gc.html} for more information about how |
| 987 | \method{__del__()} methods are handled by the cycle detector, |
| 988 | particularly the description of the \code{garbage} value. |
| 989 | \end{notice} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 990 | |
Fred Drake | 591dd8f | 2001-12-14 22:52:41 +0000 | [diff] [blame] | 991 | \begin{notice}[warning] |
| 992 | Due to the precarious circumstances under which |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 993 | \method{__del__()} methods are invoked, exceptions that occur during their |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 994 | execution are ignored, and a warning is printed to \code{sys.stderr} |
Fred Drake | 591dd8f | 2001-12-14 22:52:41 +0000 | [diff] [blame] | 995 | instead. Also, when \method{__del__()} is invoked in response to a module |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 996 | being deleted (e.g., when execution of the program is done), other |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 997 | globals referenced by the \method{__del__()} method may already have been |
| 998 | deleted. For this reason, \method{__del__()} methods should do the |
Raymond Hettinger | a0e4d6c | 2002-09-08 21:10:54 +0000 | [diff] [blame] | 999 | absolute minimum needed to maintain external invariants. Starting with |
| 1000 | version 1.5, Python guarantees that globals whose name begins with a single |
| 1001 | underscore are deleted from their module before other globals are deleted; |
| 1002 | if no other references to such globals exist, this may help in assuring that |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1003 | imported modules are still available at the time when the |
Fred Drake | 591dd8f | 2001-12-14 22:52:41 +0000 | [diff] [blame] | 1004 | \method{__del__()} method is called. |
| 1005 | \end{notice} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1006 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1007 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1008 | \begin{methoddesc}[object]{__repr__}{self} |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 1009 | Called by the \function{repr()}\bifuncindex{repr} built-in function |
| 1010 | and by string conversions (reverse quotes) to compute the ``official'' |
Andrew M. Kuchling | 68abe83 | 2000-12-19 14:09:21 +0000 | [diff] [blame] | 1011 | string representation of an object. If at all possible, this should |
Guido van Rossum | 035f7e8 | 2000-12-19 04:18:13 +0000 | [diff] [blame] | 1012 | look like a valid Python expression that could be used to recreate an |
| 1013 | object with the same value (given an appropriate environment). If |
| 1014 | this is not possible, a string of the form \samp{<\var{...some useful |
| 1015 | description...}>} should be returned. The return value must be a |
| 1016 | string object. |
| 1017 | |
| 1018 | This is typically used for debugging, so it is important that the |
| 1019 | representation is information-rich and unambiguous. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1020 | \indexii{string}{conversion} |
| 1021 | \indexii{reverse}{quotes} |
| 1022 | \indexii{backward}{quotes} |
| 1023 | \index{back-quotes} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1024 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1025 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1026 | \begin{methoddesc}[object]{__str__}{self} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1027 | Called by the \function{str()}\bifuncindex{str} built-in function and |
| 1028 | by the \keyword{print}\stindex{print} statement to compute the |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 1029 | ``informal'' string representation of an object. This differs from |
| 1030 | \method{__repr__()} in that it does not have to be a valid Python |
| 1031 | expression: a more convenient or concise representation may be used |
Guido van Rossum | 035f7e8 | 2000-12-19 04:18:13 +0000 | [diff] [blame] | 1032 | instead. The return value must be a string object. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1033 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1034 | |
Guido van Rossum | ab782dd | 2001-01-18 15:17:06 +0000 | [diff] [blame] | 1035 | \begin{methoddesc}[object]{__lt__}{self, other} |
| 1036 | \methodline[object]{__le__}{self, other} |
| 1037 | \methodline[object]{__eq__}{self, other} |
| 1038 | \methodline[object]{__ne__}{self, other} |
| 1039 | \methodline[object]{__gt__}{self, other} |
| 1040 | \methodline[object]{__ge__}{self, other} |
| 1041 | \versionadded{2.1} |
| 1042 | These are the so-called ``rich comparison'' methods, and are called |
| 1043 | for comparison operators in preference to \method{__cmp__()} below. |
| 1044 | The correspondence between operator symbols and method names is as |
| 1045 | follows: |
| 1046 | \code{\var{x}<\var{y}} calls \code{\var{x}.__lt__(\var{y})}, |
| 1047 | \code{\var{x}<=\var{y}} calls \code{\var{x}.__le__(\var{y})}, |
| 1048 | \code{\var{x}==\var{y}} calls \code{\var{x}.__eq__(\var{y})}, |
| 1049 | \code{\var{x}!=\var{y}} and \code{\var{x}<>\var{y}} call |
| 1050 | \code{\var{x}.__ne__(\var{y})}, |
| 1051 | \code{\var{x}>\var{y}} calls \code{\var{x}.__gt__(\var{y})}, and |
| 1052 | \code{\var{x}>=\var{y}} calls \code{\var{x}.__ge__(\var{y})}. |
| 1053 | These methods can return any value, but if the comparison operator is |
| 1054 | used in a Boolean context, the return value should be interpretable as |
| 1055 | a Boolean value, else a \exception{TypeError} will be raised. |
| 1056 | By convention, \code{0} is used for false and \code{1} for true. |
| 1057 | |
| 1058 | There are no reflected (swapped-argument) versions of these methods |
| 1059 | (to be used when the left argument does not support the operation but |
| 1060 | the right argument does); rather, \method{__lt__()} and |
| 1061 | \method{__gt__()} are each other's reflection, \method{__le__()} and |
| 1062 | \method{__ge__()} are each other's reflection, and \method{__eq__()} |
| 1063 | and \method{__ne__()} are their own reflection. |
| 1064 | |
| 1065 | Arguments to rich comparison methods are never coerced. A rich |
| 1066 | comparison method may return \code{NotImplemented} if it does not |
| 1067 | implement the operation for a given pair of arguments. |
| 1068 | \end{methoddesc} |
| 1069 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1070 | \begin{methoddesc}[object]{__cmp__}{self, other} |
Guido van Rossum | ab782dd | 2001-01-18 15:17:06 +0000 | [diff] [blame] | 1071 | Called by comparison operations if rich comparison (see above) is not |
Fred Drake | 597bc1d | 2001-05-29 16:02:35 +0000 | [diff] [blame] | 1072 | defined. Should return a negative integer if \code{self < other}, |
| 1073 | zero if \code{self == other}, a positive integer if \code{self > |
| 1074 | other}. If no \method{__cmp__()}, \method{__eq__()} or |
| 1075 | \method{__ne__()} operation is defined, class instances are compared |
| 1076 | by object identity (``address''). See also the description of |
| 1077 | \method{__hash__()} for some important notes on creating objects which |
| 1078 | support custom comparison operations and are usable as dictionary |
| 1079 | keys. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1080 | (Note: the restriction that exceptions are not propagated by |
Fred Drake | 8238587 | 1998-10-01 20:40:43 +0000 | [diff] [blame] | 1081 | \method{__cmp__()} has been removed in Python 1.5.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1082 | \bifuncindex{cmp} |
| 1083 | \index{comparisons} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1084 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1085 | |
Fred Drake | e57a114 | 2000-06-15 20:07:25 +0000 | [diff] [blame] | 1086 | \begin{methoddesc}[object]{__rcmp__}{self, other} |
Fred Drake | 445f832 | 2001-01-04 15:11:48 +0000 | [diff] [blame] | 1087 | \versionchanged[No longer supported]{2.1} |
Fred Drake | e57a114 | 2000-06-15 20:07:25 +0000 | [diff] [blame] | 1088 | \end{methoddesc} |
| 1089 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1090 | \begin{methoddesc}[object]{__hash__}{self} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1091 | Called for the key object for dictionary\obindex{dictionary} |
| 1092 | operations, and by the built-in function |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1093 | \function{hash()}\bifuncindex{hash}. Should return a 32-bit integer |
| 1094 | usable as a hash value |
| 1095 | for dictionary operations. The only required property is that objects |
| 1096 | which compare equal have the same hash value; it is advised to somehow |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1097 | mix together (e.g., using exclusive or) the hash values for the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1098 | components of the object that also play a part in comparison of |
| 1099 | objects. If a class does not define a \method{__cmp__()} method it should |
| 1100 | not define a \method{__hash__()} operation either; if it defines |
Fred Drake | 597bc1d | 2001-05-29 16:02:35 +0000 | [diff] [blame] | 1101 | \method{__cmp__()} or \method{__eq__()} but not \method{__hash__()}, |
| 1102 | its instances will not be usable as dictionary keys. If a class |
| 1103 | defines mutable objects and implements a \method{__cmp__()} or |
| 1104 | \method{__eq__()} method, it should not implement \method{__hash__()}, |
| 1105 | since the dictionary implementation requires that a key's hash value |
| 1106 | is immutable (if the object's hash value changes, it will be in the |
| 1107 | wrong hash bucket). |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1108 | \withsubitem{(object method)}{\ttindex{__cmp__()}} |
| 1109 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1110 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1111 | \begin{methoddesc}[object]{__nonzero__}{self} |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1112 | Called to implement truth value testing, and the built-in operation |
| 1113 | \code{bool()}; should return \code{False} or \code{True}, or their |
| 1114 | integer equivalents \code{0} or \code{1}. |
| 1115 | When this method is not defined, \method{__len__()} is |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1116 | called, if it is defined (see below). If a class defines neither |
| 1117 | \method{__len__()} nor \method{__nonzero__()}, all its instances are |
| 1118 | considered true. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1119 | \withsubitem{(mapping object method)}{\ttindex{__len__()}} |
| 1120 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1121 | |
Martin v. Löwis | 2a519f8 | 2002-04-11 12:39:35 +0000 | [diff] [blame] | 1122 | \begin{methoddesc}[object]{__unicode__}{self} |
| 1123 | Called to implement \function{unicode()}\bifuncindex{unicode} builtin; |
| 1124 | should return a Unicode object. When this method is not defined, string |
| 1125 | conversion is attempted, and the result of string conversion is converted |
| 1126 | to Unicode using the system default encoding. |
| 1127 | \end{methoddesc} |
| 1128 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1129 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1130 | \subsection{Customizing attribute access\label{attribute-access}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1131 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1132 | The following methods can be defined to customize the meaning of |
| 1133 | attribute access (use of, assignment to, or deletion of \code{x.name}) |
| 1134 | for class instances. |
| 1135 | For performance reasons, these methods are cached in the class object |
| 1136 | at class definition time; therefore, they cannot be changed after the |
| 1137 | class definition is executed. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1138 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1139 | \begin{methoddesc}[object]{__getattr__}{self, name} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1140 | Called when an attribute lookup has not found the attribute in the |
| 1141 | usual places (i.e. it is not an instance attribute nor is it found in |
| 1142 | the class tree for \code{self}). \code{name} is the attribute name. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1143 | This method should return the (computed) attribute value or raise an |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1144 | \exception{AttributeError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1145 | |
| 1146 | Note that if the attribute is found through the normal mechanism, |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1147 | \method{__getattr__()} is not called. (This is an intentional |
| 1148 | asymmetry between \method{__getattr__()} and \method{__setattr__()}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1149 | This is done both for efficiency reasons and because otherwise |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1150 | \method{__setattr__()} would have no way to access other attributes of |
| 1151 | the instance. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1152 | Note that at least for instance variables, you can fake |
| 1153 | total control by not inserting any values in the instance |
| 1154 | attribute dictionary (but instead inserting them in another object). |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1155 | \withsubitem{(object method)}{\ttindex{__setattr__()}} |
| 1156 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1157 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1158 | \begin{methoddesc}[object]{__setattr__}{self, name, value} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1159 | Called when an attribute assignment is attempted. This is called |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1160 | instead of the normal mechanism (i.e.\ store the value in the instance |
| 1161 | dictionary). \var{name} is the attribute name, \var{value} is the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1162 | value to be assigned to it. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1163 | |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1164 | If \method{__setattr__()} wants to assign to an instance attribute, it |
| 1165 | should not simply execute \samp{self.\var{name} = value} --- this |
| 1166 | would cause a recursive call to itself. Instead, it should insert the |
| 1167 | value in the dictionary of instance attributes, e.g., |
| 1168 | \samp{self.__dict__[\var{name}] = value}. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1169 | \withsubitem{(instance attribute)}{\ttindex{__dict__}} |
| 1170 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1171 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1172 | \begin{methoddesc}[object]{__delattr__}{self, name} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1173 | Like \method{__setattr__()} but for attribute deletion instead of |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1174 | assignment. This should only be implemented if \samp{del |
| 1175 | obj.\var{name}} is meaningful for the object. |
| 1176 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1177 | |
| 1178 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1179 | \subsection{Emulating callable objects\label{callable-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1180 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1181 | \begin{methoddesc}[object]{__call__}{self\optional{, args...}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1182 | Called when the instance is ``called'' as a function; if this method |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1183 | is defined, \code{\var{x}(arg1, arg2, ...)} is a shorthand for |
| 1184 | \code{\var{x}.__call__(arg1, arg2, ...)}. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1185 | \indexii{call}{instance} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1186 | \end{methoddesc} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1187 | |
| 1188 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1189 | \subsection{Emulating container types\label{sequence-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1190 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1191 | The following methods can be defined to implement container |
| 1192 | objects. Containers usually are sequences (such as lists or tuples) |
| 1193 | or mappings (like dictionaries), but can represent other containers as |
| 1194 | well. The first set of methods is used either to emulate a |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1195 | sequence or to emulate a mapping; the difference is that for a |
| 1196 | sequence, the allowable keys should be the integers \var{k} for which |
| 1197 | \code{0 <= \var{k} < \var{N}} where \var{N} is the length of the |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1198 | sequence, or slice objects, which define a range of items. (For backwards |
| 1199 | compatibility, the method \method{__getslice__()} (see below) can also be |
| 1200 | defined to handle simple, but not extended slices.) It is also recommended |
Fred Drake | a007382 | 2000-08-18 02:42:14 +0000 | [diff] [blame] | 1201 | that mappings provide the methods \method{keys()}, \method{values()}, |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1202 | \method{items()}, \method{has_key()}, \method{get()}, \method{clear()}, |
| 1203 | \method{copy()}, and \method{update()} behaving similar to those for |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1204 | Python's standard dictionary objects; mutable sequences should provide |
| 1205 | methods \method{append()}, \method{count()}, \method{index()}, |
| 1206 | \method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()} |
| 1207 | and \method{sort()}, like Python standard list objects. Finally, |
| 1208 | sequence types should implement addition (meaning concatenation) and |
| 1209 | multiplication (meaning repetition) by defining the methods |
Thomas Wouters | 12bba85 | 2000-08-24 20:06:04 +0000 | [diff] [blame] | 1210 | \method{__add__()}, \method{__radd__()}, \method{__iadd__()}, |
| 1211 | \method{__mul__()}, \method{__rmul__()} and \method{__imul__()} described |
| 1212 | below; they should not define \method{__coerce__()} or other numerical |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1213 | operators. It is recommended that both mappings and sequences |
Fred Drake | 18d8d5a | 2001-09-18 17:58:20 +0000 | [diff] [blame] | 1214 | implement the \method{__contains__()} method to allow efficient use of |
| 1215 | the \code{in} operator; for mappings, \code{in} should be equivalent |
| 1216 | of \method{has_key()}; for sequences, it should search through the |
| 1217 | values. |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 1218 | \withsubitem{(mapping object method)}{ |
| 1219 | \ttindex{keys()} |
| 1220 | \ttindex{values()} |
| 1221 | \ttindex{items()} |
| 1222 | \ttindex{has_key()} |
| 1223 | \ttindex{get()} |
| 1224 | \ttindex{clear()} |
| 1225 | \ttindex{copy()} |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1226 | \ttindex{update()} |
| 1227 | \ttindex{__contains__()}} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 1228 | \withsubitem{(sequence object method)}{ |
| 1229 | \ttindex{append()} |
| 1230 | \ttindex{count()} |
| 1231 | \ttindex{index()} |
| 1232 | \ttindex{insert()} |
| 1233 | \ttindex{pop()} |
| 1234 | \ttindex{remove()} |
| 1235 | \ttindex{reverse()} |
| 1236 | \ttindex{sort()} |
| 1237 | \ttindex{__add__()} |
| 1238 | \ttindex{__radd__()} |
Thomas Wouters | 12bba85 | 2000-08-24 20:06:04 +0000 | [diff] [blame] | 1239 | \ttindex{__iadd__()} |
Fred Drake | 4856d01 | 1999-01-12 04:15:20 +0000 | [diff] [blame] | 1240 | \ttindex{__mul__()} |
Thomas Wouters | 12bba85 | 2000-08-24 20:06:04 +0000 | [diff] [blame] | 1241 | \ttindex{__rmul__()} |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 1242 | \ttindex{__imul__()} |
| 1243 | \ttindex{__contains__()}} |
Fred Drake | ae3e574 | 1999-01-28 23:21:49 +0000 | [diff] [blame] | 1244 | \withsubitem{(numeric object method)}{\ttindex{__coerce__()}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1245 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1246 | \begin{methoddesc}[container object]{__len__}{self} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1247 | Called to implement the built-in function |
| 1248 | \function{len()}\bifuncindex{len}. Should return the length of the |
| 1249 | object, an integer \code{>=} 0. Also, an object that doesn't define a |
| 1250 | \method{__nonzero__()} method and whose \method{__len__()} method |
| 1251 | returns zero is considered to be false in a Boolean context. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1252 | \withsubitem{(object method)}{\ttindex{__nonzero__()}} |
| 1253 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1254 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1255 | \begin{methoddesc}[container object]{__getitem__}{self, key} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1256 | Called to implement evaluation of \code{\var{self}[\var{key}]}. |
Fred Drake | 31575ce | 2000-09-21 05:28:26 +0000 | [diff] [blame] | 1257 | For sequence types, the accepted keys should be integers and slice |
| 1258 | objects.\obindex{slice} Note that |
| 1259 | the special interpretation of negative indexes (if the class wishes to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1260 | emulate a sequence type) is up to the \method{__getitem__()} method. |
Fred Drake | 91826ed | 2000-07-13 04:57:58 +0000 | [diff] [blame] | 1261 | If \var{key} is of an inappropriate type, \exception{TypeError} may be |
| 1262 | raised; if of a value outside the set of indexes for the sequence |
| 1263 | (after any special interpretation of negative values), |
| 1264 | \exception{IndexError} should be raised. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1265 | \note{\keyword{for} loops expect that an |
Fred Drake | 91826ed | 2000-07-13 04:57:58 +0000 | [diff] [blame] | 1266 | \exception{IndexError} will be raised for illegal indexes to allow |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 1267 | proper detection of the end of the sequence.} |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1268 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1269 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1270 | \begin{methoddesc}[container object]{__setitem__}{self, key, value} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1271 | Called to implement assignment to \code{\var{self}[\var{key}]}. Same |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1272 | note as for \method{__getitem__()}. This should only be implemented |
| 1273 | for mappings if the objects support changes to the values for keys, or |
| 1274 | if new keys can be added, or for sequences if elements can be |
Fred Drake | 91826ed | 2000-07-13 04:57:58 +0000 | [diff] [blame] | 1275 | replaced. The same exceptions should be raised for improper |
| 1276 | \var{key} values as for the \method{__getitem__()} method. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1277 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1278 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1279 | \begin{methoddesc}[container object]{__delitem__}{self, key} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1280 | Called to implement deletion of \code{\var{self}[\var{key}]}. Same |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1281 | note as for \method{__getitem__()}. This should only be implemented |
| 1282 | for mappings if the objects support removal of keys, or for sequences |
Fred Drake | 91826ed | 2000-07-13 04:57:58 +0000 | [diff] [blame] | 1283 | if elements can be removed from the sequence. The same exceptions |
| 1284 | should be raised for improper \var{key} values as for the |
| 1285 | \method{__getitem__()} method. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1286 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1287 | |
Fred Drake | 73921b0 | 2001-10-01 16:32:13 +0000 | [diff] [blame] | 1288 | \begin{methoddesc}[container object]{__iter__}{self} |
| 1289 | This method is called when an iterator is required for a container. |
| 1290 | This method should return a new iterator object that can iterate over |
| 1291 | all the objects in the container. For mappings, it should iterate |
| 1292 | over the keys of the container, and should also be made available as |
| 1293 | the method \method{iterkeys()}. |
| 1294 | |
| 1295 | Iterator objects also need to implement this method; they are required |
| 1296 | to return themselves. For more information on iterator objects, see |
| 1297 | ``\ulink{Iterator Types}{../lib/typeiter.html}'' in the |
| 1298 | \citetitle[../lib/lib.html]{Python Library Reference}. |
| 1299 | \end{methoddesc} |
| 1300 | |
| 1301 | The membership test operators (\keyword{in} and \keyword{not in}) are |
| 1302 | normally implemented as an iteration through a sequence. However, |
| 1303 | container objects can supply the following special method with a more |
| 1304 | efficient implementation, which also does not require the object be a |
| 1305 | sequence. |
| 1306 | |
| 1307 | \begin{methoddesc}[container object]{__contains__}{self, item} |
| 1308 | Called to implement membership test operators. Should return true if |
| 1309 | \var{item} is in \var{self}, false otherwise. For mapping objects, |
| 1310 | this should consider the keys of the mapping rather than the values or |
| 1311 | the key-item pairs. |
| 1312 | \end{methoddesc} |
| 1313 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1314 | |
Fred Drake | 3041b07 | 1998-10-21 00:25:32 +0000 | [diff] [blame] | 1315 | \subsection{Additional methods for emulation of sequence types |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1316 | \label{sequence-methods}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1317 | |
| 1318 | The following methods can be defined to further emulate sequence |
| 1319 | objects. Immutable sequences methods should only define |
| 1320 | \method{__getslice__()}; mutable sequences, should define all three |
| 1321 | three methods. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1322 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1323 | \begin{methoddesc}[sequence object]{__getslice__}{self, i, j} |
Fred Drake | a007382 | 2000-08-18 02:42:14 +0000 | [diff] [blame] | 1324 | \deprecated{2.0}{Support slice objects as parameters to the |
| 1325 | \method{__getitem__()} method.} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1326 | Called to implement evaluation of \code{\var{self}[\var{i}:\var{j}]}. |
| 1327 | The returned object should be of the same type as \var{self}. Note |
| 1328 | that missing \var{i} or \var{j} in the slice expression are replaced |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 1329 | by zero or \code{sys.maxint}, respectively. If negative indexes are |
| 1330 | used in the slice, the length of the sequence is added to that index. |
| 1331 | If the instance does not implement the \method{__len__()} method, an |
| 1332 | \exception{AttributeError} is raised. |
| 1333 | No guarantee is made that indexes adjusted this way are not still |
| 1334 | negative. Indexes which are greater than the length of the sequence |
| 1335 | are not modified. |
Fred Drake | a007382 | 2000-08-18 02:42:14 +0000 | [diff] [blame] | 1336 | If no \method{__getslice__()} is found, a slice |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1337 | object is created instead, and passed to \method{__getitem__()} instead. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1338 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1339 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1340 | \begin{methoddesc}[sequence object]{__setslice__}{self, i, j, sequence} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1341 | Called to implement assignment to \code{\var{self}[\var{i}:\var{j}]}. |
| 1342 | Same notes for \var{i} and \var{j} as for \method{__getslice__()}. |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1343 | |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1344 | This method is deprecated. If no \method{__setslice__()} is found, a |
| 1345 | slice object is created instead, and passed to \method{__setitem__()} |
| 1346 | instead. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1347 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1348 | |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1349 | \begin{methoddesc}[sequence object]{__delslice__}{self, i, j} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1350 | Called to implement deletion of \code{\var{self}[\var{i}:\var{j}]}. |
| 1351 | Same notes for \var{i} and \var{j} as for \method{__getslice__()}. |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1352 | This method is deprecated. If no \method{__delslice__()} is found, a |
| 1353 | slice object is created instead, and passed to \method{__delitem__()} |
| 1354 | instead. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1355 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1356 | |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1357 | Notice that these methods are only invoked when a single slice with a |
| 1358 | single colon is used, and the slice method is available. For slice |
| 1359 | operations involving extended slice notation, or in absence of the |
| 1360 | slice methods, \method{__getitem__()}, \method{__setitem__()} or |
| 1361 | \method{__delitem__()} is called with a slice object as argument. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1362 | |
Fred Drake | f8925978 | 2000-09-21 22:27:16 +0000 | [diff] [blame] | 1363 | The following example demonstrate how to make your program or module |
| 1364 | compatible with earlier versions of Python (assuming that methods |
| 1365 | \method{__getitem__()}, \method{__setitem__()} and \method{__delitem__()} |
| 1366 | support slice objects as arguments): |
| 1367 | |
| 1368 | \begin{verbatim} |
| 1369 | class MyClass: |
| 1370 | ... |
| 1371 | def __getitem__(self, index): |
| 1372 | ... |
| 1373 | def __setitem__(self, index, value): |
| 1374 | ... |
| 1375 | def __delitem__(self, index): |
| 1376 | ... |
| 1377 | |
| 1378 | if sys.version_info < (2, 0): |
| 1379 | # They won't be defined if version is at least 2.0 final |
| 1380 | |
| 1381 | def __getslice__(self, i, j): |
| 1382 | return self[max(0, i):max(0, j):] |
| 1383 | def __setslice__(self, i, j, seq): |
| 1384 | self[max(0, i):max(0, j):] = seq |
| 1385 | def __delslice__(self, i, j): |
| 1386 | del self[max(0, i):max(0, j):] |
| 1387 | ... |
| 1388 | \end{verbatim} |
| 1389 | |
| 1390 | Note the calls to \function{max()}; these are actually necessary due |
| 1391 | to the handling of negative indices before the |
| 1392 | \method{__*slice__()} methods are called. When negative indexes are |
| 1393 | used, the \method{__*item__()} methods receive them as provided, but |
| 1394 | the \method{__*slice__()} methods get a ``cooked'' form of the index |
| 1395 | values. For each negative index value, the length of the sequence is |
| 1396 | added to the index before calling the method (which may still result |
| 1397 | in a negative index); this is the customary handling of negative |
| 1398 | indexes by the built-in sequence types, and the \method{__*item__()} |
| 1399 | methods are expected to do this as well. However, since they should |
| 1400 | already be doing that, negative indexes cannot be passed in; they must |
| 1401 | be be constrained to the bounds of the sequence before being passed to |
| 1402 | the \method{__*item__()} methods. |
| 1403 | Calling \code{max(0, i)} conveniently returns the proper value. |
| 1404 | |
Fred Drake | 15988fd | 1999-02-12 18:14:57 +0000 | [diff] [blame] | 1405 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1406 | \subsection{Emulating numeric types\label{numeric-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1407 | |
| 1408 | The following methods can be defined to emulate numeric objects. |
| 1409 | Methods corresponding to operations that are not supported by the |
| 1410 | particular kind of number implemented (e.g., bitwise operations for |
| 1411 | non-integral numbers) should be left undefined. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1412 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1413 | \begin{methoddesc}[numeric object]{__add__}{self, other} |
| 1414 | \methodline[numeric object]{__sub__}{self, other} |
| 1415 | \methodline[numeric object]{__mul__}{self, other} |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1416 | \methodline[numeric object]{__floordiv__}{self, other} |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1417 | \methodline[numeric object]{__mod__}{self, other} |
| 1418 | \methodline[numeric object]{__divmod__}{self, other} |
| 1419 | \methodline[numeric object]{__pow__}{self, other\optional{, modulo}} |
| 1420 | \methodline[numeric object]{__lshift__}{self, other} |
| 1421 | \methodline[numeric object]{__rshift__}{self, other} |
| 1422 | \methodline[numeric object]{__and__}{self, other} |
| 1423 | \methodline[numeric object]{__xor__}{self, other} |
| 1424 | \methodline[numeric object]{__or__}{self, other} |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1425 | These methods are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1426 | called to implement the binary arithmetic operations (\code{+}, |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1427 | \code{-}, \code{*}, \code{//}, \code{\%}, |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1428 | \function{divmod()}\bifuncindex{divmod}, |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1429 | \function{pow()}\bifuncindex{pow}, \code{**}, \code{<}\code{<}, |
| 1430 | \code{>}\code{>}, \code{\&}, \code{\^}, \code{|}). For instance, to |
| 1431 | evaluate the expression \var{x}\code{+}\var{y}, where \var{x} is an |
| 1432 | instance of a class that has an \method{__add__()} method, |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1433 | \code{\var{x}.__add__(\var{y})} is called. The \method{__divmod__()} |
| 1434 | method should be the equivalent to using \method{__floordiv__()} and |
| 1435 | \method{__mod__()}; it should not be related to \method{__truediv__()} |
| 1436 | (described below). Note that |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1437 | \method{__pow__()} should be defined to accept an optional third |
| 1438 | argument if the ternary version of the built-in |
| 1439 | \function{pow()}\bifuncindex{pow} function is to be supported. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1440 | \end{methoddesc} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1441 | |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1442 | \begin{methoddesc}[numeric object]{__div__}{self, other} |
| 1443 | \methodline[numeric object]{__truediv__}{self, other} |
| 1444 | The division operator (\code{/}) is implemented by these methods. The |
| 1445 | \method{__truediv__()} method is used when \code{__future__.division} |
| 1446 | is in effect, otherwise \method{__div__()} is used. If only one of |
| 1447 | these two methods is defined, the object will not support division in |
| 1448 | the alternate context; \exception{TypeError} will be raised instead. |
| 1449 | \end{methoddesc} |
| 1450 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1451 | \begin{methoddesc}[numeric object]{__radd__}{self, other} |
| 1452 | \methodline[numeric object]{__rsub__}{self, other} |
| 1453 | \methodline[numeric object]{__rmul__}{self, other} |
| 1454 | \methodline[numeric object]{__rdiv__}{self, other} |
Raymond Hettinger | 10cbe8d | 2002-06-20 06:12:37 +0000 | [diff] [blame] | 1455 | \methodline[numeric object]{__rtruediv__}{self, other} |
| 1456 | \methodline[numeric object]{__rfloordiv__}{self, other} |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1457 | \methodline[numeric object]{__rmod__}{self, other} |
| 1458 | \methodline[numeric object]{__rdivmod__}{self, other} |
| 1459 | \methodline[numeric object]{__rpow__}{self, other} |
| 1460 | \methodline[numeric object]{__rlshift__}{self, other} |
| 1461 | \methodline[numeric object]{__rrshift__}{self, other} |
| 1462 | \methodline[numeric object]{__rand__}{self, other} |
| 1463 | \methodline[numeric object]{__rxor__}{self, other} |
| 1464 | \methodline[numeric object]{__ror__}{self, other} |
Fred Drake | 3e2aca4 | 2001-08-14 20:28:08 +0000 | [diff] [blame] | 1465 | These methods are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1466 | called to implement the binary arithmetic operations (\code{+}, |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1467 | \code{-}, \code{*}, \code{/}, \code{\%}, |
| 1468 | \function{divmod()}\bifuncindex{divmod}, |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1469 | \function{pow()}\bifuncindex{pow}, \code{**}, \code{<}\code{<}, |
| 1470 | \code{>}\code{>}, \code{\&}, \code{\^}, \code{|}) with reflected |
| 1471 | (swapped) operands. These functions are only called if the left |
| 1472 | operand does not support the corresponding operation. For instance, |
| 1473 | to evaluate the expression \var{x}\code{-}\var{y}, where \var{y} is an |
| 1474 | instance of a class that has an \method{__rsub__()} method, |
| 1475 | \code{\var{y}.__rsub__(\var{x})} is called. Note that ternary |
| 1476 | \function{pow()}\bifuncindex{pow} will not try calling |
| 1477 | \method{__rpow__()} (the coercion rules would become too |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1478 | complicated). |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1479 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1480 | |
Thomas Wouters | dc90cc2 | 2000-12-11 23:11:51 +0000 | [diff] [blame] | 1481 | \begin{methoddesc}[numeric object]{__iadd__}{self, other} |
| 1482 | \methodline[numeric object]{__isub__}{self, other} |
| 1483 | \methodline[numeric object]{__imul__}{self, other} |
| 1484 | \methodline[numeric object]{__idiv__}{self, other} |
Raymond Hettinger | 10cbe8d | 2002-06-20 06:12:37 +0000 | [diff] [blame] | 1485 | \methodline[numeric object]{__itruediv__}{self, other} |
| 1486 | \methodline[numeric object]{__ifloordiv__}{self, other} |
| 1487 | \methodline[numeric object]{__imod__}{self, other} |
Thomas Wouters | dc90cc2 | 2000-12-11 23:11:51 +0000 | [diff] [blame] | 1488 | \methodline[numeric object]{__ipow__}{self, other\optional{, modulo}} |
| 1489 | \methodline[numeric object]{__ilshift__}{self, other} |
| 1490 | \methodline[numeric object]{__irshift__}{self, other} |
| 1491 | \methodline[numeric object]{__iand__}{self, other} |
| 1492 | \methodline[numeric object]{__ixor__}{self, other} |
| 1493 | \methodline[numeric object]{__ior__}{self, other} |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1494 | These methods are called to implement the augmented arithmetic |
| 1495 | operations (\code{+=}, \code{-=}, \code{*=}, \code{/=}, \code{\%=}, |
| 1496 | \code{**=}, \code{<}\code{<=}, \code{>}\code{>=}, \code{\&=}, |
| 1497 | \code{\^=}, \code{|=}). These methods should attempt to do the |
| 1498 | operation in-place (modifying \var{self}) and return the result (which |
| 1499 | could be, but does not have to be, \var{self}). If a specific method |
| 1500 | is not defined, the augmented operation falls back to the normal |
| 1501 | methods. For instance, to evaluate the expression |
| 1502 | \var{x}\code{+=}\var{y}, where \var{x} is an instance of a class that |
| 1503 | has an \method{__iadd__()} method, \code{\var{x}.__iadd__(\var{y})} is |
| 1504 | called. If \var{x} is an instance of a class that does not define a |
| 1505 | \method{__iadd()} method, \code{\var{x}.__add__(\var{y})} and |
| 1506 | \code{\var{y}.__radd__(\var{x})} are considered, as with the |
| 1507 | evaluation of \var{x}\code{+}\var{y}. |
Thomas Wouters | dc90cc2 | 2000-12-11 23:11:51 +0000 | [diff] [blame] | 1508 | \end{methoddesc} |
| 1509 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1510 | \begin{methoddesc}[numeric object]{__neg__}{self} |
| 1511 | \methodline[numeric object]{__pos__}{self} |
| 1512 | \methodline[numeric object]{__abs__}{self} |
| 1513 | \methodline[numeric object]{__invert__}{self} |
Fred Drake | fb8ffe6 | 2001-04-13 15:54:41 +0000 | [diff] [blame] | 1514 | Called to implement the unary arithmetic operations (\code{-}, |
| 1515 | \code{+}, \function{abs()}\bifuncindex{abs} and \code{\~{}}). |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1516 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1517 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1518 | \begin{methoddesc}[numeric object]{__complex__}{self} |
| 1519 | \methodline[numeric object]{__int__}{self} |
| 1520 | \methodline[numeric object]{__long__}{self} |
| 1521 | \methodline[numeric object]{__float__}{self} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1522 | Called to implement the built-in functions |
Fred Drake | 15988fd | 1999-02-12 18:14:57 +0000 | [diff] [blame] | 1523 | \function{complex()}\bifuncindex{complex}, |
| 1524 | \function{int()}\bifuncindex{int}, \function{long()}\bifuncindex{long}, |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1525 | and \function{float()}\bifuncindex{float}. Should return a value of |
| 1526 | the appropriate type. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1527 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1528 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1529 | \begin{methoddesc}[numeric object]{__oct__}{self} |
| 1530 | \methodline[numeric object]{__hex__}{self} |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1531 | Called to implement the built-in functions |
| 1532 | \function{oct()}\bifuncindex{oct} and |
| 1533 | \function{hex()}\bifuncindex{hex}. Should return a string value. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1534 | \end{methoddesc} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1535 | |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1536 | \begin{methoddesc}[numeric object]{__coerce__}{self, other} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1537 | Called to implement ``mixed-mode'' numeric arithmetic. Should either |
Fred Drake | d82575d | 1998-08-28 20:03:12 +0000 | [diff] [blame] | 1538 | return a 2-tuple containing \var{self} and \var{other} converted to |
Fred Drake | b894370 | 1999-05-10 13:43:22 +0000 | [diff] [blame] | 1539 | a common numeric type, or \code{None} if conversion is impossible. When |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1540 | the common type would be the type of \code{other}, it is sufficient to |
| 1541 | return \code{None}, since the interpreter will also ask the other |
| 1542 | object to attempt a coercion (but sometimes, if the implementation of |
| 1543 | the other type cannot be changed, it is useful to do the conversion to |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1544 | the other type here). A return value of \code{NotImplemented} is |
| 1545 | equivalent to returning \code{None}. |
Fred Drake | 1e42d8a | 1998-11-25 17:58:50 +0000 | [diff] [blame] | 1546 | \end{methoddesc} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1547 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1548 | \subsection{Coercion rules\label{coercion-rules}} |
| 1549 | |
| 1550 | This section used to document the rules for coercion. As the language |
| 1551 | has evolved, the coercion rules have become hard to document |
| 1552 | precisely; documenting what one version of one particular |
| 1553 | implementation does is undesirable. Instead, here are some informal |
| 1554 | guidelines regarding coercion. In Python 3.0, coercion will not be |
| 1555 | supported. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1556 | |
| 1557 | \begin{itemize} |
| 1558 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1559 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1560 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1561 | If the left operand of a \% operator is a string or Unicode object, no |
| 1562 | coercion takes place and the string formatting operation is invoked |
| 1563 | instead. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1564 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1565 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1566 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1567 | It is no longer recommended to define a coercion operation. |
| 1568 | Mixed-mode operations on types that don't define coercion pass the |
| 1569 | original arguments to the operation. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1570 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1571 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1572 | |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1573 | New-style classes (those derived from \class{object}) never invoke the |
| 1574 | \method{__coerce__()} method in response to a binary operator; the only |
| 1575 | time \method{__coerce__()} is invoked is when the built-in function |
| 1576 | \function{coerce()} is called. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1577 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1578 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1579 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1580 | For most intents and purposes, an operator that returns |
| 1581 | \code{NotImplemented} is treated the same as one that is not |
| 1582 | implemented at all. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1583 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1584 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1585 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1586 | Below, \method{__op__()} and \method{__rop__()} are used to signify |
| 1587 | the generic method names corresponding to an operator; |
| 1588 | \method{__iop__} is used for the corresponding in-place operator. For |
| 1589 | example, for the operator `\code{+}', \method{__add__()} and |
| 1590 | \method{__radd__()} are used for the left and right variant of the |
| 1591 | binary operator, and \method{__iadd__} for the in-place variant. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1592 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1593 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1594 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1595 | For objects \var{x} and \var{y}, first \code{\var{x}.__op__(\var{y})} |
| 1596 | is tried. If this is not implemented or returns \code{NotImplemented}, |
| 1597 | \code{\var{y}.__rop__(\var{x})} is tried. If this is also not |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1598 | implemented or returns \code{NotImplemented}, a \exception{TypeError} |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1599 | exception is raised. But see the following exception: |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1600 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1601 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1602 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1603 | Exception to the previous item: if the left operand is an instance of |
| 1604 | a built-in type or a new-style class, and the right operand is an |
| 1605 | instance of a proper subclass of that type or class, the right |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1606 | operand's \method{__rop__()} method is tried \emph{before} the left |
| 1607 | operand's \method{__op__()} method. This is done so that a subclass can |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1608 | completely override binary operators. Otherwise, the left operand's |
| 1609 | __op__ method would always accept the right operand: when an instance |
| 1610 | of a given class is expected, an instance of a subclass of that class |
| 1611 | is always acceptable. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1612 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1613 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1615 | When either operand type defines a coercion, this coercion is called |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1616 | before that type's \method{__op__()} or \method{__rop__()} method is |
| 1617 | called, but no sooner. If the coercion returns an object of a |
| 1618 | different type for the operand whose coercion is invoked, part of the |
| 1619 | process is redone using the new object. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1620 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1621 | \item |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1622 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1623 | When an in-place operator (like `\code{+=}') is used, if the left |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1624 | operand implements \method{__iop__()}, it is invoked without any |
| 1625 | coercion. When the operation falls back to \method{__op__()} and/or |
| 1626 | \method{__rop__()}, the normal coercion rules apply. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1627 | |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1628 | \item |
| 1629 | |
| 1630 | In \var{x}\code{+}\var{y}, if \var{x} is a sequence that implements |
| 1631 | sequence concatenation, sequence concatenation is invoked. |
| 1632 | |
| 1633 | \item |
| 1634 | |
| 1635 | In \var{x}\code{*}\var{y}, if one operator is a sequence that |
| 1636 | implements sequence repetition, and the other is an integer |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1637 | (\class{int} or \class{long}), sequence repetition is invoked. |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1638 | |
| 1639 | \item |
| 1640 | |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1641 | Rich comparisons (implemented by methods \method{__eq__()} and so on) |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1642 | never use coercion. Three-way comparison (implemented by |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1643 | \method{__cmp__()}) does use coercion under the same conditions as |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1644 | other binary operations use it. |
| 1645 | |
| 1646 | \item |
| 1647 | |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1648 | In the current implementation, the built-in numeric types \class{int}, |
| 1649 | \class{long} and \class{float} do not use coercion; the type |
| 1650 | \class{complex} however does use it. The difference can become |
Guido van Rossum | 92cf95f | 2002-06-03 19:06:41 +0000 | [diff] [blame] | 1651 | apparent when subclassing these types. Over time, the type |
Fred Drake | 293dd4b | 2002-06-04 16:25:57 +0000 | [diff] [blame] | 1652 | \class{complex} may be fixed to avoid coercion. All these types |
| 1653 | implement a \method{__coerce__()} method, for use by the built-in |
| 1654 | \function{coerce()} function. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1655 | |
| 1656 | \end{itemize} |