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