Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. XXX: reference/datamodel and this have quite a few overlaps! |
| 2 | |
| 3 | |
| 4 | .. _bltin-types: |
| 5 | |
| 6 | ************** |
| 7 | Built-in Types |
| 8 | ************** |
| 9 | |
| 10 | The following sections describe the standard types that are built into the |
| 11 | interpreter. |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | .. index:: pair: built-in; types |
| 14 | |
| 15 | The principal built-in types are numerics, sequences, mappings, files, classes, |
| 16 | instances and exceptions. |
| 17 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | Some operations are supported by several object types; in particular, |
| 19 | practically all objects can be compared, tested for truth value, and converted |
| 20 | to a string (with the :func:`repr` function or the slightly different |
| 21 | :func:`str` function). The latter function is implicitly used when an object is |
| 22 | written by the :func:`print` function. |
| 23 | |
| 24 | |
| 25 | .. _truth: |
| 26 | |
| 27 | Truth Value Testing |
| 28 | =================== |
| 29 | |
| 30 | .. index:: |
| 31 | statement: if |
| 32 | statement: while |
| 33 | pair: truth; value |
| 34 | pair: Boolean; operations |
| 35 | single: false |
| 36 | |
| 37 | Any object can be tested for truth value, for use in an :keyword:`if` or |
| 38 | :keyword:`while` condition or as operand of the Boolean operations below. The |
| 39 | following values are considered false: |
| 40 | |
| 41 | .. index:: single: None (Built-in object) |
| 42 | |
| 43 | * ``None`` |
| 44 | |
| 45 | .. index:: single: False (Built-in object) |
| 46 | |
| 47 | * ``False`` |
| 48 | |
| 49 | * zero of any numeric type, for example, ``0``, ``0L``, ``0.0``, ``0j``. |
| 50 | |
| 51 | * any empty sequence, for example, ``''``, ``()``, ``[]``. |
| 52 | |
| 53 | * any empty mapping, for example, ``{}``. |
| 54 | |
| 55 | * instances of user-defined classes, if the class defines a :meth:`__bool__` or |
| 56 | :meth:`__len__` method, when that method returns the integer zero or |
| 57 | :class:`bool` value ``False``. [#]_ |
| 58 | |
| 59 | .. index:: single: true |
| 60 | |
| 61 | All other values are considered true --- so objects of many types are always |
| 62 | true. |
| 63 | |
| 64 | .. index:: |
| 65 | operator: or |
| 66 | operator: and |
| 67 | single: False |
| 68 | single: True |
| 69 | |
| 70 | Operations and built-in functions that have a Boolean result always return ``0`` |
| 71 | or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated. |
| 72 | (Important exception: the Boolean operations ``or`` and ``and`` always return |
| 73 | one of their operands.) |
| 74 | |
| 75 | |
| 76 | .. _boolean: |
| 77 | |
| 78 | Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not` |
| 79 | ==================================================================== |
| 80 | |
| 81 | .. index:: pair: Boolean; operations |
| 82 | |
| 83 | These are the Boolean operations, ordered by ascending priority: |
| 84 | |
| 85 | +-------------+---------------------------------+-------+ |
| 86 | | Operation | Result | Notes | |
| 87 | +=============+=================================+=======+ |
| 88 | | ``x or y`` | if *x* is false, then *y*, else | \(1) | |
| 89 | | | *x* | | |
| 90 | +-------------+---------------------------------+-------+ |
| 91 | | ``x and y`` | if *x* is false, then *x*, else | \(2) | |
| 92 | | | *y* | | |
| 93 | +-------------+---------------------------------+-------+ |
| 94 | | ``not x`` | if *x* is false, then ``True``, | \(3) | |
| 95 | | | else ``False`` | | |
| 96 | +-------------+---------------------------------+-------+ |
| 97 | |
| 98 | .. index:: |
| 99 | operator: and |
| 100 | operator: or |
| 101 | operator: not |
| 102 | |
| 103 | Notes: |
| 104 | |
| 105 | (1) |
| 106 | This is a short-circuit operator, so it only evaluates the second |
| 107 | argument if the first one is :const:`False`. |
| 108 | |
| 109 | (2) |
| 110 | This is a short-circuit operator, so it only evaluates the second |
| 111 | argument if the first one is :const:`True`. |
| 112 | |
| 113 | (3) |
| 114 | ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is |
| 115 | interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error. |
| 116 | |
| 117 | |
| 118 | .. _stdcomparisons: |
| 119 | |
| 120 | Comparisons |
| 121 | =========== |
| 122 | |
| 123 | .. index:: pair: chaining; comparisons |
| 124 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 125 | There are eight comparison operations in Python. They all have the same |
| 126 | priority (which is higher than that of the Boolean operations). Comparisons can |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and |
| 128 | y <= z``, except that *y* is evaluated only once (but in both cases *z* is not |
| 129 | evaluated at all when ``x < y`` is found to be false). |
| 130 | |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 131 | .. index:: |
| 132 | pair: operator; comparison |
| 133 | operator: == |
| 134 | operator: < |
| 135 | operator: > |
| 136 | operator: <= |
| 137 | operator: >= |
| 138 | operator: != |
| 139 | operator: is |
| 140 | operator: is not |
| 141 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | This table summarizes the comparison operations: |
| 143 | |
| 144 | +------------+-------------------------+-------+ |
| 145 | | Operation | Meaning | Notes | |
| 146 | +============+=========================+=======+ |
| 147 | | ``<`` | strictly less than | | |
| 148 | +------------+-------------------------+-------+ |
| 149 | | ``<=`` | less than or equal | | |
| 150 | +------------+-------------------------+-------+ |
| 151 | | ``>`` | strictly greater than | | |
| 152 | +------------+-------------------------+-------+ |
| 153 | | ``>=`` | greater than or equal | | |
| 154 | +------------+-------------------------+-------+ |
| 155 | | ``==`` | equal | | |
| 156 | +------------+-------------------------+-------+ |
| 157 | | ``!=`` | not equal | | |
| 158 | +------------+-------------------------+-------+ |
| 159 | | ``is`` | object identity | | |
| 160 | +------------+-------------------------+-------+ |
| 161 | | ``is not`` | negated object identity | | |
| 162 | +------------+-------------------------+-------+ |
| 163 | |
| 164 | .. index:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | pair: object; numeric |
| 166 | pair: objects; comparing |
| 167 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 168 | Objects of different types, except different numeric types, never compare equal. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | Furthermore, some types (for example, file objects) support only a degenerate |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 170 | notion of comparison where any two objects of that type are unequal. The ``<``, |
| 171 | ``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when |
| 172 | any operand is a complex number, the objects are of different types that cannot |
| 173 | be compared, or other cases where there is no defined ordering. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 175 | .. index:: |
| 176 | single: __cmp__() (instance method) |
| 177 | single: __eq__() (instance method) |
| 178 | single: __ne__() (instance method) |
| 179 | single: __lt__() (instance method) |
| 180 | single: __le__() (instance method) |
| 181 | single: __gt__() (instance method) |
| 182 | single: __ge__() (instance method) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
| 184 | Instances of a class normally compare as non-equal unless the class defines the |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 185 | :meth:`__eq__` or :meth:`__cmp__` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 187 | Instances of a class cannot be ordered with respect to other instances of the |
| 188 | same class, or other types of object, unless the class defines enough of the |
| 189 | methods :meth:`__cmp__`, :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and |
| 190 | :meth:`__ge__` (in general, either :meth:`__cmp__` or both :meth:`__lt__` and |
| 191 | :meth:`__eq__` are sufficient, if you want the conventional meanings of the |
| 192 | comparison operators). |
| 193 | |
| 194 | The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be |
| 195 | customized; also they can be applied to any two objects and never raise an |
| 196 | exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
| 198 | .. index:: |
| 199 | operator: in |
| 200 | operator: not in |
| 201 | |
| 202 | Two more operations with the same syntactic priority, ``in`` and ``not in``, are |
| 203 | supported only by sequence types (below). |
| 204 | |
| 205 | |
| 206 | .. _typesnumeric: |
| 207 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 208 | Numeric Types --- :class:`int`, :class:`float`, :class:`complex` |
| 209 | ================================================================ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | |
| 211 | .. index:: |
| 212 | object: numeric |
| 213 | object: Boolean |
| 214 | object: integer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | object: floating point |
| 216 | object: complex number |
| 217 | pair: C; language |
| 218 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 219 | There are three distinct numeric types: :dfn:`integers`, :dfn:`floating point |
| 220 | numbers`, and :dfn:`complex numbers`. In addition, Booleans are a subtype of |
Brett Cannon | 85c1ba5 | 2007-10-07 23:12:41 +0000 | [diff] [blame] | 221 | plain integers. Integers have unlimited precision. Floating point numbers are |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 222 | implemented using :ctype:`double` in C. All bets on their precision are off |
| 223 | unless you happen to know the machine you are working with. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
| 225 | Complex numbers have a real and imaginary part, which are each implemented using |
| 226 | :ctype:`double` in C. To extract these parts from a complex number *z*, use |
| 227 | ``z.real`` and ``z.imag``. |
| 228 | |
| 229 | .. index:: |
| 230 | pair: numeric; literals |
| 231 | pair: integer; literals |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | pair: floating point; literals |
| 233 | pair: complex number; literals |
| 234 | pair: hexadecimal; literals |
| 235 | pair: octal; literals |
Neal Norwitz | 1d2aef5 | 2007-10-02 07:26:14 +0000 | [diff] [blame] | 236 | pair: binary; literals |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
| 238 | Numbers are created by numeric literals or as the result of built-in functions |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 239 | and operators. Unadorned integer literals (including hex, octal and binary |
| 240 | numbers) yield integers. Numeric literals containing a decimal point or an |
| 241 | exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a |
| 242 | numeric literal yields an imaginary number (a complex number with a zero real |
| 243 | part) which you can add to an integer or float to get a complex number with real |
| 244 | and imaginary parts. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
| 246 | .. index:: |
| 247 | single: arithmetic |
| 248 | builtin: int |
| 249 | builtin: long |
| 250 | builtin: float |
| 251 | builtin: complex |
| 252 | |
| 253 | Python fully supports mixed arithmetic: when a binary arithmetic operator has |
| 254 | operands of different numeric types, the operand with the "narrower" type is |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 255 | widened to that of the other, where integer is narrower than floating point, |
| 256 | which is narrower than complex. Comparisons between numbers of mixed type use |
| 257 | the same rule. [#]_ The constructors :func:`int`, :func:`float`, and |
| 258 | :func:`complex` can be used to produce numbers of a specific type. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | All numeric types (except complex) support the following operations, sorted by |
| 261 | ascending priority (operations in the same box have the same priority; all |
| 262 | numeric operations have a higher priority than comparison operations): |
| 263 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 264 | +---------------------+---------------------------------+-------+--------------------+ |
| 265 | | Operation | Result | Notes | Full documentation | |
Neal Norwitz | 1d2aef5 | 2007-10-02 07:26:14 +0000 | [diff] [blame] | 266 | +=====================+=================================+=======+====================+ |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 267 | | ``x + y`` | sum of *x* and *y* | | | |
| 268 | +---------------------+---------------------------------+-------+--------------------+ |
| 269 | | ``x - y`` | difference of *x* and *y* | | | |
| 270 | +---------------------+---------------------------------+-------+--------------------+ |
| 271 | | ``x * y`` | product of *x* and *y* | | | |
| 272 | +---------------------+---------------------------------+-------+--------------------+ |
| 273 | | ``x / y`` | quotient of *x* and *y* | | | |
| 274 | +---------------------+---------------------------------+-------+--------------------+ |
| 275 | | ``x // y`` | floored quotient of *x* and | \(1) | | |
| 276 | | | *y* | | | |
| 277 | +---------------------+---------------------------------+-------+--------------------+ |
| 278 | | ``x % y`` | remainder of ``x / y`` | \(2) | | |
| 279 | +---------------------+---------------------------------+-------+--------------------+ |
| 280 | | ``-x`` | *x* negated | | | |
| 281 | +---------------------+---------------------------------+-------+--------------------+ |
| 282 | | ``+x`` | *x* unchanged | | | |
| 283 | +---------------------+---------------------------------+-------+--------------------+ |
| 284 | | ``abs(x)`` | absolute value or magnitude of | | :func:`abs` | |
| 285 | | | *x* | | | |
| 286 | +---------------------+---------------------------------+-------+--------------------+ |
| 287 | | ``int(x)`` | *x* converted to integer | \(3) | :func:`int` | |
| 288 | +---------------------+---------------------------------+-------+--------------------+ |
| 289 | | ``float(x)`` | *x* converted to floating point | | :func:`float` | |
| 290 | +---------------------+---------------------------------+-------+--------------------+ |
| 291 | | ``complex(re, im)`` | a complex number with real part | | :func:`complex` | |
| 292 | | | *re*, imaginary part *im*. | | | |
| 293 | | | *im* defaults to zero. | | | |
| 294 | +---------------------+---------------------------------+-------+--------------------+ |
| 295 | | ``c.conjugate()`` | conjugate of the complex number | | | |
| 296 | | | *c* | | | |
| 297 | +---------------------+---------------------------------+-------+--------------------+ |
| 298 | | ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` | |
| 299 | +---------------------+---------------------------------+-------+--------------------+ |
| 300 | | ``pow(x, y)`` | *x* to the power *y* | | :func:`pow` | |
| 301 | +---------------------+---------------------------------+-------+--------------------+ |
| 302 | | ``x ** y`` | *x* to the power *y* | | | |
| 303 | +---------------------+---------------------------------+-------+--------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | |
| 305 | .. index:: |
| 306 | triple: operations on; numeric; types |
| 307 | single: conjugate() (complex number method) |
| 308 | |
| 309 | Notes: |
| 310 | |
| 311 | (1) |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 312 | Also referred to as integer division. The resultant value is a whole |
| 313 | integer, though the result's type is not necessarily int. The result is |
| 314 | always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is |
| 315 | ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
| 317 | (2) |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 318 | Not for complex numbers. Instead convert to floats using :func:`abs` if |
| 319 | appropriate. |
| 320 | |
| 321 | (3) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | .. index:: |
| 323 | module: math |
| 324 | single: floor() (in module math) |
| 325 | single: ceil() (in module math) |
| 326 | pair: numeric; conversions |
| 327 | pair: C; language |
| 328 | |
| 329 | Conversion from floating point to (long or plain) integer may round or truncate |
| 330 | as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module |
| 331 | for well-defined conversions. |
| 332 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | .. % XXXJH exceptions: overflow (when? what operations?) zerodivision |
| 334 | |
| 335 | |
| 336 | .. _bitstring-ops: |
| 337 | |
| 338 | Bit-string Operations on Integer Types |
| 339 | -------------------------------------- |
| 340 | |
| 341 | .. _bit-string-operations: |
| 342 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 343 | Integers support additional operations that make sense only for bit-strings. |
| 344 | Negative numbers are treated as their 2's complement value (this assumes a |
| 345 | sufficiently large number of bits that no overflow occurs during the operation). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
| 347 | The priorities of the binary bit-wise operations are all lower than the numeric |
| 348 | operations and higher than the comparisons; the unary operation ``~`` has the |
| 349 | same priority as the other unary numeric operations (``+`` and ``-``). |
| 350 | |
| 351 | This table lists the bit-string operations sorted in ascending priority |
| 352 | (operations in the same box have the same priority): |
| 353 | |
| 354 | +------------+--------------------------------+----------+ |
| 355 | | Operation | Result | Notes | |
| 356 | +============+================================+==========+ |
| 357 | | ``x | y`` | bitwise :dfn:`or` of *x* and | | |
| 358 | | | *y* | | |
| 359 | +------------+--------------------------------+----------+ |
| 360 | | ``x ^ y`` | bitwise :dfn:`exclusive or` of | | |
| 361 | | | *x* and *y* | | |
| 362 | +------------+--------------------------------+----------+ |
| 363 | | ``x & y`` | bitwise :dfn:`and` of *x* and | | |
| 364 | | | *y* | | |
| 365 | +------------+--------------------------------+----------+ |
| 366 | | ``x << n`` | *x* shifted left by *n* bits | (1), (2) | |
| 367 | +------------+--------------------------------+----------+ |
| 368 | | ``x >> n`` | *x* shifted right by *n* bits | (1), (3) | |
| 369 | +------------+--------------------------------+----------+ |
| 370 | | ``~x`` | the bits of *x* inverted | | |
| 371 | +------------+--------------------------------+----------+ |
| 372 | |
| 373 | .. index:: |
| 374 | triple: operations on; integer; types |
| 375 | pair: bit-string; operations |
| 376 | pair: shifting; operations |
| 377 | pair: masking; operations |
| 378 | |
| 379 | Notes: |
| 380 | |
| 381 | (1) |
| 382 | Negative shift counts are illegal and cause a :exc:`ValueError` to be raised. |
| 383 | |
| 384 | (2) |
| 385 | A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)`` |
| 386 | without overflow check. |
| 387 | |
| 388 | (3) |
| 389 | A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without |
| 390 | overflow check. |
| 391 | |
| 392 | |
| 393 | .. _typeiter: |
| 394 | |
| 395 | Iterator Types |
| 396 | ============== |
| 397 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | .. index:: |
| 399 | single: iterator protocol |
| 400 | single: protocol; iterator |
| 401 | single: sequence; iteration |
| 402 | single: container; iteration over |
| 403 | |
| 404 | Python supports a concept of iteration over containers. This is implemented |
| 405 | using two distinct methods; these are used to allow user-defined classes to |
| 406 | support iteration. Sequences, described below in more detail, always support |
| 407 | the iteration methods. |
| 408 | |
| 409 | One method needs to be defined for container objects to provide iteration |
| 410 | support: |
| 411 | |
| 412 | |
| 413 | .. method:: container.__iter__() |
| 414 | |
| 415 | Return an iterator object. The object is required to support the iterator |
| 416 | protocol described below. If a container supports different types of |
| 417 | iteration, additional methods can be provided to specifically request |
| 418 | iterators for those iteration types. (An example of an object supporting |
| 419 | multiple forms of iteration would be a tree structure which supports both |
| 420 | breadth-first and depth-first traversal.) This method corresponds to the |
| 421 | :attr:`tp_iter` slot of the type structure for Python objects in the Python/C |
| 422 | API. |
| 423 | |
| 424 | The iterator objects themselves are required to support the following two |
| 425 | methods, which together form the :dfn:`iterator protocol`: |
| 426 | |
| 427 | |
| 428 | .. method:: iterator.__iter__() |
| 429 | |
| 430 | Return the iterator object itself. This is required to allow both containers |
| 431 | and iterators to be used with the :keyword:`for` and :keyword:`in` statements. |
| 432 | This method corresponds to the :attr:`tp_iter` slot of the type structure for |
| 433 | Python objects in the Python/C API. |
| 434 | |
| 435 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 436 | .. method:: iterator.__next__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | |
| 438 | Return the next item from the container. If there are no further items, raise |
| 439 | the :exc:`StopIteration` exception. This method corresponds to the |
| 440 | :attr:`tp_iternext` slot of the type structure for Python objects in the |
| 441 | Python/C API. |
| 442 | |
| 443 | Python defines several iterator objects to support iteration over general and |
| 444 | specific sequence types, dictionaries, and other more specialized forms. The |
| 445 | specific types are not important beyond their implementation of the iterator |
| 446 | protocol. |
| 447 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 448 | Once an iterator's :meth:`__next__` method raises :exc:`StopIteration`, it must |
| 449 | continue to do so on subsequent calls. Implementations that do not obey this |
| 450 | property are deemed broken. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 451 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 452 | Python's :term:`generator`\s provide a convenient way to implement the iterator |
| 453 | protocol. If a container object's :meth:`__iter__` method is implemented as a |
| 454 | generator, it will automatically return an iterator object (technically, a |
| 455 | generator object) supplying the :meth:`__iter__` and :meth:`__next__` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
| 457 | |
| 458 | .. _typesseq: |
| 459 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 460 | Sequence Types --- :class:`str`, :class:`bytes`, :class:`buffer`, :class:`list`, :class:`tuple`, :class:`range` |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 461 | =============================================================================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 463 | There are five sequence types: strings, byte sequences, buffers, lists, tuples, |
| 464 | and range objects. (For other containers see the built-in :class:`dict`, |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 465 | :class:`list`, :class:`set`, and :class:`tuple` classes, and the |
| 466 | :mod:`collections` module.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | |
| 468 | .. index:: |
| 469 | object: sequence |
| 470 | object: string |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 471 | object: bytes |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 472 | object: buffer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 473 | object: tuple |
| 474 | object: list |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 475 | object: range |
| 476 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 477 | Strings contain Unicode characters. Their literals are written in single or |
| 478 | double quotes: ``'xyzzy'``, ``"frobozz"``. See :ref:`strings` for more about |
| 479 | string literals. In addition to the functionality described here, there are |
| 480 | also string-specific methods described in the :ref:`string-methods` section. |
| 481 | |
| 482 | Bytes and buffer objects contain single bytes -- the former is immutable while |
| 483 | the latter is a mutable sequence. Bytes objects can be constructed from |
| 484 | literals too; use a ``b`` prefix with normal string syntax: ``b'xyzzy'``. |
| 485 | To construct buffer objects, use the :func:`buffer` function. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 486 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 487 | .. warning:: |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 488 | |
| 489 | While string objects are sequences of characters (represented by strings of |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 490 | length 1), bytes and buffer objects are sequences of *integers* (between 0 |
| 491 | and 255), representing the ASCII value of single bytes. That means that for |
| 492 | a bytes or buffer object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` |
| 493 | will be a bytes or buffer object of length 1. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 494 | |
Georg Brandl | 2326a79 | 2007-09-01 12:08:51 +0000 | [diff] [blame] | 495 | Also, while in previous Python versions, byte strings and Unicode strings |
| 496 | could be exchanged for each other rather freely (barring encoding issues), |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 497 | strings and bytes are now completely separate concepts. There's no implicit |
| 498 | en-/decoding if you pass and object of the wrong type. A string always |
| 499 | compares unequal to a bytes or buffer object. |
Georg Brandl | 2326a79 | 2007-09-01 12:08:51 +0000 | [diff] [blame] | 500 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 501 | Lists are constructed with square brackets, separating items with commas: ``[a, |
| 502 | b, c]``. Tuples are constructed by the comma operator (not within square |
| 503 | brackets), with or without enclosing parentheses, but an empty tuple must have |
| 504 | the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple |
| 505 | must have a trailing comma, such as ``(d,)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 507 | Objects of type range are similar to buffers in that there is no specific syntax |
| 508 | to create them, but they are created using the :func:`range` function. They |
| 509 | don't support slicing, concatenation or repetition, and using ``in``, ``not |
| 510 | in``, :func:`min` or :func:`max` on them is inefficient. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 511 | |
| 512 | Most sequence types support the following operations. The ``in`` and ``not in`` |
| 513 | operations have the same priorities as the comparison operations. The ``+`` and |
| 514 | ``*`` operations have the same priority as the corresponding numeric operations. |
| 515 | [#]_ |
| 516 | |
| 517 | This table lists the sequence operations sorted in ascending priority |
| 518 | (operations in the same box have the same priority). In the table, *s* and *t* |
| 519 | are sequences of the same type; *n*, *i* and *j* are integers: |
| 520 | |
| 521 | +------------------+--------------------------------+----------+ |
| 522 | | Operation | Result | Notes | |
| 523 | +==================+================================+==========+ |
| 524 | | ``x in s`` | ``True`` if an item of *s* is | \(1) | |
| 525 | | | equal to *x*, else ``False`` | | |
| 526 | +------------------+--------------------------------+----------+ |
| 527 | | ``x not in s`` | ``False`` if an item of *s* is | \(1) | |
| 528 | | | equal to *x*, else ``True`` | | |
| 529 | +------------------+--------------------------------+----------+ |
| 530 | | ``s + t`` | the concatenation of *s* and | \(6) | |
| 531 | | | *t* | | |
| 532 | +------------------+--------------------------------+----------+ |
| 533 | | ``s * n, n * s`` | *n* shallow copies of *s* | \(2) | |
| 534 | | | concatenated | | |
| 535 | +------------------+--------------------------------+----------+ |
| 536 | | ``s[i]`` | *i*'th item of *s*, origin 0 | \(3) | |
| 537 | +------------------+--------------------------------+----------+ |
| 538 | | ``s[i:j]`` | slice of *s* from *i* to *j* | (3), (4) | |
| 539 | +------------------+--------------------------------+----------+ |
| 540 | | ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3), (5) | |
| 541 | | | with step *k* | | |
| 542 | +------------------+--------------------------------+----------+ |
| 543 | | ``len(s)`` | length of *s* | | |
| 544 | +------------------+--------------------------------+----------+ |
| 545 | | ``min(s)`` | smallest item of *s* | | |
| 546 | +------------------+--------------------------------+----------+ |
| 547 | | ``max(s)`` | largest item of *s* | | |
| 548 | +------------------+--------------------------------+----------+ |
| 549 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 550 | Sequence types also support comparisons. In particular, tuples and lists are |
| 551 | compared lexicographically by comparing corresponding elements. This means that |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 552 | to compare equal, every element must compare equal and the two sequences must be |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 553 | of the same type and have the same length. (For full details see |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 554 | :ref:`comparisons` in the language reference.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 555 | |
| 556 | .. index:: |
| 557 | triple: operations on; sequence; types |
| 558 | builtin: len |
| 559 | builtin: min |
| 560 | builtin: max |
| 561 | pair: concatenation; operation |
| 562 | pair: repetition; operation |
| 563 | pair: subscript; operation |
| 564 | pair: slice; operation |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 565 | operator: in |
| 566 | operator: not in |
| 567 | |
| 568 | Notes: |
| 569 | |
| 570 | (1) |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 571 | When *s* is a string object, the ``in`` and ``not in`` operations act like a |
| 572 | substring test. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 573 | |
| 574 | (2) |
| 575 | Values of *n* less than ``0`` are treated as ``0`` (which yields an empty |
| 576 | sequence of the same type as *s*). Note also that the copies are shallow; |
| 577 | nested structures are not copied. This often haunts new Python programmers; |
| 578 | consider:: |
| 579 | |
| 580 | >>> lists = [[]] * 3 |
| 581 | >>> lists |
| 582 | [[], [], []] |
| 583 | >>> lists[0].append(3) |
| 584 | >>> lists |
| 585 | [[3], [3], [3]] |
| 586 | |
| 587 | What has happened is that ``[[]]`` is a one-element list containing an empty |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 588 | list, so all three elements of ``[[]] * 3`` are (pointers to) this single |
| 589 | empty list. Modifying any of the elements of ``lists`` modifies this single |
| 590 | list. You can create a list of different lists this way:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 | |
| 592 | >>> lists = [[] for i in range(3)] |
| 593 | >>> lists[0].append(3) |
| 594 | >>> lists[1].append(5) |
| 595 | >>> lists[2].append(7) |
| 596 | >>> lists |
| 597 | [[3], [5], [7]] |
| 598 | |
| 599 | (3) |
| 600 | If *i* or *j* is negative, the index is relative to the end of the string: |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 601 | ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is |
| 602 | still ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 603 | |
| 604 | (4) |
| 605 | The slice of *s* from *i* to *j* is defined as the sequence of items with index |
| 606 | *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use |
| 607 | ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or |
| 608 | ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is |
| 609 | empty. |
| 610 | |
| 611 | (5) |
| 612 | The slice of *s* from *i* to *j* with step *k* is defined as the sequence of |
| 613 | items with index ``x = i + n*k`` such that 0 ≤n < (j-i)/(k). In other words, |
| 614 | the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when |
| 615 | *j* is reached (but never including *j*). If *i* or *j* is greater than |
| 616 | ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become |
| 617 | "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero. |
| 618 | If *k* is ``None``, it is treated like ``1``. |
| 619 | |
| 620 | (6) |
| 621 | If *s* and *t* are both strings, some Python implementations such as CPython can |
| 622 | usually perform an in-place optimization for assignments of the form ``s=s+t`` |
| 623 | or ``s+=t``. When applicable, this optimization makes quadratic run-time much |
| 624 | less likely. This optimization is both version and implementation dependent. |
| 625 | For performance sensitive code, it is preferable to use the :meth:`str.join` |
| 626 | method which assures consistent linear concatenation performance across versions |
| 627 | and implementations. |
| 628 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 629 | |
| 630 | .. _string-methods: |
| 631 | |
| 632 | String Methods |
| 633 | -------------- |
| 634 | |
| 635 | .. index:: pair: string; methods |
| 636 | |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 637 | String objects support the methods listed below. Note that none of these |
| 638 | methods take keyword arguments. |
| 639 | |
| 640 | In addition, Python's strings support the sequence type methods described in |
| 641 | the :ref:`typesseq` section. To output formatted strings, see the |
| 642 | :ref:`string-formatting` section. Also, see the :mod:`re` module for string |
| 643 | functions based on regular expressions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 644 | |
| 645 | .. method:: str.capitalize() |
| 646 | |
| 647 | Return a copy of the string with only its first character capitalized. |
| 648 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 649 | |
| 650 | .. method:: str.center(width[, fillchar]) |
| 651 | |
| 652 | Return centered in a string of length *width*. Padding is done using the |
| 653 | specified *fillchar* (default is a space). |
| 654 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 655 | |
| 656 | .. method:: str.count(sub[, start[, end]]) |
| 657 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 658 | Return the number of occurrences of substring *sub* in the range [*start*, |
| 659 | *end*]. Optional arguments *start* and *end* are interpreted as in slice |
| 660 | notation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 661 | |
| 662 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 663 | .. method:: str.encode([encoding[, errors]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 | |
| 665 | Return an encoded version of the string. Default encoding is the current |
| 666 | default string encoding. *errors* may be given to set a different error |
| 667 | handling scheme. The default for *errors* is ``'strict'``, meaning that |
| 668 | encoding errors raise a :exc:`UnicodeError`. Other possible values are |
| 669 | ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and |
| 670 | any other name registered via :func:`codecs.register_error`, see section |
| 671 | :ref:`codec-base-classes`. For a list of possible encodings, see section |
| 672 | :ref:`standard-encodings`. |
| 673 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 674 | |
| 675 | .. method:: str.endswith(suffix[, start[, end]]) |
| 676 | |
| 677 | Return ``True`` if the string ends with the specified *suffix*, otherwise return |
| 678 | ``False``. *suffix* can also be a tuple of suffixes to look for. With optional |
| 679 | *start*, test beginning at that position. With optional *end*, stop comparing |
| 680 | at that position. |
| 681 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 682 | |
| 683 | .. method:: str.expandtabs([tabsize]) |
| 684 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 685 | Return a copy of the string where all tab characters are replaced by one or |
| 686 | more spaces, depending on the current column and the given tab size. The |
| 687 | column number is reset to zero after each newline occurring in the string. |
| 688 | If *tabsize* is not given, a tab size of ``8`` characters is assumed. This |
| 689 | doesn't understand other non-printing characters or escape sequences. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 690 | |
| 691 | |
| 692 | .. method:: str.find(sub[, start[, end]]) |
| 693 | |
| 694 | Return the lowest index in the string where substring *sub* is found, such that |
| 695 | *sub* is contained in the range [*start*, *end*]. Optional arguments *start* |
| 696 | and *end* are interpreted as in slice notation. Return ``-1`` if *sub* is not |
| 697 | found. |
| 698 | |
| 699 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 700 | .. method:: str.format(format_string, *args, **ksargs) |
| 701 | |
| 702 | Perform a string formatting operation. The *format_string* argument can |
| 703 | contain literal text or replacement fields delimited by braces ``{}``. Each |
| 704 | replacement field contains either the numeric index of a positional argument, |
| 705 | or the name of a keyword argument. Returns a copy of *format_string* where |
| 706 | each replacement field is replaced with the string value of the corresponding |
| 707 | argument. |
| 708 | |
| 709 | >>> "The sum of 1 + 2 is {0}".format(1+2) |
| 710 | 'The sum of 1 + 2 is 3' |
| 711 | |
| 712 | See :ref:`formatstrings` for a description of the various formatting options |
| 713 | that can be specified in format strings. |
| 714 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 715 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 716 | .. method:: str.index(sub[, start[, end]]) |
| 717 | |
| 718 | Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found. |
| 719 | |
| 720 | |
| 721 | .. method:: str.isalnum() |
| 722 | |
| 723 | Return true if all characters in the string are alphanumeric and there is at |
| 724 | least one character, false otherwise. |
| 725 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 726 | |
| 727 | .. method:: str.isalpha() |
| 728 | |
| 729 | Return true if all characters in the string are alphabetic and there is at least |
| 730 | one character, false otherwise. |
| 731 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 732 | |
| 733 | .. method:: str.isdigit() |
| 734 | |
| 735 | Return true if all characters in the string are digits and there is at least one |
| 736 | character, false otherwise. |
| 737 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 738 | |
| 739 | .. method:: str.isidentifier() |
| 740 | |
| 741 | Return true if the string is a valid identifier according to the language |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 742 | definition, section :ref:`identifiers`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 743 | |
| 744 | |
| 745 | .. method:: str.islower() |
| 746 | |
| 747 | Return true if all cased characters in the string are lowercase and there is at |
| 748 | least one cased character, false otherwise. |
| 749 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 750 | |
| 751 | .. method:: str.isspace() |
| 752 | |
| 753 | Return true if there are only whitespace characters in the string and there is |
| 754 | at least one character, false otherwise. |
| 755 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 756 | |
| 757 | .. method:: str.istitle() |
| 758 | |
| 759 | Return true if the string is a titlecased string and there is at least one |
| 760 | character, for example uppercase characters may only follow uncased characters |
| 761 | and lowercase characters only cased ones. Return false otherwise. |
| 762 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 763 | |
| 764 | .. method:: str.isupper() |
| 765 | |
| 766 | Return true if all cased characters in the string are uppercase and there is at |
| 767 | least one cased character, false otherwise. |
| 768 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 769 | |
| 770 | .. method:: str.join(seq) |
| 771 | |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 772 | Return a string which is the concatenation of the values in the sequence |
| 773 | *seq*. Non-string values in *seq* will be converted to a string using their |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 774 | respective ``str()`` value. If there are any :class:`bytes` objects in |
| 775 | *seq*, a :exc:`TypeError` will be raised. The separator between elements is |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 776 | the string providing this method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 777 | |
| 778 | |
| 779 | .. method:: str.ljust(width[, fillchar]) |
| 780 | |
| 781 | Return the string left justified in a string of length *width*. Padding is done |
| 782 | using the specified *fillchar* (default is a space). The original string is |
| 783 | returned if *width* is less than ``len(s)``. |
| 784 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
| 786 | .. method:: str.lower() |
| 787 | |
| 788 | Return a copy of the string converted to lowercase. |
| 789 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 790 | |
| 791 | .. method:: str.lstrip([chars]) |
| 792 | |
| 793 | Return a copy of the string with leading characters removed. The *chars* |
| 794 | argument is a string specifying the set of characters to be removed. If omitted |
| 795 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
| 796 | argument is not a prefix; rather, all combinations of its values are stripped:: |
| 797 | |
| 798 | >>> ' spacious '.lstrip() |
| 799 | 'spacious ' |
| 800 | >>> 'www.example.com'.lstrip('cmowz.') |
| 801 | 'example.com' |
| 802 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 803 | |
| 804 | .. method:: str.partition(sep) |
| 805 | |
| 806 | Split the string at the first occurrence of *sep*, and return a 3-tuple |
| 807 | containing the part before the separator, the separator itself, and the part |
| 808 | after the separator. If the separator is not found, return a 3-tuple containing |
| 809 | the string itself, followed by two empty strings. |
| 810 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 811 | |
| 812 | .. method:: str.replace(old, new[, count]) |
| 813 | |
| 814 | Return a copy of the string with all occurrences of substring *old* replaced by |
| 815 | *new*. If the optional argument *count* is given, only the first *count* |
| 816 | occurrences are replaced. |
| 817 | |
| 818 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 819 | .. method:: str.rfind(sub[, start[, end]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 820 | |
| 821 | Return the highest index in the string where substring *sub* is found, such that |
| 822 | *sub* is contained within s[start,end]. Optional arguments *start* and *end* |
| 823 | are interpreted as in slice notation. Return ``-1`` on failure. |
| 824 | |
| 825 | |
| 826 | .. method:: str.rindex(sub[, start[, end]]) |
| 827 | |
| 828 | Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not |
| 829 | found. |
| 830 | |
| 831 | |
| 832 | .. method:: str.rjust(width[, fillchar]) |
| 833 | |
| 834 | Return the string right justified in a string of length *width*. Padding is done |
| 835 | using the specified *fillchar* (default is a space). The original string is |
| 836 | returned if *width* is less than ``len(s)``. |
| 837 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 838 | |
| 839 | .. method:: str.rpartition(sep) |
| 840 | |
| 841 | Split the string at the last occurrence of *sep*, and return a 3-tuple |
| 842 | containing the part before the separator, the separator itself, and the part |
| 843 | after the separator. If the separator is not found, return a 3-tuple containing |
| 844 | two empty strings, followed by the string itself. |
| 845 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 846 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 847 | .. method:: str.rsplit([sep[, maxsplit]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 848 | |
| 849 | Return a list of the words in the string, using *sep* as the delimiter string. |
| 850 | If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* |
| 851 | ones. If *sep* is not specified or ``None``, any whitespace string is a |
| 852 | separator. Except for splitting from the right, :meth:`rsplit` behaves like |
| 853 | :meth:`split` which is described in detail below. |
| 854 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 855 | |
| 856 | .. method:: str.rstrip([chars]) |
| 857 | |
| 858 | Return a copy of the string with trailing characters removed. The *chars* |
| 859 | argument is a string specifying the set of characters to be removed. If omitted |
| 860 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
| 861 | argument is not a suffix; rather, all combinations of its values are stripped:: |
| 862 | |
| 863 | >>> ' spacious '.rstrip() |
| 864 | ' spacious' |
| 865 | >>> 'mississippi'.rstrip('ipz') |
| 866 | 'mississ' |
| 867 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 868 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 869 | .. method:: str.split([sep[, maxsplit]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 870 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 871 | Return a list of the words in the string, using *sep* as the delimiter |
| 872 | string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, |
| 873 | the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not |
| 874 | specified, then there is no limit on the number of splits (all possible |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 875 | splits are made). |
| 876 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 877 | If *sep* is given, consecutive delimiters are not grouped together and are |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 878 | deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns |
| 879 | ``['1', '', '2']``). The *sep* argument may consist of multiple characters |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 880 | (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``). |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 881 | Splitting an empty string with a specified separator returns ``['']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 882 | |
| 883 | If *sep* is not specified or is ``None``, a different splitting algorithm is |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 884 | applied: runs of consecutive whitespace are regarded as a single separator, |
| 885 | and the result will contain no empty strings at the start or end if the |
| 886 | string has leading or trailing whitespace. Consequently, splitting an empty |
| 887 | string or a string consisting of just whitespace with a ``None`` separator |
| 888 | returns ``[]``. |
| 889 | |
| 890 | For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and |
| 891 | ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 892 | |
| 893 | |
| 894 | .. method:: str.splitlines([keepends]) |
| 895 | |
| 896 | Return a list of the lines in the string, breaking at line boundaries. Line |
| 897 | breaks are not included in the resulting list unless *keepends* is given and |
| 898 | true. |
| 899 | |
| 900 | |
| 901 | .. method:: str.startswith(prefix[, start[, end]]) |
| 902 | |
| 903 | Return ``True`` if string starts with the *prefix*, otherwise return ``False``. |
| 904 | *prefix* can also be a tuple of prefixes to look for. With optional *start*, |
| 905 | test string beginning at that position. With optional *end*, stop comparing |
| 906 | string at that position. |
| 907 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 908 | |
| 909 | .. method:: str.strip([chars]) |
| 910 | |
| 911 | Return a copy of the string with the leading and trailing characters removed. |
| 912 | The *chars* argument is a string specifying the set of characters to be removed. |
| 913 | If omitted or ``None``, the *chars* argument defaults to removing whitespace. |
| 914 | The *chars* argument is not a prefix or suffix; rather, all combinations of its |
| 915 | values are stripped:: |
| 916 | |
| 917 | >>> ' spacious '.strip() |
| 918 | 'spacious' |
| 919 | >>> 'www.example.com'.strip('cmowz.') |
| 920 | 'example' |
| 921 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 922 | |
| 923 | .. method:: str.swapcase() |
| 924 | |
| 925 | Return a copy of the string with uppercase characters converted to lowercase and |
| 926 | vice versa. |
| 927 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 928 | |
| 929 | .. method:: str.title() |
| 930 | |
| 931 | Return a titlecased version of the string: words start with uppercase |
| 932 | characters, all remaining cased characters are lowercase. |
| 933 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 934 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 935 | .. method:: str.translate(map) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 936 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 937 | Return a copy of the *s* where all characters have been mapped through the |
Georg Brandl | cb8ecb1 | 2007-09-04 06:35:14 +0000 | [diff] [blame] | 938 | *map* which must be a dictionary of characters (strings of length 1) or |
| 939 | Unicode ordinals (integers) to Unicode ordinals, strings or ``None``. |
Georg Brandl | 94c2c75 | 2007-10-23 06:52:59 +0000 | [diff] [blame] | 940 | Unmapped characters are left untouched. Characters mapped to ``None`` are |
Georg Brandl | cb8ecb1 | 2007-09-04 06:35:14 +0000 | [diff] [blame] | 941 | deleted. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 942 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 943 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 944 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 945 | A more flexible approach is to create a custom character mapping codec |
| 946 | using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an |
| 947 | example). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 948 | |
| 949 | |
| 950 | .. method:: str.upper() |
| 951 | |
| 952 | Return a copy of the string converted to uppercase. |
| 953 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 954 | |
| 955 | .. method:: str.zfill(width) |
| 956 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 957 | Return the numeric string left filled with zeros in a string of length |
| 958 | *width*. A sign prefix is handled correctly. The original string is |
| 959 | returned if *width* is less than ``len(s)``. |
| 960 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 961 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 962 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 963 | .. _old-string-formatting: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 964 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 965 | Old String Formatting Operations |
| 966 | -------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 967 | |
| 968 | .. index:: |
| 969 | single: formatting, string (%) |
| 970 | single: interpolation, string (%) |
| 971 | single: string; formatting |
| 972 | single: string; interpolation |
| 973 | single: printf-style formatting |
| 974 | single: sprintf-style formatting |
| 975 | single: % formatting |
| 976 | single: % interpolation |
| 977 | |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 978 | .. XXX is the note enough? |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 979 | |
| 980 | .. note:: |
| 981 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 982 | The formatting operations described here are obsolete and may go away in future |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 983 | versions of Python. Use the new :ref:`string-formatting` in new code. |
| 984 | |
| 985 | String objects have one unique built-in operation: the ``%`` operator (modulo). |
| 986 | This is also known as the string *formatting* or *interpolation* operator. |
| 987 | Given ``format % values`` (where *format* is a string), ``%`` conversion |
| 988 | specifications in *format* are replaced with zero or more elements of *values*. |
| 989 | The effect is similar to the using :cfunc:`sprintf` in the C language. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 990 | |
| 991 | If *format* requires a single argument, *values* may be a single non-tuple |
| 992 | object. [#]_ Otherwise, *values* must be a tuple with exactly the number of |
| 993 | items specified by the format string, or a single mapping object (for example, a |
| 994 | dictionary). |
| 995 | |
| 996 | A conversion specifier contains two or more characters and has the following |
| 997 | components, which must occur in this order: |
| 998 | |
| 999 | #. The ``'%'`` character, which marks the start of the specifier. |
| 1000 | |
| 1001 | #. Mapping key (optional), consisting of a parenthesised sequence of characters |
| 1002 | (for example, ``(somename)``). |
| 1003 | |
| 1004 | #. Conversion flags (optional), which affect the result of some conversion |
| 1005 | types. |
| 1006 | |
| 1007 | #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the |
| 1008 | actual width is read from the next element of the tuple in *values*, and the |
| 1009 | object to convert comes after the minimum field width and optional precision. |
| 1010 | |
| 1011 | #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If |
| 1012 | specified as ``'*'`` (an asterisk), the actual width is read from the next |
| 1013 | element of the tuple in *values*, and the value to convert comes after the |
| 1014 | precision. |
| 1015 | |
| 1016 | #. Length modifier (optional). |
| 1017 | |
| 1018 | #. Conversion type. |
| 1019 | |
| 1020 | When the right argument is a dictionary (or other mapping type), then the |
| 1021 | formats in the string *must* include a parenthesised mapping key into that |
| 1022 | dictionary inserted immediately after the ``'%'`` character. The mapping key |
| 1023 | selects the value to be formatted from the mapping. For example:: |
| 1024 | |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 1025 | >>> print('%(language)s has %(#)03d quote types.' % |
| 1026 | {'language': "Python", "#": 2}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1027 | Python has 002 quote types. |
| 1028 | |
| 1029 | In this case no ``*`` specifiers may occur in a format (since they require a |
| 1030 | sequential parameter list). |
| 1031 | |
| 1032 | The conversion flag characters are: |
| 1033 | |
| 1034 | +---------+---------------------------------------------------------------------+ |
| 1035 | | Flag | Meaning | |
| 1036 | +=========+=====================================================================+ |
| 1037 | | ``'#'`` | The value conversion will use the "alternate form" (where defined | |
| 1038 | | | below). | |
| 1039 | +---------+---------------------------------------------------------------------+ |
| 1040 | | ``'0'`` | The conversion will be zero padded for numeric values. | |
| 1041 | +---------+---------------------------------------------------------------------+ |
| 1042 | | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | |
| 1043 | | | conversion if both are given). | |
| 1044 | +---------+---------------------------------------------------------------------+ |
| 1045 | | ``' '`` | (a space) A blank should be left before a positive number (or empty | |
| 1046 | | | string) produced by a signed conversion. | |
| 1047 | +---------+---------------------------------------------------------------------+ |
| 1048 | | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | |
| 1049 | | | (overrides a "space" flag). | |
| 1050 | +---------+---------------------------------------------------------------------+ |
| 1051 | |
| 1052 | A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it |
| 1053 | is not necessary for Python. |
| 1054 | |
| 1055 | The conversion types are: |
| 1056 | |
| 1057 | +------------+-----------------------------------------------------+-------+ |
| 1058 | | Conversion | Meaning | Notes | |
| 1059 | +============+=====================================================+=======+ |
| 1060 | | ``'d'`` | Signed integer decimal. | | |
| 1061 | +------------+-----------------------------------------------------+-------+ |
| 1062 | | ``'i'`` | Signed integer decimal. | | |
| 1063 | +------------+-----------------------------------------------------+-------+ |
| 1064 | | ``'o'`` | Unsigned octal. | \(1) | |
| 1065 | +------------+-----------------------------------------------------+-------+ |
| 1066 | | ``'u'`` | Unsigned decimal. | | |
| 1067 | +------------+-----------------------------------------------------+-------+ |
| 1068 | | ``'x'`` | Unsigned hexadecimal (lowercase). | \(2) | |
| 1069 | +------------+-----------------------------------------------------+-------+ |
| 1070 | | ``'X'`` | Unsigned hexadecimal (uppercase). | \(2) | |
| 1071 | +------------+-----------------------------------------------------+-------+ |
| 1072 | | ``'e'`` | Floating point exponential format (lowercase). | \(3) | |
| 1073 | +------------+-----------------------------------------------------+-------+ |
| 1074 | | ``'E'`` | Floating point exponential format (uppercase). | \(3) | |
| 1075 | +------------+-----------------------------------------------------+-------+ |
| 1076 | | ``'f'`` | Floating point decimal format. | \(3) | |
| 1077 | +------------+-----------------------------------------------------+-------+ |
| 1078 | | ``'F'`` | Floating point decimal format. | \(3) | |
| 1079 | +------------+-----------------------------------------------------+-------+ |
| 1080 | | ``'g'`` | Floating point format. Uses exponential format if | \(4) | |
| 1081 | | | exponent is greater than -4 or less than precision, | | |
| 1082 | | | decimal format otherwise. | | |
| 1083 | +------------+-----------------------------------------------------+-------+ |
| 1084 | | ``'G'`` | Floating point format. Uses exponential format if | \(4) | |
| 1085 | | | exponent is greater than -4 or less than precision, | | |
| 1086 | | | decimal format otherwise. | | |
| 1087 | +------------+-----------------------------------------------------+-------+ |
| 1088 | | ``'c'`` | Single character (accepts integer or single | | |
| 1089 | | | character string). | | |
| 1090 | +------------+-----------------------------------------------------+-------+ |
| 1091 | | ``'r'`` | String (converts any python object using | \(5) | |
| 1092 | | | :func:`repr`). | | |
| 1093 | +------------+-----------------------------------------------------+-------+ |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1094 | | ``'s'`` | String (converts any python object using | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1095 | | | :func:`str`). | | |
| 1096 | +------------+-----------------------------------------------------+-------+ |
| 1097 | | ``'%'`` | No argument is converted, results in a ``'%'`` | | |
| 1098 | | | character in the result. | | |
| 1099 | +------------+-----------------------------------------------------+-------+ |
| 1100 | |
| 1101 | Notes: |
| 1102 | |
| 1103 | (1) |
| 1104 | The alternate form causes a leading zero (``'0'``) to be inserted between |
| 1105 | left-hand padding and the formatting of the number if the leading character |
| 1106 | of the result is not already a zero. |
| 1107 | |
| 1108 | (2) |
| 1109 | The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether |
| 1110 | the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding |
| 1111 | and the formatting of the number if the leading character of the result is not |
| 1112 | already a zero. |
| 1113 | |
| 1114 | (3) |
| 1115 | The alternate form causes the result to always contain a decimal point, even if |
| 1116 | no digits follow it. |
| 1117 | |
| 1118 | The precision determines the number of digits after the decimal point and |
| 1119 | defaults to 6. |
| 1120 | |
| 1121 | (4) |
| 1122 | The alternate form causes the result to always contain a decimal point, and |
| 1123 | trailing zeroes are not removed as they would otherwise be. |
| 1124 | |
| 1125 | The precision determines the number of significant digits before and after the |
| 1126 | decimal point and defaults to 6. |
| 1127 | |
| 1128 | (5) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1129 | The precision determines the maximal number of characters used. |
| 1130 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1131 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1132 | Since Python strings have an explicit length, ``%s`` conversions do not assume |
| 1133 | that ``'\0'`` is the end of the string. |
| 1134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1135 | For safety reasons, floating point precisions are clipped to 50; ``%f`` |
| 1136 | conversions for numbers whose absolute value is over 1e25 are replaced by ``%g`` |
| 1137 | conversions. [#]_ All other errors raise exceptions. |
| 1138 | |
| 1139 | .. index:: |
| 1140 | module: string |
| 1141 | module: re |
| 1142 | |
| 1143 | Additional string operations are defined in standard modules :mod:`string` and |
| 1144 | :mod:`re`. |
| 1145 | |
| 1146 | |
| 1147 | .. _typesseq-range: |
| 1148 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 1149 | Range Type |
| 1150 | ---------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1151 | |
| 1152 | .. index:: object: range |
| 1153 | |
| 1154 | The :class:`range` type is an immutable sequence which is commonly used for |
| 1155 | looping. The advantage of the :class:`range` type is that an :class:`range` |
| 1156 | object will always take the same amount of memory, no matter the size of the |
| 1157 | range it represents. There are no consistent performance advantages. |
| 1158 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 1159 | Range objects have very little behavior: they only support indexing, iteration, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1160 | and the :func:`len` function. |
| 1161 | |
| 1162 | |
| 1163 | .. _typesseq-mutable: |
| 1164 | |
| 1165 | Mutable Sequence Types |
| 1166 | ---------------------- |
| 1167 | |
| 1168 | .. index:: |
| 1169 | triple: mutable; sequence; types |
| 1170 | object: list |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1171 | object: buffer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1172 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1173 | List and buffer objects support additional operations that allow in-place |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1174 | modification of the object. Other mutable sequence types (when added to the |
| 1175 | language) should also support these operations. Strings and tuples are |
| 1176 | immutable sequence types: such objects cannot be modified once created. The |
| 1177 | following operations are defined on mutable sequence types (where *x* is an |
| 1178 | arbitrary object). |
| 1179 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1180 | Note that while lists allow their items to be of any type, buffer object |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1181 | "items" are all integers in the range 0 <= x < 256. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1182 | |
| 1183 | +------------------------------+--------------------------------+---------------------+ |
| 1184 | | Operation | Result | Notes | |
| 1185 | +==============================+================================+=====================+ |
| 1186 | | ``s[i] = x`` | item *i* of *s* is replaced by | | |
| 1187 | | | *x* | | |
| 1188 | +------------------------------+--------------------------------+---------------------+ |
| 1189 | | ``s[i:j] = t`` | slice of *s* from *i* to *j* | | |
| 1190 | | | is replaced by the contents of | | |
| 1191 | | | the iterable *t* | | |
| 1192 | +------------------------------+--------------------------------+---------------------+ |
| 1193 | | ``del s[i:j]`` | same as ``s[i:j] = []`` | | |
| 1194 | +------------------------------+--------------------------------+---------------------+ |
| 1195 | | ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) | |
| 1196 | | | are replaced by those of *t* | | |
| 1197 | +------------------------------+--------------------------------+---------------------+ |
| 1198 | | ``del s[i:j:k]`` | removes the elements of | | |
| 1199 | | | ``s[i:j:k]`` from the list | | |
| 1200 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1201 | | ``s.append(x)`` | same as ``s[len(s):len(s)] = | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1202 | | | [x]`` | | |
| 1203 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1204 | | ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1205 | | | x`` | | |
| 1206 | +------------------------------+--------------------------------+---------------------+ |
| 1207 | | ``s.count(x)`` | return number of *i*'s for | | |
| 1208 | | | which ``s[i] == x`` | | |
| 1209 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1210 | | ``s.index(x[, i[, j]])`` | return smallest *k* such that | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1211 | | | ``s[k] == x`` and ``i <= k < | | |
| 1212 | | | j`` | | |
| 1213 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1214 | | ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | \(4) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1215 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1216 | | ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | \(5) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1217 | | | return x`` | | |
| 1218 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1219 | | ``s.remove(x)`` | same as ``del s[s.index(x)]`` | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1220 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1221 | | ``s.reverse()`` | reverses the items of *s* in | \(6) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1222 | | | place | | |
| 1223 | +------------------------------+--------------------------------+---------------------+ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1224 | | ``s.sort([cmp[, key[, | sort the items of *s* in place | (6), (7) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1225 | | reverse]]])`` | | | |
| 1226 | +------------------------------+--------------------------------+---------------------+ |
| 1227 | |
| 1228 | .. index:: |
| 1229 | triple: operations on; sequence; types |
| 1230 | triple: operations on; list; type |
| 1231 | pair: subscript; assignment |
| 1232 | pair: slice; assignment |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1233 | statement: del |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1234 | single: append() (sequence method) |
| 1235 | single: extend() (sequence method) |
| 1236 | single: count() (sequence method) |
| 1237 | single: index() (sequence method) |
| 1238 | single: insert() (sequence method) |
| 1239 | single: pop() (sequence method) |
| 1240 | single: remove() (sequence method) |
| 1241 | single: reverse() (sequence method) |
| 1242 | single: sort() (sequence method) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1243 | |
| 1244 | Notes: |
| 1245 | |
| 1246 | (1) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1247 | *t* must have the same length as the slice it is replacing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1248 | |
| 1249 | (2) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1250 | *x* can be any iterable object. |
| 1251 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1252 | (3) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1253 | Raises :exc:`ValueError` when *x* is not found in *s*. When a negative index is |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1254 | passed as the second or third parameter to the :meth:`index` method, the sequence |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1255 | length is added, as for slice indices. If it is still negative, it is truncated |
| 1256 | to zero, as for slice indices. |
| 1257 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1258 | (4) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1259 | When a negative index is passed as the first parameter to the :meth:`insert` |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1260 | method, the sequence length is added, as for slice indices. If it is still |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1261 | negative, it is truncated to zero, as for slice indices. |
| 1262 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1263 | (5) |
| 1264 | The optional argument *i* defaults to ``-1``, so that by default the last |
| 1265 | item is removed and returned. |
| 1266 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1267 | (6) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1268 | The :meth:`sort` and :meth:`reverse` methods modify the sequence in place for |
| 1269 | economy of space when sorting or reversing a large sequence. To remind you |
| 1270 | that they operate by side effect, they don't return the sorted or reversed |
| 1271 | sequence. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1272 | |
| 1273 | (7) |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1274 | :meth:`sort` is not supported by buffer objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1275 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1276 | The :meth:`sort` method takes optional arguments for controlling the |
| 1277 | comparisons. |
| 1278 | |
| 1279 | *cmp* specifies a custom comparison function of two arguments (list items) which |
| 1280 | should return a negative, zero or positive number depending on whether the first |
| 1281 | argument is considered smaller than, equal to, or larger than the second |
| 1282 | argument: ``cmp=lambda x,y: cmp(x.lower(), y.lower())`` |
| 1283 | |
| 1284 | *key* specifies a function of one argument that is used to extract a comparison |
| 1285 | key from each list element: ``key=str.lower`` |
| 1286 | |
| 1287 | *reverse* is a boolean value. If set to ``True``, then the list elements are |
| 1288 | sorted as if each comparison were reversed. |
| 1289 | |
| 1290 | In general, the *key* and *reverse* conversion processes are much faster than |
| 1291 | specifying an equivalent *cmp* function. This is because *cmp* is called |
| 1292 | multiple times for each list element while *key* and *reverse* touch each |
| 1293 | element only once. |
| 1294 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1295 | Starting with Python 2.3, the :meth:`sort` method is guaranteed to be stable. A |
| 1296 | sort is stable if it guarantees not to change the relative order of elements |
| 1297 | that compare equal --- this is helpful for sorting in multiple passes (for |
| 1298 | example, sort by department, then by salary grade). |
| 1299 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1300 | While a list is being sorted, the effect of attempting to mutate, or even |
| 1301 | inspect, the list is undefined. The C implementation of Python 2.3 and newer |
| 1302 | makes the list appear empty for the duration, and raises :exc:`ValueError` if it |
| 1303 | can detect that the list has been mutated during a sort. |
| 1304 | |
| 1305 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1306 | .. _bytes-methods: |
| 1307 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1308 | Bytes and Buffer Methods |
| 1309 | ------------------------ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1310 | |
| 1311 | .. index:: pair: bytes; methods |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1312 | pair: buffer; methods |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1313 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1314 | Bytes and buffer objects, being "strings of bytes", have all methods found on |
| 1315 | strings, with the exception of :func:`encode`, :func:`format` and |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 1316 | :func:`isidentifier`, which do not make sense with these types. For converting |
| 1317 | the objects to strings, they have a :func:`decode` method. |
| 1318 | |
| 1319 | Wherever one of these methods needs to interpret the bytes as characters |
| 1320 | (e.g. the :func:`is...` methods), the ASCII character set is assumed. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1321 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1322 | .. note:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1323 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1324 | The methods on bytes and buffer objects don't accept strings as their |
| 1325 | arguments, just as the methods on strings don't accept bytes as their |
| 1326 | arguments. For example, you have to write :: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1327 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1328 | a = "abc" |
| 1329 | b = a.replace("a", "f") |
| 1330 | |
| 1331 | and :: |
| 1332 | |
| 1333 | a = b"abc" |
| 1334 | b = a.replace(b"a", b"f") |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1335 | |
| 1336 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1337 | The bytes and buffer types have an additional class method: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1338 | |
| 1339 | .. method:: bytes.fromhex(string) |
| 1340 | |
| 1341 | This :class:`bytes` class method returns a bytes object, decoding the given |
| 1342 | string object. The string must contain two hexadecimal digits per byte, spaces |
| 1343 | are ignored. |
| 1344 | |
| 1345 | Example:: |
| 1346 | |
| 1347 | >>> bytes.fromhex('f0 f1f2 ') |
| 1348 | b'\xf0\xf1\xf2' |
| 1349 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1350 | .. XXX verify/document translate() semantics! |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1351 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 1352 | .. method:: bytes.translate(table[, delete]) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1353 | |
| 1354 | Return a copy of the bytes object where all bytes occurring in the optional |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 1355 | argument *delete* are removed, and the remaining bytes have been mapped |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1356 | through the given translation table, which must be a bytes object of length |
| 1357 | 256. |
| 1358 | |
| 1359 | You can use the :func:`maketrans` helper function in the :mod:`string` module to |
| 1360 | create a translation table. |
| 1361 | |
| 1362 | .. XXX a None table doesn't seem to be supported |
Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 1363 | Set the *table* argument to ``None`` for translations that only delete characters:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1364 | |
| 1365 | >>> 'read this short text'.translate(None, 'aeiou') |
| 1366 | 'rd ths shrt txt' |
| 1367 | |
| 1368 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1369 | .. _types-set: |
| 1370 | |
| 1371 | Set Types --- :class:`set`, :class:`frozenset` |
| 1372 | ============================================== |
| 1373 | |
| 1374 | .. index:: object: set |
| 1375 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1376 | A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1377 | Common uses include membership testing, removing duplicates from a sequence, and |
| 1378 | computing mathematical operations such as intersection, union, difference, and |
| 1379 | symmetric difference. |
| 1380 | (For other containers see the built in :class:`dict`, :class:`list`, |
| 1381 | and :class:`tuple` classes, and the :mod:`collections` module.) |
| 1382 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1383 | Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in |
| 1384 | set``. Being an unordered collection, sets do not record element position or |
| 1385 | order of insertion. Accordingly, sets do not support indexing, slicing, or |
| 1386 | other sequence-like behavior. |
| 1387 | |
| 1388 | There are currently two builtin set types, :class:`set` and :class:`frozenset`. |
| 1389 | The :class:`set` type is mutable --- the contents can be changed using methods |
| 1390 | like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value |
| 1391 | and cannot be used as either a dictionary key or as an element of another set. |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1392 | The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1393 | altered after it is created; it can therefore be used as a dictionary key or as |
| 1394 | an element of another set. |
| 1395 | |
| 1396 | The constructors for both classes work the same: |
| 1397 | |
| 1398 | .. class:: set([iterable]) |
| 1399 | frozenset([iterable]) |
| 1400 | |
| 1401 | Return a new set or frozenset object whose elements are taken from |
| 1402 | *iterable*. The elements of a set must be hashable. To represent sets of |
| 1403 | sets, the inner sets must be :class:`frozenset` objects. If *iterable* is |
| 1404 | not specified, a new empty set is returned. |
| 1405 | |
| 1406 | Instances of :class:`set` and :class:`frozenset` provide the following |
| 1407 | operations: |
| 1408 | |
| 1409 | .. describe:: len(s) |
| 1410 | |
| 1411 | Return the cardinality of set *s*. |
| 1412 | |
| 1413 | .. describe:: x in s |
| 1414 | |
| 1415 | Test *x* for membership in *s*. |
| 1416 | |
| 1417 | .. describe:: x not in s |
| 1418 | |
| 1419 | Test *x* for non-membership in *s*. |
| 1420 | |
| 1421 | .. method:: set.issubset(other) |
| 1422 | set <= other |
| 1423 | |
| 1424 | Test whether every element in the set is in *other*. |
| 1425 | |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 1426 | .. method:: set < other |
| 1427 | |
| 1428 | Test whether the set is a true subset of *other*, that is, |
| 1429 | ``set <= other and set != other``. |
| 1430 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1431 | .. method:: set.issuperset(other) |
| 1432 | set >= other |
| 1433 | |
| 1434 | Test whether every element in *other* is in the set. |
| 1435 | |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 1436 | .. method:: set > other |
| 1437 | |
| 1438 | Test whether the set is a true superset of *other*, that is, |
| 1439 | ``set >= other and set != other``. |
| 1440 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1441 | .. method:: set.union(other) |
| 1442 | set | other |
| 1443 | |
| 1444 | Return a new set with elements from both sets. |
| 1445 | |
| 1446 | .. method:: set.intersection(other) |
| 1447 | set & other |
| 1448 | |
| 1449 | Return a new set with elements common to both sets. |
| 1450 | |
| 1451 | .. method:: set.difference(other) |
| 1452 | set - other |
| 1453 | |
| 1454 | Return a new set with elements in the set that are not in *other*. |
| 1455 | |
| 1456 | .. method:: set.symmetric_difference(other) |
| 1457 | set ^ other |
| 1458 | |
| 1459 | Return a new set with elements in either the set or *other* but not both. |
| 1460 | |
| 1461 | .. method:: set.copy() |
| 1462 | |
| 1463 | Return a new set with a shallow copy of *s*. |
| 1464 | |
| 1465 | |
| 1466 | Note, the non-operator versions of :meth:`union`, :meth:`intersection`, |
| 1467 | :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and |
| 1468 | :meth:`issuperset` methods will accept any iterable as an argument. In |
| 1469 | contrast, their operator based counterparts require their arguments to be sets. |
| 1470 | This precludes error-prone constructions like ``set('abc') & 'cbs'`` in favor of |
| 1471 | the more readable ``set('abc').intersection('cbs')``. |
| 1472 | |
| 1473 | Both :class:`set` and :class:`frozenset` support set to set comparisons. Two |
| 1474 | sets are equal if and only if every element of each set is contained in the |
| 1475 | other (each is a subset of the other). A set is less than another set if and |
| 1476 | only if the first set is a proper subset of the second set (is a subset, but is |
| 1477 | not equal). A set is greater than another set if and only if the first set is a |
| 1478 | proper superset of the second set (is a superset, but is not equal). |
| 1479 | |
| 1480 | Instances of :class:`set` are compared to instances of :class:`frozenset` based |
| 1481 | on their members. For example, ``set('abc') == frozenset('abc')`` returns |
| 1482 | ``True``. |
| 1483 | |
| 1484 | The subset and equality comparisons do not generalize to a complete ordering |
| 1485 | function. For example, any two disjoint sets are not equal and are not subsets |
| 1486 | of each other, so *all* of the following return ``False``: ``a<b``, ``a==b``, |
| 1487 | or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__` method. |
| 1488 | |
| 1489 | Since sets only define partial ordering (subset relationships), the output of |
| 1490 | the :meth:`list.sort` method is undefined for lists of sets. |
| 1491 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1492 | Set elements, like dictionary keys, must be :term:`hashable`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1493 | |
| 1494 | Binary operations that mix :class:`set` instances with :class:`frozenset` return |
| 1495 | the type of the first operand. For example: ``frozenset('ab') | set('bc')`` |
| 1496 | returns an instance of :class:`frozenset`. |
| 1497 | |
| 1498 | The following table lists operations available for :class:`set` that do not |
| 1499 | apply to immutable instances of :class:`frozenset`: |
| 1500 | |
| 1501 | .. method:: set.update(other) |
| 1502 | set |= other |
| 1503 | |
| 1504 | Update the set, adding elements from *other*. |
| 1505 | |
| 1506 | .. method:: set.intersection_update(other) |
| 1507 | set &= other |
| 1508 | |
| 1509 | Update the set, keeping only elements found in it and *other*. |
| 1510 | |
| 1511 | .. method:: set.difference_update(other) |
| 1512 | set -= other |
| 1513 | |
| 1514 | Update the set, removing elements found in *other*. |
| 1515 | |
| 1516 | .. method:: set.symmetric_difference_update(other) |
| 1517 | set ^= other |
| 1518 | |
| 1519 | Update the set, keeping only elements found in either set, but not in both. |
| 1520 | |
| 1521 | .. method:: set.add(el) |
| 1522 | |
| 1523 | Add element *el* to the set. |
| 1524 | |
| 1525 | .. method:: set.remove(el) |
| 1526 | |
| 1527 | Remove element *el* from the set. Raises :exc:`KeyError` if *el* is not |
| 1528 | contained in the set. |
| 1529 | |
| 1530 | .. method:: set.discard(el) |
| 1531 | |
| 1532 | Remove element *el* from the set if it is present. |
| 1533 | |
| 1534 | .. method:: set.pop() |
| 1535 | |
| 1536 | Remove and return an arbitrary element from the set. Raises :exc:`KeyError` |
| 1537 | if the set is empty. |
| 1538 | |
| 1539 | .. method:: set.clear() |
| 1540 | |
| 1541 | Remove all elements from the set. |
| 1542 | |
| 1543 | |
| 1544 | Note, the non-operator versions of the :meth:`update`, |
| 1545 | :meth:`intersection_update`, :meth:`difference_update`, and |
| 1546 | :meth:`symmetric_difference_update` methods will accept any iterable as an |
| 1547 | argument. |
| 1548 | |
| 1549 | |
| 1550 | .. _typesmapping: |
| 1551 | |
| 1552 | Mapping Types --- :class:`dict` |
| 1553 | =============================== |
| 1554 | |
| 1555 | .. index:: |
| 1556 | object: mapping |
| 1557 | object: dictionary |
| 1558 | triple: operations on; mapping; types |
| 1559 | triple: operations on; dictionary; type |
| 1560 | statement: del |
| 1561 | builtin: len |
| 1562 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1563 | A :dfn:`mapping` object maps :term:`hashable` values to arbitrary objects. |
| 1564 | Mappings are mutable objects. There is currently only one standard mapping |
| 1565 | type, the :dfn:`dictionary`. (For other containers see the built in |
| 1566 | :class:`list`, :class:`set`, and :class:`tuple` classes, and the |
| 1567 | :mod:`collections` module.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1569 | A dictionary's keys are *almost* arbitrary values. Values that are not |
| 1570 | :term:`hashable`, that is, values containing lists, dictionaries or other |
| 1571 | mutable types (that are compared by value rather than by object identity) may |
| 1572 | not be used as keys. Numeric types used for keys obey the normal rules for |
| 1573 | numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``) |
| 1574 | then they can be used interchangeably to index the same dictionary entry. (Note |
| 1575 | however, that since computers store floating-point numbers as approximations it |
| 1576 | is usually unwise to use them as dictionary keys.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1577 | |
| 1578 | Dictionaries can be created by placing a comma-separated list of ``key: value`` |
| 1579 | pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: |
| 1580 | 'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. |
| 1581 | |
| 1582 | .. class:: dict([arg]) |
| 1583 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1584 | Return a new dictionary initialized from an optional positional argument or |
| 1585 | from a set of keyword arguments. If no arguments are given, return a new |
| 1586 | empty dictionary. If the positional argument *arg* is a mapping object, |
| 1587 | return a dictionary mapping the same keys to the same values as does the |
| 1588 | mapping object. Otherwise the positional argument must be a sequence, a |
| 1589 | container that supports iteration, or an iterator object. The elements of |
| 1590 | the argument must each also be of one of those kinds, and each must in turn |
| 1591 | contain exactly two objects. The first is used as a key in the new |
| 1592 | dictionary, and the second as the key's value. If a given key is seen more |
| 1593 | than once, the last value associated with it is retained in the new |
| 1594 | dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1595 | |
| 1596 | If keyword arguments are given, the keywords themselves with their associated |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1597 | values are added as items to the dictionary. If a key is specified both in |
| 1598 | the positional argument and as a keyword argument, the value associated with |
| 1599 | the keyword is retained in the dictionary. For example, these all return a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1600 | dictionary equal to ``{"one": 2, "two": 3}``: |
| 1601 | |
| 1602 | * ``dict(one=2, two=3)`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1603 | * ``dict({'one': 2, 'two': 3})`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1604 | * ``dict(zip(('one', 'two'), (2, 3)))`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1605 | * ``dict([['two', 3], ['one', 2]])`` |
| 1606 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1607 | The first example only works for keys that are valid Python identifiers; the |
| 1608 | others work with any valid keys. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1609 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1610 | |
| 1611 | These are the operations that dictionaries support (and therefore, custom mapping |
| 1612 | types should support too): |
| 1613 | |
| 1614 | .. describe:: len(d) |
| 1615 | |
| 1616 | Return the number of items in the dictionary *d*. |
| 1617 | |
| 1618 | .. describe:: d[key] |
| 1619 | |
| 1620 | Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is |
| 1621 | not in the map. |
| 1622 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 1623 | If a subclass of dict defines a method :meth:`__missing__`, if the key *key* |
| 1624 | is not present, the ``d[key]`` operation calls that method with the key *key* |
| 1625 | as argument. The ``d[key]`` operation then returns or raises whatever is |
| 1626 | returned or raised by the ``__missing__(key)`` call if the key is not |
| 1627 | present. No other operations or methods invoke :meth:`__missing__`. If |
| 1628 | :meth:`__missing__` is not defined, :exc:`KeyError` is raised. |
| 1629 | :meth:`__missing__` must be a method; it cannot be an instance variable. For |
| 1630 | an example, see :class:`collections.defaultdict`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1631 | |
| 1632 | .. describe:: d[key] = value |
| 1633 | |
| 1634 | Set ``d[key]`` to *value*. |
| 1635 | |
| 1636 | .. describe:: del d[key] |
| 1637 | |
| 1638 | Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the |
| 1639 | map. |
| 1640 | |
| 1641 | .. describe:: key in d |
| 1642 | |
| 1643 | Return ``True`` if *d* has a key *key*, else ``False``. |
| 1644 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1645 | .. describe:: key not in d |
| 1646 | |
| 1647 | Equivalent to ``not key in d``. |
| 1648 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1649 | .. method:: dict.clear() |
| 1650 | |
| 1651 | Remove all items from the dictionary. |
| 1652 | |
| 1653 | .. method:: dict.copy() |
| 1654 | |
| 1655 | Return a shallow copy of the dictionary. |
| 1656 | |
| 1657 | .. method:: dict.fromkeys(seq[, value]) |
| 1658 | |
| 1659 | Create a new dictionary with keys from *seq* and values set to *value*. |
| 1660 | |
| 1661 | :func:`fromkeys` is a class method that returns a new dictionary. *value* |
| 1662 | defaults to ``None``. |
| 1663 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1664 | .. method:: dict.get(key[, default]) |
| 1665 | |
| 1666 | Return the value for *key* if *key* is in the dictionary, else *default*. If |
| 1667 | *default* is not given, it defaults to ``None``, so that this method never |
| 1668 | raises a :exc:`KeyError`. |
| 1669 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1670 | .. method:: dict.items() |
| 1671 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1672 | Return a new view of the dictionary's items (``(key, value)`` pairs). See |
| 1673 | below for documentation of view objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1674 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1675 | .. method:: dict.keys() |
| 1676 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1677 | Return a new view of the dictionary's keys. See below for documentation of |
| 1678 | view objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1679 | |
| 1680 | .. method:: dict.pop(key[, default]) |
| 1681 | |
| 1682 | If *key* is in the dictionary, remove it and return its value, else return |
| 1683 | *default*. If *default* is not given and *key* is not in the dictionary, a |
| 1684 | :exc:`KeyError` is raised. |
| 1685 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1686 | .. method:: dict.popitem() |
| 1687 | |
| 1688 | Remove and return an arbitrary ``(key, value)`` pair from the dictionary. |
| 1689 | |
| 1690 | :func:`popitem` is useful to destructively iterate over a dictionary, as |
| 1691 | often used in set algorithms. If the dictionary is empty, calling |
| 1692 | :func:`popitem` raises a :exc:`KeyError`. |
| 1693 | |
| 1694 | .. method:: dict.setdefault(key[, default]) |
| 1695 | |
Fred Drake | 2e74878 | 2007-09-04 17:33:11 +0000 | [diff] [blame] | 1696 | If *key* is in the dictionary, return its value. If not, insert *key* with |
| 1697 | a value of *default* and return *default*. *default* defaults to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1698 | |
| 1699 | .. method:: dict.update([other]) |
| 1700 | |
Fred Drake | 2e74878 | 2007-09-04 17:33:11 +0000 | [diff] [blame] | 1701 | Update the dictionary with the key/value pairs from *other*, overwriting |
| 1702 | existing keys. Return ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1703 | |
| 1704 | :func:`update` accepts either another dictionary object or an iterable of |
| 1705 | key/value pairs (as a tuple or other iterable of length two). If keyword |
| 1706 | arguments are specified, the dictionary is then is updated with those |
| 1707 | key/value pairs: ``d.update(red=1, blue=2)``. |
| 1708 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1709 | .. method:: dict.values() |
| 1710 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 1711 | Return a new view of the dictionary's values. See below for documentation of |
| 1712 | view objects. |
| 1713 | |
| 1714 | |
| 1715 | Dictionary view objects |
| 1716 | ----------------------- |
| 1717 | |
| 1718 | The objects returned by :meth:`dict.keys`, :meth:`dict.values` and |
| 1719 | :meth:`dict.items` are *view objects*. They provide a dynamic view on the |
| 1720 | dictionary's entries, which means that when the dictionary changes, the view |
| 1721 | reflects these changes. The keys and items views have a set-like character |
| 1722 | since their entries |
| 1723 | |
| 1724 | Dictionary views can be iterated over to yield their respective data, and |
| 1725 | support membership tests: |
| 1726 | |
| 1727 | .. describe:: len(dictview) |
| 1728 | |
| 1729 | Return the number of entries in the dictionary. |
| 1730 | |
| 1731 | .. describe:: iter(dictview) |
| 1732 | |
| 1733 | Return an iterator over the keys, values or items (represented as tuples of |
| 1734 | ``(key, value)``) in the dictionary. |
| 1735 | |
| 1736 | Keys and values are iterated over in an arbitrary order which is non-random, |
| 1737 | varies across Python implementations, and depends on the dictionary's history |
| 1738 | of insertions and deletions. If keys, values and items views are iterated |
| 1739 | over with no intervening modifications to the dictionary, the order of items |
| 1740 | will directly correspond. This allows the creation of ``(value, key)`` pairs |
| 1741 | using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to |
| 1742 | create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``. |
| 1743 | |
| 1744 | .. describe:: x in dictview |
| 1745 | |
| 1746 | Return ``True`` if *x* is in the underlying dictionary's keys, values or |
| 1747 | items (in the latter case, *x* should be a ``(key, value)`` tuple). |
| 1748 | |
| 1749 | |
| 1750 | The keys and items views also provide set-like operations ("other" here refers |
| 1751 | to another dictionary view or a set): |
| 1752 | |
| 1753 | .. describe:: dictview & other |
| 1754 | |
| 1755 | Return the intersection of the dictview and the other object as a new set. |
| 1756 | |
| 1757 | .. describe:: dictview | other |
| 1758 | |
| 1759 | Return the union of the dictview and the other object as a new set. |
| 1760 | |
| 1761 | .. describe:: dictview - other |
| 1762 | |
| 1763 | Return the difference between the dictview and the other object (all elements |
| 1764 | in *dictview* that aren't in *other*) as a new set. |
| 1765 | |
| 1766 | .. describe:: dictview ^ other |
| 1767 | |
| 1768 | Return the symmetric difference (all elements either in *dictview* or |
| 1769 | *other*, but not in both) of the dictview and the other object as a new set. |
| 1770 | |
| 1771 | .. warning:: |
| 1772 | |
| 1773 | Since a dictionary's values are not required to be hashable, any of these |
| 1774 | four operations will fail if an involved dictionary contains such a value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1775 | |
| 1776 | |
Georg Brandl | c53c966 | 2007-09-04 17:58:02 +0000 | [diff] [blame] | 1777 | An example of dictionary view usage:: |
| 1778 | |
| 1779 | >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} |
| 1780 | >>> keys = dishes.keys() |
| 1781 | >>> values = dishes.values() |
| 1782 | |
| 1783 | >>> # iteration |
| 1784 | >>> n = 0 |
| 1785 | >>> for val in values: |
| 1786 | ... n += val |
| 1787 | >>> print(n) |
| 1788 | 504 |
| 1789 | |
| 1790 | >>> # keys and values are iterated over in the same order |
| 1791 | >>> list(keys) |
| 1792 | ['eggs', 'bacon', 'sausage', 'spam'] |
| 1793 | >>> list(values) |
| 1794 | [2, 1, 1, 500] |
| 1795 | |
| 1796 | >>> # view objects are dynamic and reflect dict changes |
| 1797 | >>> del dishes['eggs'] |
| 1798 | >>> del dishes['sausage'] |
| 1799 | >>> list(keys) |
| 1800 | ['spam', 'bacon'] |
| 1801 | |
| 1802 | >>> # set operations |
| 1803 | >>> keys & {'eggs', 'bacon', 'salad'} |
| 1804 | {'eggs', 'bacon'} |
| 1805 | |
| 1806 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1807 | .. _bltin-file-objects: |
| 1808 | |
| 1809 | File Objects |
| 1810 | ============ |
| 1811 | |
| 1812 | .. index:: |
| 1813 | object: file |
| 1814 | builtin: file |
| 1815 | module: os |
| 1816 | module: socket |
| 1817 | |
Georg Brandl | 81ac1ce | 2007-08-31 17:17:17 +0000 | [diff] [blame] | 1818 | .. XXX this is quite out of date, must be updated with "io" module |
| 1819 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1820 | File objects are implemented using C's ``stdio`` package and can be |
| 1821 | created with the built-in :func:`file` and (more usually) :func:`open` |
| 1822 | constructors described in the :ref:`built-in-funcs` section. [#]_ File |
| 1823 | objects are also returned by some other built-in functions and methods, |
| 1824 | such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile` |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1825 | method of socket objects. Temporary files can be created using the |
| 1826 | :mod:`tempfile` module, and high-level file operations such as copying, |
| 1827 | moving, and deleting files and directories can be achieved with the |
| 1828 | :mod:`shutil` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1829 | |
| 1830 | When a file operation fails for an I/O-related reason, the exception |
| 1831 | :exc:`IOError` is raised. This includes situations where the operation is not |
| 1832 | defined for some reason, like :meth:`seek` on a tty device or writing a file |
| 1833 | opened for reading. |
| 1834 | |
| 1835 | Files have the following methods: |
| 1836 | |
| 1837 | |
| 1838 | .. method:: file.close() |
| 1839 | |
| 1840 | Close the file. A closed file cannot be read or written any more. Any operation |
| 1841 | which requires that the file be open will raise a :exc:`ValueError` after the |
| 1842 | file has been closed. Calling :meth:`close` more than once is allowed. |
| 1843 | |
| 1844 | As of Python 2.5, you can avoid having to call this method explicitly if you use |
| 1845 | the :keyword:`with` statement. For example, the following code will |
| 1846 | automatically close ``f`` when the :keyword:`with` block is exited:: |
| 1847 | |
| 1848 | from __future__ import with_statement |
| 1849 | |
| 1850 | with open("hello.txt") as f: |
| 1851 | for line in f: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 1852 | print(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1853 | |
| 1854 | In older versions of Python, you would have needed to do this to get the same |
| 1855 | effect:: |
| 1856 | |
| 1857 | f = open("hello.txt") |
| 1858 | try: |
| 1859 | for line in f: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 1860 | print(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1861 | finally: |
| 1862 | f.close() |
| 1863 | |
| 1864 | .. note:: |
| 1865 | |
| 1866 | Not all "file-like" types in Python support use as a context manager for the |
| 1867 | :keyword:`with` statement. If your code is intended to work with any file-like |
| 1868 | object, you can use the function :func:`contextlib.closing` instead of using |
| 1869 | the object directly. |
| 1870 | |
| 1871 | |
| 1872 | .. method:: file.flush() |
| 1873 | |
| 1874 | Flush the internal buffer, like ``stdio``'s :cfunc:`fflush`. This may be a |
| 1875 | no-op on some file-like objects. |
| 1876 | |
| 1877 | |
| 1878 | .. method:: file.fileno() |
| 1879 | |
| 1880 | .. index:: |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1881 | pair: file; descriptor |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1882 | module: fcntl |
| 1883 | |
| 1884 | Return the integer "file descriptor" that is used by the underlying |
| 1885 | implementation to request I/O operations from the operating system. This can be |
| 1886 | useful for other, lower level interfaces that use file descriptors, such as the |
| 1887 | :mod:`fcntl` module or :func:`os.read` and friends. |
| 1888 | |
| 1889 | .. note:: |
| 1890 | |
| 1891 | File-like objects which do not have a real file descriptor should *not* provide |
| 1892 | this method! |
| 1893 | |
| 1894 | |
| 1895 | .. method:: file.isatty() |
| 1896 | |
| 1897 | Return ``True`` if the file is connected to a tty(-like) device, else ``False``. |
| 1898 | |
| 1899 | .. note:: |
| 1900 | |
| 1901 | If a file-like object is not associated with a real file, this method should |
| 1902 | *not* be implemented. |
| 1903 | |
| 1904 | |
| 1905 | .. method:: file.__next__() |
| 1906 | |
| 1907 | A file object is its own iterator, for example ``iter(f)`` returns *f* (unless |
| 1908 | *f* is closed). When a file is used as an iterator, typically in a |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 1909 | :keyword:`for` loop (for example, ``for line in f: print(line)``), the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1910 | :meth:`__next__` method is called repeatedly. This method returns the next |
| 1911 | input line, or raises :exc:`StopIteration` when EOF is hit when the file is open |
| 1912 | for reading (behavior is undefined when the file is open for writing). In order |
| 1913 | to make a :keyword:`for` loop the most efficient way of looping over the lines |
| 1914 | of a file (a very common operation), the :meth:`__next__` method uses a hidden |
| 1915 | read-ahead buffer. As a consequence of using a read-ahead buffer, combining |
| 1916 | :meth:`__next__` with other file methods (like :meth:`readline`) does not work |
| 1917 | right. However, using :meth:`seek` to reposition the file to an absolute |
| 1918 | position will flush the read-ahead buffer. |
| 1919 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1920 | |
| 1921 | .. method:: file.read([size]) |
| 1922 | |
| 1923 | Read at most *size* bytes from the file (less if the read hits EOF before |
| 1924 | obtaining *size* bytes). If the *size* argument is negative or omitted, read |
| 1925 | all data until EOF is reached. The bytes are returned as a string object. An |
| 1926 | empty string is returned when EOF is encountered immediately. (For certain |
| 1927 | files, like ttys, it makes sense to continue reading after an EOF is hit.) Note |
| 1928 | that this method may call the underlying C function :cfunc:`fread` more than |
| 1929 | once in an effort to acquire as close to *size* bytes as possible. Also note |
| 1930 | that when in non-blocking mode, less data than what was requested may be |
| 1931 | returned, even if no *size* parameter was given. |
| 1932 | |
| 1933 | |
| 1934 | .. method:: file.readline([size]) |
| 1935 | |
| 1936 | Read one entire line from the file. A trailing newline character is kept in the |
| 1937 | string (but may be absent when a file ends with an incomplete line). [#]_ If |
| 1938 | the *size* argument is present and non-negative, it is a maximum byte count |
| 1939 | (including the trailing newline) and an incomplete line may be returned. An |
| 1940 | empty string is returned *only* when EOF is encountered immediately. |
| 1941 | |
| 1942 | .. note:: |
| 1943 | |
| 1944 | Unlike ``stdio``'s :cfunc:`fgets`, the returned string contains null characters |
| 1945 | (``'\0'``) if they occurred in the input. |
| 1946 | |
| 1947 | |
| 1948 | .. method:: file.readlines([sizehint]) |
| 1949 | |
| 1950 | Read until EOF using :meth:`readline` and return a list containing the lines |
| 1951 | thus read. If the optional *sizehint* argument is present, instead of |
| 1952 | reading up to EOF, whole lines totalling approximately *sizehint* bytes |
| 1953 | (possibly after rounding up to an internal buffer size) are read. Objects |
| 1954 | implementing a file-like interface may choose to ignore *sizehint* if it |
| 1955 | cannot be implemented, or cannot be implemented efficiently. |
| 1956 | |
| 1957 | |
| 1958 | .. method:: file.seek(offset[, whence]) |
| 1959 | |
| 1960 | Set the file's current position, like ``stdio``'s :cfunc:`fseek`. The *whence* |
| 1961 | argument is optional and defaults to ``os.SEEK_SET`` or ``0`` (absolute file |
| 1962 | positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the |
| 1963 | current position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's |
| 1964 | end). There is no return value. Note that if the file is opened for appending |
| 1965 | (mode ``'a'`` or ``'a+'``), any :meth:`seek` operations will be undone at the |
| 1966 | next write. If the file is only opened for writing in append mode (mode |
| 1967 | ``'a'``), this method is essentially a no-op, but it remains useful for files |
| 1968 | opened in append mode with reading enabled (mode ``'a+'``). If the file is |
| 1969 | opened in text mode (without ``'b'``), only offsets returned by :meth:`tell` are |
| 1970 | legal. Use of other offsets causes undefined behavior. |
| 1971 | |
| 1972 | Note that not all file objects are seekable. |
| 1973 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1974 | |
| 1975 | .. method:: file.tell() |
| 1976 | |
| 1977 | Return the file's current position, like ``stdio``'s :cfunc:`ftell`. |
| 1978 | |
| 1979 | .. note:: |
| 1980 | |
| 1981 | On Windows, :meth:`tell` can return illegal values (after an :cfunc:`fgets`) |
| 1982 | when reading files with Unix-style line-endings. Use binary mode (``'rb'``) to |
| 1983 | circumvent this problem. |
| 1984 | |
| 1985 | |
| 1986 | .. method:: file.truncate([size]) |
| 1987 | |
| 1988 | Truncate the file's size. If the optional *size* argument is present, the file |
| 1989 | is truncated to (at most) that size. The size defaults to the current position. |
| 1990 | The current file position is not changed. Note that if a specified size exceeds |
| 1991 | the file's current size, the result is platform-dependent: possibilities |
| 1992 | include that the file may remain unchanged, increase to the specified size as if |
| 1993 | zero-filled, or increase to the specified size with undefined new content. |
| 1994 | Availability: Windows, many Unix variants. |
| 1995 | |
| 1996 | |
| 1997 | .. method:: file.write(str) |
| 1998 | |
| 1999 | Write a string to the file. There is no return value. Due to buffering, the |
| 2000 | string may not actually show up in the file until the :meth:`flush` or |
| 2001 | :meth:`close` method is called. |
| 2002 | |
| 2003 | |
| 2004 | .. method:: file.writelines(sequence) |
| 2005 | |
| 2006 | Write a sequence of strings to the file. The sequence can be any iterable |
| 2007 | object producing strings, typically a list of strings. There is no return value. |
| 2008 | (The name is intended to match :meth:`readlines`; :meth:`writelines` does not |
| 2009 | add line separators.) |
| 2010 | |
| 2011 | Files support the iterator protocol. Each iteration returns the same result as |
| 2012 | ``file.readline()``, and iteration ends when the :meth:`readline` method returns |
| 2013 | an empty string. |
| 2014 | |
| 2015 | File objects also offer a number of other interesting attributes. These are not |
| 2016 | required for file-like objects, but should be implemented if they make sense for |
| 2017 | the particular object. |
| 2018 | |
| 2019 | |
| 2020 | .. attribute:: file.closed |
| 2021 | |
| 2022 | bool indicating the current state of the file object. This is a read-only |
| 2023 | attribute; the :meth:`close` method changes the value. It may not be available |
| 2024 | on all file-like objects. |
| 2025 | |
| 2026 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 2027 | .. XXX does this still apply? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2028 | .. attribute:: file.encoding |
| 2029 | |
| 2030 | The encoding that this file uses. When Unicode strings are written to a file, |
| 2031 | they will be converted to byte strings using this encoding. In addition, when |
| 2032 | the file is connected to a terminal, the attribute gives the encoding that the |
| 2033 | terminal is likely to use (that information might be incorrect if the user has |
| 2034 | misconfigured the terminal). The attribute is read-only and may not be present |
| 2035 | on all file-like objects. It may also be ``None``, in which case the file uses |
| 2036 | the system default encoding for converting Unicode strings. |
| 2037 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2038 | |
| 2039 | .. attribute:: file.mode |
| 2040 | |
| 2041 | The I/O mode for the file. If the file was created using the :func:`open` |
| 2042 | built-in function, this will be the value of the *mode* parameter. This is a |
| 2043 | read-only attribute and may not be present on all file-like objects. |
| 2044 | |
| 2045 | |
| 2046 | .. attribute:: file.name |
| 2047 | |
| 2048 | If the file object was created using :func:`open`, the name of the file. |
| 2049 | Otherwise, some string that indicates the source of the file object, of the |
| 2050 | form ``<...>``. This is a read-only attribute and may not be present on all |
| 2051 | file-like objects. |
| 2052 | |
| 2053 | |
| 2054 | .. attribute:: file.newlines |
| 2055 | |
| 2056 | If Python was built with the :option:`--with-universal-newlines` option to |
| 2057 | :program:`configure` (the default) this read-only attribute exists, and for |
| 2058 | files opened in universal newline read mode it keeps track of the types of |
| 2059 | newlines encountered while reading the file. The values it can take are |
| 2060 | ``'\r'``, ``'\n'``, ``'\r\n'``, ``None`` (unknown, no newlines read yet) or a |
| 2061 | tuple containing all the newline types seen, to indicate that multiple newline |
| 2062 | conventions were encountered. For files not opened in universal newline read |
| 2063 | mode the value of this attribute will be ``None``. |
| 2064 | |
| 2065 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2066 | .. _typecontextmanager: |
| 2067 | |
| 2068 | Context Manager Types |
| 2069 | ===================== |
| 2070 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2071 | .. index:: |
| 2072 | single: context manager |
| 2073 | single: context management protocol |
| 2074 | single: protocol; context management |
| 2075 | |
| 2076 | Python's :keyword:`with` statement supports the concept of a runtime context |
| 2077 | defined by a context manager. This is implemented using two separate methods |
| 2078 | that allow user-defined classes to define a runtime context that is entered |
| 2079 | before the statement body is executed and exited when the statement ends. |
| 2080 | |
| 2081 | The :dfn:`context management protocol` consists of a pair of methods that need |
| 2082 | to be provided for a context manager object to define a runtime context: |
| 2083 | |
| 2084 | |
| 2085 | .. method:: contextmanager.__enter__() |
| 2086 | |
| 2087 | Enter the runtime context and return either this object or another object |
| 2088 | related to the runtime context. The value returned by this method is bound to |
| 2089 | the identifier in the :keyword:`as` clause of :keyword:`with` statements using |
| 2090 | this context manager. |
| 2091 | |
| 2092 | An example of a context manager that returns itself is a file object. File |
| 2093 | objects return themselves from __enter__() to allow :func:`open` to be used as |
| 2094 | the context expression in a :keyword:`with` statement. |
| 2095 | |
| 2096 | An example of a context manager that returns a related object is the one |
| 2097 | returned by ``decimal.Context.get_manager()``. These managers set the active |
| 2098 | decimal context to a copy of the original decimal context and then return the |
| 2099 | copy. This allows changes to be made to the current decimal context in the body |
| 2100 | of the :keyword:`with` statement without affecting code outside the |
| 2101 | :keyword:`with` statement. |
| 2102 | |
| 2103 | |
| 2104 | .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb) |
| 2105 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 2106 | Exit the runtime context and return a Boolean flag indicating if any exception |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2107 | that occurred should be suppressed. If an exception occurred while executing the |
| 2108 | body of the :keyword:`with` statement, the arguments contain the exception type, |
| 2109 | value and traceback information. Otherwise, all three arguments are ``None``. |
| 2110 | |
| 2111 | Returning a true value from this method will cause the :keyword:`with` statement |
| 2112 | to suppress the exception and continue execution with the statement immediately |
| 2113 | following the :keyword:`with` statement. Otherwise the exception continues |
| 2114 | propagating after this method has finished executing. Exceptions that occur |
| 2115 | during execution of this method will replace any exception that occurred in the |
| 2116 | body of the :keyword:`with` statement. |
| 2117 | |
| 2118 | The exception passed in should never be reraised explicitly - instead, this |
| 2119 | method should return a false value to indicate that the method completed |
| 2120 | successfully and does not want to suppress the raised exception. This allows |
| 2121 | context management code (such as ``contextlib.nested``) to easily detect whether |
| 2122 | or not an :meth:`__exit__` method has actually failed. |
| 2123 | |
| 2124 | Python defines several context managers to support easy thread synchronisation, |
| 2125 | prompt closure of files or other objects, and simpler manipulation of the active |
| 2126 | decimal arithmetic context. The specific types are not treated specially beyond |
| 2127 | their implementation of the context management protocol. See the |
| 2128 | :mod:`contextlib` module for some examples. |
| 2129 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 2130 | Python's :term:`generator`\s and the ``contextlib.contextfactory`` decorator provide a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2131 | convenient way to implement these protocols. If a generator function is |
| 2132 | decorated with the ``contextlib.contextfactory`` decorator, it will return a |
| 2133 | context manager implementing the necessary :meth:`__enter__` and |
| 2134 | :meth:`__exit__` methods, rather than the iterator produced by an undecorated |
| 2135 | generator function. |
| 2136 | |
| 2137 | Note that there is no specific slot for any of these methods in the type |
| 2138 | structure for Python objects in the Python/C API. Extension types wanting to |
| 2139 | define these methods must provide them as a normal Python accessible method. |
| 2140 | Compared to the overhead of setting up the runtime context, the overhead of a |
| 2141 | single class dictionary lookup is negligible. |
| 2142 | |
| 2143 | |
| 2144 | .. _typesother: |
| 2145 | |
| 2146 | Other Built-in Types |
| 2147 | ==================== |
| 2148 | |
| 2149 | The interpreter supports several other kinds of objects. Most of these support |
| 2150 | only one or two operations. |
| 2151 | |
| 2152 | |
| 2153 | .. _typesmodules: |
| 2154 | |
| 2155 | Modules |
| 2156 | ------- |
| 2157 | |
| 2158 | The only special operation on a module is attribute access: ``m.name``, where |
| 2159 | *m* is a module and *name* accesses a name defined in *m*'s symbol table. |
| 2160 | Module attributes can be assigned to. (Note that the :keyword:`import` |
| 2161 | statement is not, strictly speaking, an operation on a module object; ``import |
| 2162 | foo`` does not require a module object named *foo* to exist, rather it requires |
| 2163 | an (external) *definition* for a module named *foo* somewhere.) |
| 2164 | |
| 2165 | A special member of every module is :attr:`__dict__`. This is the dictionary |
| 2166 | containing the module's symbol table. Modifying this dictionary will actually |
| 2167 | change the module's symbol table, but direct assignment to the :attr:`__dict__` |
| 2168 | attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines |
| 2169 | ``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying |
| 2170 | :attr:`__dict__` directly is not recommended. |
| 2171 | |
| 2172 | Modules built into the interpreter are written like this: ``<module 'sys' |
| 2173 | (built-in)>``. If loaded from a file, they are written as ``<module 'os' from |
| 2174 | '/usr/local/lib/pythonX.Y/os.pyc'>``. |
| 2175 | |
| 2176 | |
| 2177 | .. _typesobjects: |
| 2178 | |
| 2179 | Classes and Class Instances |
| 2180 | --------------------------- |
| 2181 | |
| 2182 | See :ref:`objects` and :ref:`class` for these. |
| 2183 | |
| 2184 | |
| 2185 | .. _typesfunctions: |
| 2186 | |
| 2187 | Functions |
| 2188 | --------- |
| 2189 | |
| 2190 | Function objects are created by function definitions. The only operation on a |
| 2191 | function object is to call it: ``func(argument-list)``. |
| 2192 | |
| 2193 | There are really two flavors of function objects: built-in functions and |
| 2194 | user-defined functions. Both support the same operation (to call the function), |
| 2195 | but the implementation is different, hence the different object types. |
| 2196 | |
| 2197 | See :ref:`function` for more information. |
| 2198 | |
| 2199 | |
| 2200 | .. _typesmethods: |
| 2201 | |
| 2202 | Methods |
| 2203 | ------- |
| 2204 | |
| 2205 | .. index:: object: method |
| 2206 | |
| 2207 | Methods are functions that are called using the attribute notation. There are |
| 2208 | two flavors: built-in methods (such as :meth:`append` on lists) and class |
| 2209 | instance methods. Built-in methods are described with the types that support |
| 2210 | them. |
| 2211 | |
| 2212 | The implementation adds two special read-only attributes to class instance |
| 2213 | methods: ``m.im_self`` is the object on which the method operates, and |
| 2214 | ``m.im_func`` is the function implementing the method. Calling ``m(arg-1, |
| 2215 | arg-2, ..., arg-n)`` is completely equivalent to calling ``m.im_func(m.im_self, |
| 2216 | arg-1, arg-2, ..., arg-n)``. |
| 2217 | |
| 2218 | Class instance methods are either *bound* or *unbound*, referring to whether the |
| 2219 | method was accessed through an instance or a class, respectively. When a method |
| 2220 | is unbound, its ``im_self`` attribute will be ``None`` and if called, an |
| 2221 | explicit ``self`` object must be passed as the first argument. In this case, |
| 2222 | ``self`` must be an instance of the unbound method's class (or a subclass of |
| 2223 | that class), otherwise a :exc:`TypeError` is raised. |
| 2224 | |
| 2225 | Like function objects, methods objects support getting arbitrary attributes. |
| 2226 | However, since method attributes are actually stored on the underlying function |
| 2227 | object (``meth.im_func``), setting method attributes on either bound or unbound |
| 2228 | methods is disallowed. Attempting to set a method attribute results in a |
| 2229 | :exc:`TypeError` being raised. In order to set a method attribute, you need to |
| 2230 | explicitly set it on the underlying function object:: |
| 2231 | |
| 2232 | class C: |
| 2233 | def method(self): |
| 2234 | pass |
| 2235 | |
| 2236 | c = C() |
| 2237 | c.method.im_func.whoami = 'my name is c' |
| 2238 | |
| 2239 | See :ref:`types` for more information. |
| 2240 | |
| 2241 | |
| 2242 | .. _bltin-code-objects: |
| 2243 | |
| 2244 | Code Objects |
| 2245 | ------------ |
| 2246 | |
| 2247 | .. index:: object: code |
| 2248 | |
| 2249 | .. index:: |
| 2250 | builtin: compile |
| 2251 | single: __code__ (function object attribute) |
| 2252 | |
| 2253 | Code objects are used by the implementation to represent "pseudo-compiled" |
| 2254 | executable Python code such as a function body. They differ from function |
| 2255 | objects because they don't contain a reference to their global execution |
| 2256 | environment. Code objects are returned by the built-in :func:`compile` function |
| 2257 | and can be extracted from function objects through their :attr:`__code__` |
| 2258 | attribute. See also the :mod:`code` module. |
| 2259 | |
| 2260 | .. index:: |
| 2261 | builtin: exec |
| 2262 | builtin: eval |
| 2263 | |
| 2264 | A code object can be executed or evaluated by passing it (instead of a source |
| 2265 | string) to the :func:`exec` or :func:`eval` built-in functions. |
| 2266 | |
| 2267 | See :ref:`types` for more information. |
| 2268 | |
| 2269 | |
| 2270 | .. _bltin-type-objects: |
| 2271 | |
| 2272 | Type Objects |
| 2273 | ------------ |
| 2274 | |
| 2275 | .. index:: |
| 2276 | builtin: type |
| 2277 | module: types |
| 2278 | |
| 2279 | Type objects represent the various object types. An object's type is accessed |
| 2280 | by the built-in function :func:`type`. There are no special operations on |
| 2281 | types. The standard module :mod:`types` defines names for all standard built-in |
| 2282 | types. |
| 2283 | |
| 2284 | Types are written like this: ``<type 'int'>``. |
| 2285 | |
| 2286 | |
| 2287 | .. _bltin-null-object: |
| 2288 | |
| 2289 | The Null Object |
| 2290 | --------------- |
| 2291 | |
| 2292 | This object is returned by functions that don't explicitly return a value. It |
| 2293 | supports no special operations. There is exactly one null object, named |
| 2294 | ``None`` (a built-in name). |
| 2295 | |
| 2296 | It is written as ``None``. |
| 2297 | |
| 2298 | |
| 2299 | .. _bltin-ellipsis-object: |
| 2300 | |
| 2301 | The Ellipsis Object |
| 2302 | ------------------- |
| 2303 | |
Georg Brandl | cb8ecb1 | 2007-09-04 06:35:14 +0000 | [diff] [blame] | 2304 | This object is commonly used by slicing (see :ref:`slicings`). It supports no |
| 2305 | special operations. There is exactly one ellipsis object, named |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2306 | :const:`Ellipsis` (a built-in name). |
| 2307 | |
| 2308 | It is written as ``Ellipsis`` or ``...``. |
| 2309 | |
| 2310 | |
| 2311 | Boolean Values |
| 2312 | -------------- |
| 2313 | |
| 2314 | Boolean values are the two constant objects ``False`` and ``True``. They are |
| 2315 | used to represent truth values (although other values can also be considered |
| 2316 | false or true). In numeric contexts (for example when used as the argument to |
| 2317 | an arithmetic operator), they behave like the integers 0 and 1, respectively. |
| 2318 | The built-in function :func:`bool` can be used to cast any value to a Boolean, |
| 2319 | if the value can be interpreted as a truth value (see section Truth Value |
| 2320 | Testing above). |
| 2321 | |
| 2322 | .. index:: |
| 2323 | single: False |
| 2324 | single: True |
| 2325 | pair: Boolean; values |
| 2326 | |
| 2327 | They are written as ``False`` and ``True``, respectively. |
| 2328 | |
| 2329 | |
| 2330 | .. _typesinternal: |
| 2331 | |
| 2332 | Internal Objects |
| 2333 | ---------------- |
| 2334 | |
| 2335 | See :ref:`types` for this information. It describes stack frame objects, |
| 2336 | traceback objects, and slice objects. |
| 2337 | |
| 2338 | |
| 2339 | .. _specialattrs: |
| 2340 | |
| 2341 | Special Attributes |
| 2342 | ================== |
| 2343 | |
| 2344 | The implementation adds a few special read-only attributes to several object |
| 2345 | types, where they are relevant. Some of these are not reported by the |
| 2346 | :func:`dir` built-in function. |
| 2347 | |
| 2348 | |
| 2349 | .. attribute:: object.__dict__ |
| 2350 | |
| 2351 | A dictionary or other mapping object used to store an object's (writable) |
| 2352 | attributes. |
| 2353 | |
| 2354 | |
| 2355 | .. attribute:: instance.__class__ |
| 2356 | |
| 2357 | The class to which a class instance belongs. |
| 2358 | |
| 2359 | |
| 2360 | .. attribute:: class.__bases__ |
| 2361 | |
| 2362 | The tuple of base classes of a class object. If there are no base classes, this |
| 2363 | will be an empty tuple. |
| 2364 | |
| 2365 | |
| 2366 | .. attribute:: class.__name__ |
| 2367 | |
| 2368 | The name of the class or type. |
| 2369 | |
| 2370 | .. rubric:: Footnotes |
| 2371 | |
| 2372 | .. [#] Additional information on these special methods may be found in the Python |
| 2373 | Reference Manual (:ref:`customization`). |
| 2374 | |
| 2375 | .. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and |
| 2376 | similarly for tuples. |
| 2377 | |
| 2378 | .. [#] They must have since the parser can't tell the type of the operands. |
| 2379 | |
| 2380 | .. [#] To format only a tuple you should therefore provide a singleton tuple whose only |
| 2381 | element is the tuple to be formatted. |
| 2382 | |
| 2383 | .. [#] These numbers are fairly arbitrary. They are intended to avoid printing endless |
| 2384 | strings of meaningless digits without hampering correct use and without having |
| 2385 | to know the exact precision of floating point values on a particular machine. |
| 2386 | |
| 2387 | .. [#] :func:`file` is new in Python 2.2. The older built-in :func:`open` is an alias |
| 2388 | for :func:`file`. |
| 2389 | |
| 2390 | .. [#] The advantage of leaving the newline on is that returning an empty string is |
| 2391 | then an unambiguous EOF indication. It is also possible (in cases where it |
| 2392 | might matter, for example, if you want to make an exact copy of a file while |
| 2393 | scanning its lines) to tell whether the last line of a file ended in a newline |
| 2394 | or not (yes this happens!). |