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 | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 3 | \section{Objects, values and types\label{objects}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 4 | |
| 5 | \dfn{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 |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 8 | ``stored program computer,'' code is also represented by objects.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 9 | \index{object} |
| 10 | \index{data} |
| 11 | |
| 12 | Every object has an identity, a type and a value. An object's |
| 13 | \emph{identity} never changes once it has been created; you may think |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 14 | of it as the object's address in memory. The `\code{is}' operator |
| 15 | compares the identity of two objects; the `\code{id()}' function |
| 16 | returns an integer representing its identity (currently implemented as |
| 17 | its address). |
| 18 | An object's \dfn{type} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 19 | also unchangeable. It determines the operations that an object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 20 | supports (e.g., ``does it have a length?'') and also defines the |
| 21 | possible values for objects of that type. The `\code{type()}' |
| 22 | function returns an object's type (which is an object itself). |
| 23 | The \emph{value} of some |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 24 | objects can change. Objects whose value can change are said to be |
| 25 | \emph{mutable}; objects whose value is unchangeable once they are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 26 | created are called \emph{immutable}. |
| 27 | An object's mutability is determined by its type; for instance, |
| 28 | numbers, strings and tuples are immutable, while dictionaries and |
| 29 | lists are mutable. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 30 | \index{identity of an object} |
| 31 | \index{value of an object} |
| 32 | \index{type of an object} |
| 33 | \index{mutable object} |
| 34 | \index{immutable object} |
| 35 | |
| 36 | Objects are never explicitly destroyed; however, when they become |
| 37 | unreachable they may be garbage-collected. An implementation is |
Barry Warsaw | 92a6ed9 | 1998-08-07 16:33:51 +0000 | [diff] [blame] | 38 | allowed to postpone garbage collection or omit it altogether --- it is |
| 39 | a matter of implementation quality how garbage collection is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 40 | implemented, as long as no objects are collected that are still |
| 41 | reachable. (Implementation note: the current implementation uses a |
| 42 | reference-counting scheme which collects most objects as soon as they |
| 43 | become unreachable, but never collects garbage containing circular |
| 44 | references.) |
| 45 | \index{garbage collection} |
| 46 | \index{reference counting} |
| 47 | \index{unreachable object} |
| 48 | |
| 49 | Note that the use of the implementation's tracing or debugging |
| 50 | facilities may keep objects alive that would normally be collectable. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 51 | Also note that catching an exception with a |
| 52 | `\code{try}...\code{except}' statement may keep objects alive. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 53 | |
| 54 | Some objects contain references to ``external'' resources such as open |
| 55 | files or windows. It is understood that these resources are freed |
| 56 | when the object is garbage-collected, but since garbage collection is |
| 57 | not guaranteed to happen, such objects also provide an explicit way to |
| 58 | release the external resource, usually a \method{close()} method. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 59 | Programs are strongly recommended to explicitly close such |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 60 | objects. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 61 | The `\code{try}...\code{finally}' statement provides a convenient way |
| 62 | to do this. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 63 | |
| 64 | Some objects contain references to other objects; these are called |
| 65 | \emph{containers}. Examples of containers are tuples, lists and |
| 66 | dictionaries. The references are part of a container's value. In |
| 67 | most cases, when we talk about the value of a container, we imply the |
| 68 | values, not the identities of the contained objects; however, when we |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 69 | talk about the mutability of a container, only the identities of |
| 70 | the immediately contained objects are implied. So, if an immutable |
| 71 | container (like a tuple) |
| 72 | contains a reference to a mutable object, its value changes |
| 73 | if that mutable object is changed. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 74 | \index{container} |
| 75 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 76 | Types affect almost all aspects of object behavior. Even the importance |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 77 | of object identity is affected in some sense: for immutable types, |
| 78 | operations that compute new values may actually return a reference to |
| 79 | 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] | 80 | objects this is not allowed. E.g., after |
| 81 | ``\code{a = 1; b = 1}'', |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 82 | \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] | 83 | value one, depending on the implementation, but after |
| 84 | ``\code{c = []; d = []}'', \code{c} and \code{d} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 85 | are guaranteed to refer to two different, unique, newly created empty |
| 86 | lists. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 87 | (Note that ``\code{c = d = []}'' assigns the same object to both |
| 88 | \code{c} and \code{d}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 89 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 90 | \section{The standard type hierarchy\label{types}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 91 | |
| 92 | 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] | 93 | modules written in \C{} can define additional types. Future versions of |
| 94 | Python may add types to the type hierarchy (e.g., rational |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 95 | numbers, efficiently stored arrays of integers, etc.). |
| 96 | \index{type} |
| 97 | \indexii{data}{type} |
| 98 | \indexii{type}{hierarchy} |
| 99 | \indexii{extension}{module} |
| 100 | \indexii{C}{language} |
| 101 | |
| 102 | Some of the type descriptions below contain a paragraph listing |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 103 | `special attributes.' These are attributes that provide access to the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 104 | implementation and are not intended for general use. Their definition |
| 105 | may change in the future. There are also some `generic' special |
| 106 | attributes, not listed with the individual objects: \member{__methods__} |
| 107 | is a list of the method names of a built-in object, if it has any; |
| 108 | \member{__members__} is a list of the data attribute names of a built-in |
| 109 | object, if it has any. |
| 110 | \index{attribute} |
| 111 | \indexii{special}{attribute} |
| 112 | \indexiii{generic}{special}{attribute} |
| 113 | \ttindex{__methods__} |
| 114 | \ttindex{__members__} |
| 115 | |
| 116 | \begin{description} |
| 117 | |
| 118 | \item[None] |
| 119 | This type has a single value. There is a single object with this value. |
| 120 | This object is accessed through the built-in name \code{None}. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 121 | It is used to signify the absence of a value in many situations, e.g., |
| 122 | it is returned from functions that don't explicitly return anything. |
| 123 | Its truth value is false. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 124 | \ttindex{None} |
| 125 | \obindex{None@{\tt None}} |
| 126 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 127 | \item[Ellipsis] |
| 128 | This type has a single value. There is a single object with this value. |
| 129 | This object is accessed through the built-in name \code{Ellipsis}. |
| 130 | It is used to indicate the presence of the ``\code{...}'' syntax in a |
| 131 | slice. Its truth value is true. |
| 132 | \ttindex{Ellipsis} |
| 133 | \obindex{Ellipsis@{\tt Ellipsis}} |
| 134 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 135 | \item[Numbers] |
| 136 | These are created by numeric literals and returned as results by |
| 137 | arithmetic operators and arithmetic built-in functions. Numeric |
| 138 | objects are immutable; once created their value never changes. Python |
| 139 | numbers are of course strongly related to mathematical numbers, but |
| 140 | subject to the limitations of numerical representation in computers. |
| 141 | \obindex{number} |
| 142 | \obindex{numeric} |
| 143 | |
| 144 | Python distinguishes between integers and floating point numbers: |
| 145 | |
| 146 | \begin{description} |
| 147 | \item[Integers] |
| 148 | These represent elements from the mathematical set of whole numbers. |
| 149 | \obindex{integer} |
| 150 | |
| 151 | There are two types of integers: |
| 152 | |
| 153 | \begin{description} |
| 154 | |
| 155 | \item[Plain integers] |
| 156 | These represent numbers in the range -2147483648 through 2147483647. |
| 157 | (The range may be larger on machines with a larger natural word |
| 158 | size, but not smaller.) |
| 159 | When the result of an operation falls outside this range, the |
| 160 | exception \exception{OverflowError} is raised. |
| 161 | For the purpose of shift and mask operations, integers are assumed to |
| 162 | have a binary, 2's complement notation using 32 or more bits, and |
| 163 | hiding no bits from the user (i.e., all 4294967296 different bit |
| 164 | patterns correspond to different values). |
| 165 | \obindex{plain integer} |
| 166 | \withsubitem{(built-in exception)}{\ttindex{OverflowError}} |
| 167 | |
| 168 | \item[Long integers] |
| 169 | These represent numbers in an unlimited range, subject to available |
| 170 | (virtual) memory only. For the purpose of shift and mask operations, |
| 171 | a binary representation is assumed, and negative numbers are |
| 172 | represented in a variant of 2's complement which gives the illusion of |
| 173 | an infinite string of sign bits extending to the left. |
| 174 | \obindex{long integer} |
| 175 | |
| 176 | \end{description} % Integers |
| 177 | |
| 178 | The rules for integer representation are intended to give the most |
| 179 | meaningful interpretation of shift and mask operations involving |
| 180 | negative integers and the least surprises when switching between the |
| 181 | plain and long integer domains. For any operation except left shift, |
| 182 | if it yields a result in the plain integer domain without causing |
| 183 | overflow, it will yield the same result in the long integer domain or |
| 184 | when using mixed operands. |
| 185 | \indexii{integer}{representation} |
| 186 | |
| 187 | \item[Floating point numbers] |
| 188 | These represent machine-level double precision floating point numbers. |
| 189 | You are at the mercy of the underlying machine architecture and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 190 | \C{} implementation for the accepted range and handling of overflow. |
| 191 | Python does not support single-precision floating point numbers; the |
| 192 | savings in CPU and memory usage that are usually the reason for using |
| 193 | these is dwarfed by the overhead of using objects in Python, so there |
| 194 | is no reason to complicate the language with two kinds of floating |
| 195 | point numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 196 | \obindex{floating point} |
| 197 | \indexii{floating point}{number} |
| 198 | \indexii{C}{language} |
| 199 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 200 | \item[Complex numbers] |
| 201 | These represent complex numbers as a pair of machine-level double |
| 202 | precision floating point numbers. The same caveats apply as for |
| 203 | floating point numbers. The real and imaginary value of a complex |
| 204 | number \code{z} can be retrieved through the attributes \code{z.real} |
| 205 | and \code{z.imag}. |
| 206 | \obindex{complex} |
| 207 | \indexii{complex}{number} |
| 208 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 209 | \end{description} % Numbers |
| 210 | |
| 211 | \item[Sequences] |
| 212 | These represent finite ordered sets indexed by natural numbers. |
| 213 | The built-in function \function{len()}\bifuncindex{len} returns the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 214 | number of items of a sequence. |
| 215 | When the lenth of a sequence is \var{n}, the |
| 216 | index set contains the numbers 0, 1, \ldots, \var{n}-1. Item |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 217 | \var{i} of sequence \var{a} is selected by \code{\var{a}[\var{i}]}. |
| 218 | \obindex{seqence} |
| 219 | \index{index operation} |
| 220 | \index{item selection} |
| 221 | \index{subscription} |
| 222 | |
| 223 | Sequences also support slicing: \code{\var{a}[\var{i}:\var{j}]} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 224 | selects all items with index \var{k} such that \var{i} \code{<=} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 225 | \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] | 226 | sequence of the same type. This implies that the index set is |
| 227 | renumbered so that it starts at 0. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 228 | \index{slicing} |
| 229 | |
| 230 | Sequences are distinguished according to their mutability: |
| 231 | |
| 232 | \begin{description} |
| 233 | % |
| 234 | \item[Immutable sequences] |
| 235 | An object of an immutable sequence type cannot change once it is |
| 236 | created. (If the object contains references to other objects, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 237 | these other objects may be mutable and may be changed; however, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 238 | the collection of objects directly referenced by an immutable object |
| 239 | cannot change.) |
| 240 | \obindex{immutable sequence} |
| 241 | \obindex{immutable} |
| 242 | |
| 243 | The following types are immutable sequences: |
| 244 | |
| 245 | \begin{description} |
| 246 | |
| 247 | \item[Strings] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 248 | The items of a string are characters. There is no separate |
| 249 | character type; a character is represented by a string of one item. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 250 | Characters represent (at least) 8-bit bytes. The built-in |
| 251 | functions \function{chr()}\bifuncindex{chr} and |
| 252 | \function{ord()}\bifuncindex{ord} convert between characters and |
| 253 | nonnegative integers representing the byte values. Bytes with the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 254 | values 0-127 usually represent the corresponding \ASCII{} values, but |
| 255 | the interpretation of values is up to the program. The string |
| 256 | 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] | 257 | read from a file. |
| 258 | \obindex{string} |
| 259 | \index{character} |
| 260 | \index{byte} |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 261 | \index{ASCII@\ASCII{}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 262 | |
| 263 | (On systems whose native character set is not \ASCII{}, strings may use |
| 264 | EBCDIC in their internal representation, provided the functions |
| 265 | \function{chr()} and \function{ord()} implement a mapping between \ASCII{} and |
| 266 | EBCDIC, and string comparison preserves the \ASCII{} order. |
| 267 | Or perhaps someone can propose a better rule?) |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 268 | \index{ASCII@\ASCII{}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 269 | \index{EBCDIC} |
| 270 | \index{character set} |
| 271 | \indexii{string}{comparison} |
| 272 | \bifuncindex{chr} |
| 273 | \bifuncindex{ord} |
| 274 | |
| 275 | \item[Tuples] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 276 | The items of a tuple are arbitrary Python objects. |
| 277 | Tuples of two or more items are formed by comma-separated lists |
| 278 | of expressions. A tuple of one item (a `singleton') can be formed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 279 | by affixing a comma to an expression (an expression by itself does |
| 280 | 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] | 281 | expressions). An empty tuple can be formed by an empty pair of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 282 | parentheses. |
| 283 | \obindex{tuple} |
| 284 | \indexii{singleton}{tuple} |
| 285 | \indexii{empty}{tuple} |
| 286 | |
| 287 | \end{description} % Immutable sequences |
| 288 | |
| 289 | \item[Mutable sequences] |
| 290 | Mutable sequences can be changed after they are created. The |
| 291 | subscription and slicing notations can be used as the target of |
| 292 | assignment and \keyword{del} (delete) statements. |
| 293 | \obindex{mutable sequece} |
| 294 | \obindex{mutable} |
| 295 | \indexii{assignment}{statement} |
| 296 | \index{delete} |
| 297 | \stindex{del} |
| 298 | \index{subscription} |
| 299 | \index{slicing} |
| 300 | |
| 301 | There is currently a single mutable sequence type: |
| 302 | |
| 303 | \begin{description} |
| 304 | |
| 305 | \item[Lists] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 306 | The items of a list are arbitrary Python objects. Lists are formed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 307 | by placing a comma-separated list of expressions in square brackets. |
| 308 | (Note that there are no special cases needed to form lists of length 0 |
| 309 | or 1.) |
| 310 | \obindex{list} |
| 311 | |
| 312 | \end{description} % Mutable sequences |
| 313 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 314 | The extension module \module{array}\refstmodindex{array} provides an |
| 315 | additional example of a mutable sequence type. |
| 316 | |
| 317 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 318 | \end{description} % Sequences |
| 319 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 320 | \item[Mappings] |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 321 | These represent finite sets of objects indexed by arbitrary index sets. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 322 | The subscript notation \code{a[k]} selects the item indexed |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 323 | by \code{k} from the mapping \code{a}; this can be used in |
| 324 | expressions and as the target of assignments or \keyword{del} statements. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 325 | The built-in function \function{len()} returns the number of items |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 326 | in a mapping. |
| 327 | \bifuncindex{len} |
| 328 | \index{subscription} |
| 329 | \obindex{mapping} |
| 330 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 331 | There is currently a single intrinsic mapping type: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 332 | |
| 333 | \begin{description} |
| 334 | |
| 335 | \item[Dictionaries] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 336 | These represent finite sets of objects indexed by nearly arbitrary |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 337 | values. The only types of values not acceptable as keys are values |
| 338 | containing lists or dictionaries or other mutable types that are |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 339 | compared by value rather than by object identity, the reason being |
| 340 | that the efficient implementation of dictionaries requires a key's |
| 341 | hash value to remain constant. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 342 | Numeric types used for keys obey the normal rules for numeric |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 343 | comparison: if two numbers compare equal (e.g., \code{1} and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 344 | \code{1.0}) then they can be used interchangeably to index the same |
| 345 | dictionary entry. |
| 346 | |
| 347 | Dictionaries are mutable; they are created by the \code{...} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 348 | notation (see section \ref{dict}, ``Dictionary Displays''). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 349 | \obindex{dictionary} |
| 350 | \obindex{mutable} |
| 351 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 352 | The extension modules \module{dbm}\refstmodindex{dbm}, |
| 353 | \module{gdbm}\refstmodindex{gdbm}, \module{bsddb}\refstmodindex{bsddb} |
| 354 | provide additional examples of mapping types. |
| 355 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 356 | \end{description} % Mapping types |
| 357 | |
| 358 | \item[Callable types] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 359 | These are the types to which the function call operation (see section |
| 360 | \ref{calls}, ``Calls'') can be applied: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 361 | \indexii{function}{call} |
| 362 | \index{invocation} |
| 363 | \indexii{function}{argument} |
| 364 | \obindex{callable} |
| 365 | |
| 366 | \begin{description} |
| 367 | |
| 368 | \item[User-defined functions] |
| 369 | A user-defined function object is created by a function definition |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 370 | (see section \ref{function}, ``Function definitions''). It should be |
| 371 | called with an argument |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 372 | list containing the same number of items as the function's formal |
| 373 | parameter list. |
| 374 | \indexii{user-defined}{function} |
| 375 | \obindex{function} |
| 376 | \obindex{user-defined function} |
| 377 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 378 | Special read-only attributes: \code{func_doc} or \code{__doc__} is the |
| 379 | function's documentation string, or None if unavailable; |
| 380 | \code{func_name} or \code{__name__} is the function's name; |
| 381 | \code{func_defaults} is a tuple containing default argument values for |
| 382 | those arguments that have defaults, or \code{None} if no arguments |
| 383 | have a default value; \code{func_code} is the code object representing |
| 384 | the compiled function body; \code{func_globals} is (a reference to) |
| 385 | the dictionary that holds the function's global variables --- it |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 386 | defines the global namespace of the module in which the function was |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 387 | defined. Additional information about a function's definition can be |
| 388 | retrieved from its code object; see the description of internal types |
| 389 | below. |
| 390 | \ttindex{func_doc} |
| 391 | \ttindex{__doc__} |
| 392 | \ttindex{__name__} |
| 393 | \ttindex{func_defaults} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 394 | \ttindex{func_code} |
| 395 | \ttindex{func_globals} |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 396 | \indexii{global}{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 397 | |
| 398 | \item[User-defined methods] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 399 | A user-defined method object combines a class, a class instance (or |
| 400 | \code{None}) and a user-defined function. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 401 | \obindex{method} |
| 402 | \obindex{user-defined method} |
| 403 | \indexii{user-defined}{method} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 404 | |
| 405 | Special read-only attributes: \member{im_self} is the class instance |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 406 | object, \member{im_func} is the function object; |
| 407 | \code{im_class} is the class that defined the method (which may be a |
| 408 | base class of the class of which \code{im_self} is an instance); |
| 409 | \code{__doc__} is the method's documentation (same as |
| 410 | \code{im_func.__doc__}); \code{__name__} is the method name (same as |
| 411 | \code{im_func.__name__}). |
| 412 | |
| 413 | User-defined method objects are created in two ways: when getting an |
| 414 | attribute of a class that is a user-defined function object, or when |
| 415 | getting an attributes of a class instance that is a user-defined |
| 416 | function object. In the former case (class attribute), the |
| 417 | \code{im_self} attribute is \code{None}, and the method object is said |
| 418 | to be unbound; in the latter case (instance attribute), \code{im_self} |
| 419 | is the instance, and the method object is said to be bound. For |
| 420 | instance, when \code{C} is a class which contains a definition for a |
| 421 | function \code{f}, \code{C.f} does not yield the function object |
| 422 | \code{f}; rather, it yields an unbound method object \code{m} where |
Fred Drake | 70da192 | 1998-08-07 16:28:13 +0000 | [diff] [blame] | 423 | \code{m.im_class} is \code{C}, \code{m.im_func} is \code{f}, and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 424 | m\code{.im_self} is \code{None}. When \code{x} is a \code{C} |
| 425 | instance, \code{x.f} yields a bound method object \code{m} where |
Fred Drake | 70da192 | 1998-08-07 16:28:13 +0000 | [diff] [blame] | 426 | m\code{.im_class} is \code{C}, \code{m.im_func} is \code{f}, and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 427 | \code{m.im_self} is \code{x}. |
| 428 | |
| 429 | When an unbound user-defined method object is called, the underlying |
| 430 | function (\code{im_func}) is called, with the restriction that the |
| 431 | first argument must be an instance of the proper class |
| 432 | (\code{im_class}) or of a derived class thereof. |
| 433 | |
| 434 | When a bound user-defined method object is called, the underlying |
| 435 | function (\code{im_func}) is called, inserting the class instance |
| 436 | (\code{im_self}) in front of the argument list. For instance, when |
| 437 | \code{C} is a class which contains a definition for a function |
| 438 | \code{f}, and \code{x} is an instance of \code{C}, calling |
| 439 | \code{x.f(1)} is equivalent to calling \code{C.f(x, 1)}. |
| 440 | |
| 441 | Note that the transformation from function object to (unbound or |
| 442 | bound) method object happens each time the attribute is retrieved from |
| 443 | the class or instance. In some cases, a fruitful optimization is to |
| 444 | assign the attribute to a local variable and call that local variable. |
| 445 | Also notice that this transformation only happens for user-defined |
| 446 | functions; other callable objects (and all non-callable objects) are |
| 447 | retrieved without transformation. |
| 448 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 449 | \ttindex{im_func} |
| 450 | \ttindex{im_self} |
| 451 | |
| 452 | \item[Built-in functions] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 453 | A built-in function object is a wrapper around a \C{} function. Examples |
| 454 | of built-in functions are \function{len()} and \function{math.sin()} |
| 455 | (\module{math} is a standard built-in module). |
| 456 | The number and type of the arguments are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 457 | determined by the C function. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 458 | Special read-only attributes: \code{__doc__} is the function's |
| 459 | documentation string, or \code{None} if unavailable; \code{__name__} |
| 460 | is the function's name; \code{__self__} is set to \code{None} (but see |
| 461 | the next item). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 462 | \obindex{built-in function} |
| 463 | \obindex{function} |
| 464 | \indexii{C}{language} |
| 465 | |
| 466 | \item[Built-in methods] |
| 467 | This is really a different disguise of a built-in function, this time |
| 468 | 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] | 469 | argument. An example of a built-in method is |
| 470 | \code{\var{list}.append()}, assuming |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 471 | \var{list} is a list object. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 472 | In this case, the special read-only attribute \code{__self__} is set |
| 473 | to the object denoted by \code{list}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 474 | \obindex{built-in method} |
| 475 | \obindex{method} |
| 476 | \indexii{built-in}{method} |
| 477 | |
| 478 | \item[Classes] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 479 | Class objects are described below. When a class object is called, |
| 480 | a new class instance (also described below) is created and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 481 | returned. This implies a call to the class's \method{__init__()} method |
| 482 | 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] | 483 | 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] | 484 | without arguments. |
| 485 | \ttindex{__init__} |
| 486 | \obindex{class} |
| 487 | \obindex{class instance} |
| 488 | \obindex{instance} |
| 489 | \indexii{class object}{call} |
| 490 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 491 | \item[Class instances] |
| 492 | Class instances are described below. Class instances are callable |
| 493 | only when the class has a \code{__call__} method; \code{x(arguments)} |
| 494 | is a shorthand for \code{x.__call__(arguments)}. |
| 495 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 496 | \end{description} |
| 497 | |
| 498 | \item[Modules] |
| 499 | Modules are imported by the \keyword{import} statement (see section |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 500 | \ref{import}, ``The \keyword{import} statement''). |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 501 | A module object has a namespace implemented by a dictionary object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 502 | (this is the dictionary referenced by the func_globals attribute of |
| 503 | functions defined in the module). Attribute references are translated |
| 504 | to lookups in this dictionary, e.g., \code{m.x} is equivalent to |
| 505 | \code{m.__dict__["x"]}. |
| 506 | A module object does not contain the code object used to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 507 | initialize the module (since it isn't needed once the initialization |
| 508 | is done). |
| 509 | \stindex{import} |
| 510 | \obindex{module} |
| 511 | |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 512 | Attribute assignment updates the module's namespace dictionary, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 513 | e.g., ``\code{m.x = 1}'' is equivalent to ``\code{m.__dict__["x"] = 1}''. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 515 | Special read-only attribute: \member{__dict__} is the module's |
| 516 | namespace as a dictionary object. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 517 | \ttindex{__dict__} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 518 | |
| 519 | Predefined (writable) attributes: \member{__name__} |
| 520 | is the module's name; \member{__doc__} is the |
| 521 | module's documentation string, or |
| 522 | \code{None} if unavailable; \code{__file__} is the pathname of the |
| 523 | file from which the module was loaded, if it was loaded from a file. |
| 524 | The \code{__file__} attribute is not present for C{} modules that are |
| 525 | statically linked into the interpreter; for extension modules loaded |
| 526 | dynamically from a shared library, it is the pathname of the shared |
| 527 | library file. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 528 | \ttindex{__name__} |
| 529 | \ttindex{__doc__} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 530 | \ttindex{__file__} |
Guido van Rossum | dfb658c | 1998-07-23 17:54:36 +0000 | [diff] [blame] | 531 | \indexii{module}{namespace} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 532 | |
| 533 | \item[Classes] |
| 534 | Class objects are created by class definitions (see section |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 535 | \ref{class}, ``Class definitions''). |
| 536 | A class has a namespace implemented by a dictionary object. |
| 537 | Class attribute references are translated to |
| 538 | lookups in this dictionary, |
| 539 | e.g., ``\code{C.x}'' is translated to ``\code{C.__dict__["x"]}''. |
| 540 | When the attribute name is not found |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 541 | there, the attribute search continues in the base classes. The search |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 542 | 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] | 543 | base class list. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 544 | When a class attribute reference would yield a user-defined function |
| 545 | object, it is transformed into an unbound user-defined method object |
| 546 | (see above). The \code{im_class} attribute of this method object is the |
| 547 | class in which the function object was found, not necessarily the |
| 548 | class for which the attribute reference was initiated. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 549 | \obindex{class} |
| 550 | \obindex{class instance} |
| 551 | \obindex{instance} |
| 552 | \indexii{class object}{call} |
| 553 | \index{container} |
| 554 | \obindex{dictionary} |
| 555 | \indexii{class}{attribute} |
| 556 | |
| 557 | Class attribute assignments update the class's dictionary, never the |
| 558 | dictionary of a base class. |
| 559 | \indexiii{class}{attribute}{assignment} |
| 560 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 561 | A class object can be called (see above) to yield a class instance (see |
| 562 | below). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 563 | \indexii{class object}{call} |
| 564 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 565 | Special attributes: \member{__name__} is the class name; |
| 566 | \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] | 567 | \member{__dict__} is the dictionary containing the class's namespace; |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 568 | \member{__bases__} is a tuple (possibly empty or a singleton) |
| 569 | containing the base classes, in the order of their occurrence in the |
| 570 | base class list; \code{__doc__} is the class's documentation string, |
| 571 | or None if undefined. |
| 572 | \ttindex{__name__} |
| 573 | \ttindex{__module__} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 574 | \ttindex{__dict__} |
| 575 | \ttindex{__bases__} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 576 | \ttindex{__doc__} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 577 | |
| 578 | \item[Class instances] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 579 | A class instance is created by calling a class object (see above). |
| 580 | A class instance has a namespace implemented as a dictionary which |
| 581 | is the first place in which |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 582 | attribute references are searched. When an attribute is not found |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 583 | there, and the instance's class has an attribute by that name, |
| 584 | the search continues with the class attributes. If a class attribute |
| 585 | is found that is a user-defined function object (and in no other |
| 586 | case), it is transformed into an unbound user-defined method object |
| 587 | (see above). The \code{im_class} attribute of this method object is |
| 588 | the class in which the function object was found, not necessarily the |
| 589 | class of the instance for which the attribute reference was initiated. |
| 590 | If no class attribute is found, and the object's class has a |
| 591 | \code{__getattr__} method, that is called to satisfy the lookup. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 592 | \obindex{class instance} |
| 593 | \obindex{instance} |
| 594 | \indexii{class}{instance} |
| 595 | \indexii{class instance}{attribute} |
| 596 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 597 | Attribute assignments and deletions update the instance's dictionary, |
| 598 | never a class's dictionary. If the class has a \code{__setattr__} or |
| 599 | \code{__delattr__} method, this is called instead of updating the |
| 600 | instance dictionary directly. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 601 | \indexiii{class instance}{attribute}{assignment} |
| 602 | |
| 603 | Class instances can pretend to be numbers, sequences, or mappings if |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 604 | they have methods with certain special names. See |
| 605 | section \ref{specialnames}, ``Special method names.'' |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 606 | \obindex{number} |
| 607 | \obindex{sequence} |
| 608 | \obindex{mapping} |
| 609 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 610 | Special attributes: \member{__dict__} is the attribute |
| 611 | dictionary; \member{__class__} is the instance's class. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 612 | \ttindex{__dict__} |
| 613 | \ttindex{__class__} |
| 614 | |
| 615 | \item[Files] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 616 | A file object represents an open file. File objects are created by the |
| 617 | \function{open()} built-in function, and also by |
| 618 | \function{os.popen()}, \function{os.fdopen()}, and the |
| 619 | \method{makefile()} method of socket objects (and perhaps by other |
| 620 | functions or methods provided by extension modules). The objects |
| 621 | \code{sys.stdin}, \code{sys.stdout} and \code{sys.stderr} are |
| 622 | initialized to file objects corresponding to the interpreter's |
| 623 | standard input, output and error streams. See the \emph{Python |
| 624 | Library Reference} for complete documentation of file objects. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 625 | \obindex{file} |
| 626 | \indexii{C}{language} |
| 627 | \index{stdio} |
| 628 | \bifuncindex{open} |
| 629 | \bifuncindex{popen} |
| 630 | \bifuncindex{makefile} |
| 631 | \ttindex{stdin} |
| 632 | \ttindex{stdout} |
| 633 | \ttindex{stderr} |
| 634 | \ttindex{sys.stdin} |
| 635 | \ttindex{sys.stdout} |
| 636 | \ttindex{sys.stderr} |
| 637 | |
| 638 | \item[Internal types] |
| 639 | 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] | 640 | Their definitions may change with future versions of the interpreter, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 641 | but they are mentioned here for completeness. |
| 642 | \index{internal type} |
| 643 | \index{types, internal} |
| 644 | |
| 645 | \begin{description} |
| 646 | |
| 647 | \item[Code objects] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 648 | Code objects represent \emph{byte-compiled} executable Python code, or |
| 649 | \emph{bytecode}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 650 | The difference between a code |
| 651 | 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] | 652 | explicit reference to the function's globals (the module in which it |
| 653 | was defined), while a code object contains no context; |
| 654 | also the default argument values are stored in the function object, |
| 655 | not in the code object (because they represent values calculated at |
| 656 | run-time). Unlike function objects, code objects are immutable and |
| 657 | contain no references (directly or indirectly) to mutable objects. |
| 658 | \index{bytecode} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 659 | \obindex{code} |
| 660 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 661 | Special read-only attributes: \code{co_name}\ttindex{co_name} gives |
| 662 | the function name; \code{co_argcount}\ttindex{co_argcount} |
| 663 | is the number of positional arguments (including arguments with |
| 664 | default values); \code{co_nlocals}\ttindex{co_nlocals} is the number |
| 665 | of local variables used by the function (including arguments); |
| 666 | \code{co_varnames}\ttindex{co_varnames} is a tuple containing the |
| 667 | names of the local variables (starting with the argument names); |
| 668 | \code{co_code}\ttindex{co_code} is a string representing the sequence |
| 669 | of bytecode instructions; \code{co_consts}\ttindex{co_consts} is a |
| 670 | tuple containing the literals used by the bytecode; |
| 671 | \code{co_names}\ttindex{co_names} is a tuple containing the names used |
| 672 | by the bytecode; \code{co_filename}\ttindex{co_filename} is the |
| 673 | filename from which the code was compiled; |
| 674 | \code{co_firstlineno}\ttindex{co_firstlineno} is the first line number |
| 675 | of the function; \code{co_lnotab}\ttindex{co_lnotab} is a string |
| 676 | encoding the mapping from byte code offsets to line numbers (for |
| 677 | detais see the source code of the interpreter); |
| 678 | \code{co_stacksize}\ttindex{co_stacksize} is the required stack size |
| 679 | (including local variables); \code{co_flags}\ttindex{co_flags} is an |
| 680 | integer encoding a number of flags for the interpreter. |
| 681 | |
| 682 | The following flag bits are defined for \code{co_flags}: bit 2 is set |
| 683 | if the function uses the ``\code{*arguments}'' syntax to accept an |
| 684 | arbitrary number of positional arguments; bit 3 is set if the function |
| 685 | uses the ``\code{**keywords}'' syntax to accept arbitrary keyword |
| 686 | arguments; other bits are used internally or reserved for future use. |
| 687 | If a code object represents a function, the first item in |
| 688 | \code{co_consts} is the documentation string of the |
| 689 | function, or \code{None} if undefined. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 690 | |
| 691 | \item[Frame objects] |
| 692 | Frame objects represent execution frames. They may occur in traceback |
| 693 | objects (see below). |
| 694 | \obindex{frame} |
| 695 | |
| 696 | Special read-only attributes: \member{f_back} is to the previous |
| 697 | stack frame (towards the caller), or \code{None} if this is the bottom |
| 698 | 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] | 699 | frame; \member{f_locals} is the dictionary used to look up local |
| 700 | variables; \member{f_globals} is used for global variables; |
| 701 | \code{f_builtins} is used for built-in (intrinsic) names; |
| 702 | \code{f_restricted} is a flag indicating whether the function is |
| 703 | executing in restricted execution mode; |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 704 | \member{f_lineno} gives the line number and \member{f_lasti} gives the |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 705 | precise instruction (this is an index into the bytecode string of |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 706 | the code object). |
| 707 | \ttindex{f_back} |
| 708 | \ttindex{f_code} |
| 709 | \ttindex{f_globals} |
| 710 | \ttindex{f_locals} |
| 711 | \ttindex{f_lineno} |
| 712 | \ttindex{f_lasti} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 713 | \ttindex{f_builtins} |
| 714 | \ttindex{f_restricted} |
| 715 | |
| 716 | Special writable attributes: \code{f_trace}, if not \code{None}, is a |
| 717 | function called at the start of each source code line (this is used by |
| 718 | the debugger); \code{f_exc_type}, \code{f_exc_value}, |
| 719 | \code{f_exc_traceback} represent the most recent exception caught in |
| 720 | this frame. |
| 721 | \ttindex{f_trace} |
| 722 | \ttindex{f_exc_type} |
| 723 | \ttindex{f_exc_value} |
| 724 | \ttindex{f_exc_traceback} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 725 | |
| 726 | \item[Traceback objects] \label{traceback} |
| 727 | Traceback objects represent a stack trace of an exception. A |
| 728 | traceback object is created when an exception occurs. When the search |
| 729 | for an exception handler unwinds the execution stack, at each unwound |
| 730 | level a traceback object is inserted in front of the current |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 731 | traceback. When an exception handler is entered, the stack trace is |
| 732 | made available to the program. |
| 733 | (See section \ref{try}, ``The \code{try} statement.'') |
| 734 | It is accessible as \code{sys.exc_traceback}, and also as the third |
| 735 | item of the tuple returned by \code{sys.exc_info()}. The latter is |
| 736 | the preferred interface, since it works correctly when the program is |
| 737 | using multiple threads. |
| 738 | When the program contains no suitable handler, the stack trace is written |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 739 | (nicely formatted) to the standard error stream; if the interpreter is |
| 740 | interactive, it is also made available to the user as |
| 741 | \code{sys.last_traceback}. |
| 742 | \obindex{traceback} |
| 743 | \indexii{stack}{trace} |
| 744 | \indexii{exception}{handler} |
| 745 | \indexii{execution}{stack} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 746 | \ttindex{exc_info} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 747 | \ttindex{exc_traceback} |
| 748 | \ttindex{last_traceback} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 749 | \ttindex{sys.exc_info} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 750 | \ttindex{sys.exc_traceback} |
| 751 | \ttindex{sys.last_traceback} |
| 752 | |
| 753 | Special read-only attributes: \member{tb_next} is the next level in the |
| 754 | stack trace (towards the frame where the exception occurred), or |
| 755 | \code{None} if there is no next level; \member{tb_frame} points to the |
| 756 | execution frame of the current level; \member{tb_lineno} gives the line |
| 757 | number where the exception occurred; \member{tb_lasti} indicates the |
| 758 | precise instruction. The line number and last instruction in the |
| 759 | traceback may differ from the line number of its frame object if the |
| 760 | exception occurred in a \keyword{try} statement with no matching |
| 761 | except clause or with a finally clause. |
| 762 | \ttindex{tb_next} |
| 763 | \ttindex{tb_frame} |
| 764 | \ttindex{tb_lineno} |
| 765 | \ttindex{tb_lasti} |
| 766 | \stindex{try} |
| 767 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 768 | \item[Slice objects] |
| 769 | Slice objects are used to represent slices when \emph{extended slice |
| 770 | syntax} is used. This is a slice using two colons, or multiple slices |
| 771 | or ellipses separated by commas, e.g., \code{a[i:j:step]}, \code{a[i:j, |
| 772 | k:l]}, or \code{a[..., i:j])}. They are also created by the built-in |
| 773 | \function{slice()} function. |
| 774 | |
| 775 | Special read-only attributes: \code{start} is the lowerbound; |
| 776 | \code{stop} is the upperbound; \code{step} is the step value; each is |
| 777 | \code{None} if omitted. These attributes can have any type. |
| 778 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 779 | \end{description} % Internal types |
| 780 | |
| 781 | \end{description} % Types |
| 782 | |
| 783 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 784 | \section{Special method names\label{specialnames}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 785 | |
| 786 | A class can implement certain operations that are invoked by special |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 787 | syntax (such as arithmetic operations or subscripting and slicing) by defining |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 788 | methods with special names. For instance, if a class defines a |
| 789 | method named \method{__getitem__()}, and \code{x} is an instance of this |
| 790 | class, then \code{x[i]} is equivalent to \code{x.__getitem__(i)}. |
| 791 | (The reverse is not true --- if \code{x} is a list object, |
| 792 | \code{x.__getitem__(i)} is not equivalent to \code{x[i]}.) |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 793 | Except where mentioned, attempts to execute an |
| 794 | operation raise an exception when no appropriate method is defined. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 795 | \ttindex{__getitem__} |
| 796 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 797 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 798 | \subsection{Basic customization\label{customization}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 799 | |
| 800 | \begin{description} |
| 801 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 802 | \item[{\tt __init__(self, [args...])}] |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 803 | Called when the instance is created. The arguments are those passed |
| 804 | to the class constructor expression. If a base class has an |
| 805 | \code{__init__} method the derived class's \code{__init__} method must |
| 806 | explicitly call it to ensure proper initialization of the base class |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 807 | part of the instance, e.g., ``\code{BaseClass.__init__(self, [args...])}''. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 808 | \ttindex{__init__} |
| 809 | \indexii{class}{constructor} |
| 810 | |
| 811 | |
| 812 | \item[{\tt __del__(self)}] |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 813 | Called when the instance is about to be destroyed. This is also |
| 814 | called a destructor\index{destructor}. If a base class |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 815 | has a \method{__del__()} method, the derived class's \method{__del__()} method |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 816 | 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] | 817 | part of the instance. Note that it is possible (though not recommended!) |
| 818 | for the \method{__del__()} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 819 | method to postpone destruction of the instance by creating a new |
| 820 | reference to it. It may then be called at a later time when this new |
| 821 | reference is deleted. It is not guaranteed that |
| 822 | \method{__del__()} methods are called for objects that still exist when |
| 823 | the interpreter exits. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 824 | \ttindex{__del__} |
| 825 | \stindex{del} |
| 826 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 827 | \strong{Programmer's note:} ``\code{del x}'' doesn't directly call |
| 828 | \code{x.__del__()} --- the former decrements the reference count for |
| 829 | \code{x} by one, and the latter is only called when its reference |
| 830 | count reaches zero. Some common situations that may prevent the |
| 831 | reference count of an object to go to zero include: circular |
| 832 | references between objects (e.g., a doubly-linked list or a tree data |
| 833 | structure with parent and child pointers); a reference to the object |
| 834 | on the stack frame of a function that caught an exception (the |
| 835 | traceback stored in \code{sys.exc_traceback} keeps the stack frame |
| 836 | alive); or a reference to the object on the stack frame that raised an |
| 837 | unhandled exception in interactive mode (the traceback stored in |
| 838 | \code{sys.last_traceback} keeps the stack frame alive). The first |
| 839 | situation can only be remedied by explicitly breaking the cycles; the |
| 840 | latter two situations can be resolved by storing None in |
| 841 | \code{sys.exc_traceback} or \code{sys.last_traceback}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 842 | |
| 843 | \strong{Warning:} due to the precarious circumstances under which |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 844 | \code{__del__} methods are invoked, exceptions that occur during their |
| 845 | execution are ignored, and a warning is printed to \code{sys.stderr} |
| 846 | instead. Also, when \code{__del__} is invoked is response to a module |
| 847 | being deleted (e.g., when execution of the program is done), other |
| 848 | globals referenced by the \code{__del__} method may already have been |
| 849 | deleted. For this reason, \code{__del__} methods should do the |
| 850 | absolute minimum needed to maintain external invariants. Python 1.5 |
| 851 | guarantees that globals whose name begins with a single underscore are |
| 852 | deleted from their module before other globals are deleted; if no |
| 853 | other references to such globals exist, this may help in assuring that |
| 854 | imported modules are still available at the time when the |
| 855 | \code{__del__} method is called. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 856 | |
| 857 | \item[{\tt __repr__(self)}] |
| 858 | Called by the \function{repr()} built-in function and by string conversions |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 859 | (reverse quotes) to compute the ``official'' string representation of |
| 860 | an object. This should normally look like a valid Python expression |
| 861 | that can be used to recreate an object with the same value. |
| 862 | This differs from \code{__repr__} in that it doesn't have to look like |
| 863 | a valid Python expression: a more convenient or concise representation |
| 864 | may be used instead. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 865 | \ttindex{__repr__} |
| 866 | \bifuncindex{repr} |
| 867 | \indexii{string}{conversion} |
| 868 | \indexii{reverse}{quotes} |
| 869 | \indexii{backward}{quotes} |
| 870 | \index{back-quotes} |
| 871 | |
| 872 | \item[{\tt __str__(self)}] |
| 873 | Called by the \function{str()} built-in function and by the \keyword{print} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 874 | statement to compute the ``informal'' string representation of an object. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 875 | \ttindex{__str__} |
| 876 | \bifuncindex{str} |
| 877 | \stindex{print} |
| 878 | |
| 879 | \item[{\tt __cmp__(self, other)}] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 880 | Called by all comparison operations. Should return a negative integer if |
| 881 | \code{self < other}, zero if \code{self == other}, a positive integer if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 882 | \code{self > other}. If no \method{__cmp__()} operation is defined, class |
| 883 | instances are compared by object identity (``address''). |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 884 | (Note: the restriction that exceptions are not propagated by |
| 885 | \code{__cmp__} has been removed in Python 1.5.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 886 | \ttindex{__cmp__} |
| 887 | \bifuncindex{cmp} |
| 888 | \index{comparisons} |
| 889 | |
| 890 | \item[{\tt __hash__(self)}] |
| 891 | Called for the key object for dictionary operations, |
| 892 | and by the built-in function |
| 893 | \function{hash()}\bifuncindex{hash}. Should return a 32-bit integer |
| 894 | usable as a hash value |
| 895 | for dictionary operations. The only required property is that objects |
| 896 | 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] | 897 | mix together (e.g., using exclusive or) the hash values for the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 898 | components of the object that also play a part in comparison of |
| 899 | objects. If a class does not define a \method{__cmp__()} method it should |
| 900 | not define a \method{__hash__()} operation either; if it defines |
| 901 | \method{__cmp__()} but not \method{__hash__()} its instances will not be |
| 902 | usable as dictionary keys. If a class defines mutable objects and |
| 903 | implements a \method{__cmp__()} method it should not implement |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 904 | \method{__hash__()}, since the dictionary implementation requires that |
| 905 | a key's hash value is immutable (if the object's hash value changes, it |
| 906 | will be in the wrong hash bucket). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 907 | \obindex{dictionary} |
| 908 | \ttindex{__cmp__} |
| 909 | \ttindex{__hash__} |
| 910 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 911 | \item[__nonzero__(self)] |
| 912 | Called to implement truth value testing; should return 0 or 1. When |
| 913 | this method is not defined, \code{__len__} is called, if it is defined |
| 914 | (see below). If a class defines neither \code{__len__} nor |
| 915 | \code{__nonzero__}, all its instances are considered true. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 916 | |
| 917 | \end{description} |
| 918 | |
| 919 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 920 | \subsection{Customizing attribute access\label{attribute-access}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 921 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 922 | The following methods can be defined to customize the meaning of |
| 923 | attribute access (use of, assignment to, or deletion of \code{x.name}) |
| 924 | for class instances. |
| 925 | For performance reasons, these methods are cached in the class object |
| 926 | at class definition time; therefore, they cannot be changed after the |
| 927 | class definition is executed. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 928 | |
| 929 | \begin{description} |
| 930 | |
| 931 | \item[{\tt __getattr__(self, name)}] |
| 932 | Called when an attribute lookup has not found the attribute in the |
| 933 | usual places (i.e. it is not an instance attribute nor is it found in |
| 934 | 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] | 935 | This method should return the (computed) attribute value or raise an |
| 936 | \code{AttributeError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 937 | \ttindex{__getattr__} |
| 938 | |
| 939 | Note that if the attribute is found through the normal mechanism, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 940 | \code{__getattr__} is not called. (This is an intentional asymmetry between |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 941 | \code{__getattr__} and \code{__setattr__}.) |
| 942 | This is done both for efficiency reasons and because otherwise |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 943 | \code{__setattr__} would have no way to access other attributes of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 944 | instance. |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 945 | Note that at least for instance variables, you can fake |
| 946 | total control by not inserting any values in the instance |
| 947 | attribute dictionary (but instead inserting them in another object). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 948 | \ttindex{__setattr__} |
| 949 | |
| 950 | \item[{\tt __setattr__(self, name, value)}] |
| 951 | Called when an attribute assignment is attempted. This is called |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 952 | instead of the normal mechanism (i.e. store the value in the instance |
| 953 | dictionary). \code{name} is the attribute name, \code{value} is the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 954 | value to be assigned to it. |
| 955 | \ttindex{__setattr__} |
| 956 | |
| 957 | If \code{__setattr__} wants to assign to an instance attribute, it |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 958 | should not simply execute ``\code{self.\var{name} = value}'' --- this would |
| 959 | cause a recursive call to itself. Instead, it should insert the value in the |
| 960 | dictionary of instance attributes, e.g., |
| 961 | ``\code{self.__dict__[name] = value}.'' |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 962 | \ttindex{__dict__} |
| 963 | |
| 964 | \item[{\tt __delattr__(self, name)}] |
| 965 | Like \code{__setattr__} but for attribute deletion instead of |
| 966 | assignment. |
| 967 | \ttindex{__delattr__} |
| 968 | |
| 969 | \end{description} |
| 970 | |
| 971 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 972 | \subsection{Emulating callable objects\label{callable-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 973 | |
| 974 | \begin{description} |
| 975 | |
| 976 | \item[{\tt __call__(self, [args...])}] |
| 977 | Called when the instance is ``called'' as a function; if this method |
| 978 | is defined, \code{x(arg1, arg2, ...)} is a shorthand for |
| 979 | \code{x.__call__(arg1, arg2, ...)}. |
| 980 | \ttindex{__call__} |
| 981 | \indexii{call}{instance} |
| 982 | |
| 983 | \end{description} |
| 984 | |
| 985 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 986 | \subsection{Emulating sequence and mapping types\label{sequence-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 987 | |
| 988 | The following methods can be defined to emulate sequence or mapping |
| 989 | objects. The first set of methods is used either to emulate a |
| 990 | sequence or to emulate a mapping; the difference is that for a |
| 991 | sequence, the allowable keys should be the integers \var{k} for which |
| 992 | \code{0 <= \var{k} < \var{N}} where \var{N} is the length of the |
| 993 | sequence, and the method \method{__getslice__()} (see below) should be |
| 994 | defined. It is also recommended that mappings provide methods |
| 995 | \method{keys()}, \method{values()}, \method{items()}, |
| 996 | \method{has_key()}, \method{get()}, \method{clear()}, \method{copy()}, |
| 997 | and \method{update()} behaving similar to those for |
| 998 | Python's standard dictionary objects; mutable sequences should provide |
| 999 | methods \method{append()}, \method{count()}, \method{index()}, |
| 1000 | \method{insert()}, \method{pop()}, \method{remove()}, \method{reverse()} |
| 1001 | and \method{sort()}, like Python standard list objects. Finally, |
| 1002 | sequence types should implement addition (meaning concatenation) and |
| 1003 | multiplication (meaning repetition) by defining the methods |
| 1004 | \method{__add__()}, \method{__radd__()}, \method{__mul__()} and |
| 1005 | \method{__rmul__()} described below; they should not define |
| 1006 | \method{__coerce__()} or other numerical operators. |
| 1007 | \ttindex{keys} |
| 1008 | \ttindex{values} |
| 1009 | \ttindex{items} |
| 1010 | \ttindex{has_key} |
| 1011 | \ttindex{get} |
| 1012 | \ttindex{clear} |
| 1013 | \ttindex{copy} |
| 1014 | \ttindex{update} |
| 1015 | \ttindex{append} |
| 1016 | \ttindex{count} |
| 1017 | \ttindex{index} |
| 1018 | \ttindex{insert} |
| 1019 | \ttindex{pop} |
| 1020 | \ttindex{remove} |
| 1021 | \ttindex{reverse} |
| 1022 | \ttindex{sort} |
| 1023 | \ttindex{__add__} |
| 1024 | \ttindex{__radd__} |
| 1025 | \ttindex{__mul__} |
| 1026 | \ttindex{__rmul__} |
| 1027 | \ttindex{__coerce__} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1028 | |
| 1029 | \begin{description} |
| 1030 | |
| 1031 | \item[{\tt __len__(self)}] |
| 1032 | Called to implement the built-in function \function{len()}. Should return |
| 1033 | the length of the object, an integer \code{>=} 0. Also, an object |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1034 | that doesn't define a \method{__nonzero__()} method and |
| 1035 | whose \method{__len__()} method returns zero is considered to be false in a |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1036 | Boolean context. |
| 1037 | \ttindex{__len__} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1038 | \ttindex{__nonzero__} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1039 | |
| 1040 | \item[{\tt __getitem__(self, key)}] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1041 | Called to implement evaluation of \code{self[key]}. |
| 1042 | For a sequence types, the accepted keys should be integers. Note that the |
| 1043 | special interpretation of negative indices (if the class wishes to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1044 | emulate a sequence type) is up to the \method{__getitem__()} method. |
| 1045 | \ttindex{__getitem__} |
| 1046 | |
| 1047 | \item[{\tt __setitem__(self, key, value)}] |
| 1048 | Called to implement assignment to \code{self[key]}. Same note as for |
| 1049 | \method{__getitem__()}. |
| 1050 | \ttindex{__setitem__} |
| 1051 | |
| 1052 | \item[{\tt __delitem__(self, key)}] |
| 1053 | Called to implement deletion of \code{self[key]}. Same note as for |
| 1054 | \method{__getitem__()}. |
| 1055 | \ttindex{__delitem__} |
| 1056 | |
| 1057 | \end{description} |
| 1058 | |
| 1059 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1060 | \subsection{Additional methods for emulation of sequence types% |
| 1061 | \label{sequence-methods}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1062 | |
| 1063 | The following methods can be defined to further emulate sequence |
| 1064 | objects. Immutable sequences methods should only define |
| 1065 | \method{__getslice__()}; mutable sequences, should define all three |
| 1066 | three methods. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1067 | |
| 1068 | \begin{description} |
| 1069 | |
| 1070 | \item[{\tt __getslice__(self, i, j)}] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1071 | Called to implement evaluation of \code{self[i:j]}. The returned |
| 1072 | object should be of the same type as \code{self}. Note that missing |
| 1073 | \var{i} or \var{j} in the slice expression are replaced by zero or |
| 1074 | \code{sys.maxint}, respectively, and no further transformations on the |
| 1075 | indices is performed. The interpretation of negative indices and |
| 1076 | indices larger than the length of the sequence is up to the method. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1077 | \ttindex{__getslice__} |
| 1078 | |
| 1079 | \item[{\tt __setslice__(self, i, j, sequence)}] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1080 | Called to implement assignment to \code{self[i:j]}. Same notes for |
| 1081 | \var{i} and \var{j} as for \method{__getslice__()}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1082 | \ttindex{__setslice__} |
| 1083 | |
| 1084 | \item[{\tt __delslice__(self, i, j)}] |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1085 | Called to implement deletion of \code{self[i:j]}. Same notes for |
| 1086 | \var{i} and \var{j} as for \method{__getslice__()}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1087 | \ttindex{__delslice__} |
| 1088 | |
| 1089 | \end{description} |
| 1090 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1091 | Notice that these methods are only invoked when a single slice with a |
| 1092 | single colon is used. For slice operations involving extended slice |
| 1093 | notation, \method{__getitem__()}, \method{__setitem__()} |
| 1094 | or\method{__delitem__()} is called. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1095 | |
Fred Drake | 61c7728 | 1998-07-28 19:34:22 +0000 | [diff] [blame] | 1096 | \subsection{Emulating numeric types\label{numeric-types}} |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1097 | |
| 1098 | The following methods can be defined to emulate numeric objects. |
| 1099 | Methods corresponding to operations that are not supported by the |
| 1100 | particular kind of number implemented (e.g., bitwise operations for |
| 1101 | non-integral numbers) should be left undefined. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1102 | |
| 1103 | \begin{description} |
| 1104 | |
| 1105 | \item[{\tt __add__(self, other)}]\itemjoin |
| 1106 | \item[{\tt __sub__(self, other)}]\itemjoin |
| 1107 | \item[{\tt __mul__(self, other)}]\itemjoin |
| 1108 | \item[{\tt __div__(self, other)}]\itemjoin |
| 1109 | \item[{\tt __mod__(self, other)}]\itemjoin |
| 1110 | \item[{\tt __divmod__(self, other)}]\itemjoin |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1111 | \item[{\tt __pow__(self, other \optional{, modulo})}]\itemjoin |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1112 | \item[{\tt __lshift__(self, other)}]\itemjoin |
| 1113 | \item[{\tt __rshift__(self, other)}]\itemjoin |
| 1114 | \item[{\tt __and__(self, other)}]\itemjoin |
| 1115 | \item[{\tt __xor__(self, other)}]\itemjoin |
| 1116 | \item[{\tt __or__(self, other)}]\itembreak |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1117 | These functions are |
| 1118 | called to implement the binary arithmetic operations (\code{+}, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1119 | \code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()}, |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1120 | \code{**}, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1121 | \code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}). |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1122 | For instance, to evaluate the expression \var{x}\code{+}\var{y}, where |
| 1123 | \var{x} is an instance of a class that has an \method{__add__()} |
| 1124 | method, \code{\var{x}.__add__(\var{y})} is called. |
| 1125 | Note that \function{__pow__()} should be defined to accept an optional |
| 1126 | third argument if the ternary version of the built-in \function{pow()} |
| 1127 | function is to be supported. |
| 1128 | \ttindex{__or__} |
| 1129 | \ttindex{__xor__} |
| 1130 | \ttindex{__and__} |
| 1131 | \ttindex{__rshift__} |
| 1132 | \ttindex{__lshift__} |
| 1133 | \ttindex{__pow__} |
| 1134 | \ttindex{__divmod__} |
| 1135 | \ttindex{__mod__} |
| 1136 | \ttindex{__div__} |
| 1137 | \ttindex{__mul__} |
| 1138 | \ttindex{__sub__} |
| 1139 | \ttindex{__add__} |
| 1140 | |
| 1141 | \item[{\tt __radd__(self, other)}]\itemjoin |
| 1142 | \item[{\tt __rsub__(self, other)}]\itemjoin |
| 1143 | \item[{\tt __rmul__(self, other)}]\itemjoin |
| 1144 | \item[{\tt __rdiv__(self, other)}]\itemjoin |
| 1145 | \item[{\tt __rmod__(self, other)}]\itemjoin |
| 1146 | \item[{\tt __rdivmod__(self, other)}]\itemjoin |
| 1147 | \item[{\tt __rpow__(self, other)}]\itemjoin |
| 1148 | \item[{\tt __rlshift__(self, other)}]\itemjoin |
| 1149 | \item[{\tt __rrshift__(self, other)}]\itemjoin |
| 1150 | \item[{\tt __rand__(self, other)}]\itemjoin |
| 1151 | \item[{\tt __rxor__(self, other)}]\itemjoin |
| 1152 | \item[{\tt __ror__(self, other)}]\itembreak |
| 1153 | These functions are |
| 1154 | called to implement the binary arithmetic operations (\code{+}, |
| 1155 | \code{-}, \code{*}, \code{/}, \code{\%}, \function{divmod()}, \function{pow()}, |
| 1156 | \code{**}, |
| 1157 | \code{<<}, \code{>>}, \code{\&}, \code{\^}, \code{|}) with reversed operands. |
| 1158 | These functions are only called if the left operand does not support |
| 1159 | the corresponding operation. |
| 1160 | For instance, to evaluate the expression \var{x}\code{-}\var{y}, where |
| 1161 | \var{y} is an instance of a class that has an \method{__rsub__()} |
| 1162 | method, \code{\var{y}.__rsub__(\var{x})} is called. |
| 1163 | Note that ternary \function{pow()} will not try calling |
| 1164 | \method{__rpow__()} (the coercion rules would become too |
| 1165 | complicated). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1166 | \ttindex{__or__} |
| 1167 | \ttindex{__xor__} |
| 1168 | \ttindex{__and__} |
| 1169 | \ttindex{__rshift__} |
| 1170 | \ttindex{__lshift__} |
| 1171 | \ttindex{__pow__} |
| 1172 | \ttindex{__divmod__} |
| 1173 | \ttindex{__mod__} |
| 1174 | \ttindex{__div__} |
| 1175 | \ttindex{__mul__} |
| 1176 | \ttindex{__sub__} |
| 1177 | \ttindex{__add__} |
| 1178 | |
| 1179 | \item[{\tt __neg__(self)}]\itemjoin |
| 1180 | \item[{\tt __pos__(self)}]\itemjoin |
| 1181 | \item[{\tt __abs__(self)}]\itemjoin |
| 1182 | \item[{\tt __invert__(self)}]\itembreak |
| 1183 | Called to implement the unary arithmetic operations (\code{-}, \code{+}, |
| 1184 | \function{abs()} and \code{~}). |
| 1185 | \ttindex{__invert__} |
| 1186 | \ttindex{__abs__} |
| 1187 | \ttindex{__pos__} |
| 1188 | \ttindex{__neg__} |
| 1189 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1190 | \item[{\tt __int__(self)}]\itemjoin |
| 1191 | \item[{\tt __long__(self)}]\itemjoin |
| 1192 | \item[{\tt __float__(self)}]\itembreak |
| 1193 | Called to implement the built-in functions \function{int()}, \function{long()} |
| 1194 | and \function{float()}. Should return a value of the appropriate type. |
| 1195 | \ttindex{__float__} |
| 1196 | \ttindex{__long__} |
| 1197 | \ttindex{__int__} |
| 1198 | |
| 1199 | \item[{\tt __oct__(self)}]\itemjoin |
| 1200 | \item[{\tt __hex__(self)}]\itembreak |
| 1201 | Called to implement the built-in functions \function{oct()} and |
| 1202 | \function{hex()}. Should return a string value. |
| 1203 | \ttindex{__hex__} |
| 1204 | \ttindex{__oct__} |
| 1205 | |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1206 | \item[{\tt __coerce__(self, other)}] |
| 1207 | Called to implement ``mixed-mode'' numeric arithmetic. Should either |
| 1208 | return a 2-tuple containing \code{self} and \code{other} converted to |
| 1209 | a common numeric type, or \code{None} if conversion is possible. When |
| 1210 | the common type would be the type of \code{other}, it is sufficient to |
| 1211 | return \code{None}, since the interpreter will also ask the other |
| 1212 | object to attempt a coercion (but sometimes, if the implementation of |
| 1213 | the other type cannot be changed, it is useful to do the conversion to |
| 1214 | the other type here). |
| 1215 | \ttindex{__coerce__} |
| 1216 | |
| 1217 | \strong{Coercion rules}: to evaluate \var{x} \var{op} \var{y}, the |
| 1218 | following steps are taken (where \method{__op__()} and |
| 1219 | \method{__rop__()} are the method names corresponding to \var{op}, |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 1220 | e.g., if var{op} is `\code{+}', \method{__add__()} and |
Guido van Rossum | 83b2f8a | 1998-07-23 17:12:46 +0000 | [diff] [blame] | 1221 | \method{__radd__()} are used). If an exception occurs at any point, |
| 1222 | the evaluation is abandoned and exception handling takes over. |
| 1223 | |
| 1224 | \begin{itemize} |
| 1225 | |
| 1226 | \item[0.] If \var{x} is a string object and op is the modulo operator (\%), |
| 1227 | the string formatting operation is invoked and the remaining steps are |
| 1228 | skipped. |
| 1229 | |
| 1230 | \item[1.] If \var{x} is a class instance: |
| 1231 | |
| 1232 | \begin{itemize} |
| 1233 | |
| 1234 | \item[1a.] If \var{x} has a \method{__coerce__()} method: |
| 1235 | replace \var{x} and \var{y} with the 2-tuple returned by |
| 1236 | \code{\var{x}.__coerce__(\var{y})}; skip to step 2 if the |
| 1237 | coercion returns \code{None}. |
| 1238 | |
| 1239 | \item[1b.] If neither \var{x} nor \var{y} is a class instance |
| 1240 | after coercion, go to step 3. |
| 1241 | |
| 1242 | \item[1c.] If \var{x} has a method \method{__op__()}, return |
| 1243 | \code{\var{x}.__op__(\var{y})}; otherwise, restore \var{x} and |
| 1244 | \var{y} to their value before step 1a. |
| 1245 | |
| 1246 | \end{itemize} |
| 1247 | |
| 1248 | \item[2.] If \var{y} is a class instance: |
| 1249 | |
| 1250 | \begin{itemize} |
| 1251 | |
| 1252 | \item[2a.] If \var{y} has a \method{__coerce__()} method: |
| 1253 | replace \var{y} and \var{x} with the 2-tuple returned by |
| 1254 | \code{\var{y}.__coerce__(\var{x})}; skip to step 3 if the |
| 1255 | coercion returns \code{None}. |
| 1256 | |
| 1257 | \item[2b.] If neither \var{x} nor \var{y} is a class instance |
| 1258 | after coercion, go to step 3. |
| 1259 | |
| 1260 | \item[2b.] If \var{y} has a method \method{__rop__()}, return |
| 1261 | \code{\var{y}.__rop__(\var{x})}; otherwise, restore \var{x} |
| 1262 | and \var{y} to their value before step 2a. |
| 1263 | |
| 1264 | \end{itemize} |
| 1265 | |
| 1266 | \item[3.] We only get here if neither \var{x} nor \var{y} is a class |
| 1267 | instance. |
| 1268 | |
| 1269 | \begin{itemize} |
| 1270 | |
| 1271 | \item[3a.] If op is `\code{+}' and \var{x} is a sequence, |
| 1272 | sequence concatenation is invoked. |
| 1273 | |
| 1274 | \item[3b.] If op is `\code{*}' and one operand is a sequence |
| 1275 | and the other an integer, sequence repetition is invoked. |
| 1276 | |
| 1277 | \item[3c.] Otherwise, both operands must be numbers; they are |
| 1278 | coerced to a common type if possible, and the numeric |
| 1279 | operation is invoked for that type. |
| 1280 | |
| 1281 | \end{itemize} |
| 1282 | |
| 1283 | \end{itemize} |
| 1284 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 1285 | \end{description} |