Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | .. _datamodel: |
| 3 | |
| 4 | ********** |
| 5 | Data model |
| 6 | ********** |
| 7 | |
| 8 | |
| 9 | .. _objects: |
| 10 | |
| 11 | Objects, values and types |
| 12 | ========================= |
| 13 | |
| 14 | .. index:: |
| 15 | single: object |
| 16 | single: data |
| 17 | |
| 18 | :dfn:`Objects` are Python's abstraction for data. All data in a Python program |
| 19 | is represented by objects or by relations between objects. (In a sense, and in |
| 20 | conformance to Von Neumann's model of a "stored program computer," code is also |
| 21 | represented by objects.) |
| 22 | |
| 23 | .. index:: |
| 24 | builtin: id |
| 25 | builtin: type |
| 26 | single: identity of an object |
| 27 | single: value of an object |
| 28 | single: type of an object |
| 29 | single: mutable object |
| 30 | single: immutable object |
| 31 | |
| 32 | Every object has an identity, a type and a value. An object's *identity* never |
| 33 | changes once it has been created; you may think of it as the object's address in |
| 34 | memory. The ':keyword:`is`' operator compares the identity of two objects; the |
| 35 | :func:`id` function returns an integer representing its identity (currently |
| 36 | implemented as its address). An object's :dfn:`type` is also unchangeable. [#]_ |
| 37 | An object's type determines the operations that the object supports (e.g., "does |
| 38 | it have a length?") and also defines the possible values for objects of that |
| 39 | type. The :func:`type` function returns an object's type (which is an object |
| 40 | itself). The *value* of some objects can change. Objects whose value can |
| 41 | change are said to be *mutable*; objects whose value is unchangeable once they |
| 42 | are created are called *immutable*. (The value of an immutable container object |
| 43 | that contains a reference to a mutable object can change when the latter's value |
| 44 | is changed; however the container is still considered immutable, because the |
| 45 | collection of objects it contains cannot be changed. So, immutability is not |
| 46 | strictly the same as having an unchangeable value, it is more subtle.) An |
| 47 | object's mutability is determined by its type; for instance, numbers, strings |
| 48 | and tuples are immutable, while dictionaries and lists are mutable. |
| 49 | |
| 50 | .. index:: |
| 51 | single: garbage collection |
| 52 | single: reference counting |
| 53 | single: unreachable object |
| 54 | |
| 55 | Objects are never explicitly destroyed; however, when they become unreachable |
| 56 | they may be garbage-collected. An implementation is allowed to postpone garbage |
| 57 | collection or omit it altogether --- it is a matter of implementation quality |
| 58 | how garbage collection is implemented, as long as no objects are collected that |
| 59 | are still reachable. (Implementation note: the current implementation uses a |
| 60 | reference-counting scheme with (optional) delayed detection of cyclically linked |
| 61 | garbage, which collects most objects as soon as they become unreachable, but is |
| 62 | not guaranteed to collect garbage containing circular references. See the |
| 63 | documentation of the :mod:`gc` module for information on controlling the |
| 64 | collection of cyclic garbage.) |
| 65 | |
| 66 | Note that the use of the implementation's tracing or debugging facilities may |
| 67 | keep objects alive that would normally be collectable. Also note that catching |
| 68 | an exception with a ':keyword:`try`...\ :keyword:`except`' statement may keep |
| 69 | objects alive. |
| 70 | |
| 71 | Some objects contain references to "external" resources such as open files or |
| 72 | windows. It is understood that these resources are freed when the object is |
| 73 | garbage-collected, but since garbage collection is not guaranteed to happen, |
| 74 | such objects also provide an explicit way to release the external resource, |
| 75 | usually a :meth:`close` method. Programs are strongly recommended to explicitly |
| 76 | close such objects. The ':keyword:`try`...\ :keyword:`finally`' statement |
| 77 | provides a convenient way to do this. |
| 78 | |
| 79 | .. index:: single: container |
| 80 | |
| 81 | Some objects contain references to other objects; these are called *containers*. |
| 82 | Examples of containers are tuples, lists and dictionaries. The references are |
| 83 | part of a container's value. In most cases, when we talk about the value of a |
| 84 | container, we imply the values, not the identities of the contained objects; |
| 85 | however, when we talk about the mutability of a container, only the identities |
| 86 | of the immediately contained objects are implied. So, if an immutable container |
| 87 | (like a tuple) contains a reference to a mutable object, its value changes if |
| 88 | that mutable object is changed. |
| 89 | |
| 90 | Types affect almost all aspects of object behavior. Even the importance of |
| 91 | object identity is affected in some sense: for immutable types, operations that |
| 92 | compute new values may actually return a reference to any existing object with |
| 93 | the same type and value, while for mutable objects this is not allowed. E.g., |
| 94 | after ``a = 1; b = 1``, ``a`` and ``b`` may or may not refer to the same object |
| 95 | with the value one, depending on the implementation, but after ``c = []; d = |
| 96 | []``, ``c`` and ``d`` are guaranteed to refer to two different, unique, newly |
| 97 | created empty lists. (Note that ``c = d = []`` assigns the same object to both |
| 98 | ``c`` and ``d``.) |
| 99 | |
| 100 | |
| 101 | .. _types: |
| 102 | |
| 103 | The standard type hierarchy |
| 104 | =========================== |
| 105 | |
| 106 | .. index:: |
| 107 | single: type |
| 108 | pair: data; type |
| 109 | pair: type; hierarchy |
| 110 | pair: extension; module |
| 111 | pair: C; language |
| 112 | |
| 113 | Below is a list of the types that are built into Python. Extension modules |
| 114 | (written in C, Java, or other languages, depending on the implementation) can |
| 115 | define additional types. Future versions of Python may add types to the type |
| 116 | hierarchy (e.g., rational numbers, efficiently stored arrays of integers, etc.). |
| 117 | |
| 118 | .. index:: |
| 119 | single: attribute |
| 120 | pair: special; attribute |
| 121 | triple: generic; special; attribute |
| 122 | |
| 123 | Some of the type descriptions below contain a paragraph listing 'special |
| 124 | attributes.' These are attributes that provide access to the implementation and |
| 125 | are not intended for general use. Their definition may change in the future. |
| 126 | |
| 127 | None |
| 128 | .. index:: object: None |
| 129 | |
| 130 | This type has a single value. There is a single object with this value. This |
| 131 | object is accessed through the built-in name ``None``. It is used to signify the |
| 132 | absence of a value in many situations, e.g., it is returned from functions that |
| 133 | don't explicitly return anything. Its truth value is false. |
| 134 | |
| 135 | NotImplemented |
| 136 | .. index:: object: NotImplemented |
| 137 | |
| 138 | This type has a single value. There is a single object with this value. This |
| 139 | object is accessed through the built-in name ``NotImplemented``. Numeric methods |
| 140 | and rich comparison methods may return this value if they do not implement the |
| 141 | operation for the operands provided. (The interpreter will then try the |
| 142 | reflected operation, or some other fallback, depending on the operator.) Its |
| 143 | truth value is true. |
| 144 | |
| 145 | Ellipsis |
| 146 | .. index:: object: Ellipsis |
| 147 | |
| 148 | This type has a single value. There is a single object with this value. This |
| 149 | object is accessed through the built-in name ``Ellipsis``. It is used to |
| 150 | indicate the presence of the ``...`` syntax in a slice. Its truth value is |
| 151 | true. |
| 152 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 153 | :class:`numbers.Number` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 154 | .. index:: object: numeric |
| 155 | |
| 156 | These are created by numeric literals and returned as results by arithmetic |
| 157 | operators and arithmetic built-in functions. Numeric objects are immutable; |
| 158 | once created their value never changes. Python numbers are of course strongly |
| 159 | related to mathematical numbers, but subject to the limitations of numerical |
| 160 | representation in computers. |
| 161 | |
| 162 | Python distinguishes between integers, floating point numbers, and complex |
| 163 | numbers: |
| 164 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 165 | :class:`numbers.Integral` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 166 | .. index:: object: integer |
| 167 | |
| 168 | These represent elements from the mathematical set of integers (positive and |
| 169 | negative). |
| 170 | |
| 171 | There are three types of integers: |
| 172 | |
| 173 | Plain integers |
| 174 | .. index:: |
| 175 | object: plain integer |
| 176 | single: OverflowError (built-in exception) |
| 177 | |
Georg Brandl | e9135ba | 2008-05-11 10:55:59 +0000 | [diff] [blame] | 178 | These represent numbers in the range -2147483648 through 2147483647. |
| 179 | (The range may be larger on machines with a larger natural word size, |
| 180 | but not smaller.) When the result of an operation would fall outside |
| 181 | this range, the result is normally returned as a long integer (in some |
| 182 | cases, the exception :exc:`OverflowError` is raised instead). For the |
| 183 | purpose of shift and mask operations, integers are assumed to have a |
| 184 | binary, 2's complement notation using 32 or more bits, and hiding no |
| 185 | bits from the user (i.e., all 4294967296 different bit patterns |
| 186 | correspond to different values). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 187 | |
| 188 | Long integers |
| 189 | .. index:: object: long integer |
| 190 | |
Georg Brandl | e9135ba | 2008-05-11 10:55:59 +0000 | [diff] [blame] | 191 | These represent numbers in an unlimited range, subject to available |
| 192 | (virtual) memory only. For the purpose of shift and mask operations, a |
| 193 | binary representation is assumed, and negative numbers are represented |
| 194 | in a variant of 2's complement which gives the illusion of an infinite |
| 195 | string of sign bits extending to the left. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 196 | |
| 197 | Booleans |
| 198 | .. index:: |
| 199 | object: Boolean |
| 200 | single: False |
| 201 | single: True |
| 202 | |
Georg Brandl | e9135ba | 2008-05-11 10:55:59 +0000 | [diff] [blame] | 203 | These represent the truth values False and True. The two objects |
| 204 | representing the values False and True are the only Boolean objects. |
| 205 | The Boolean type is a subtype of plain integers, and Boolean values |
| 206 | behave like the values 0 and 1, respectively, in almost all contexts, |
| 207 | the exception being that when converted to a string, the strings |
| 208 | ``"False"`` or ``"True"`` are returned, respectively. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 209 | |
| 210 | .. index:: pair: integer; representation |
| 211 | |
Georg Brandl | e9135ba | 2008-05-11 10:55:59 +0000 | [diff] [blame] | 212 | The rules for integer representation are intended to give the most |
| 213 | meaningful interpretation of shift and mask operations involving negative |
| 214 | integers and the least surprises when switching between the plain and long |
| 215 | integer domains. Any operation, if it yields a result in the plain |
| 216 | integer domain, will yield the same result in the long integer domain or |
| 217 | when using mixed operands. The switch between domains is transparent to |
| 218 | the programmer. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 219 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 220 | :class:`numbers.Real` (:class:`float`) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 221 | .. index:: |
| 222 | object: floating point |
| 223 | pair: floating point; number |
| 224 | pair: C; language |
| 225 | pair: Java; language |
| 226 | |
| 227 | These represent machine-level double precision floating point numbers. You are |
| 228 | at the mercy of the underlying machine architecture (and C or Java |
| 229 | implementation) for the accepted range and handling of overflow. Python does not |
| 230 | support single-precision floating point numbers; the savings in processor and |
| 231 | memory usage that are usually the reason for using these is dwarfed by the |
| 232 | overhead of using objects in Python, so there is no reason to complicate the |
| 233 | language with two kinds of floating point numbers. |
| 234 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 235 | :class:`numbers.Complex` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 236 | .. index:: |
| 237 | object: complex |
| 238 | pair: complex; number |
| 239 | |
| 240 | These represent complex numbers as a pair of machine-level double precision |
| 241 | floating point numbers. The same caveats apply as for floating point numbers. |
| 242 | The real and imaginary parts of a complex number ``z`` can be retrieved through |
| 243 | the read-only attributes ``z.real`` and ``z.imag``. |
| 244 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 245 | Sequences |
| 246 | .. index:: |
| 247 | builtin: len |
| 248 | object: sequence |
| 249 | single: index operation |
| 250 | single: item selection |
| 251 | single: subscription |
| 252 | |
| 253 | These represent finite ordered sets indexed by non-negative numbers. The |
| 254 | built-in function :func:`len` returns the number of items of a sequence. When |
| 255 | the length of a sequence is *n*, the index set contains the numbers 0, 1, |
| 256 | ..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. |
| 257 | |
| 258 | .. index:: single: slicing |
| 259 | |
| 260 | Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such |
| 261 | that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a |
| 262 | sequence of the same type. This implies that the index set is renumbered so |
| 263 | that it starts at 0. |
| 264 | |
| 265 | .. index:: single: extended slicing |
| 266 | |
| 267 | Some sequences also support "extended slicing" with a third "step" parameter: |
| 268 | ``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n* |
| 269 | ``>=`` ``0`` and *i* ``<=`` *x* ``<`` *j*. |
| 270 | |
| 271 | Sequences are distinguished according to their mutability: |
| 272 | |
| 273 | Immutable sequences |
| 274 | .. index:: |
| 275 | object: immutable sequence |
| 276 | object: immutable |
| 277 | |
| 278 | An object of an immutable sequence type cannot change once it is created. (If |
| 279 | the object contains references to other objects, these other objects may be |
| 280 | mutable and may be changed; however, the collection of objects directly |
| 281 | referenced by an immutable object cannot change.) |
| 282 | |
| 283 | The following types are immutable sequences: |
| 284 | |
| 285 | Strings |
| 286 | .. index:: |
| 287 | builtin: chr |
| 288 | builtin: ord |
| 289 | object: string |
| 290 | single: character |
| 291 | single: byte |
| 292 | single: ASCII@ASCII |
| 293 | |
| 294 | The items of a string are characters. There is no separate character type; a |
| 295 | character is represented by a string of one item. Characters represent (at |
| 296 | least) 8-bit bytes. The built-in functions :func:`chr` and :func:`ord` convert |
| 297 | between characters and nonnegative integers representing the byte values. Bytes |
| 298 | with the values 0-127 usually represent the corresponding ASCII values, but the |
| 299 | interpretation of values is up to the program. The string data type is also |
| 300 | used to represent arrays of bytes, e.g., to hold data read from a file. |
| 301 | |
| 302 | .. index:: |
| 303 | single: ASCII@ASCII |
| 304 | single: EBCDIC |
| 305 | single: character set |
| 306 | pair: string; comparison |
| 307 | builtin: chr |
| 308 | builtin: ord |
| 309 | |
| 310 | (On systems whose native character set is not ASCII, strings may use EBCDIC in |
| 311 | their internal representation, provided the functions :func:`chr` and |
| 312 | :func:`ord` implement a mapping between ASCII and EBCDIC, and string comparison |
| 313 | preserves the ASCII order. Or perhaps someone can propose a better rule?) |
| 314 | |
| 315 | Unicode |
| 316 | .. index:: |
| 317 | builtin: unichr |
| 318 | builtin: ord |
| 319 | builtin: unicode |
| 320 | object: unicode |
| 321 | single: character |
| 322 | single: integer |
| 323 | single: Unicode |
| 324 | |
| 325 | The items of a Unicode object are Unicode code units. A Unicode code unit is |
| 326 | represented by a Unicode object of one item and can hold either a 16-bit or |
| 327 | 32-bit value representing a Unicode ordinal (the maximum value for the ordinal |
| 328 | is given in ``sys.maxunicode``, and depends on how Python is configured at |
| 329 | compile time). Surrogate pairs may be present in the Unicode object, and will |
| 330 | be reported as two separate items. The built-in functions :func:`unichr` and |
| 331 | :func:`ord` convert between code units and nonnegative integers representing the |
| 332 | Unicode ordinals as defined in the Unicode Standard 3.0. Conversion from and to |
| 333 | other encodings are possible through the Unicode method :meth:`encode` and the |
| 334 | built-in function :func:`unicode`. |
| 335 | |
| 336 | Tuples |
| 337 | .. index:: |
| 338 | object: tuple |
| 339 | pair: singleton; tuple |
| 340 | pair: empty; tuple |
| 341 | |
| 342 | The items of a tuple are arbitrary Python objects. Tuples of two or more items |
| 343 | are formed by comma-separated lists of expressions. A tuple of one item (a |
| 344 | 'singleton') can be formed by affixing a comma to an expression (an expression |
| 345 | by itself does not create a tuple, since parentheses must be usable for grouping |
| 346 | of expressions). An empty tuple can be formed by an empty pair of parentheses. |
| 347 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 348 | Mutable sequences |
| 349 | .. index:: |
| 350 | object: mutable sequence |
| 351 | object: mutable |
| 352 | pair: assignment; statement |
| 353 | single: delete |
| 354 | statement: del |
| 355 | single: subscription |
| 356 | single: slicing |
| 357 | |
| 358 | Mutable sequences can be changed after they are created. The subscription and |
| 359 | slicing notations can be used as the target of assignment and :keyword:`del` |
| 360 | (delete) statements. |
| 361 | |
Benjamin Peterson | b746448 | 2009-01-18 01:28:46 +0000 | [diff] [blame] | 362 | There are currently two intrinsic mutable sequence types: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 363 | |
| 364 | Lists |
| 365 | .. index:: object: list |
| 366 | |
| 367 | The items of a list are arbitrary Python objects. Lists are formed by placing a |
| 368 | comma-separated list of expressions in square brackets. (Note that there are no |
| 369 | special cases needed to form lists of length 0 or 1.) |
| 370 | |
Benjamin Peterson | f1a4069 | 2009-01-18 01:28:09 +0000 | [diff] [blame] | 371 | Byte Arrays |
| 372 | .. index:: bytearray |
| 373 | |
| 374 | A bytearray object is a mutable array. They are created by the built-in |
| 375 | :func:`bytearray` constructor. Aside from being mutable (and hence |
| 376 | unhashable), byte arrays otherwise provide the same interface and |
| 377 | functionality as immutable bytes objects. |
| 378 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 379 | .. index:: module: array |
| 380 | |
| 381 | The extension module :mod:`array` provides an additional example of a mutable |
| 382 | sequence type. |
| 383 | |
Benjamin Peterson | f1a4069 | 2009-01-18 01:28:09 +0000 | [diff] [blame] | 384 | Set types../ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 385 | .. index:: |
| 386 | builtin: len |
| 387 | object: set type |
| 388 | |
| 389 | These represent unordered, finite sets of unique, immutable objects. As such, |
| 390 | they cannot be indexed by any subscript. However, they can be iterated over, and |
| 391 | the built-in function :func:`len` returns the number of items in a set. Common |
| 392 | uses for sets are fast membership testing, removing duplicates from a sequence, |
| 393 | and computing mathematical operations such as intersection, union, difference, |
| 394 | and symmetric difference. |
| 395 | |
| 396 | For set elements, the same immutability rules apply as for dictionary keys. Note |
| 397 | that numeric types obey the normal rules for numeric comparison: if two numbers |
| 398 | compare equal (e.g., ``1`` and ``1.0``), only one of them can be contained in a |
| 399 | set. |
| 400 | |
| 401 | There are currently two intrinsic set types: |
| 402 | |
| 403 | Sets |
| 404 | .. index:: object: set |
| 405 | |
| 406 | These represent a mutable set. They are created by the built-in :func:`set` |
| 407 | constructor and can be modified afterwards by several methods, such as |
| 408 | :meth:`add`. |
| 409 | |
| 410 | Frozen sets |
| 411 | .. index:: object: frozenset |
| 412 | |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 413 | These represent an immutable set. They are created by the built-in |
| 414 | :func:`frozenset` constructor. As a frozenset is immutable and |
| 415 | :term:`hashable`, it can be used again as an element of another set, or as |
| 416 | a dictionary key. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 417 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 418 | Mappings |
| 419 | .. index:: |
| 420 | builtin: len |
| 421 | single: subscription |
| 422 | object: mapping |
| 423 | |
| 424 | These represent finite sets of objects indexed by arbitrary index sets. The |
| 425 | subscript notation ``a[k]`` selects the item indexed by ``k`` from the mapping |
| 426 | ``a``; this can be used in expressions and as the target of assignments or |
| 427 | :keyword:`del` statements. The built-in function :func:`len` returns the number |
| 428 | of items in a mapping. |
| 429 | |
| 430 | There is currently a single intrinsic mapping type: |
| 431 | |
| 432 | Dictionaries |
| 433 | .. index:: object: dictionary |
| 434 | |
| 435 | These represent finite sets of objects indexed by nearly arbitrary values. The |
| 436 | only types of values not acceptable as keys are values containing lists or |
| 437 | dictionaries or other mutable types that are compared by value rather than by |
| 438 | object identity, the reason being that the efficient implementation of |
| 439 | dictionaries requires a key's hash value to remain constant. Numeric types used |
| 440 | for keys obey the normal rules for numeric comparison: if two numbers compare |
| 441 | equal (e.g., ``1`` and ``1.0``) then they can be used interchangeably to index |
| 442 | the same dictionary entry. |
| 443 | |
| 444 | Dictionaries are mutable; they can be created by the ``{...}`` notation (see |
| 445 | section :ref:`dict`). |
| 446 | |
| 447 | .. index:: |
| 448 | module: dbm |
| 449 | module: gdbm |
| 450 | module: bsddb |
| 451 | |
| 452 | The extension modules :mod:`dbm`, :mod:`gdbm`, and :mod:`bsddb` provide |
| 453 | additional examples of mapping types. |
| 454 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 455 | Callable types |
| 456 | .. index:: |
| 457 | object: callable |
| 458 | pair: function; call |
| 459 | single: invocation |
| 460 | pair: function; argument |
| 461 | |
| 462 | These are the types to which the function call operation (see section |
| 463 | :ref:`calls`) can be applied: |
| 464 | |
| 465 | User-defined functions |
| 466 | .. index:: |
| 467 | pair: user-defined; function |
| 468 | object: function |
| 469 | object: user-defined function |
| 470 | |
| 471 | A user-defined function object is created by a function definition (see |
| 472 | section :ref:`function`). It should be called with an argument list |
| 473 | containing the same number of items as the function's formal parameter |
| 474 | list. |
| 475 | |
| 476 | Special attributes: |
| 477 | |
| 478 | +-----------------------+-------------------------------+-----------+ |
| 479 | | Attribute | Meaning | | |
| 480 | +=======================+===============================+===========+ |
| 481 | | :attr:`func_doc` | The function's documentation | Writable | |
| 482 | | | string, or ``None`` if | | |
| 483 | | | unavailable | | |
| 484 | +-----------------------+-------------------------------+-----------+ |
| 485 | | :attr:`__doc__` | Another way of spelling | Writable | |
| 486 | | | :attr:`func_doc` | | |
| 487 | +-----------------------+-------------------------------+-----------+ |
| 488 | | :attr:`func_name` | The function's name | Writable | |
| 489 | +-----------------------+-------------------------------+-----------+ |
| 490 | | :attr:`__name__` | Another way of spelling | Writable | |
| 491 | | | :attr:`func_name` | | |
| 492 | +-----------------------+-------------------------------+-----------+ |
| 493 | | :attr:`__module__` | The name of the module the | Writable | |
| 494 | | | function was defined in, or | | |
| 495 | | | ``None`` if unavailable. | | |
| 496 | +-----------------------+-------------------------------+-----------+ |
| 497 | | :attr:`func_defaults` | A tuple containing default | Writable | |
| 498 | | | argument values for those | | |
| 499 | | | arguments that have defaults, | | |
| 500 | | | or ``None`` if no arguments | | |
| 501 | | | have a default value | | |
| 502 | +-----------------------+-------------------------------+-----------+ |
| 503 | | :attr:`func_code` | The code object representing | Writable | |
| 504 | | | the compiled function body. | | |
| 505 | +-----------------------+-------------------------------+-----------+ |
| 506 | | :attr:`func_globals` | A reference to the dictionary | Read-only | |
| 507 | | | that holds the function's | | |
| 508 | | | global variables --- the | | |
| 509 | | | global namespace of the | | |
| 510 | | | module in which the function | | |
| 511 | | | was defined. | | |
| 512 | +-----------------------+-------------------------------+-----------+ |
| 513 | | :attr:`func_dict` | The namespace supporting | Writable | |
| 514 | | | arbitrary function | | |
| 515 | | | attributes. | | |
| 516 | +-----------------------+-------------------------------+-----------+ |
| 517 | | :attr:`func_closure` | ``None`` or a tuple of cells | Read-only | |
| 518 | | | that contain bindings for the | | |
| 519 | | | function's free variables. | | |
| 520 | +-----------------------+-------------------------------+-----------+ |
| 521 | |
| 522 | Most of the attributes labelled "Writable" check the type of the assigned value. |
| 523 | |
| 524 | .. versionchanged:: 2.4 |
| 525 | ``func_name`` is now writable. |
| 526 | |
| 527 | Function objects also support getting and setting arbitrary attributes, which |
| 528 | can be used, for example, to attach metadata to functions. Regular attribute |
| 529 | dot-notation is used to get and set such attributes. *Note that the current |
| 530 | implementation only supports function attributes on user-defined functions. |
| 531 | Function attributes on built-in functions may be supported in the future.* |
| 532 | |
| 533 | Additional information about a function's definition can be retrieved from its |
| 534 | code object; see the description of internal types below. |
| 535 | |
| 536 | .. index:: |
| 537 | single: func_doc (function attribute) |
| 538 | single: __doc__ (function attribute) |
| 539 | single: __name__ (function attribute) |
| 540 | single: __module__ (function attribute) |
| 541 | single: __dict__ (function attribute) |
| 542 | single: func_defaults (function attribute) |
| 543 | single: func_closure (function attribute) |
| 544 | single: func_code (function attribute) |
| 545 | single: func_globals (function attribute) |
| 546 | single: func_dict (function attribute) |
| 547 | pair: global; namespace |
| 548 | |
| 549 | User-defined methods |
| 550 | .. index:: |
| 551 | object: method |
| 552 | object: user-defined method |
| 553 | pair: user-defined; method |
| 554 | |
| 555 | A user-defined method object combines a class, a class instance (or ``None``) |
| 556 | and any callable object (normally a user-defined function). |
| 557 | |
| 558 | Special read-only attributes: :attr:`im_self` is the class instance object, |
| 559 | :attr:`im_func` is the function object; :attr:`im_class` is the class of |
| 560 | :attr:`im_self` for bound methods or the class that asked for the method for |
| 561 | unbound methods; :attr:`__doc__` is the method's documentation (same as |
| 562 | ``im_func.__doc__``); :attr:`__name__` is the method name (same as |
| 563 | ``im_func.__name__``); :attr:`__module__` is the name of the module the method |
| 564 | was defined in, or ``None`` if unavailable. |
| 565 | |
| 566 | .. versionchanged:: 2.2 |
| 567 | :attr:`im_self` used to refer to the class that defined the method. |
| 568 | |
Georg Brandl | 3fbe20c | 2008-03-21 19:20:21 +0000 | [diff] [blame] | 569 | .. versionchanged:: 2.6 |
| 570 | For 3.0 forward-compatibility, :attr:`im_func` is also available as |
| 571 | :attr:`__func__`, and :attr:`im_self` as :attr:`__self__`. |
| 572 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 573 | .. index:: |
| 574 | single: __doc__ (method attribute) |
| 575 | single: __name__ (method attribute) |
| 576 | single: __module__ (method attribute) |
| 577 | single: im_func (method attribute) |
| 578 | single: im_self (method attribute) |
| 579 | |
| 580 | Methods also support accessing (but not setting) the arbitrary function |
| 581 | attributes on the underlying function object. |
| 582 | |
| 583 | User-defined method objects may be created when getting an attribute of a class |
| 584 | (perhaps via an instance of that class), if that attribute is a user-defined |
| 585 | function object, an unbound user-defined method object, or a class method |
| 586 | object. When the attribute is a user-defined method object, a new method object |
| 587 | is only created if the class from which it is being retrieved is the same as, or |
| 588 | a derived class of, the class stored in the original method object; otherwise, |
| 589 | the original method object is used as it is. |
| 590 | |
| 591 | .. index:: |
| 592 | single: im_class (method attribute) |
| 593 | single: im_func (method attribute) |
| 594 | single: im_self (method attribute) |
| 595 | |
| 596 | When a user-defined method object is created by retrieving a user-defined |
| 597 | function object from a class, its :attr:`im_self` attribute is ``None`` |
| 598 | and the method object is said to be unbound. When one is created by |
| 599 | retrieving a user-defined function object from a class via one of its |
| 600 | instances, its :attr:`im_self` attribute is the instance, and the method |
| 601 | object is said to be bound. In either case, the new method's |
| 602 | :attr:`im_class` attribute is the class from which the retrieval takes |
| 603 | place, and its :attr:`im_func` attribute is the original function object. |
| 604 | |
| 605 | .. index:: single: im_func (method attribute) |
| 606 | |
| 607 | When a user-defined method object is created by retrieving another method object |
| 608 | from a class or instance, the behaviour is the same as for a function object, |
| 609 | except that the :attr:`im_func` attribute of the new instance is not the |
| 610 | original method object but its :attr:`im_func` attribute. |
| 611 | |
| 612 | .. index:: |
| 613 | single: im_class (method attribute) |
| 614 | single: im_func (method attribute) |
| 615 | single: im_self (method attribute) |
| 616 | |
| 617 | When a user-defined method object is created by retrieving a class method object |
| 618 | from a class or instance, its :attr:`im_self` attribute is the class itself (the |
| 619 | same as the :attr:`im_class` attribute), and its :attr:`im_func` attribute is |
| 620 | the function object underlying the class method. |
| 621 | |
| 622 | When an unbound user-defined method object is called, the underlying function |
| 623 | (:attr:`im_func`) is called, with the restriction that the first argument must |
| 624 | be an instance of the proper class (:attr:`im_class`) or of a derived class |
| 625 | thereof. |
| 626 | |
| 627 | When a bound user-defined method object is called, the underlying function |
| 628 | (:attr:`im_func`) is called, inserting the class instance (:attr:`im_self`) in |
| 629 | front of the argument list. For instance, when :class:`C` is a class which |
| 630 | contains a definition for a function :meth:`f`, and ``x`` is an instance of |
| 631 | :class:`C`, calling ``x.f(1)`` is equivalent to calling ``C.f(x, 1)``. |
| 632 | |
| 633 | When a user-defined method object is derived from a class method object, the |
| 634 | "class instance" stored in :attr:`im_self` will actually be the class itself, so |
| 635 | that calling either ``x.f(1)`` or ``C.f(1)`` is equivalent to calling ``f(C,1)`` |
| 636 | where ``f`` is the underlying function. |
| 637 | |
| 638 | Note that the transformation from function object to (unbound or bound) method |
| 639 | object happens each time the attribute is retrieved from the class or instance. |
| 640 | In some cases, a fruitful optimization is to assign the attribute to a local |
| 641 | variable and call that local variable. Also notice that this transformation only |
| 642 | happens for user-defined functions; other callable objects (and all non-callable |
| 643 | objects) are retrieved without transformation. It is also important to note |
| 644 | that user-defined functions which are attributes of a class instance are not |
| 645 | converted to bound methods; this *only* happens when the function is an |
| 646 | attribute of the class. |
| 647 | |
| 648 | Generator functions |
| 649 | .. index:: |
| 650 | single: generator; function |
| 651 | single: generator; iterator |
| 652 | |
| 653 | A function or method which uses the :keyword:`yield` statement (see section |
| 654 | :ref:`yield`) is called a :dfn:`generator |
| 655 | function`. Such a function, when called, always returns an iterator object |
| 656 | which can be used to execute the body of the function: calling the iterator's |
| 657 | :meth:`next` method will cause the function to execute until it provides a value |
| 658 | using the :keyword:`yield` statement. When the function executes a |
| 659 | :keyword:`return` statement or falls off the end, a :exc:`StopIteration` |
| 660 | exception is raised and the iterator will have reached the end of the set of |
| 661 | values to be returned. |
| 662 | |
| 663 | Built-in functions |
| 664 | .. index:: |
| 665 | object: built-in function |
| 666 | object: function |
| 667 | pair: C; language |
| 668 | |
| 669 | A built-in function object is a wrapper around a C function. Examples of |
| 670 | built-in functions are :func:`len` and :func:`math.sin` (:mod:`math` is a |
| 671 | standard built-in module). The number and type of the arguments are |
| 672 | determined by the C function. Special read-only attributes: |
| 673 | :attr:`__doc__` is the function's documentation string, or ``None`` if |
| 674 | unavailable; :attr:`__name__` is the function's name; :attr:`__self__` is |
| 675 | set to ``None`` (but see the next item); :attr:`__module__` is the name of |
| 676 | the module the function was defined in or ``None`` if unavailable. |
| 677 | |
| 678 | Built-in methods |
| 679 | .. index:: |
| 680 | object: built-in method |
| 681 | object: method |
| 682 | pair: built-in; method |
| 683 | |
| 684 | This is really a different disguise of a built-in function, this time containing |
| 685 | an object passed to the C function as an implicit extra argument. An example of |
| 686 | a built-in method is ``alist.append()``, assuming *alist* is a list object. In |
| 687 | this case, the special read-only attribute :attr:`__self__` is set to the object |
| 688 | denoted by *list*. |
| 689 | |
| 690 | Class Types |
| 691 | Class types, or "new-style classes," are callable. These objects normally act |
| 692 | as factories for new instances of themselves, but variations are possible for |
| 693 | class types that override :meth:`__new__`. The arguments of the call are passed |
| 694 | to :meth:`__new__` and, in the typical case, to :meth:`__init__` to initialize |
| 695 | the new instance. |
| 696 | |
| 697 | Classic Classes |
| 698 | .. index:: |
| 699 | single: __init__() (object method) |
| 700 | object: class |
| 701 | object: class instance |
| 702 | object: instance |
| 703 | pair: class object; call |
| 704 | |
| 705 | Class objects are described below. When a class object is called, a new class |
| 706 | instance (also described below) is created and returned. This implies a call to |
| 707 | the class's :meth:`__init__` method if it has one. Any arguments are passed on |
| 708 | to the :meth:`__init__` method. If there is no :meth:`__init__` method, the |
| 709 | class must be called without arguments. |
| 710 | |
| 711 | Class instances |
| 712 | Class instances are described below. Class instances are callable only when the |
| 713 | class has a :meth:`__call__` method; ``x(arguments)`` is a shorthand for |
| 714 | ``x.__call__(arguments)``. |
| 715 | |
| 716 | Modules |
| 717 | .. index:: |
| 718 | statement: import |
| 719 | object: module |
| 720 | |
| 721 | Modules are imported by the :keyword:`import` statement (see section |
| 722 | :ref:`import`). A module object has a |
| 723 | namespace implemented by a dictionary object (this is the dictionary referenced |
| 724 | by the func_globals attribute of functions defined in the module). Attribute |
| 725 | references are translated to lookups in this dictionary, e.g., ``m.x`` is |
| 726 | equivalent to ``m.__dict__["x"]``. A module object does not contain the code |
| 727 | object used to initialize the module (since it isn't needed once the |
| 728 | initialization is done). |
| 729 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 730 | Attribute assignment updates the module's namespace dictionary, e.g., ``m.x = |
| 731 | 1`` is equivalent to ``m.__dict__["x"] = 1``. |
| 732 | |
| 733 | .. index:: single: __dict__ (module attribute) |
| 734 | |
| 735 | Special read-only attribute: :attr:`__dict__` is the module's namespace as a |
| 736 | dictionary object. |
| 737 | |
| 738 | .. index:: |
| 739 | single: __name__ (module attribute) |
| 740 | single: __doc__ (module attribute) |
| 741 | single: __file__ (module attribute) |
| 742 | pair: module; namespace |
| 743 | |
| 744 | Predefined (writable) attributes: :attr:`__name__` is the module's name; |
| 745 | :attr:`__doc__` is the module's documentation string, or ``None`` if |
| 746 | unavailable; :attr:`__file__` is the pathname of the file from which the module |
| 747 | was loaded, if it was loaded from a file. The :attr:`__file__` attribute is not |
| 748 | present for C modules that are statically linked into the interpreter; for |
| 749 | extension modules loaded dynamically from a shared library, it is the pathname |
| 750 | of the shared library file. |
| 751 | |
| 752 | Classes |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 753 | Both class types (new-style classes) and class objects (old-style/classic |
| 754 | classes) are typically created by class definitions (see section |
| 755 | :ref:`class`). A class has a namespace implemented by a dictionary object. |
| 756 | Class attribute references are translated to lookups in this dictionary, e.g., |
| 757 | ``C.x`` is translated to ``C.__dict__["x"]`` (although for new-style classes |
| 758 | in particular there are a number of hooks which allow for other means of |
| 759 | locating attributes). When the attribute name is not found there, the |
| 760 | attribute search continues in the base classes. For old-style classes, the |
| 761 | search is depth-first, left-to-right in the order of occurrence in the base |
| 762 | class list. New-style classes use the more complex C3 method resolution |
| 763 | order which behaves correctly even in the presence of 'diamond' |
| 764 | inheritance structures where there are multiple inheritance paths |
| 765 | leading back to a common ancestor. Additional details on the C3 MRO used by |
| 766 | new-style classes can be found in the documentation accompanying the |
| 767 | 2.3 release at http://www.python.org/download/releases/2.3/mro/. |
| 768 | |
| 769 | .. XXX: Could we add that MRO doc as an appendix to the language ref? |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 770 | |
| 771 | .. index:: |
| 772 | object: class |
| 773 | object: class instance |
| 774 | object: instance |
| 775 | pair: class object; call |
| 776 | single: container |
| 777 | object: dictionary |
| 778 | pair: class; attribute |
| 779 | |
| 780 | When a class attribute reference (for class :class:`C`, say) would yield a |
| 781 | user-defined function object or an unbound user-defined method object whose |
| 782 | associated class is either :class:`C` or one of its base classes, it is |
| 783 | transformed into an unbound user-defined method object whose :attr:`im_class` |
| 784 | attribute is :class:`C`. When it would yield a class method object, it is |
| 785 | transformed into a bound user-defined method object whose :attr:`im_class` |
| 786 | and :attr:`im_self` attributes are both :class:`C`. When it would yield a |
| 787 | static method object, it is transformed into the object wrapped by the static |
| 788 | method object. See section :ref:`descriptors` for another way in which |
| 789 | attributes retrieved from a class may differ from those actually contained in |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 790 | its :attr:`__dict__` (note that only new-style classes support descriptors). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 791 | |
| 792 | .. index:: triple: class; attribute; assignment |
| 793 | |
| 794 | Class attribute assignments update the class's dictionary, never the dictionary |
| 795 | of a base class. |
| 796 | |
| 797 | .. index:: pair: class object; call |
| 798 | |
| 799 | A class object can be called (see above) to yield a class instance (see below). |
| 800 | |
| 801 | .. index:: |
| 802 | single: __name__ (class attribute) |
| 803 | single: __module__ (class attribute) |
| 804 | single: __dict__ (class attribute) |
| 805 | single: __bases__ (class attribute) |
| 806 | single: __doc__ (class attribute) |
| 807 | |
| 808 | Special attributes: :attr:`__name__` is the class name; :attr:`__module__` is |
| 809 | the module name in which the class was defined; :attr:`__dict__` is the |
| 810 | dictionary containing the class's namespace; :attr:`__bases__` is a tuple |
| 811 | (possibly empty or a singleton) containing the base classes, in the order of |
| 812 | their occurrence in the base class list; :attr:`__doc__` is the class's |
| 813 | documentation string, or None if undefined. |
| 814 | |
| 815 | Class instances |
| 816 | .. index:: |
| 817 | object: class instance |
| 818 | object: instance |
| 819 | pair: class; instance |
| 820 | pair: class instance; attribute |
| 821 | |
| 822 | A class instance is created by calling a class object (see above). A class |
| 823 | instance has a namespace implemented as a dictionary which is the first place in |
| 824 | which attribute references are searched. When an attribute is not found there, |
| 825 | and the instance's class has an attribute by that name, the search continues |
| 826 | with the class attributes. If a class attribute is found that is a user-defined |
| 827 | function object or an unbound user-defined method object whose associated class |
| 828 | is the class (call it :class:`C`) of the instance for which the attribute |
| 829 | reference was initiated or one of its bases, it is transformed into a bound |
| 830 | user-defined method object whose :attr:`im_class` attribute is :class:`C` and |
| 831 | whose :attr:`im_self` attribute is the instance. Static method and class method |
| 832 | objects are also transformed, as if they had been retrieved from class |
| 833 | :class:`C`; see above under "Classes". See section :ref:`descriptors` for |
| 834 | another way in which attributes of a class retrieved via its instances may |
| 835 | differ from the objects actually stored in the class's :attr:`__dict__`. If no |
| 836 | class attribute is found, and the object's class has a :meth:`__getattr__` |
| 837 | method, that is called to satisfy the lookup. |
| 838 | |
| 839 | .. index:: triple: class instance; attribute; assignment |
| 840 | |
| 841 | Attribute assignments and deletions update the instance's dictionary, never a |
| 842 | class's dictionary. If the class has a :meth:`__setattr__` or |
| 843 | :meth:`__delattr__` method, this is called instead of updating the instance |
| 844 | dictionary directly. |
| 845 | |
| 846 | .. index:: |
| 847 | object: numeric |
| 848 | object: sequence |
| 849 | object: mapping |
| 850 | |
| 851 | Class instances can pretend to be numbers, sequences, or mappings if they have |
| 852 | methods with certain special names. See section :ref:`specialnames`. |
| 853 | |
| 854 | .. index:: |
| 855 | single: __dict__ (instance attribute) |
| 856 | single: __class__ (instance attribute) |
| 857 | |
| 858 | Special attributes: :attr:`__dict__` is the attribute dictionary; |
| 859 | :attr:`__class__` is the instance's class. |
| 860 | |
| 861 | Files |
| 862 | .. index:: |
| 863 | object: file |
| 864 | builtin: open |
| 865 | single: popen() (in module os) |
| 866 | single: makefile() (socket method) |
| 867 | single: sys.stdin |
| 868 | single: sys.stdout |
| 869 | single: sys.stderr |
| 870 | single: stdio |
| 871 | single: stdin (in module sys) |
| 872 | single: stdout (in module sys) |
| 873 | single: stderr (in module sys) |
| 874 | |
| 875 | A file object represents an open file. File objects are created by the |
| 876 | :func:`open` built-in function, and also by :func:`os.popen`, |
| 877 | :func:`os.fdopen`, and the :meth:`makefile` method of socket objects (and |
| 878 | perhaps by other functions or methods provided by extension modules). The |
| 879 | objects ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are initialized to |
| 880 | file objects corresponding to the interpreter's standard input, output and |
| 881 | error streams. See :ref:`bltin-file-objects` for complete documentation of |
| 882 | file objects. |
| 883 | |
| 884 | Internal types |
| 885 | .. index:: |
| 886 | single: internal type |
| 887 | single: types, internal |
| 888 | |
| 889 | A few types used internally by the interpreter are exposed to the user. Their |
| 890 | definitions may change with future versions of the interpreter, but they are |
| 891 | mentioned here for completeness. |
| 892 | |
| 893 | Code objects |
| 894 | .. index:: |
| 895 | single: bytecode |
| 896 | object: code |
| 897 | |
Georg Brandl | 63fa168 | 2007-10-21 10:24:20 +0000 | [diff] [blame] | 898 | Code objects represent *byte-compiled* executable Python code, or :term:`bytecode`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 899 | The difference between a code object and a function object is that the function |
| 900 | object contains an explicit reference to the function's globals (the module in |
| 901 | which it was defined), while a code object contains no context; also the default |
| 902 | argument values are stored in the function object, not in the code object |
| 903 | (because they represent values calculated at run-time). Unlike function |
| 904 | objects, code objects are immutable and contain no references (directly or |
| 905 | indirectly) to mutable objects. |
| 906 | |
| 907 | Special read-only attributes: :attr:`co_name` gives the function name; |
| 908 | :attr:`co_argcount` is the number of positional arguments (including arguments |
| 909 | with default values); :attr:`co_nlocals` is the number of local variables used |
| 910 | by the function (including arguments); :attr:`co_varnames` is a tuple containing |
| 911 | the names of the local variables (starting with the argument names); |
| 912 | :attr:`co_cellvars` is a tuple containing the names of local variables that are |
| 913 | referenced by nested functions; :attr:`co_freevars` is a tuple containing the |
| 914 | names of free variables; :attr:`co_code` is a string representing the sequence |
| 915 | of bytecode instructions; :attr:`co_consts` is a tuple containing the literals |
| 916 | used by the bytecode; :attr:`co_names` is a tuple containing the names used by |
| 917 | the bytecode; :attr:`co_filename` is the filename from which the code was |
| 918 | compiled; :attr:`co_firstlineno` is the first line number of the function; |
Georg Brandl | 63fa168 | 2007-10-21 10:24:20 +0000 | [diff] [blame] | 919 | :attr:`co_lnotab` is a string encoding the mapping from bytecode offsets to |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 920 | line numbers (for details see the source code of the interpreter); |
| 921 | :attr:`co_stacksize` is the required stack size (including local variables); |
| 922 | :attr:`co_flags` is an integer encoding a number of flags for the interpreter. |
| 923 | |
| 924 | .. index:: |
| 925 | single: co_argcount (code object attribute) |
| 926 | single: co_code (code object attribute) |
| 927 | single: co_consts (code object attribute) |
| 928 | single: co_filename (code object attribute) |
| 929 | single: co_firstlineno (code object attribute) |
| 930 | single: co_flags (code object attribute) |
| 931 | single: co_lnotab (code object attribute) |
| 932 | single: co_name (code object attribute) |
| 933 | single: co_names (code object attribute) |
| 934 | single: co_nlocals (code object attribute) |
| 935 | single: co_stacksize (code object attribute) |
| 936 | single: co_varnames (code object attribute) |
| 937 | single: co_cellvars (code object attribute) |
| 938 | single: co_freevars (code object attribute) |
| 939 | |
| 940 | .. index:: object: generator |
| 941 | |
| 942 | The following flag bits are defined for :attr:`co_flags`: bit ``0x04`` is set if |
| 943 | the function uses the ``*arguments`` syntax to accept an arbitrary number of |
| 944 | positional arguments; bit ``0x08`` is set if the function uses the |
| 945 | ``**keywords`` syntax to accept arbitrary keyword arguments; bit ``0x20`` is set |
| 946 | if the function is a generator. |
| 947 | |
| 948 | Future feature declarations (``from __future__ import division``) also use bits |
| 949 | in :attr:`co_flags` to indicate whether a code object was compiled with a |
| 950 | particular feature enabled: bit ``0x2000`` is set if the function was compiled |
| 951 | with future division enabled; bits ``0x10`` and ``0x1000`` were used in earlier |
| 952 | versions of Python. |
| 953 | |
| 954 | Other bits in :attr:`co_flags` are reserved for internal use. |
| 955 | |
| 956 | .. index:: single: documentation string |
| 957 | |
| 958 | If a code object represents a function, the first item in :attr:`co_consts` is |
| 959 | the documentation string of the function, or ``None`` if undefined. |
| 960 | |
| 961 | Frame objects |
| 962 | .. index:: object: frame |
| 963 | |
| 964 | Frame objects represent execution frames. They may occur in traceback objects |
| 965 | (see below). |
| 966 | |
| 967 | .. index:: |
| 968 | single: f_back (frame attribute) |
| 969 | single: f_code (frame attribute) |
| 970 | single: f_globals (frame attribute) |
| 971 | single: f_locals (frame attribute) |
| 972 | single: f_lasti (frame attribute) |
| 973 | single: f_builtins (frame attribute) |
| 974 | single: f_restricted (frame attribute) |
| 975 | |
| 976 | Special read-only attributes: :attr:`f_back` is to the previous stack frame |
| 977 | (towards the caller), or ``None`` if this is the bottom stack frame; |
| 978 | :attr:`f_code` is the code object being executed in this frame; :attr:`f_locals` |
| 979 | is the dictionary used to look up local variables; :attr:`f_globals` is used for |
| 980 | global variables; :attr:`f_builtins` is used for built-in (intrinsic) names; |
| 981 | :attr:`f_restricted` is a flag indicating whether the function is executing in |
| 982 | restricted execution mode; :attr:`f_lasti` gives the precise instruction (this |
| 983 | is an index into the bytecode string of the code object). |
| 984 | |
| 985 | .. index:: |
| 986 | single: f_trace (frame attribute) |
| 987 | single: f_exc_type (frame attribute) |
| 988 | single: f_exc_value (frame attribute) |
| 989 | single: f_exc_traceback (frame attribute) |
| 990 | single: f_lineno (frame attribute) |
| 991 | |
| 992 | Special writable attributes: :attr:`f_trace`, if not ``None``, is a function |
| 993 | called at the start of each source code line (this is used by the debugger); |
| 994 | :attr:`f_exc_type`, :attr:`f_exc_value`, :attr:`f_exc_traceback` represent the |
| 995 | last exception raised in the parent frame provided another exception was ever |
| 996 | raised in the current frame (in all other cases they are None); :attr:`f_lineno` |
| 997 | is the current line number of the frame --- writing to this from within a trace |
| 998 | function jumps to the given line (only for the bottom-most frame). A debugger |
| 999 | can implement a Jump command (aka Set Next Statement) by writing to f_lineno. |
| 1000 | |
| 1001 | Traceback objects |
| 1002 | .. index:: |
| 1003 | object: traceback |
| 1004 | pair: stack; trace |
| 1005 | pair: exception; handler |
| 1006 | pair: execution; stack |
| 1007 | single: exc_info (in module sys) |
| 1008 | single: exc_traceback (in module sys) |
| 1009 | single: last_traceback (in module sys) |
| 1010 | single: sys.exc_info |
| 1011 | single: sys.exc_traceback |
| 1012 | single: sys.last_traceback |
| 1013 | |
| 1014 | Traceback objects represent a stack trace of an exception. A traceback object |
| 1015 | is created when an exception occurs. When the search for an exception handler |
| 1016 | unwinds the execution stack, at each unwound level a traceback object is |
| 1017 | inserted in front of the current traceback. When an exception handler is |
| 1018 | entered, the stack trace is made available to the program. (See section |
| 1019 | :ref:`try`.) It is accessible as ``sys.exc_traceback``, |
| 1020 | and also as the third item of the tuple returned by ``sys.exc_info()``. The |
| 1021 | latter is the preferred interface, since it works correctly when the program is |
| 1022 | using multiple threads. When the program contains no suitable handler, the stack |
| 1023 | trace is written (nicely formatted) to the standard error stream; if the |
| 1024 | interpreter is interactive, it is also made available to the user as |
| 1025 | ``sys.last_traceback``. |
| 1026 | |
| 1027 | .. index:: |
| 1028 | single: tb_next (traceback attribute) |
| 1029 | single: tb_frame (traceback attribute) |
| 1030 | single: tb_lineno (traceback attribute) |
| 1031 | single: tb_lasti (traceback attribute) |
| 1032 | statement: try |
| 1033 | |
| 1034 | Special read-only attributes: :attr:`tb_next` is the next level in the stack |
| 1035 | trace (towards the frame where the exception occurred), or ``None`` if there is |
| 1036 | no next level; :attr:`tb_frame` points to the execution frame of the current |
| 1037 | level; :attr:`tb_lineno` gives the line number where the exception occurred; |
| 1038 | :attr:`tb_lasti` indicates the precise instruction. The line number and last |
| 1039 | instruction in the traceback may differ from the line number of its frame object |
| 1040 | if the exception occurred in a :keyword:`try` statement with no matching except |
| 1041 | clause or with a finally clause. |
| 1042 | |
| 1043 | Slice objects |
| 1044 | .. index:: builtin: slice |
| 1045 | |
| 1046 | Slice objects are used to represent slices when *extended slice syntax* is used. |
| 1047 | This is a slice using two colons, or multiple slices or ellipses separated by |
| 1048 | commas, e.g., ``a[i:j:step]``, ``a[i:j, k:l]``, or ``a[..., i:j]``. They are |
| 1049 | also created by the built-in :func:`slice` function. |
| 1050 | |
| 1051 | .. index:: |
| 1052 | single: start (slice object attribute) |
| 1053 | single: stop (slice object attribute) |
| 1054 | single: step (slice object attribute) |
| 1055 | |
| 1056 | Special read-only attributes: :attr:`start` is the lower bound; :attr:`stop` is |
| 1057 | the upper bound; :attr:`step` is the step value; each is ``None`` if omitted. |
| 1058 | These attributes can have any type. |
| 1059 | |
| 1060 | Slice objects support one method: |
| 1061 | |
| 1062 | |
| 1063 | .. method:: slice.indices(self, length) |
| 1064 | |
| 1065 | This method takes a single integer argument *length* and computes information |
| 1066 | about the extended slice that the slice object would describe if applied to a |
| 1067 | sequence of *length* items. It returns a tuple of three integers; respectively |
| 1068 | these are the *start* and *stop* indices and the *step* or stride length of the |
| 1069 | slice. Missing or out-of-bounds indices are handled in a manner consistent with |
| 1070 | regular slices. |
| 1071 | |
| 1072 | .. versionadded:: 2.3 |
| 1073 | |
| 1074 | Static method objects |
| 1075 | Static method objects provide a way of defeating the transformation of function |
| 1076 | objects to method objects described above. A static method object is a wrapper |
| 1077 | around any other object, usually a user-defined method object. When a static |
| 1078 | method object is retrieved from a class or a class instance, the object actually |
| 1079 | returned is the wrapped object, which is not subject to any further |
| 1080 | transformation. Static method objects are not themselves callable, although the |
| 1081 | objects they wrap usually are. Static method objects are created by the built-in |
| 1082 | :func:`staticmethod` constructor. |
| 1083 | |
| 1084 | Class method objects |
| 1085 | A class method object, like a static method object, is a wrapper around another |
| 1086 | object that alters the way in which that object is retrieved from classes and |
| 1087 | class instances. The behaviour of class method objects upon such retrieval is |
| 1088 | described above, under "User-defined methods". Class method objects are created |
| 1089 | by the built-in :func:`classmethod` constructor. |
| 1090 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1091 | |
Georg Brandl | a739503 | 2007-10-21 12:15:05 +0000 | [diff] [blame] | 1092 | .. _newstyle: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1093 | |
| 1094 | New-style and classic classes |
| 1095 | ============================= |
| 1096 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1097 | Classes and instances come in two flavors: old-style (or classic) and new-style. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1098 | |
| 1099 | Up to Python 2.1, old-style classes were the only flavour available to the user. |
| 1100 | The concept of (old-style) class is unrelated to the concept of type: if *x* is |
| 1101 | an instance of an old-style class, then ``x.__class__`` designates the class of |
| 1102 | *x*, but ``type(x)`` is always ``<type 'instance'>``. This reflects the fact |
| 1103 | that all old-style instances, independently of their class, are implemented with |
| 1104 | a single built-in type, called ``instance``. |
| 1105 | |
| 1106 | New-style classes were introduced in Python 2.2 to unify classes and types. A |
Georg Brandl | 63cdb86 | 2008-02-03 12:29:00 +0000 | [diff] [blame] | 1107 | new-style class is neither more nor less than a user-defined type. If *x* is an |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1108 | instance of a new-style class, then ``type(x)`` is typically the same as |
| 1109 | ``x.__class__`` (although this is not guaranteed - a new-style class instance is |
| 1110 | permitted to override the value returned for ``x.__class__``). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1111 | |
| 1112 | The major motivation for introducing new-style classes is to provide a unified |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1113 | object model with a full meta-model. It also has a number of practical |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1114 | benefits, like the ability to subclass most built-in types, or the introduction |
| 1115 | of "descriptors", which enable computed properties. |
| 1116 | |
| 1117 | For compatibility reasons, classes are still old-style by default. New-style |
| 1118 | classes are created by specifying another new-style class (i.e. a type) as a |
| 1119 | parent class, or the "top-level type" :class:`object` if no other parent is |
| 1120 | needed. The behaviour of new-style classes differs from that of old-style |
| 1121 | classes in a number of important details in addition to what :func:`type` |
| 1122 | returns. Some of these changes are fundamental to the new object model, like |
| 1123 | the way special methods are invoked. Others are "fixes" that could not be |
| 1124 | implemented before for compatibility concerns, like the method resolution order |
| 1125 | in case of multiple inheritance. |
| 1126 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1127 | While this manual aims to provide comprehensive coverage of Python's class |
| 1128 | mechanics, it may still be lacking in some areas when it comes to its coverage |
| 1129 | of new-style classes. Please see http://www.python.org/doc/newstyle/ for |
| 1130 | sources of additional information. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1131 | |
| 1132 | .. index:: |
Georg Brandl | 6265833 | 2008-01-05 19:29:45 +0000 | [diff] [blame] | 1133 | single: class; new-style |
| 1134 | single: class; classic |
| 1135 | single: class; old-style |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1136 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1137 | Old-style classes are removed in Python 3.0, leaving only the semantics of |
| 1138 | new-style classes. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1139 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1140 | |
| 1141 | .. _specialnames: |
| 1142 | |
| 1143 | Special method names |
| 1144 | ==================== |
| 1145 | |
| 1146 | .. index:: |
| 1147 | pair: operator; overloading |
| 1148 | single: __getitem__() (mapping object method) |
| 1149 | |
| 1150 | A class can implement certain operations that are invoked by special syntax |
| 1151 | (such as arithmetic operations or subscripting and slicing) by defining methods |
| 1152 | with special names. This is Python's approach to :dfn:`operator overloading`, |
| 1153 | allowing classes to define their own behavior with respect to language |
| 1154 | operators. For instance, if a class defines a method named :meth:`__getitem__`, |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1155 | and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent |
| 1156 | to ``x.__getitem__(i)`` for old-style classes and ``type(x).__getitem__(x, i)`` |
| 1157 | for new-style classes. Except where mentioned, attempts to execute an |
| 1158 | operation raise an exception when no appropriate method is defined (typically |
| 1159 | :exc:`AttributeError` or :exc:`TypeError`). |
Georg Brandl | 5768d57 | 2007-09-05 13:36:44 +0000 | [diff] [blame] | 1160 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1161 | When implementing a class that emulates any built-in type, it is important that |
| 1162 | the emulation only be implemented to the degree that it makes sense for the |
| 1163 | object being modelled. For example, some sequences may work well with retrieval |
| 1164 | of individual elements, but extracting a slice may not make sense. (One example |
| 1165 | of this is the :class:`NodeList` interface in the W3C's Document Object Model.) |
| 1166 | |
| 1167 | |
| 1168 | .. _customization: |
| 1169 | |
| 1170 | Basic customization |
| 1171 | ------------------- |
| 1172 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1173 | .. method:: object.__new__(cls[, ...]) |
| 1174 | |
Georg Brandl | 3fc4226 | 2008-12-05 08:06:57 +0000 | [diff] [blame] | 1175 | .. index:: pair: subclassing; immutable types |
| 1176 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1177 | Called to create a new instance of class *cls*. :meth:`__new__` is a static |
| 1178 | method (special-cased so you need not declare it as such) that takes the class |
| 1179 | of which an instance was requested as its first argument. The remaining |
| 1180 | arguments are those passed to the object constructor expression (the call to the |
| 1181 | class). The return value of :meth:`__new__` should be the new object instance |
| 1182 | (usually an instance of *cls*). |
| 1183 | |
| 1184 | Typical implementations create a new instance of the class by invoking the |
| 1185 | superclass's :meth:`__new__` method using ``super(currentclass, |
| 1186 | cls).__new__(cls[, ...])`` with appropriate arguments and then modifying the |
| 1187 | newly-created instance as necessary before returning it. |
| 1188 | |
| 1189 | If :meth:`__new__` returns an instance of *cls*, then the new instance's |
| 1190 | :meth:`__init__` method will be invoked like ``__init__(self[, ...])``, where |
| 1191 | *self* is the new instance and the remaining arguments are the same as were |
| 1192 | passed to :meth:`__new__`. |
| 1193 | |
| 1194 | If :meth:`__new__` does not return an instance of *cls*, then the new instance's |
| 1195 | :meth:`__init__` method will not be invoked. |
| 1196 | |
| 1197 | :meth:`__new__` is intended mainly to allow subclasses of immutable types (like |
Georg Brandl | 3ccb49a | 2008-01-07 19:17:10 +0000 | [diff] [blame] | 1198 | int, str, or tuple) to customize instance creation. It is also commonly |
| 1199 | overridden in custom metaclasses in order to customize class creation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1200 | |
| 1201 | |
| 1202 | .. method:: object.__init__(self[, ...]) |
| 1203 | |
| 1204 | .. index:: pair: class; constructor |
| 1205 | |
| 1206 | Called when the instance is created. The arguments are those passed to the |
| 1207 | class constructor expression. If a base class has an :meth:`__init__` method, |
| 1208 | the derived class's :meth:`__init__` method, if any, must explicitly call it to |
| 1209 | ensure proper initialization of the base class part of the instance; for |
| 1210 | example: ``BaseClass.__init__(self, [args...])``. As a special constraint on |
| 1211 | constructors, no value may be returned; doing so will cause a :exc:`TypeError` |
| 1212 | to be raised at runtime. |
| 1213 | |
| 1214 | |
| 1215 | .. method:: object.__del__(self) |
| 1216 | |
| 1217 | .. index:: |
| 1218 | single: destructor |
| 1219 | statement: del |
| 1220 | |
| 1221 | Called when the instance is about to be destroyed. This is also called a |
| 1222 | destructor. If a base class has a :meth:`__del__` method, the derived class's |
| 1223 | :meth:`__del__` method, if any, must explicitly call it to ensure proper |
| 1224 | deletion of the base class part of the instance. Note that it is possible |
| 1225 | (though not recommended!) for the :meth:`__del__` method to postpone destruction |
| 1226 | of the instance by creating a new reference to it. It may then be called at a |
| 1227 | later time when this new reference is deleted. It is not guaranteed that |
| 1228 | :meth:`__del__` methods are called for objects that still exist when the |
| 1229 | interpreter exits. |
| 1230 | |
| 1231 | .. note:: |
| 1232 | |
| 1233 | ``del x`` doesn't directly call ``x.__del__()`` --- the former decrements |
| 1234 | the reference count for ``x`` by one, and the latter is only called when |
| 1235 | ``x``'s reference count reaches zero. Some common situations that may |
| 1236 | prevent the reference count of an object from going to zero include: |
| 1237 | circular references between objects (e.g., a doubly-linked list or a tree |
| 1238 | data structure with parent and child pointers); a reference to the object |
| 1239 | on the stack frame of a function that caught an exception (the traceback |
| 1240 | stored in ``sys.exc_traceback`` keeps the stack frame alive); or a |
| 1241 | reference to the object on the stack frame that raised an unhandled |
| 1242 | exception in interactive mode (the traceback stored in |
| 1243 | ``sys.last_traceback`` keeps the stack frame alive). The first situation |
| 1244 | can only be remedied by explicitly breaking the cycles; the latter two |
| 1245 | situations can be resolved by storing ``None`` in ``sys.exc_traceback`` or |
| 1246 | ``sys.last_traceback``. Circular references which are garbage are |
| 1247 | detected when the option cycle detector is enabled (it's on by default), |
| 1248 | but can only be cleaned up if there are no Python-level :meth:`__del__` |
| 1249 | methods involved. Refer to the documentation for the :mod:`gc` module for |
| 1250 | more information about how :meth:`__del__` methods are handled by the |
| 1251 | cycle detector, particularly the description of the ``garbage`` value. |
| 1252 | |
| 1253 | .. warning:: |
| 1254 | |
| 1255 | Due to the precarious circumstances under which :meth:`__del__` methods are |
| 1256 | invoked, exceptions that occur during their execution are ignored, and a warning |
| 1257 | is printed to ``sys.stderr`` instead. Also, when :meth:`__del__` is invoked in |
| 1258 | response to a module being deleted (e.g., when execution of the program is |
| 1259 | done), other globals referenced by the :meth:`__del__` method may already have |
Brett Cannon | 5b0d553 | 2009-01-29 00:54:11 +0000 | [diff] [blame] | 1260 | been deleted or in the process of being torn down (e.g. the import |
| 1261 | machinery shutting down). For this reason, :meth:`__del__` methods |
| 1262 | should do the absolute |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1263 | minimum needed to maintain external invariants. Starting with version 1.5, |
| 1264 | Python guarantees that globals whose name begins with a single underscore are |
| 1265 | deleted from their module before other globals are deleted; if no other |
| 1266 | references to such globals exist, this may help in assuring that imported |
| 1267 | modules are still available at the time when the :meth:`__del__` method is |
| 1268 | called. |
| 1269 | |
| 1270 | |
| 1271 | .. method:: object.__repr__(self) |
| 1272 | |
| 1273 | .. index:: builtin: repr |
| 1274 | |
| 1275 | Called by the :func:`repr` built-in function and by string conversions (reverse |
| 1276 | quotes) to compute the "official" string representation of an object. If at all |
| 1277 | possible, this should look like a valid Python expression that could be used to |
| 1278 | recreate an object with the same value (given an appropriate environment). If |
| 1279 | this is not possible, a string of the form ``<...some useful description...>`` |
| 1280 | should be returned. The return value must be a string object. If a class |
| 1281 | defines :meth:`__repr__` but not :meth:`__str__`, then :meth:`__repr__` is also |
| 1282 | used when an "informal" string representation of instances of that class is |
| 1283 | required. |
| 1284 | |
| 1285 | .. index:: |
| 1286 | pair: string; conversion |
| 1287 | pair: reverse; quotes |
| 1288 | pair: backward; quotes |
| 1289 | single: back-quotes |
| 1290 | |
| 1291 | This is typically used for debugging, so it is important that the representation |
| 1292 | is information-rich and unambiguous. |
| 1293 | |
| 1294 | |
| 1295 | .. method:: object.__str__(self) |
| 1296 | |
| 1297 | .. index:: |
| 1298 | builtin: str |
| 1299 | statement: print |
| 1300 | |
| 1301 | Called by the :func:`str` built-in function and by the :keyword:`print` |
| 1302 | statement to compute the "informal" string representation of an object. This |
| 1303 | differs from :meth:`__repr__` in that it does not have to be a valid Python |
| 1304 | expression: a more convenient or concise representation may be used instead. |
| 1305 | The return value must be a string object. |
| 1306 | |
| 1307 | |
| 1308 | .. method:: object.__lt__(self, other) |
| 1309 | object.__le__(self, other) |
| 1310 | object.__eq__(self, other) |
| 1311 | object.__ne__(self, other) |
| 1312 | object.__gt__(self, other) |
| 1313 | object.__ge__(self, other) |
| 1314 | |
| 1315 | .. versionadded:: 2.1 |
| 1316 | |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1317 | .. index:: |
| 1318 | single: comparisons |
| 1319 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1320 | These are the so-called "rich comparison" methods, and are called for comparison |
| 1321 | operators in preference to :meth:`__cmp__` below. The correspondence between |
| 1322 | operator symbols and method names is as follows: ``x<y`` calls ``x.__lt__(y)``, |
| 1323 | ``x<=y`` calls ``x.__le__(y)``, ``x==y`` calls ``x.__eq__(y)``, ``x!=y`` and |
| 1324 | ``x<>y`` call ``x.__ne__(y)``, ``x>y`` calls ``x.__gt__(y)``, and ``x>=y`` calls |
| 1325 | ``x.__ge__(y)``. |
| 1326 | |
| 1327 | A rich comparison method may return the singleton ``NotImplemented`` if it does |
| 1328 | not implement the operation for a given pair of arguments. By convention, |
| 1329 | ``False`` and ``True`` are returned for a successful comparison. However, these |
| 1330 | methods can return any value, so if the comparison operator is used in a Boolean |
| 1331 | context (e.g., in the condition of an ``if`` statement), Python will call |
| 1332 | :func:`bool` on the value to determine if the result is true or false. |
| 1333 | |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1334 | There are no implied relationships among the comparison operators. The truth |
| 1335 | of ``x==y`` does not imply that ``x!=y`` is false. Accordingly, when |
| 1336 | defining :meth:`__eq__`, one should also define :meth:`__ne__` so that the |
| 1337 | operators will behave as expected. See the paragraph on :meth:`__hash__` for |
| 1338 | some important notes on creating :term:`hashable` objects which support |
| 1339 | custom comparison operations and are usable as dictionary keys. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1340 | |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1341 | There are no swapped-argument versions of these methods (to be used when the |
| 1342 | left argument does not support the operation but the right argument does); |
| 1343 | rather, :meth:`__lt__` and :meth:`__gt__` are each other's reflection, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1344 | :meth:`__le__` and :meth:`__ge__` are each other's reflection, and |
| 1345 | :meth:`__eq__` and :meth:`__ne__` are their own reflection. |
| 1346 | |
| 1347 | Arguments to rich comparison methods are never coerced. |
| 1348 | |
Raymond Hettinger | 351de80 | 2009-03-12 00:25:03 +0000 | [diff] [blame] | 1349 | To automatically generate ordering operations from a single root operation, |
| 1350 | see the `Total Ordering recipe in the ASPN cookbook |
| 1351 | <http://code.activestate.com/recipes/576529/>`_\. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1352 | |
| 1353 | .. method:: object.__cmp__(self, other) |
| 1354 | |
| 1355 | .. index:: |
| 1356 | builtin: cmp |
| 1357 | single: comparisons |
| 1358 | |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1359 | Called by comparison operations if rich comparison (see above) is not |
| 1360 | defined. Should return a negative integer if ``self < other``, zero if |
| 1361 | ``self == other``, a positive integer if ``self > other``. If no |
| 1362 | :meth:`__cmp__`, :meth:`__eq__` or :meth:`__ne__` operation is defined, class |
| 1363 | instances are compared by object identity ("address"). See also the |
| 1364 | description of :meth:`__hash__` for some important notes on creating |
| 1365 | :term:`hashable` objects which support custom comparison operations and are |
| 1366 | usable as dictionary keys. (Note: the restriction that exceptions are not |
| 1367 | propagated by :meth:`__cmp__` has been removed since Python 1.5.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1368 | |
| 1369 | |
| 1370 | .. method:: object.__rcmp__(self, other) |
| 1371 | |
| 1372 | .. versionchanged:: 2.1 |
| 1373 | No longer supported. |
| 1374 | |
| 1375 | |
| 1376 | .. method:: object.__hash__(self) |
| 1377 | |
| 1378 | .. index:: |
| 1379 | object: dictionary |
| 1380 | builtin: hash |
| 1381 | |
Benjamin Peterson | 233bb00 | 2008-11-17 22:05:19 +0000 | [diff] [blame] | 1382 | Called by built-in function :func:`hash` and for operations on members of |
| 1383 | hashed collections including :class:`set`, :class:`frozenset`, and |
| 1384 | :class:`dict`. :meth:`__hash__` should return an integer. The only required |
| 1385 | property is that objects which compare equal have the same hash value; it is |
| 1386 | advised to somehow mix together (e.g. using exclusive or) the hash values for |
| 1387 | the components of the object that also play a part in comparison of objects. |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1388 | |
| 1389 | If a class does not define a :meth:`__cmp__` or :meth:`__eq__` method it |
| 1390 | should not define a :meth:`__hash__` operation either; if it defines |
| 1391 | :meth:`__cmp__` or :meth:`__eq__` but not :meth:`__hash__`, its instances |
Benjamin Peterson | 233bb00 | 2008-11-17 22:05:19 +0000 | [diff] [blame] | 1392 | will not be usable in hashed collections. If a class defines mutable objects |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1393 | and implements a :meth:`__cmp__` or :meth:`__eq__` method, it should not |
Benjamin Peterson | 233bb00 | 2008-11-17 22:05:19 +0000 | [diff] [blame] | 1394 | implement :meth:`__hash__`, since hashable collection implementations require |
| 1395 | that a object's hash value is immutable (if the object's hash value changes, |
| 1396 | it will be in the wrong hash bucket). |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1397 | |
| 1398 | User-defined classes have :meth:`__cmp__` and :meth:`__hash__` methods |
Nick Coghlan | 8235869 | 2008-08-31 13:10:50 +0000 | [diff] [blame] | 1399 | by default; with them, all objects compare unequal (except with themselves) |
| 1400 | and ``x.__hash__()`` returns ``id(x)``. |
| 1401 | |
| 1402 | Classes which inherit a :meth:`__hash__` method from a parent class but |
| 1403 | change the meaning of :meth:`__cmp__` or :meth:`__eq__` such that the hash |
| 1404 | value returned is no longer appropriate (e.g. by switching to a value-based |
| 1405 | concept of equality instead of the default identity based equality) can |
Benjamin Peterson | 233bb00 | 2008-11-17 22:05:19 +0000 | [diff] [blame] | 1406 | explicitly flag themselves as being unhashable by setting ``__hash__ = None`` |
| 1407 | in the class definition. Doing so means that not only will instances of the |
| 1408 | class raise an appropriate :exc:`TypeError` when a program attempts to |
| 1409 | retrieve their hash value, but they will also be correctly identified as |
| 1410 | unhashable when checking ``isinstance(obj, collections.Hashable)`` (unlike |
| 1411 | classes which define their own :meth:`__hash__` to explicitly raise |
| 1412 | :exc:`TypeError`). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1413 | |
| 1414 | .. versionchanged:: 2.5 |
Georg Brandl | 7c3e79f | 2007-11-02 20:06:17 +0000 | [diff] [blame] | 1415 | :meth:`__hash__` may now also return a long integer object; the 32-bit |
| 1416 | integer is then derived from the hash of that object. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1417 | |
Nick Coghlan | 8235869 | 2008-08-31 13:10:50 +0000 | [diff] [blame] | 1418 | .. versionchanged:: 2.6 |
| 1419 | :attr:`__hash__` may now be set to :const:`None` to explicitly flag |
| 1420 | instances of a class as unhashable. |
| 1421 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1422 | |
| 1423 | .. method:: object.__nonzero__(self) |
| 1424 | |
| 1425 | .. index:: single: __len__() (mapping object method) |
| 1426 | |
Georg Brandl | 3259ef3 | 2009-03-15 21:37:16 +0000 | [diff] [blame^] | 1427 | Called to implement truth value testing and the built-in operation ``bool()``; |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1428 | should return ``False`` or ``True``, or their integer equivalents ``0`` or |
Georg Brandl | 3259ef3 | 2009-03-15 21:37:16 +0000 | [diff] [blame^] | 1429 | ``1``. When this method is not defined, :meth:`__len__` is called, if it is |
| 1430 | defined, and the object is considered true if its result is nonzero. |
| 1431 | If a class defines neither :meth:`__len__` nor :meth:`__nonzero__`, all its |
| 1432 | instances are considered true. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1433 | |
| 1434 | |
| 1435 | .. method:: object.__unicode__(self) |
| 1436 | |
| 1437 | .. index:: builtin: unicode |
| 1438 | |
| 1439 | Called to implement :func:`unicode` builtin; should return a Unicode object. |
| 1440 | When this method is not defined, string conversion is attempted, and the result |
| 1441 | of string conversion is converted to Unicode using the system default encoding. |
| 1442 | |
| 1443 | |
| 1444 | .. _attribute-access: |
| 1445 | |
| 1446 | Customizing attribute access |
| 1447 | ---------------------------- |
| 1448 | |
| 1449 | The following methods can be defined to customize the meaning of attribute |
| 1450 | access (use of, assignment to, or deletion of ``x.name``) for class instances. |
| 1451 | |
| 1452 | |
| 1453 | .. method:: object.__getattr__(self, name) |
| 1454 | |
| 1455 | Called when an attribute lookup has not found the attribute in the usual places |
| 1456 | (i.e. it is not an instance attribute nor is it found in the class tree for |
| 1457 | ``self``). ``name`` is the attribute name. This method should return the |
| 1458 | (computed) attribute value or raise an :exc:`AttributeError` exception. |
| 1459 | |
| 1460 | .. index:: single: __setattr__() (object method) |
| 1461 | |
| 1462 | Note that if the attribute is found through the normal mechanism, |
| 1463 | :meth:`__getattr__` is not called. (This is an intentional asymmetry between |
| 1464 | :meth:`__getattr__` and :meth:`__setattr__`.) This is done both for efficiency |
Georg Brandl | c176814 | 2008-08-30 09:52:44 +0000 | [diff] [blame] | 1465 | reasons and because otherwise :meth:`__getattr__` would have no way to access |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1466 | other attributes of the instance. Note that at least for instance variables, |
| 1467 | you can fake total control by not inserting any values in the instance attribute |
| 1468 | dictionary (but instead inserting them in another object). See the |
| 1469 | :meth:`__getattribute__` method below for a way to actually get total control in |
| 1470 | new-style classes. |
| 1471 | |
| 1472 | |
| 1473 | .. method:: object.__setattr__(self, name, value) |
| 1474 | |
| 1475 | Called when an attribute assignment is attempted. This is called instead of the |
| 1476 | normal mechanism (i.e. store the value in the instance dictionary). *name* is |
| 1477 | the attribute name, *value* is the value to be assigned to it. |
| 1478 | |
| 1479 | .. index:: single: __dict__ (instance attribute) |
| 1480 | |
| 1481 | If :meth:`__setattr__` wants to assign to an instance attribute, it should not |
| 1482 | simply execute ``self.name = value`` --- this would cause a recursive call to |
| 1483 | itself. Instead, it should insert the value in the dictionary of instance |
| 1484 | attributes, e.g., ``self.__dict__[name] = value``. For new-style classes, |
| 1485 | rather than accessing the instance dictionary, it should call the base class |
| 1486 | method with the same name, for example, ``object.__setattr__(self, name, |
| 1487 | value)``. |
| 1488 | |
| 1489 | |
| 1490 | .. method:: object.__delattr__(self, name) |
| 1491 | |
| 1492 | Like :meth:`__setattr__` but for attribute deletion instead of assignment. This |
| 1493 | should only be implemented if ``del obj.name`` is meaningful for the object. |
| 1494 | |
| 1495 | |
| 1496 | .. _new-style-attribute-access: |
| 1497 | |
| 1498 | More attribute access for new-style classes |
| 1499 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1500 | |
| 1501 | The following methods only apply to new-style classes. |
| 1502 | |
| 1503 | |
| 1504 | .. method:: object.__getattribute__(self, name) |
| 1505 | |
| 1506 | Called unconditionally to implement attribute accesses for instances of the |
| 1507 | class. If the class also defines :meth:`__getattr__`, the latter will not be |
| 1508 | called unless :meth:`__getattribute__` either calls it explicitly or raises an |
| 1509 | :exc:`AttributeError`. This method should return the (computed) attribute value |
| 1510 | or raise an :exc:`AttributeError` exception. In order to avoid infinite |
| 1511 | recursion in this method, its implementation should always call the base class |
| 1512 | method with the same name to access any attributes it needs, for example, |
| 1513 | ``object.__getattribute__(self, name)``. |
| 1514 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 1515 | .. note:: |
| 1516 | |
| 1517 | This method may still be bypassed when looking up special methods as the |
| 1518 | result of implicit invocation via language syntax or builtin functions. |
| 1519 | See :ref:`new-style-special-lookup`. |
| 1520 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1521 | |
| 1522 | .. _descriptors: |
| 1523 | |
| 1524 | Implementing Descriptors |
| 1525 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1526 | |
| 1527 | The following methods only apply when an instance of the class containing the |
| 1528 | method (a so-called *descriptor* class) appears in the class dictionary of |
| 1529 | another new-style class, known as the *owner* class. In the examples below, "the |
| 1530 | attribute" refers to the attribute whose name is the key of the property in the |
| 1531 | owner class' ``__dict__``. Descriptors can only be implemented as new-style |
| 1532 | classes themselves. |
| 1533 | |
| 1534 | |
| 1535 | .. method:: object.__get__(self, instance, owner) |
| 1536 | |
| 1537 | Called to get the attribute of the owner class (class attribute access) or of an |
| 1538 | instance of that class (instance attribute access). *owner* is always the owner |
| 1539 | class, while *instance* is the instance that the attribute was accessed through, |
| 1540 | or ``None`` when the attribute is accessed through the *owner*. This method |
| 1541 | should return the (computed) attribute value or raise an :exc:`AttributeError` |
| 1542 | exception. |
| 1543 | |
| 1544 | |
| 1545 | .. method:: object.__set__(self, instance, value) |
| 1546 | |
| 1547 | Called to set the attribute on an instance *instance* of the owner class to a |
| 1548 | new value, *value*. |
| 1549 | |
| 1550 | |
| 1551 | .. method:: object.__delete__(self, instance) |
| 1552 | |
| 1553 | Called to delete the attribute on an instance *instance* of the owner class. |
| 1554 | |
| 1555 | |
| 1556 | .. _descriptor-invocation: |
| 1557 | |
| 1558 | Invoking Descriptors |
| 1559 | ^^^^^^^^^^^^^^^^^^^^ |
| 1560 | |
| 1561 | In general, a descriptor is an object attribute with "binding behavior", one |
| 1562 | whose attribute access has been overridden by methods in the descriptor |
| 1563 | protocol: :meth:`__get__`, :meth:`__set__`, and :meth:`__delete__`. If any of |
| 1564 | those methods are defined for an object, it is said to be a descriptor. |
| 1565 | |
| 1566 | The default behavior for attribute access is to get, set, or delete the |
| 1567 | attribute from an object's dictionary. For instance, ``a.x`` has a lookup chain |
| 1568 | starting with ``a.__dict__['x']``, then ``type(a).__dict__['x']``, and |
| 1569 | continuing through the base classes of ``type(a)`` excluding metaclasses. |
| 1570 | |
| 1571 | However, if the looked-up value is an object defining one of the descriptor |
| 1572 | methods, then Python may override the default behavior and invoke the descriptor |
| 1573 | method instead. Where this occurs in the precedence chain depends on which |
| 1574 | descriptor methods were defined and how they were called. Note that descriptors |
| 1575 | are only invoked for new style objects or classes (ones that subclass |
| 1576 | :class:`object()` or :class:`type()`). |
| 1577 | |
| 1578 | The starting point for descriptor invocation is a binding, ``a.x``. How the |
| 1579 | arguments are assembled depends on ``a``: |
| 1580 | |
| 1581 | Direct Call |
| 1582 | The simplest and least common call is when user code directly invokes a |
| 1583 | descriptor method: ``x.__get__(a)``. |
| 1584 | |
| 1585 | Instance Binding |
| 1586 | If binding to a new-style object instance, ``a.x`` is transformed into the call: |
| 1587 | ``type(a).__dict__['x'].__get__(a, type(a))``. |
| 1588 | |
| 1589 | Class Binding |
| 1590 | If binding to a new-style class, ``A.x`` is transformed into the call: |
| 1591 | ``A.__dict__['x'].__get__(None, A)``. |
| 1592 | |
| 1593 | Super Binding |
| 1594 | If ``a`` is an instance of :class:`super`, then the binding ``super(B, |
| 1595 | obj).m()`` searches ``obj.__class__.__mro__`` for the base class ``A`` |
| 1596 | immediately preceding ``B`` and then invokes the descriptor with the call: |
| 1597 | ``A.__dict__['m'].__get__(obj, A)``. |
| 1598 | |
| 1599 | For instance bindings, the precedence of descriptor invocation depends on the |
Georg Brandl | 3761422 | 2007-08-23 21:42:54 +0000 | [diff] [blame] | 1600 | which descriptor methods are defined. Normally, data descriptors define both |
| 1601 | :meth:`__get__` and :meth:`__set__`, while non-data descriptors have just the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1602 | :meth:`__get__` method. Data descriptors always override a redefinition in an |
| 1603 | instance dictionary. In contrast, non-data descriptors can be overridden by |
Georg Brandl | 3761422 | 2007-08-23 21:42:54 +0000 | [diff] [blame] | 1604 | instances. [#]_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1605 | |
| 1606 | Python methods (including :func:`staticmethod` and :func:`classmethod`) are |
| 1607 | implemented as non-data descriptors. Accordingly, instances can redefine and |
| 1608 | override methods. This allows individual instances to acquire behaviors that |
| 1609 | differ from other instances of the same class. |
| 1610 | |
| 1611 | The :func:`property` function is implemented as a data descriptor. Accordingly, |
| 1612 | instances cannot override the behavior of a property. |
| 1613 | |
| 1614 | |
| 1615 | .. _slots: |
| 1616 | |
| 1617 | __slots__ |
| 1618 | ^^^^^^^^^ |
| 1619 | |
| 1620 | By default, instances of both old and new-style classes have a dictionary for |
| 1621 | attribute storage. This wastes space for objects having very few instance |
| 1622 | variables. The space consumption can become acute when creating large numbers |
| 1623 | of instances. |
| 1624 | |
| 1625 | The default can be overridden by defining *__slots__* in a new-style class |
| 1626 | definition. The *__slots__* declaration takes a sequence of instance variables |
| 1627 | and reserves just enough space in each instance to hold a value for each |
| 1628 | variable. Space is saved because *__dict__* is not created for each instance. |
| 1629 | |
| 1630 | |
| 1631 | .. data:: __slots__ |
| 1632 | |
| 1633 | This class variable can be assigned a string, iterable, or sequence of strings |
| 1634 | with variable names used by instances. If defined in a new-style class, |
| 1635 | *__slots__* reserves space for the declared variables and prevents the automatic |
| 1636 | creation of *__dict__* and *__weakref__* for each instance. |
| 1637 | |
| 1638 | .. versionadded:: 2.2 |
| 1639 | |
| 1640 | Notes on using *__slots__* |
| 1641 | |
Georg Brandl | 3de1e69 | 2008-07-19 13:09:42 +0000 | [diff] [blame] | 1642 | * When inheriting from a class without *__slots__*, the *__dict__* attribute of |
| 1643 | that class will always be accessible, so a *__slots__* definition in the |
| 1644 | subclass is meaningless. |
| 1645 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1646 | * Without a *__dict__* variable, instances cannot be assigned new variables not |
| 1647 | listed in the *__slots__* definition. Attempts to assign to an unlisted |
| 1648 | variable name raises :exc:`AttributeError`. If dynamic assignment of new |
| 1649 | variables is desired, then add ``'__dict__'`` to the sequence of strings in the |
| 1650 | *__slots__* declaration. |
| 1651 | |
| 1652 | .. versionchanged:: 2.3 |
| 1653 | Previously, adding ``'__dict__'`` to the *__slots__* declaration would not |
| 1654 | enable the assignment of new attributes not specifically listed in the sequence |
| 1655 | of instance variable names. |
| 1656 | |
| 1657 | * Without a *__weakref__* variable for each instance, classes defining |
| 1658 | *__slots__* do not support weak references to its instances. If weak reference |
| 1659 | support is needed, then add ``'__weakref__'`` to the sequence of strings in the |
| 1660 | *__slots__* declaration. |
| 1661 | |
| 1662 | .. versionchanged:: 2.3 |
| 1663 | Previously, adding ``'__weakref__'`` to the *__slots__* declaration would not |
| 1664 | enable support for weak references. |
| 1665 | |
| 1666 | * *__slots__* are implemented at the class level by creating descriptors |
| 1667 | (:ref:`descriptors`) for each variable name. As a result, class attributes |
| 1668 | cannot be used to set default values for instance variables defined by |
| 1669 | *__slots__*; otherwise, the class attribute would overwrite the descriptor |
| 1670 | assignment. |
| 1671 | |
| 1672 | * If a class defines a slot also defined in a base class, the instance variable |
| 1673 | defined by the base class slot is inaccessible (except by retrieving its |
| 1674 | descriptor directly from the base class). This renders the meaning of the |
| 1675 | program undefined. In the future, a check may be added to prevent this. |
| 1676 | |
| 1677 | * The action of a *__slots__* declaration is limited to the class where it is |
| 1678 | defined. As a result, subclasses will have a *__dict__* unless they also define |
| 1679 | *__slots__*. |
| 1680 | |
Benjamin Peterson | c756dcd | 2008-10-23 21:43:48 +0000 | [diff] [blame] | 1681 | * Nonempty *__slots__* does not work for classes derived from "variable-length" |
| 1682 | built-in types such as :class:`long`, :class:`str` and :class:`tuple`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1683 | |
| 1684 | * Any non-string iterable may be assigned to *__slots__*. Mappings may also be |
| 1685 | used; however, in the future, special meaning may be assigned to the values |
| 1686 | corresponding to each key. |
| 1687 | |
| 1688 | * *__class__* assignment works only if both classes have the same *__slots__*. |
| 1689 | |
| 1690 | .. versionchanged:: 2.6 |
| 1691 | Previously, *__class__* assignment raised an error if either new or old class |
| 1692 | had *__slots__*. |
| 1693 | |
| 1694 | |
| 1695 | .. _metaclasses: |
| 1696 | |
| 1697 | Customizing class creation |
| 1698 | -------------------------- |
| 1699 | |
| 1700 | By default, new-style classes are constructed using :func:`type`. A class |
| 1701 | definition is read into a separate namespace and the value of class name is |
| 1702 | bound to the result of ``type(name, bases, dict)``. |
| 1703 | |
| 1704 | When the class definition is read, if *__metaclass__* is defined then the |
Georg Brandl | 3ccb49a | 2008-01-07 19:17:10 +0000 | [diff] [blame] | 1705 | callable assigned to it will be called instead of :func:`type`. This allows |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1706 | classes or functions to be written which monitor or alter the class creation |
| 1707 | process: |
| 1708 | |
| 1709 | * Modifying the class dictionary prior to the class being created. |
| 1710 | |
| 1711 | * Returning an instance of another class -- essentially performing the role of a |
| 1712 | factory function. |
| 1713 | |
Georg Brandl | 3ccb49a | 2008-01-07 19:17:10 +0000 | [diff] [blame] | 1714 | These steps will have to be performed in the metaclass's :meth:`__new__` method |
| 1715 | -- :meth:`type.__new__` can then be called from this method to create a class |
| 1716 | with different properties. This example adds a new element to the class |
| 1717 | dictionary before creating the class:: |
| 1718 | |
| 1719 | class metacls(type): |
| 1720 | def __new__(mcs, name, bases, dict): |
| 1721 | dict['foo'] = 'metacls was here' |
| 1722 | return type.__new__(mcs, name, bases, dict) |
| 1723 | |
| 1724 | You can of course also override other class methods (or add new methods); for |
| 1725 | example defining a custom :meth:`__call__` method in the metaclass allows custom |
| 1726 | behavior when the class is called, e.g. not always creating a new instance. |
| 1727 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1728 | |
| 1729 | .. data:: __metaclass__ |
| 1730 | |
| 1731 | This variable can be any callable accepting arguments for ``name``, ``bases``, |
| 1732 | and ``dict``. Upon class creation, the callable is used instead of the built-in |
| 1733 | :func:`type`. |
| 1734 | |
| 1735 | .. versionadded:: 2.2 |
| 1736 | |
| 1737 | The appropriate metaclass is determined by the following precedence rules: |
| 1738 | |
| 1739 | * If ``dict['__metaclass__']`` exists, it is used. |
| 1740 | |
| 1741 | * Otherwise, if there is at least one base class, its metaclass is used (this |
| 1742 | looks for a *__class__* attribute first and if not found, uses its type). |
| 1743 | |
| 1744 | * Otherwise, if a global variable named __metaclass__ exists, it is used. |
| 1745 | |
| 1746 | * Otherwise, the old-style, classic metaclass (types.ClassType) is used. |
| 1747 | |
| 1748 | The potential uses for metaclasses are boundless. Some ideas that have been |
| 1749 | explored including logging, interface checking, automatic delegation, automatic |
| 1750 | property creation, proxies, frameworks, and automatic resource |
| 1751 | locking/synchronization. |
| 1752 | |
| 1753 | |
| 1754 | .. _callable-types: |
| 1755 | |
| 1756 | Emulating callable objects |
| 1757 | -------------------------- |
| 1758 | |
| 1759 | |
| 1760 | .. method:: object.__call__(self[, args...]) |
| 1761 | |
| 1762 | .. index:: pair: call; instance |
| 1763 | |
| 1764 | Called when the instance is "called" as a function; if this method is defined, |
| 1765 | ``x(arg1, arg2, ...)`` is a shorthand for ``x.__call__(arg1, arg2, ...)``. |
| 1766 | |
| 1767 | |
| 1768 | .. _sequence-types: |
| 1769 | |
| 1770 | Emulating container types |
| 1771 | ------------------------- |
| 1772 | |
| 1773 | The following methods can be defined to implement container objects. Containers |
| 1774 | usually are sequences (such as lists or tuples) or mappings (like dictionaries), |
| 1775 | but can represent other containers as well. The first set of methods is used |
| 1776 | either to emulate a sequence or to emulate a mapping; the difference is that for |
| 1777 | a sequence, the allowable keys should be the integers *k* for which ``0 <= k < |
| 1778 | N`` where *N* is the length of the sequence, or slice objects, which define a |
| 1779 | range of items. (For backwards compatibility, the method :meth:`__getslice__` |
| 1780 | (see below) can also be defined to handle simple, but not extended slices.) It |
| 1781 | is also recommended that mappings provide the methods :meth:`keys`, |
| 1782 | :meth:`values`, :meth:`items`, :meth:`has_key`, :meth:`get`, :meth:`clear`, |
| 1783 | :meth:`setdefault`, :meth:`iterkeys`, :meth:`itervalues`, :meth:`iteritems`, |
| 1784 | :meth:`pop`, :meth:`popitem`, :meth:`copy`, and :meth:`update` behaving similar |
| 1785 | to those for Python's standard dictionary objects. The :mod:`UserDict` module |
| 1786 | provides a :class:`DictMixin` class to help create those methods from a base set |
| 1787 | of :meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`, and |
| 1788 | :meth:`keys`. Mutable sequences should provide methods :meth:`append`, |
| 1789 | :meth:`count`, :meth:`index`, :meth:`extend`, :meth:`insert`, :meth:`pop`, |
| 1790 | :meth:`remove`, :meth:`reverse` and :meth:`sort`, like Python standard list |
| 1791 | objects. Finally, sequence types should implement addition (meaning |
| 1792 | concatenation) and multiplication (meaning repetition) by defining the methods |
| 1793 | :meth:`__add__`, :meth:`__radd__`, :meth:`__iadd__`, :meth:`__mul__`, |
| 1794 | :meth:`__rmul__` and :meth:`__imul__` described below; they should not define |
| 1795 | :meth:`__coerce__` or other numerical operators. It is recommended that both |
| 1796 | mappings and sequences implement the :meth:`__contains__` method to allow |
| 1797 | efficient use of the ``in`` operator; for mappings, ``in`` should be equivalent |
| 1798 | of :meth:`has_key`; for sequences, it should search through the values. It is |
| 1799 | further recommended that both mappings and sequences implement the |
| 1800 | :meth:`__iter__` method to allow efficient iteration through the container; for |
| 1801 | mappings, :meth:`__iter__` should be the same as :meth:`iterkeys`; for |
| 1802 | sequences, it should iterate through the values. |
| 1803 | |
| 1804 | |
| 1805 | .. method:: object.__len__(self) |
| 1806 | |
| 1807 | .. index:: |
| 1808 | builtin: len |
| 1809 | single: __nonzero__() (object method) |
| 1810 | |
| 1811 | Called to implement the built-in function :func:`len`. Should return the length |
| 1812 | of the object, an integer ``>=`` 0. Also, an object that doesn't define a |
| 1813 | :meth:`__nonzero__` method and whose :meth:`__len__` method returns zero is |
| 1814 | considered to be false in a Boolean context. |
| 1815 | |
| 1816 | |
| 1817 | .. method:: object.__getitem__(self, key) |
| 1818 | |
| 1819 | .. index:: object: slice |
| 1820 | |
| 1821 | Called to implement evaluation of ``self[key]``. For sequence types, the |
| 1822 | accepted keys should be integers and slice objects. Note that the special |
| 1823 | interpretation of negative indexes (if the class wishes to emulate a sequence |
| 1824 | type) is up to the :meth:`__getitem__` method. If *key* is of an inappropriate |
| 1825 | type, :exc:`TypeError` may be raised; if of a value outside the set of indexes |
| 1826 | for the sequence (after any special interpretation of negative values), |
| 1827 | :exc:`IndexError` should be raised. For mapping types, if *key* is missing (not |
| 1828 | in the container), :exc:`KeyError` should be raised. |
| 1829 | |
| 1830 | .. note:: |
| 1831 | |
| 1832 | :keyword:`for` loops expect that an :exc:`IndexError` will be raised for illegal |
| 1833 | indexes to allow proper detection of the end of the sequence. |
| 1834 | |
| 1835 | |
| 1836 | .. method:: object.__setitem__(self, key, value) |
| 1837 | |
| 1838 | Called to implement assignment to ``self[key]``. Same note as for |
| 1839 | :meth:`__getitem__`. This should only be implemented for mappings if the |
| 1840 | objects support changes to the values for keys, or if new keys can be added, or |
| 1841 | for sequences if elements can be replaced. The same exceptions should be raised |
| 1842 | for improper *key* values as for the :meth:`__getitem__` method. |
| 1843 | |
| 1844 | |
| 1845 | .. method:: object.__delitem__(self, key) |
| 1846 | |
| 1847 | Called to implement deletion of ``self[key]``. Same note as for |
| 1848 | :meth:`__getitem__`. This should only be implemented for mappings if the |
| 1849 | objects support removal of keys, or for sequences if elements can be removed |
| 1850 | from the sequence. The same exceptions should be raised for improper *key* |
| 1851 | values as for the :meth:`__getitem__` method. |
| 1852 | |
| 1853 | |
| 1854 | .. method:: object.__iter__(self) |
| 1855 | |
| 1856 | This method is called when an iterator is required for a container. This method |
| 1857 | should return a new iterator object that can iterate over all the objects in the |
| 1858 | container. For mappings, it should iterate over the keys of the container, and |
| 1859 | should also be made available as the method :meth:`iterkeys`. |
| 1860 | |
| 1861 | Iterator objects also need to implement this method; they are required to return |
| 1862 | themselves. For more information on iterator objects, see :ref:`typeiter`. |
| 1863 | |
Georg Brandl | 81de0d2 | 2008-01-06 16:17:56 +0000 | [diff] [blame] | 1864 | |
| 1865 | .. method:: object.__reversed__(self) |
| 1866 | |
| 1867 | Called (if present) by the :func:`reversed` builtin to implement |
| 1868 | reverse iteration. It should return a new iterator object that iterates |
| 1869 | over all the objects in the container in reverse order. |
| 1870 | |
| 1871 | If the :meth:`__reversed__` method is not provided, the |
| 1872 | :func:`reversed` builtin will fall back to using the sequence protocol |
| 1873 | (:meth:`__len__` and :meth:`__getitem__`). Objects should normally |
| 1874 | only provide :meth:`__reversed__` if they do not support the sequence |
| 1875 | protocol and an efficient implementation of reverse iteration is possible. |
| 1876 | |
| 1877 | .. versionadded:: 2.6 |
| 1878 | |
| 1879 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1880 | The membership test operators (:keyword:`in` and :keyword:`not in`) are normally |
| 1881 | implemented as an iteration through a sequence. However, container objects can |
| 1882 | supply the following special method with a more efficient implementation, which |
| 1883 | also does not require the object be a sequence. |
| 1884 | |
| 1885 | |
| 1886 | .. method:: object.__contains__(self, item) |
| 1887 | |
| 1888 | Called to implement membership test operators. Should return true if *item* is |
| 1889 | in *self*, false otherwise. For mapping objects, this should consider the keys |
| 1890 | of the mapping rather than the values or the key-item pairs. |
| 1891 | |
| 1892 | |
| 1893 | .. _sequence-methods: |
| 1894 | |
| 1895 | Additional methods for emulation of sequence types |
| 1896 | -------------------------------------------------- |
| 1897 | |
| 1898 | The following optional methods can be defined to further emulate sequence |
| 1899 | objects. Immutable sequences methods should at most only define |
| 1900 | :meth:`__getslice__`; mutable sequences might define all three methods. |
| 1901 | |
| 1902 | |
| 1903 | .. method:: object.__getslice__(self, i, j) |
| 1904 | |
| 1905 | .. deprecated:: 2.0 |
| 1906 | Support slice objects as parameters to the :meth:`__getitem__` method. |
Georg Brandl | 8d9e845 | 2007-08-23 20:35:00 +0000 | [diff] [blame] | 1907 | (However, built-in types in CPython currently still implement |
| 1908 | :meth:`__getslice__`. Therefore, you have to override it in derived |
| 1909 | classes when implementing slicing.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1910 | |
| 1911 | Called to implement evaluation of ``self[i:j]``. The returned object should be |
| 1912 | of the same type as *self*. Note that missing *i* or *j* in the slice |
| 1913 | expression are replaced by zero or ``sys.maxint``, respectively. If negative |
| 1914 | indexes are used in the slice, the length of the sequence is added to that |
| 1915 | index. If the instance does not implement the :meth:`__len__` method, an |
| 1916 | :exc:`AttributeError` is raised. No guarantee is made that indexes adjusted this |
| 1917 | way are not still negative. Indexes which are greater than the length of the |
| 1918 | sequence are not modified. If no :meth:`__getslice__` is found, a slice object |
| 1919 | is created instead, and passed to :meth:`__getitem__` instead. |
| 1920 | |
| 1921 | |
| 1922 | .. method:: object.__setslice__(self, i, j, sequence) |
| 1923 | |
| 1924 | Called to implement assignment to ``self[i:j]``. Same notes for *i* and *j* as |
| 1925 | for :meth:`__getslice__`. |
| 1926 | |
| 1927 | This method is deprecated. If no :meth:`__setslice__` is found, or for extended |
| 1928 | slicing of the form ``self[i:j:k]``, a slice object is created, and passed to |
| 1929 | :meth:`__setitem__`, instead of :meth:`__setslice__` being called. |
| 1930 | |
| 1931 | |
| 1932 | .. method:: object.__delslice__(self, i, j) |
| 1933 | |
| 1934 | Called to implement deletion of ``self[i:j]``. Same notes for *i* and *j* as for |
| 1935 | :meth:`__getslice__`. This method is deprecated. If no :meth:`__delslice__` is |
| 1936 | found, or for extended slicing of the form ``self[i:j:k]``, a slice object is |
| 1937 | created, and passed to :meth:`__delitem__`, instead of :meth:`__delslice__` |
| 1938 | being called. |
| 1939 | |
| 1940 | Notice that these methods are only invoked when a single slice with a single |
| 1941 | colon is used, and the slice method is available. For slice operations |
| 1942 | involving extended slice notation, or in absence of the slice methods, |
| 1943 | :meth:`__getitem__`, :meth:`__setitem__` or :meth:`__delitem__` is called with a |
| 1944 | slice object as argument. |
| 1945 | |
| 1946 | The following example demonstrate how to make your program or module compatible |
| 1947 | with earlier versions of Python (assuming that methods :meth:`__getitem__`, |
| 1948 | :meth:`__setitem__` and :meth:`__delitem__` support slice objects as |
| 1949 | arguments):: |
| 1950 | |
| 1951 | class MyClass: |
| 1952 | ... |
| 1953 | def __getitem__(self, index): |
| 1954 | ... |
| 1955 | def __setitem__(self, index, value): |
| 1956 | ... |
| 1957 | def __delitem__(self, index): |
| 1958 | ... |
| 1959 | |
| 1960 | if sys.version_info < (2, 0): |
| 1961 | # They won't be defined if version is at least 2.0 final |
| 1962 | |
| 1963 | def __getslice__(self, i, j): |
| 1964 | return self[max(0, i):max(0, j):] |
| 1965 | def __setslice__(self, i, j, seq): |
| 1966 | self[max(0, i):max(0, j):] = seq |
| 1967 | def __delslice__(self, i, j): |
| 1968 | del self[max(0, i):max(0, j):] |
| 1969 | ... |
| 1970 | |
| 1971 | Note the calls to :func:`max`; these are necessary because of the handling of |
| 1972 | negative indices before the :meth:`__\*slice__` methods are called. When |
| 1973 | negative indexes are used, the :meth:`__\*item__` methods receive them as |
| 1974 | provided, but the :meth:`__\*slice__` methods get a "cooked" form of the index |
| 1975 | values. For each negative index value, the length of the sequence is added to |
| 1976 | the index before calling the method (which may still result in a negative |
| 1977 | index); this is the customary handling of negative indexes by the built-in |
| 1978 | sequence types, and the :meth:`__\*item__` methods are expected to do this as |
| 1979 | well. However, since they should already be doing that, negative indexes cannot |
| 1980 | be passed in; they must be constrained to the bounds of the sequence before |
| 1981 | being passed to the :meth:`__\*item__` methods. Calling ``max(0, i)`` |
| 1982 | conveniently returns the proper value. |
| 1983 | |
| 1984 | |
| 1985 | .. _numeric-types: |
| 1986 | |
| 1987 | Emulating numeric types |
| 1988 | ----------------------- |
| 1989 | |
| 1990 | The following methods can be defined to emulate numeric objects. Methods |
| 1991 | corresponding to operations that are not supported by the particular kind of |
| 1992 | number implemented (e.g., bitwise operations for non-integral numbers) should be |
| 1993 | left undefined. |
| 1994 | |
| 1995 | |
| 1996 | .. method:: object.__add__(self, other) |
| 1997 | object.__sub__(self, other) |
| 1998 | object.__mul__(self, other) |
| 1999 | object.__floordiv__(self, other) |
| 2000 | object.__mod__(self, other) |
| 2001 | object.__divmod__(self, other) |
| 2002 | object.__pow__(self, other[, modulo]) |
| 2003 | object.__lshift__(self, other) |
| 2004 | object.__rshift__(self, other) |
| 2005 | object.__and__(self, other) |
| 2006 | object.__xor__(self, other) |
| 2007 | object.__or__(self, other) |
| 2008 | |
| 2009 | .. index:: |
| 2010 | builtin: divmod |
| 2011 | builtin: pow |
| 2012 | builtin: pow |
| 2013 | |
| 2014 | These methods are called to implement the binary arithmetic operations (``+``, |
| 2015 | ``-``, ``*``, ``//``, ``%``, :func:`divmod`, :func:`pow`, ``**``, ``<<``, |
| 2016 | ``>>``, ``&``, ``^``, ``|``). For instance, to evaluate the expression |
Brett Cannon | 9329846 | 2008-08-14 05:55:18 +0000 | [diff] [blame] | 2017 | ``x + y``, where *x* is an instance of a class that has an :meth:`__add__` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2018 | method, ``x.__add__(y)`` is called. The :meth:`__divmod__` method should be the |
| 2019 | equivalent to using :meth:`__floordiv__` and :meth:`__mod__`; it should not be |
| 2020 | related to :meth:`__truediv__` (described below). Note that :meth:`__pow__` |
| 2021 | should be defined to accept an optional third argument if the ternary version of |
| 2022 | the built-in :func:`pow` function is to be supported. |
| 2023 | |
| 2024 | If one of those methods does not support the operation with the supplied |
| 2025 | arguments, it should return ``NotImplemented``. |
| 2026 | |
| 2027 | |
| 2028 | .. method:: object.__div__(self, other) |
| 2029 | object.__truediv__(self, other) |
| 2030 | |
| 2031 | The division operator (``/``) is implemented by these methods. The |
| 2032 | :meth:`__truediv__` method is used when ``__future__.division`` is in effect, |
| 2033 | otherwise :meth:`__div__` is used. If only one of these two methods is defined, |
| 2034 | the object will not support division in the alternate context; :exc:`TypeError` |
| 2035 | will be raised instead. |
| 2036 | |
| 2037 | |
| 2038 | .. method:: object.__radd__(self, other) |
| 2039 | object.__rsub__(self, other) |
| 2040 | object.__rmul__(self, other) |
| 2041 | object.__rdiv__(self, other) |
| 2042 | object.__rtruediv__(self, other) |
| 2043 | object.__rfloordiv__(self, other) |
| 2044 | object.__rmod__(self, other) |
| 2045 | object.__rdivmod__(self, other) |
| 2046 | object.__rpow__(self, other) |
| 2047 | object.__rlshift__(self, other) |
| 2048 | object.__rrshift__(self, other) |
| 2049 | object.__rand__(self, other) |
| 2050 | object.__rxor__(self, other) |
| 2051 | object.__ror__(self, other) |
| 2052 | |
| 2053 | .. index:: |
| 2054 | builtin: divmod |
| 2055 | builtin: pow |
| 2056 | |
| 2057 | These methods are called to implement the binary arithmetic operations (``+``, |
| 2058 | ``-``, ``*``, ``/``, ``%``, :func:`divmod`, :func:`pow`, ``**``, ``<<``, ``>>``, |
| 2059 | ``&``, ``^``, ``|``) with reflected (swapped) operands. These functions are |
| 2060 | only called if the left operand does not support the corresponding operation and |
| 2061 | the operands are of different types. [#]_ For instance, to evaluate the |
Brett Cannon | 9329846 | 2008-08-14 05:55:18 +0000 | [diff] [blame] | 2062 | expression ``x - y``, where *y* is an instance of a class that has an |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2063 | :meth:`__rsub__` method, ``y.__rsub__(x)`` is called if ``x.__sub__(y)`` returns |
| 2064 | *NotImplemented*. |
| 2065 | |
| 2066 | .. index:: builtin: pow |
| 2067 | |
| 2068 | Note that ternary :func:`pow` will not try calling :meth:`__rpow__` (the |
| 2069 | coercion rules would become too complicated). |
| 2070 | |
| 2071 | .. note:: |
| 2072 | |
| 2073 | If the right operand's type is a subclass of the left operand's type and that |
| 2074 | subclass provides the reflected method for the operation, this method will be |
| 2075 | called before the left operand's non-reflected method. This behavior allows |
| 2076 | subclasses to override their ancestors' operations. |
| 2077 | |
| 2078 | |
| 2079 | .. method:: object.__iadd__(self, other) |
| 2080 | object.__isub__(self, other) |
| 2081 | object.__imul__(self, other) |
| 2082 | object.__idiv__(self, other) |
| 2083 | object.__itruediv__(self, other) |
| 2084 | object.__ifloordiv__(self, other) |
| 2085 | object.__imod__(self, other) |
| 2086 | object.__ipow__(self, other[, modulo]) |
| 2087 | object.__ilshift__(self, other) |
| 2088 | object.__irshift__(self, other) |
| 2089 | object.__iand__(self, other) |
| 2090 | object.__ixor__(self, other) |
| 2091 | object.__ior__(self, other) |
| 2092 | |
Georg Brandl | fe11f4d | 2009-01-18 18:25:30 +0000 | [diff] [blame] | 2093 | These methods are called to implement the augmented arithmetic assignments |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2094 | (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``, ``**=``, ``<<=``, ``>>=``, |
| 2095 | ``&=``, ``^=``, ``|=``). These methods should attempt to do the operation |
| 2096 | in-place (modifying *self*) and return the result (which could be, but does |
| 2097 | not have to be, *self*). If a specific method is not defined, the augmented |
Georg Brandl | fe11f4d | 2009-01-18 18:25:30 +0000 | [diff] [blame] | 2098 | assignment falls back to the normal methods. For instance, to execute the |
| 2099 | statement ``x += y``, where *x* is an instance of a class that has an |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2100 | :meth:`__iadd__` method, ``x.__iadd__(y)`` is called. If *x* is an instance |
| 2101 | of a class that does not define a :meth:`__iadd__` method, ``x.__add__(y)`` |
Brett Cannon | 9329846 | 2008-08-14 05:55:18 +0000 | [diff] [blame] | 2102 | and ``y.__radd__(x)`` are considered, as with the evaluation of ``x + y``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2103 | |
| 2104 | |
| 2105 | .. method:: object.__neg__(self) |
| 2106 | object.__pos__(self) |
| 2107 | object.__abs__(self) |
| 2108 | object.__invert__(self) |
| 2109 | |
| 2110 | .. index:: builtin: abs |
| 2111 | |
| 2112 | Called to implement the unary arithmetic operations (``-``, ``+``, :func:`abs` |
| 2113 | and ``~``). |
| 2114 | |
| 2115 | |
| 2116 | .. method:: object.__complex__(self) |
| 2117 | object.__int__(self) |
| 2118 | object.__long__(self) |
| 2119 | object.__float__(self) |
| 2120 | |
| 2121 | .. index:: |
| 2122 | builtin: complex |
| 2123 | builtin: int |
| 2124 | builtin: long |
| 2125 | builtin: float |
| 2126 | |
| 2127 | Called to implement the built-in functions :func:`complex`, :func:`int`, |
| 2128 | :func:`long`, and :func:`float`. Should return a value of the appropriate type. |
| 2129 | |
| 2130 | |
| 2131 | .. method:: object.__oct__(self) |
| 2132 | object.__hex__(self) |
| 2133 | |
| 2134 | .. index:: |
| 2135 | builtin: oct |
| 2136 | builtin: hex |
| 2137 | |
| 2138 | Called to implement the built-in functions :func:`oct` and :func:`hex`. Should |
| 2139 | return a string value. |
| 2140 | |
| 2141 | |
| 2142 | .. method:: object.__index__(self) |
| 2143 | |
| 2144 | Called to implement :func:`operator.index`. Also called whenever Python needs |
| 2145 | an integer object (such as in slicing). Must return an integer (int or long). |
| 2146 | |
| 2147 | .. versionadded:: 2.5 |
| 2148 | |
| 2149 | |
| 2150 | .. method:: object.__coerce__(self, other) |
| 2151 | |
| 2152 | Called to implement "mixed-mode" numeric arithmetic. Should either return a |
| 2153 | 2-tuple containing *self* and *other* converted to a common numeric type, or |
| 2154 | ``None`` if conversion is impossible. When the common type would be the type of |
| 2155 | ``other``, it is sufficient to return ``None``, since the interpreter will also |
| 2156 | ask the other object to attempt a coercion (but sometimes, if the implementation |
| 2157 | of the other type cannot be changed, it is useful to do the conversion to the |
| 2158 | other type here). A return value of ``NotImplemented`` is equivalent to |
| 2159 | returning ``None``. |
| 2160 | |
| 2161 | |
| 2162 | .. _coercion-rules: |
| 2163 | |
| 2164 | Coercion rules |
| 2165 | -------------- |
| 2166 | |
| 2167 | This section used to document the rules for coercion. As the language has |
| 2168 | evolved, the coercion rules have become hard to document precisely; documenting |
| 2169 | what one version of one particular implementation does is undesirable. Instead, |
| 2170 | here are some informal guidelines regarding coercion. In Python 3.0, coercion |
| 2171 | will not be supported. |
| 2172 | |
| 2173 | * |
| 2174 | |
| 2175 | If the left operand of a % operator is a string or Unicode object, no coercion |
| 2176 | takes place and the string formatting operation is invoked instead. |
| 2177 | |
| 2178 | * |
| 2179 | |
| 2180 | It is no longer recommended to define a coercion operation. Mixed-mode |
| 2181 | operations on types that don't define coercion pass the original arguments to |
| 2182 | the operation. |
| 2183 | |
| 2184 | * |
| 2185 | |
| 2186 | New-style classes (those derived from :class:`object`) never invoke the |
| 2187 | :meth:`__coerce__` method in response to a binary operator; the only time |
| 2188 | :meth:`__coerce__` is invoked is when the built-in function :func:`coerce` is |
| 2189 | called. |
| 2190 | |
| 2191 | * |
| 2192 | |
| 2193 | For most intents and purposes, an operator that returns ``NotImplemented`` is |
| 2194 | treated the same as one that is not implemented at all. |
| 2195 | |
| 2196 | * |
| 2197 | |
| 2198 | Below, :meth:`__op__` and :meth:`__rop__` are used to signify the generic method |
| 2199 | names corresponding to an operator; :meth:`__iop__` is used for the |
| 2200 | corresponding in-place operator. For example, for the operator '``+``', |
| 2201 | :meth:`__add__` and :meth:`__radd__` are used for the left and right variant of |
| 2202 | the binary operator, and :meth:`__iadd__` for the in-place variant. |
| 2203 | |
| 2204 | * |
| 2205 | |
| 2206 | For objects *x* and *y*, first ``x.__op__(y)`` is tried. If this is not |
| 2207 | implemented or returns ``NotImplemented``, ``y.__rop__(x)`` is tried. If this |
| 2208 | is also not implemented or returns ``NotImplemented``, a :exc:`TypeError` |
| 2209 | exception is raised. But see the following exception: |
| 2210 | |
| 2211 | * |
| 2212 | |
| 2213 | Exception to the previous item: if the left operand is an instance of a built-in |
| 2214 | type or a new-style class, and the right operand is an instance of a proper |
| 2215 | subclass of that type or class and overrides the base's :meth:`__rop__` method, |
| 2216 | the right operand's :meth:`__rop__` method is tried *before* the left operand's |
| 2217 | :meth:`__op__` method. |
| 2218 | |
| 2219 | This is done so that a subclass can completely override binary operators. |
| 2220 | Otherwise, the left operand's :meth:`__op__` method would always accept the |
| 2221 | right operand: when an instance of a given class is expected, an instance of a |
| 2222 | subclass of that class is always acceptable. |
| 2223 | |
| 2224 | * |
| 2225 | |
| 2226 | When either operand type defines a coercion, this coercion is called before that |
| 2227 | type's :meth:`__op__` or :meth:`__rop__` method is called, but no sooner. If |
| 2228 | the coercion returns an object of a different type for the operand whose |
| 2229 | coercion is invoked, part of the process is redone using the new object. |
| 2230 | |
| 2231 | * |
| 2232 | |
| 2233 | When an in-place operator (like '``+=``') is used, if the left operand |
| 2234 | implements :meth:`__iop__`, it is invoked without any coercion. When the |
| 2235 | operation falls back to :meth:`__op__` and/or :meth:`__rop__`, the normal |
| 2236 | coercion rules apply. |
| 2237 | |
| 2238 | * |
| 2239 | |
Brett Cannon | 9329846 | 2008-08-14 05:55:18 +0000 | [diff] [blame] | 2240 | In ``x + y``, if *x* is a sequence that implements sequence concatenation, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2241 | sequence concatenation is invoked. |
| 2242 | |
| 2243 | * |
| 2244 | |
Brett Cannon | 9329846 | 2008-08-14 05:55:18 +0000 | [diff] [blame] | 2245 | In ``x * y``, if one operator is a sequence that implements sequence |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2246 | repetition, and the other is an integer (:class:`int` or :class:`long`), |
| 2247 | sequence repetition is invoked. |
| 2248 | |
| 2249 | * |
| 2250 | |
| 2251 | Rich comparisons (implemented by methods :meth:`__eq__` and so on) never use |
| 2252 | coercion. Three-way comparison (implemented by :meth:`__cmp__`) does use |
| 2253 | coercion under the same conditions as other binary operations use it. |
| 2254 | |
| 2255 | * |
| 2256 | |
| 2257 | In the current implementation, the built-in numeric types :class:`int`, |
| 2258 | :class:`long` and :class:`float` do not use coercion; the type :class:`complex` |
Georg Brandl | 9834dd7 | 2009-02-13 10:44:17 +0000 | [diff] [blame] | 2259 | however does use coercion for binary operators and rich comparisons, despite |
| 2260 | the above rules. The difference can become apparent when subclassing these |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2261 | types. Over time, the type :class:`complex` may be fixed to avoid coercion. |
| 2262 | All these types implement a :meth:`__coerce__` method, for use by the built-in |
| 2263 | :func:`coerce` function. |
| 2264 | |
| 2265 | |
| 2266 | .. _context-managers: |
| 2267 | |
| 2268 | With Statement Context Managers |
| 2269 | ------------------------------- |
| 2270 | |
| 2271 | .. versionadded:: 2.5 |
| 2272 | |
| 2273 | A :dfn:`context manager` is an object that defines the runtime context to be |
| 2274 | established when executing a :keyword:`with` statement. The context manager |
| 2275 | handles the entry into, and the exit from, the desired runtime context for the |
| 2276 | execution of the block of code. Context managers are normally invoked using the |
| 2277 | :keyword:`with` statement (described in section :ref:`with`), but can also be |
| 2278 | used by directly invoking their methods. |
| 2279 | |
| 2280 | .. index:: |
| 2281 | statement: with |
| 2282 | single: context manager |
| 2283 | |
| 2284 | Typical uses of context managers include saving and restoring various kinds of |
| 2285 | global state, locking and unlocking resources, closing opened files, etc. |
| 2286 | |
| 2287 | For more information on context managers, see :ref:`typecontextmanager`. |
| 2288 | |
| 2289 | |
| 2290 | .. method:: object.__enter__(self) |
| 2291 | |
| 2292 | Enter the runtime context related to this object. The :keyword:`with` statement |
| 2293 | will bind this method's return value to the target(s) specified in the |
| 2294 | :keyword:`as` clause of the statement, if any. |
| 2295 | |
| 2296 | |
| 2297 | .. method:: object.__exit__(self, exc_type, exc_value, traceback) |
| 2298 | |
| 2299 | Exit the runtime context related to this object. The parameters describe the |
| 2300 | exception that caused the context to be exited. If the context was exited |
| 2301 | without an exception, all three arguments will be :const:`None`. |
| 2302 | |
| 2303 | If an exception is supplied, and the method wishes to suppress the exception |
| 2304 | (i.e., prevent it from being propagated), it should return a true value. |
| 2305 | Otherwise, the exception will be processed normally upon exit from this method. |
| 2306 | |
| 2307 | Note that :meth:`__exit__` methods should not reraise the passed-in exception; |
| 2308 | this is the caller's responsibility. |
| 2309 | |
| 2310 | |
| 2311 | .. seealso:: |
| 2312 | |
| 2313 | :pep:`0343` - The "with" statement |
| 2314 | The specification, background, and examples for the Python :keyword:`with` |
| 2315 | statement. |
| 2316 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 2317 | |
| 2318 | .. _old-style-special-lookup: |
| 2319 | |
| 2320 | Special method lookup for old-style classes |
| 2321 | ------------------------------------------- |
| 2322 | |
| 2323 | For old-style classes, special methods are always looked up in exactly the |
| 2324 | same way as any other method or attribute. This is the case regardless of |
| 2325 | whether the method is being looked up explicitly as in ``x.__getitem__(i)`` |
| 2326 | or implicitly as in ``x[i]``. |
| 2327 | |
| 2328 | This behaviour means that special methods may exhibit different behaviour |
| 2329 | for different instances of a single old-style class if the appropriate |
| 2330 | special attributes are set differently:: |
| 2331 | |
| 2332 | >>> class C: |
| 2333 | ... pass |
| 2334 | ... |
| 2335 | >>> c1 = C() |
| 2336 | >>> c2 = C() |
| 2337 | >>> c1.__len__ = lambda: 5 |
| 2338 | >>> c2.__len__ = lambda: 9 |
| 2339 | >>> len(c1) |
| 2340 | 5 |
| 2341 | >>> len(c2) |
| 2342 | 9 |
| 2343 | |
| 2344 | |
| 2345 | .. _new-style-special-lookup: |
| 2346 | |
| 2347 | Special method lookup for new-style classes |
| 2348 | ------------------------------------------- |
| 2349 | |
| 2350 | For new-style classes, implicit invocations of special methods are only guaranteed |
| 2351 | to work correctly if defined on an object's type, not in the object's instance |
| 2352 | dictionary. That behaviour is the reason why the following code raises an |
| 2353 | exception (unlike the equivalent example with old-style classes):: |
| 2354 | |
| 2355 | >>> class C(object): |
| 2356 | ... pass |
| 2357 | ... |
| 2358 | >>> c = C() |
| 2359 | >>> c.__len__ = lambda: 5 |
| 2360 | >>> len(c) |
| 2361 | Traceback (most recent call last): |
| 2362 | File "<stdin>", line 1, in <module> |
| 2363 | TypeError: object of type 'C' has no len() |
| 2364 | |
| 2365 | The rationale behind this behaviour lies with a number of special methods such |
| 2366 | as :meth:`__hash__` and :meth:`__repr__` that are implemented by all objects, |
| 2367 | including type objects. If the implicit lookup of these methods used the |
| 2368 | conventional lookup process, they would fail when invoked on the type object |
| 2369 | itself:: |
| 2370 | |
| 2371 | >>> 1 .__hash__() == hash(1) |
| 2372 | True |
| 2373 | >>> int.__hash__() == hash(int) |
| 2374 | Traceback (most recent call last): |
| 2375 | File "<stdin>", line 1, in <module> |
| 2376 | TypeError: descriptor '__hash__' of 'int' object needs an argument |
| 2377 | |
| 2378 | Incorrectly attempting to invoke an unbound method of a class in this way is |
| 2379 | sometimes referred to as 'metaclass confusion', and is avoided by bypassing |
| 2380 | the instance when looking up special methods:: |
| 2381 | |
| 2382 | >>> type(1).__hash__(1) == hash(1) |
| 2383 | True |
| 2384 | >>> type(int).__hash__(int) == hash(int) |
| 2385 | True |
| 2386 | |
| 2387 | In addition to bypassing any instance attributes in the interest of |
Georg Brandl | 9a05373 | 2008-12-05 15:29:39 +0000 | [diff] [blame] | 2388 | correctness, implicit special method lookup generally also bypasses the |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 2389 | :meth:`__getattribute__` method even of the object's metaclass:: |
| 2390 | |
| 2391 | >>> class Meta(type): |
| 2392 | ... def __getattribute__(*args): |
| 2393 | ... print "Metaclass getattribute invoked" |
| 2394 | ... return type.__getattribute__(*args) |
| 2395 | ... |
| 2396 | >>> class C(object): |
| 2397 | ... __metaclass__ = Meta |
| 2398 | ... def __len__(self): |
| 2399 | ... return 10 |
| 2400 | ... def __getattribute__(*args): |
| 2401 | ... print "Class getattribute invoked" |
| 2402 | ... return object.__getattribute__(*args) |
| 2403 | ... |
| 2404 | >>> c = C() |
| 2405 | >>> c.__len__() # Explicit lookup via instance |
| 2406 | Class getattribute invoked |
| 2407 | 10 |
| 2408 | >>> type(c).__len__(c) # Explicit lookup via type |
| 2409 | Metaclass getattribute invoked |
| 2410 | 10 |
| 2411 | >>> len(c) # Implicit lookup |
| 2412 | 10 |
| 2413 | |
| 2414 | Bypassing the :meth:`__getattribute__` machinery in this fashion |
| 2415 | provides significant scope for speed optimisations within the |
| 2416 | interpreter, at the cost of some flexibility in the handling of |
| 2417 | special methods (the special method *must* be set on the class |
| 2418 | object itself in order to be consistently invoked by the interpreter). |
| 2419 | |
| 2420 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2421 | .. rubric:: Footnotes |
| 2422 | |
Nick Coghlan | a510748 | 2008-08-04 12:40:59 +0000 | [diff] [blame] | 2423 | .. [#] It *is* possible in some cases to change an object's type, under certain |
| 2424 | controlled conditions. It generally isn't a good idea though, since it can |
| 2425 | lead to some very strange behaviour if it is handled incorrectly. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2426 | |
Georg Brandl | 3761422 | 2007-08-23 21:42:54 +0000 | [diff] [blame] | 2427 | .. [#] A descriptor can define any combination of :meth:`__get__`, |
| 2428 | :meth:`__set__` and :meth:`__delete__`. If it does not define :meth:`__get__`, |
| 2429 | then accessing the attribute even on an instance will return the descriptor |
| 2430 | object itself. If the descriptor defines :meth:`__set__` and/or |
| 2431 | :meth:`__delete__`, it is a data descriptor; if it defines neither, it is a |
| 2432 | non-data descriptor. |
| 2433 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2434 | .. [#] For operands of the same type, it is assumed that if the non-reflected method |
| 2435 | (such as :meth:`__add__`) fails the operation is not supported, which is why the |
| 2436 | reflected method is not called. |
| 2437 | |