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