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 | |
Antoine Pitrou | e231e39 | 2009-12-19 18:22:15 +0000 | [diff] [blame] | 15 | The principal built-in types are numerics, sequences, mappings, classes, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | instances and exceptions. |
| 17 | |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 18 | Some collection classes are mutable. The methods that add, subtract, or |
| 19 | rearrange their members in place, and don't return a specific item, never return |
| 20 | the collection instance itself but ``None``. |
| 21 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | Some operations are supported by several object types; in particular, |
| 23 | practically all objects can be compared, tested for truth value, and converted |
| 24 | to a string (with the :func:`repr` function or the slightly different |
| 25 | :func:`str` function). The latter function is implicitly used when an object is |
| 26 | written by the :func:`print` function. |
| 27 | |
| 28 | |
| 29 | .. _truth: |
| 30 | |
| 31 | Truth Value Testing |
| 32 | =================== |
| 33 | |
| 34 | .. index:: |
| 35 | statement: if |
| 36 | statement: while |
| 37 | pair: truth; value |
| 38 | pair: Boolean; operations |
| 39 | single: false |
| 40 | |
| 41 | Any object can be tested for truth value, for use in an :keyword:`if` or |
| 42 | :keyword:`while` condition or as operand of the Boolean operations below. The |
| 43 | following values are considered false: |
| 44 | |
| 45 | .. index:: single: None (Built-in object) |
| 46 | |
| 47 | * ``None`` |
| 48 | |
| 49 | .. index:: single: False (Built-in object) |
| 50 | |
| 51 | * ``False`` |
| 52 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 53 | * zero of any numeric type, for example, ``0``, ``0.0``, ``0j``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | * any empty sequence, for example, ``''``, ``()``, ``[]``. |
| 56 | |
| 57 | * any empty mapping, for example, ``{}``. |
| 58 | |
| 59 | * instances of user-defined classes, if the class defines a :meth:`__bool__` or |
| 60 | :meth:`__len__` method, when that method returns the integer zero or |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 61 | :class:`bool` value ``False``. [1]_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | .. index:: single: true |
| 64 | |
| 65 | All other values are considered true --- so objects of many types are always |
| 66 | true. |
| 67 | |
| 68 | .. index:: |
| 69 | operator: or |
| 70 | operator: and |
| 71 | single: False |
| 72 | single: True |
| 73 | |
| 74 | Operations and built-in functions that have a Boolean result always return ``0`` |
| 75 | or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated. |
| 76 | (Important exception: the Boolean operations ``or`` and ``and`` always return |
| 77 | one of their operands.) |
| 78 | |
| 79 | |
| 80 | .. _boolean: |
| 81 | |
| 82 | Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not` |
| 83 | ==================================================================== |
| 84 | |
| 85 | .. index:: pair: Boolean; operations |
| 86 | |
| 87 | These are the Boolean operations, ordered by ascending priority: |
| 88 | |
| 89 | +-------------+---------------------------------+-------+ |
| 90 | | Operation | Result | Notes | |
| 91 | +=============+=================================+=======+ |
| 92 | | ``x or y`` | if *x* is false, then *y*, else | \(1) | |
| 93 | | | *x* | | |
| 94 | +-------------+---------------------------------+-------+ |
| 95 | | ``x and y`` | if *x* is false, then *x*, else | \(2) | |
| 96 | | | *y* | | |
| 97 | +-------------+---------------------------------+-------+ |
| 98 | | ``not x`` | if *x* is false, then ``True``, | \(3) | |
| 99 | | | else ``False`` | | |
| 100 | +-------------+---------------------------------+-------+ |
| 101 | |
| 102 | .. index:: |
| 103 | operator: and |
| 104 | operator: or |
| 105 | operator: not |
| 106 | |
| 107 | Notes: |
| 108 | |
| 109 | (1) |
| 110 | This is a short-circuit operator, so it only evaluates the second |
| 111 | argument if the first one is :const:`False`. |
| 112 | |
| 113 | (2) |
| 114 | This is a short-circuit operator, so it only evaluates the second |
| 115 | argument if the first one is :const:`True`. |
| 116 | |
| 117 | (3) |
| 118 | ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is |
| 119 | interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error. |
| 120 | |
| 121 | |
| 122 | .. _stdcomparisons: |
| 123 | |
| 124 | Comparisons |
| 125 | =========== |
| 126 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 127 | .. index:: |
| 128 | pair: chaining; comparisons |
| 129 | pair: operator; comparison |
| 130 | operator: == |
| 131 | operator: < |
| 132 | operator: <= |
| 133 | operator: > |
| 134 | operator: >= |
| 135 | operator: != |
| 136 | operator: is |
| 137 | operator: is not |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 139 | There are eight comparison operations in Python. They all have the same |
| 140 | priority (which is higher than that of the Boolean operations). Comparisons can |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and |
| 142 | y <= z``, except that *y* is evaluated only once (but in both cases *z* is not |
| 143 | evaluated at all when ``x < y`` is found to be false). |
| 144 | |
| 145 | This table summarizes the comparison operations: |
| 146 | |
Georg Brandl | fd85516 | 2008-01-07 09:13:03 +0000 | [diff] [blame] | 147 | +------------+-------------------------+ |
| 148 | | Operation | Meaning | |
| 149 | +============+=========================+ |
| 150 | | ``<`` | strictly less than | |
| 151 | +------------+-------------------------+ |
| 152 | | ``<=`` | less than or equal | |
| 153 | +------------+-------------------------+ |
| 154 | | ``>`` | strictly greater than | |
| 155 | +------------+-------------------------+ |
| 156 | | ``>=`` | greater than or equal | |
| 157 | +------------+-------------------------+ |
| 158 | | ``==`` | equal | |
| 159 | +------------+-------------------------+ |
| 160 | | ``!=`` | not equal | |
| 161 | +------------+-------------------------+ |
| 162 | | ``is`` | object identity | |
| 163 | +------------+-------------------------+ |
| 164 | | ``is not`` | negated object identity | |
| 165 | +------------+-------------------------+ |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 166 | |
| 167 | .. index:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | pair: object; numeric |
| 169 | pair: objects; comparing |
| 170 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 171 | Objects of different types, except different numeric types, never compare equal. |
Antoine Pitrou | e231e39 | 2009-12-19 18:22:15 +0000 | [diff] [blame] | 172 | Furthermore, some types (for example, function objects) support only a degenerate |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 173 | notion of comparison where any two objects of that type are unequal. The ``<``, |
| 174 | ``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when |
Mark Dickinson | f673f0c | 2010-03-13 09:48:39 +0000 | [diff] [blame] | 175 | comparing a complex number with another built-in numeric type, when the objects |
| 176 | are of different types that cannot be compared, or in other cases where there is |
| 177 | no defined ordering. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 179 | .. index:: |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 180 | single: __eq__() (instance method) |
| 181 | single: __ne__() (instance method) |
| 182 | single: __lt__() (instance method) |
| 183 | single: __le__() (instance method) |
| 184 | single: __gt__() (instance method) |
| 185 | single: __ge__() (instance method) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 05f5ab7 | 2008-09-24 09:11:47 +0000 | [diff] [blame] | 187 | Non-identical instances of a class normally compare as non-equal unless the |
| 188 | class defines the :meth:`__eq__` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 190 | Instances of a class cannot be ordered with respect to other instances of the |
| 191 | same class, or other types of object, unless the class defines enough of the |
Georg Brandl | 05f5ab7 | 2008-09-24 09:11:47 +0000 | [diff] [blame] | 192 | methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in |
| 193 | general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the |
| 194 | conventional meanings of the comparison operators). |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 195 | |
| 196 | The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be |
| 197 | customized; also they can be applied to any two objects and never raise an |
| 198 | exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
| 200 | .. index:: |
| 201 | operator: in |
| 202 | operator: not in |
| 203 | |
Georg Brandl | 375aec2 | 2011-01-15 17:03:02 +0000 | [diff] [blame] | 204 | Two more operations with the same syntactic priority, :keyword:`in` and |
| 205 | :keyword:`not in`, are supported only by sequence types (below). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
| 207 | |
| 208 | .. _typesnumeric: |
| 209 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 210 | Numeric Types --- :class:`int`, :class:`float`, :class:`complex` |
| 211 | ================================================================ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | |
| 213 | .. index:: |
| 214 | object: numeric |
| 215 | object: Boolean |
| 216 | object: integer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | object: floating point |
| 218 | object: complex number |
| 219 | pair: C; language |
| 220 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 221 | There are three distinct numeric types: :dfn:`integers`, :dfn:`floating |
| 222 | point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a |
| 223 | subtype of integers. Integers have unlimited precision. Floating point |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 224 | numbers are usually implemented using :c:type:`double` in C; information |
Mark Dickinson | 74f5902 | 2010-08-04 18:42:43 +0000 | [diff] [blame] | 225 | about the precision and internal representation of floating point |
| 226 | numbers for the machine on which your program is running is available |
| 227 | in :data:`sys.float_info`. Complex numbers have a real and imaginary |
| 228 | part, which are each a floating point number. To extract these parts |
| 229 | from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard |
| 230 | library includes additional numeric types, :mod:`fractions` that hold |
| 231 | rationals, and :mod:`decimal` that hold floating-point numbers with |
| 232 | user-definable precision.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | .. index:: |
| 235 | pair: numeric; literals |
| 236 | pair: integer; literals |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | pair: floating point; literals |
| 238 | pair: complex number; literals |
| 239 | pair: hexadecimal; literals |
| 240 | pair: octal; literals |
Neal Norwitz | 1d2aef5 | 2007-10-02 07:26:14 +0000 | [diff] [blame] | 241 | pair: binary; literals |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | 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] | 244 | and operators. Unadorned integer literals (including hex, octal and binary |
| 245 | numbers) yield integers. Numeric literals containing a decimal point or an |
| 246 | exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a |
| 247 | numeric literal yields an imaginary number (a complex number with a zero real |
| 248 | part) which you can add to an integer or float to get a complex number with real |
| 249 | and imaginary parts. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | |
| 251 | .. index:: |
| 252 | single: arithmetic |
| 253 | builtin: int |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | builtin: float |
| 255 | builtin: complex |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 256 | operator: + |
| 257 | operator: - |
| 258 | operator: * |
| 259 | operator: / |
| 260 | operator: // |
| 261 | operator: % |
| 262 | operator: ** |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
| 264 | Python fully supports mixed arithmetic: when a binary arithmetic operator has |
| 265 | operands of different numeric types, the operand with the "narrower" type is |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 266 | widened to that of the other, where integer is narrower than floating point, |
| 267 | which is narrower than complex. Comparisons between numbers of mixed type use |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 268 | the same rule. [2]_ The constructors :func:`int`, :func:`float`, and |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 269 | :func:`complex` can be used to produce numbers of a specific type. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 270 | |
| 271 | All numeric types (except complex) support the following operations, sorted by |
| 272 | ascending priority (operations in the same box have the same priority; all |
| 273 | numeric operations have a higher priority than comparison operations): |
| 274 | |
Raymond Hettinger | c706dbf | 2011-03-22 17:33:17 -0700 | [diff] [blame] | 275 | +---------------------+---------------------------------+---------+--------------------+ |
| 276 | | Operation | Result | Notes | Full documentation | |
| 277 | +=====================+=================================+=========+====================+ |
| 278 | | ``x + y`` | sum of *x* and *y* | | | |
| 279 | +---------------------+---------------------------------+---------+--------------------+ |
| 280 | | ``x - y`` | difference of *x* and *y* | | | |
| 281 | +---------------------+---------------------------------+---------+--------------------+ |
| 282 | | ``x * y`` | product of *x* and *y* | | | |
| 283 | +---------------------+---------------------------------+---------+--------------------+ |
| 284 | | ``x / y`` | quotient of *x* and *y* | | | |
| 285 | +---------------------+---------------------------------+---------+--------------------+ |
| 286 | | ``x // y`` | floored quotient of *x* and | \(1) | | |
| 287 | | | *y* | | | |
| 288 | +---------------------+---------------------------------+---------+--------------------+ |
| 289 | | ``x % y`` | remainder of ``x / y`` | \(2) | | |
| 290 | +---------------------+---------------------------------+---------+--------------------+ |
| 291 | | ``-x`` | *x* negated | | | |
| 292 | +---------------------+---------------------------------+---------+--------------------+ |
| 293 | | ``+x`` | *x* unchanged | | | |
| 294 | +---------------------+---------------------------------+---------+--------------------+ |
| 295 | | ``abs(x)`` | absolute value or magnitude of | | :func:`abs` | |
| 296 | | | *x* | | | |
| 297 | +---------------------+---------------------------------+---------+--------------------+ |
| 298 | | ``int(x)`` | *x* converted to integer | \(3)\(6)| :func:`int` | |
| 299 | +---------------------+---------------------------------+---------+--------------------+ |
| 300 | | ``float(x)`` | *x* converted to floating point | \(4)\(6)| :func:`float` | |
| 301 | +---------------------+---------------------------------+---------+--------------------+ |
| 302 | | ``complex(re, im)`` | a complex number with real part | \(6) | :func:`complex` | |
| 303 | | | *re*, imaginary part *im*. | | | |
| 304 | | | *im* defaults to zero. | | | |
| 305 | +---------------------+---------------------------------+---------+--------------------+ |
| 306 | | ``c.conjugate()`` | conjugate of the complex number | | | |
| 307 | | | *c* | | | |
| 308 | +---------------------+---------------------------------+---------+--------------------+ |
| 309 | | ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` | |
| 310 | +---------------------+---------------------------------+---------+--------------------+ |
| 311 | | ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` | |
| 312 | +---------------------+---------------------------------+---------+--------------------+ |
| 313 | | ``x ** y`` | *x* to the power *y* | \(5) | | |
| 314 | +---------------------+---------------------------------+---------+--------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | |
| 316 | .. index:: |
| 317 | triple: operations on; numeric; types |
| 318 | single: conjugate() (complex number method) |
| 319 | |
| 320 | Notes: |
| 321 | |
| 322 | (1) |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 323 | Also referred to as integer division. The resultant value is a whole |
| 324 | integer, though the result's type is not necessarily int. The result is |
| 325 | always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is |
| 326 | ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | |
| 328 | (2) |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 329 | Not for complex numbers. Instead convert to floats using :func:`abs` if |
| 330 | appropriate. |
| 331 | |
| 332 | (3) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | .. index:: |
| 334 | module: math |
| 335 | single: floor() (in module math) |
| 336 | single: ceil() (in module math) |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 337 | single: trunc() (in module math) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | pair: numeric; conversions |
| 339 | pair: C; language |
| 340 | |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 341 | Conversion from floating point to integer may round or truncate |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 342 | as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module |
| 343 | for well-defined conversions. |
| 344 | |
Georg Brandl | 74f3669 | 2008-01-06 17:39:49 +0000 | [diff] [blame] | 345 | (4) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 346 | float also accepts the strings "nan" and "inf" with an optional prefix "+" |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 347 | or "-" for Not a Number (NaN) and positive or negative infinity. |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 348 | |
Georg Brandl | 74f3669 | 2008-01-06 17:39:49 +0000 | [diff] [blame] | 349 | (5) |
Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 350 | Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for |
| 351 | programming languages. |
| 352 | |
Raymond Hettinger | c706dbf | 2011-03-22 17:33:17 -0700 | [diff] [blame] | 353 | (6) |
| 354 | The numeric literals accepted include the digits ``0`` to ``9`` or any |
| 355 | Unicode equivalent (code points with the ``Nd`` property). |
| 356 | |
| 357 | See http://www.unicode.org/Public/6.0.0/ucd/extracted/DerivedNumericType.txt |
| 358 | for a complete list of code points with the ``Nd`` property. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 359 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 360 | |
Benjamin Peterson | 10116d4 | 2011-05-01 17:38:17 -0500 | [diff] [blame] | 361 | All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include |
| 362 | the following operations: |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 363 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 364 | +--------------------+------------------------------------+--------+ |
| 365 | | Operation | Result | Notes | |
| 366 | +====================+====================================+========+ |
| 367 | | ``math.trunc(x)`` | *x* truncated to Integral | | |
| 368 | +--------------------+------------------------------------+--------+ |
| 369 | | ``round(x[, n])`` | *x* rounded to n digits, | | |
| 370 | | | rounding half to even. If n is | | |
| 371 | | | omitted, it defaults to 0. | | |
| 372 | +--------------------+------------------------------------+--------+ |
| 373 | | ``math.floor(x)`` | the greatest integral float <= *x* | | |
| 374 | +--------------------+------------------------------------+--------+ |
| 375 | | ``math.ceil(x)`` | the least integral float >= *x* | | |
| 376 | +--------------------+------------------------------------+--------+ |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 377 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 378 | For additional numeric operations see the :mod:`math` and :mod:`cmath` |
| 379 | modules. |
| 380 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 381 | .. XXXJH exceptions: overflow (when? what operations?) zerodivision |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
| 383 | |
| 384 | .. _bitstring-ops: |
| 385 | |
Benjamin Peterson | e9fca25 | 2012-01-25 16:29:03 -0500 | [diff] [blame] | 386 | Bitwise Operations on Integer Types |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | -------------------------------------- |
| 388 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 389 | .. index:: |
| 390 | triple: operations on; integer; types |
Benjamin Peterson | e9fca25 | 2012-01-25 16:29:03 -0500 | [diff] [blame] | 391 | pair: bitwise; operations |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 392 | pair: shifting; operations |
| 393 | pair: masking; operations |
| 394 | operator: ^ |
| 395 | operator: & |
| 396 | operator: << |
| 397 | operator: >> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
Benjamin Peterson | b4b0b35 | 2012-01-25 16:30:18 -0500 | [diff] [blame] | 399 | Bitwise operations only make sense for integers. Negative numbers are treated |
| 400 | as their 2's complement value (this assumes a sufficiently large number of bits |
| 401 | that no overflow occurs during the operation). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 402 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 403 | The priorities of the binary bitwise operations are all lower than the numeric |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | operations and higher than the comparisons; the unary operation ``~`` has the |
| 405 | same priority as the other unary numeric operations (``+`` and ``-``). |
| 406 | |
Benjamin Peterson | e9fca25 | 2012-01-25 16:29:03 -0500 | [diff] [blame] | 407 | This table lists the bitwise operations sorted in ascending priority |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | (operations in the same box have the same priority): |
| 409 | |
| 410 | +------------+--------------------------------+----------+ |
| 411 | | Operation | Result | Notes | |
| 412 | +============+================================+==========+ |
| 413 | | ``x | y`` | bitwise :dfn:`or` of *x* and | | |
| 414 | | | *y* | | |
| 415 | +------------+--------------------------------+----------+ |
| 416 | | ``x ^ y`` | bitwise :dfn:`exclusive or` of | | |
| 417 | | | *x* and *y* | | |
| 418 | +------------+--------------------------------+----------+ |
| 419 | | ``x & y`` | bitwise :dfn:`and` of *x* and | | |
| 420 | | | *y* | | |
| 421 | +------------+--------------------------------+----------+ |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 422 | | ``x << n`` | *x* shifted left by *n* bits | (1)(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | +------------+--------------------------------+----------+ |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 424 | | ``x >> n`` | *x* shifted right by *n* bits | (1)(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | +------------+--------------------------------+----------+ |
| 426 | | ``~x`` | the bits of *x* inverted | | |
| 427 | +------------+--------------------------------+----------+ |
| 428 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | Notes: |
| 430 | |
| 431 | (1) |
| 432 | Negative shift counts are illegal and cause a :exc:`ValueError` to be raised. |
| 433 | |
| 434 | (2) |
| 435 | A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)`` |
| 436 | without overflow check. |
| 437 | |
| 438 | (3) |
| 439 | A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without |
| 440 | overflow check. |
| 441 | |
| 442 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 443 | Additional Methods on Integer Types |
| 444 | ----------------------------------- |
| 445 | |
Raymond Hettinger | 9b2fd32 | 2011-05-01 18:14:49 -0700 | [diff] [blame] | 446 | The int type implements the :class:`numbers.Integral` :term:`abstract base |
Éric Araujo | b79c234 | 2011-05-02 13:10:18 +0200 | [diff] [blame] | 447 | class`. In addition, it provides one more method: |
Benjamin Peterson | 10116d4 | 2011-05-01 17:38:17 -0500 | [diff] [blame] | 448 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 449 | .. method:: int.bit_length() |
| 450 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 451 | Return the number of bits necessary to represent an integer in binary, |
| 452 | excluding the sign and leading zeros:: |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 453 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 454 | >>> n = -37 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 455 | >>> bin(n) |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 456 | '-0b100101' |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 457 | >>> n.bit_length() |
| 458 | 6 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 459 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 460 | More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the |
| 461 | unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``. |
| 462 | Equivalently, when ``abs(x)`` is small enough to have a correctly |
| 463 | rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``. |
| 464 | If ``x`` is zero, then ``x.bit_length()`` returns ``0``. |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 465 | |
| 466 | Equivalent to:: |
| 467 | |
| 468 | def bit_length(self): |
Senthil Kumaran | 0aae6dc | 2010-06-22 02:57:23 +0000 | [diff] [blame] | 469 | s = bin(self) # binary representation: bin(-37) --> '-0b100101' |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 470 | s = s.lstrip('-0b') # remove leading zeros and minus sign |
| 471 | return len(s) # len('100101') --> 6 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 472 | |
| 473 | .. versionadded:: 3.1 |
| 474 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 475 | .. method:: int.to_bytes(length, byteorder, \*, signed=False) |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 476 | |
| 477 | Return an array of bytes representing an integer. |
| 478 | |
| 479 | >>> (1024).to_bytes(2, byteorder='big') |
| 480 | b'\x04\x00' |
| 481 | >>> (1024).to_bytes(10, byteorder='big') |
| 482 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00' |
| 483 | >>> (-1024).to_bytes(10, byteorder='big', signed=True) |
| 484 | b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00' |
| 485 | >>> x = 1000 |
| 486 | >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder='little') |
| 487 | b'\xe8\x03' |
| 488 | |
| 489 | The integer is represented using *length* bytes. An :exc:`OverflowError` |
| 490 | is raised if the integer is not representable with the given number of |
| 491 | bytes. |
| 492 | |
| 493 | The *byteorder* argument determines the byte order used to represent the |
| 494 | integer. If *byteorder* is ``"big"``, the most significant byte is at the |
| 495 | beginning of the byte array. If *byteorder* is ``"little"``, the most |
| 496 | significant byte is at the end of the byte array. To request the native |
| 497 | byte order of the host system, use :data:`sys.byteorder` as the byte order |
| 498 | value. |
| 499 | |
| 500 | The *signed* argument determines whether two's complement is used to |
| 501 | represent the integer. If *signed* is ``False`` and a negative integer is |
| 502 | given, an :exc:`OverflowError` is raised. The default value for *signed* |
| 503 | is ``False``. |
| 504 | |
| 505 | .. versionadded:: 3.2 |
| 506 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 507 | .. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False) |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 508 | |
| 509 | Return the integer represented by the given array of bytes. |
| 510 | |
| 511 | >>> int.from_bytes(b'\x00\x10', byteorder='big') |
| 512 | 16 |
| 513 | >>> int.from_bytes(b'\x00\x10', byteorder='little') |
| 514 | 4096 |
| 515 | >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) |
| 516 | -1024 |
| 517 | >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False) |
| 518 | 64512 |
| 519 | >>> int.from_bytes([255, 0, 0], byteorder='big') |
| 520 | 16711680 |
| 521 | |
| 522 | The argument *bytes* must either support the buffer protocol or be an |
| 523 | iterable producing bytes. :class:`bytes` and :class:`bytearray` are |
| 524 | examples of built-in objects that support the buffer protocol. |
| 525 | |
| 526 | The *byteorder* argument determines the byte order used to represent the |
| 527 | integer. If *byteorder* is ``"big"``, the most significant byte is at the |
| 528 | beginning of the byte array. If *byteorder* is ``"little"``, the most |
| 529 | significant byte is at the end of the byte array. To request the native |
| 530 | byte order of the host system, use :data:`sys.byteorder` as the byte order |
| 531 | value. |
| 532 | |
| 533 | The *signed* argument indicates whether two's complement is used to |
| 534 | represent the integer. |
| 535 | |
| 536 | .. versionadded:: 3.2 |
| 537 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 538 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 539 | Additional Methods on Float |
| 540 | --------------------------- |
| 541 | |
Benjamin Peterson | 10116d4 | 2011-05-01 17:38:17 -0500 | [diff] [blame] | 542 | The float type implements the :class:`numbers.Real` :term:`abstract base |
| 543 | class`. float also has the following additional methods. |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 544 | |
| 545 | .. method:: float.as_integer_ratio() |
| 546 | |
Mark Dickinson | 4a3c7c4 | 2010-11-07 12:48:18 +0000 | [diff] [blame] | 547 | Return a pair of integers whose ratio is exactly equal to the |
| 548 | original float and with a positive denominator. Raises |
| 549 | :exc:`OverflowError` on infinities and a :exc:`ValueError` on |
| 550 | NaNs. |
| 551 | |
| 552 | .. method:: float.is_integer() |
| 553 | |
| 554 | Return ``True`` if the float instance is finite with integral |
| 555 | value, and ``False`` otherwise:: |
| 556 | |
| 557 | >>> (-2.0).is_integer() |
| 558 | True |
| 559 | >>> (3.2).is_integer() |
| 560 | False |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 561 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 562 | Two methods support conversion to |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 563 | and from hexadecimal strings. Since Python's floats are stored |
| 564 | internally as binary numbers, converting a float to or from a |
| 565 | *decimal* string usually involves a small rounding error. In |
| 566 | contrast, hexadecimal strings allow exact representation and |
| 567 | specification of floating-point numbers. This can be useful when |
| 568 | debugging, and in numerical work. |
| 569 | |
| 570 | |
| 571 | .. method:: float.hex() |
| 572 | |
| 573 | Return a representation of a floating-point number as a hexadecimal |
| 574 | string. For finite floating-point numbers, this representation |
| 575 | will always include a leading ``0x`` and a trailing ``p`` and |
| 576 | exponent. |
| 577 | |
| 578 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 579 | .. classmethod:: float.fromhex(s) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 580 | |
| 581 | Class method to return the float represented by a hexadecimal |
| 582 | string *s*. The string *s* may have leading and trailing |
| 583 | whitespace. |
| 584 | |
| 585 | |
| 586 | Note that :meth:`float.hex` is an instance method, while |
| 587 | :meth:`float.fromhex` is a class method. |
| 588 | |
| 589 | A hexadecimal string takes the form:: |
| 590 | |
| 591 | [sign] ['0x'] integer ['.' fraction] ['p' exponent] |
| 592 | |
| 593 | where the optional ``sign`` may by either ``+`` or ``-``, ``integer`` |
| 594 | and ``fraction`` are strings of hexadecimal digits, and ``exponent`` |
| 595 | is a decimal integer with an optional leading sign. Case is not |
| 596 | significant, and there must be at least one hexadecimal digit in |
| 597 | either the integer or the fraction. This syntax is similar to the |
| 598 | syntax specified in section 6.4.4.2 of the C99 standard, and also to |
| 599 | the syntax used in Java 1.5 onwards. In particular, the output of |
| 600 | :meth:`float.hex` is usable as a hexadecimal floating-point literal in |
| 601 | C or Java code, and hexadecimal strings produced by C's ``%a`` format |
| 602 | character or Java's ``Double.toHexString`` are accepted by |
| 603 | :meth:`float.fromhex`. |
| 604 | |
| 605 | |
| 606 | Note that the exponent is written in decimal rather than hexadecimal, |
| 607 | and that it gives the power of 2 by which to multiply the coefficient. |
| 608 | For example, the hexadecimal string ``0x3.a7p10`` represents the |
| 609 | floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or |
| 610 | ``3740.0``:: |
| 611 | |
| 612 | >>> float.fromhex('0x3.a7p10') |
| 613 | 3740.0 |
| 614 | |
| 615 | |
| 616 | Applying the reverse conversion to ``3740.0`` gives a different |
| 617 | hexadecimal string representing the same number:: |
| 618 | |
| 619 | >>> float.hex(3740.0) |
| 620 | '0x1.d380000000000p+11' |
| 621 | |
| 622 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 623 | .. _numeric-hash: |
| 624 | |
| 625 | Hashing of numeric types |
| 626 | ------------------------ |
| 627 | |
| 628 | For numbers ``x`` and ``y``, possibly of different types, it's a requirement |
| 629 | that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__` |
| 630 | method documentation for more details). For ease of implementation and |
| 631 | efficiency across a variety of numeric types (including :class:`int`, |
| 632 | :class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`) |
| 633 | Python's hash for numeric types is based on a single mathematical function |
| 634 | that's defined for any rational number, and hence applies to all instances of |
| 635 | :class:`int` and :class:`fraction.Fraction`, and all finite instances of |
| 636 | :class:`float` and :class:`decimal.Decimal`. Essentially, this function is |
| 637 | given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is |
| 638 | made available to Python as the :attr:`modulus` attribute of |
| 639 | :data:`sys.hash_info`. |
| 640 | |
| 641 | .. impl-detail:: |
| 642 | |
| 643 | Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C |
| 644 | longs and ``P = 2**61 - 1`` on machines with 64-bit C longs. |
| 645 | |
| 646 | Here are the rules in detail: |
| 647 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 648 | - If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible |
| 649 | by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n, |
| 650 | P)`` gives the inverse of ``n`` modulo ``P``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 651 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 652 | - If ``x = m / n`` is a nonnegative rational number and ``n`` is |
| 653 | divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse |
| 654 | modulo ``P`` and the rule above doesn't apply; in this case define |
| 655 | ``hash(x)`` to be the constant value ``sys.hash_info.inf``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 656 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 657 | - If ``x = m / n`` is a negative rational number define ``hash(x)`` |
| 658 | as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with |
| 659 | ``-2``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 660 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 661 | - The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf`` |
| 662 | and ``sys.hash_info.nan`` are used as hash values for positive |
| 663 | infinity, negative infinity, or nans (respectively). (All hashable |
| 664 | nans have the same hash value.) |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 665 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 666 | - For a :class:`complex` number ``z``, the hash values of the real |
| 667 | and imaginary parts are combined by computing ``hash(z.real) + |
| 668 | sys.hash_info.imag * hash(z.imag)``, reduced modulo |
| 669 | ``2**sys.hash_info.width`` so that it lies in |
| 670 | ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - |
| 671 | 1))``. Again, if the result is ``-1``, it's replaced with ``-2``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 672 | |
| 673 | |
| 674 | To clarify the above rules, here's some example Python code, |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 675 | equivalent to the built-in hash, for computing the hash of a rational |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 676 | number, :class:`float`, or :class:`complex`:: |
| 677 | |
| 678 | |
| 679 | import sys, math |
| 680 | |
| 681 | def hash_fraction(m, n): |
| 682 | """Compute the hash of a rational number m / n. |
| 683 | |
| 684 | Assumes m and n are integers, with n positive. |
| 685 | Equivalent to hash(fractions.Fraction(m, n)). |
| 686 | |
| 687 | """ |
| 688 | P = sys.hash_info.modulus |
| 689 | # Remove common factors of P. (Unnecessary if m and n already coprime.) |
| 690 | while m % P == n % P == 0: |
| 691 | m, n = m // P, n // P |
| 692 | |
| 693 | if n % P == 0: |
| 694 | hash_ = sys.hash_info.inf |
| 695 | else: |
| 696 | # Fermat's Little Theorem: pow(n, P-1, P) is 1, so |
| 697 | # pow(n, P-2, P) gives the inverse of n modulo P. |
| 698 | hash_ = (abs(m) % P) * pow(n, P - 2, P) % P |
| 699 | if m < 0: |
| 700 | hash_ = -hash_ |
| 701 | if hash_ == -1: |
| 702 | hash_ = -2 |
| 703 | return hash_ |
| 704 | |
| 705 | def hash_float(x): |
| 706 | """Compute the hash of a float x.""" |
| 707 | |
| 708 | if math.isnan(x): |
| 709 | return sys.hash_info.nan |
| 710 | elif math.isinf(x): |
| 711 | return sys.hash_info.inf if x > 0 else -sys.hash_info.inf |
| 712 | else: |
| 713 | return hash_fraction(*x.as_integer_ratio()) |
| 714 | |
| 715 | def hash_complex(z): |
| 716 | """Compute the hash of a complex number z.""" |
| 717 | |
| 718 | hash_ = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag) |
| 719 | # do a signed reduction modulo 2**sys.hash_info.width |
| 720 | M = 2**(sys.hash_info.width - 1) |
| 721 | hash_ = (hash_ & (M - 1)) - (hash & M) |
| 722 | if hash_ == -1: |
| 723 | hash_ == -2 |
| 724 | return hash_ |
| 725 | |
Georg Brandl | 6ea420b | 2008-07-16 12:58:29 +0000 | [diff] [blame] | 726 | .. _typeiter: |
| 727 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 728 | Iterator Types |
| 729 | ============== |
| 730 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 731 | .. index:: |
| 732 | single: iterator protocol |
| 733 | single: protocol; iterator |
| 734 | single: sequence; iteration |
| 735 | single: container; iteration over |
| 736 | |
| 737 | Python supports a concept of iteration over containers. This is implemented |
| 738 | using two distinct methods; these are used to allow user-defined classes to |
| 739 | support iteration. Sequences, described below in more detail, always support |
| 740 | the iteration methods. |
| 741 | |
| 742 | One method needs to be defined for container objects to provide iteration |
| 743 | support: |
| 744 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 745 | .. XXX duplicated in reference/datamodel! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 746 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 747 | .. method:: container.__iter__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | |
| 749 | Return an iterator object. The object is required to support the iterator |
| 750 | protocol described below. If a container supports different types of |
| 751 | iteration, additional methods can be provided to specifically request |
| 752 | iterators for those iteration types. (An example of an object supporting |
| 753 | multiple forms of iteration would be a tree structure which supports both |
| 754 | breadth-first and depth-first traversal.) This method corresponds to the |
| 755 | :attr:`tp_iter` slot of the type structure for Python objects in the Python/C |
| 756 | API. |
| 757 | |
| 758 | The iterator objects themselves are required to support the following two |
| 759 | methods, which together form the :dfn:`iterator protocol`: |
| 760 | |
| 761 | |
| 762 | .. method:: iterator.__iter__() |
| 763 | |
| 764 | Return the iterator object itself. This is required to allow both containers |
| 765 | and iterators to be used with the :keyword:`for` and :keyword:`in` statements. |
| 766 | This method corresponds to the :attr:`tp_iter` slot of the type structure for |
| 767 | Python objects in the Python/C API. |
| 768 | |
| 769 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 770 | .. method:: iterator.__next__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 771 | |
| 772 | Return the next item from the container. If there are no further items, raise |
| 773 | the :exc:`StopIteration` exception. This method corresponds to the |
| 774 | :attr:`tp_iternext` slot of the type structure for Python objects in the |
| 775 | Python/C API. |
| 776 | |
| 777 | Python defines several iterator objects to support iteration over general and |
| 778 | specific sequence types, dictionaries, and other more specialized forms. The |
| 779 | specific types are not important beyond their implementation of the iterator |
| 780 | protocol. |
| 781 | |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 782 | Once an iterator's :meth:`~iterator.__next__` method raises |
| 783 | :exc:`StopIteration`, it must continue to do so on subsequent calls. |
| 784 | Implementations that do not obey this property are deemed broken. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 786 | |
| 787 | .. _generator-types: |
| 788 | |
| 789 | Generator Types |
| 790 | --------------- |
| 791 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 792 | Python's :term:`generator`\s provide a convenient way to implement the iterator |
| 793 | protocol. If a container object's :meth:`__iter__` method is implemented as a |
| 794 | generator, it will automatically return an iterator object (technically, a |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 795 | generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__` |
| 796 | methods. |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 797 | More information about generators can be found in :ref:`the documentation for |
| 798 | the yield expression <yieldexpr>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 799 | |
| 800 | |
| 801 | .. _typesseq: |
| 802 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 803 | Sequence Types --- :class:`list`, :class:`tuple`, :class:`range` |
| 804 | ================================================================ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 805 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 806 | There are three basic sequence types: lists, tuples, and range objects. |
| 807 | Additional sequence types tailored for processing of |
| 808 | :ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are |
| 809 | described in dedicated sections. |
Georg Brandl | e17d586 | 2009-01-18 10:40:25 +0000 | [diff] [blame] | 810 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 811 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 812 | .. _typesseq-common: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 813 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 814 | Common Sequence Operations |
| 815 | -------------------------- |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 816 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 817 | .. index:: object: sequence |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 818 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 819 | The operations in the following table are supported by most sequence types, |
| 820 | both mutable and immutable. The :class:`collections.abc.Sequence` ABC is |
| 821 | provided to make it easier to correctly implement these operations on |
| 822 | custom sequence types. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 823 | |
| 824 | This table lists the sequence operations sorted in ascending priority |
| 825 | (operations in the same box have the same priority). In the table, *s* and *t* |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 826 | are sequences of the same type, *n*, *i*, *j* and *k* are integers and *x* is |
| 827 | an arbitrary object that meets any type and value restrictions imposed by *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 828 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 829 | The ``in`` and ``not in`` operations have the same priorities as the |
| 830 | comparison operations. The ``+`` (concatenation) and ``*`` (repetition) |
| 831 | operations have the same priority as the corresponding numeric operations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 832 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 833 | .. index:: |
| 834 | triple: operations on; sequence; types |
| 835 | builtin: len |
| 836 | builtin: min |
| 837 | builtin: max |
| 838 | pair: concatenation; operation |
| 839 | pair: repetition; operation |
| 840 | pair: subscript; operation |
| 841 | pair: slice; operation |
| 842 | operator: in |
| 843 | operator: not in |
| 844 | single: count() (sequence method) |
| 845 | single: index() (sequence method) |
| 846 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 847 | +--------------------------+--------------------------------+----------+ |
| 848 | | Operation | Result | Notes | |
| 849 | +==========================+================================+==========+ |
| 850 | | ``x in s`` | ``True`` if an item of *s* is | \(1) | |
| 851 | | | equal to *x*, else ``False`` | | |
| 852 | +--------------------------+--------------------------------+----------+ |
| 853 | | ``x not in s`` | ``False`` if an item of *s* is | \(1) | |
| 854 | | | equal to *x*, else ``True`` | | |
| 855 | +--------------------------+--------------------------------+----------+ |
| 856 | | ``s + t`` | the concatenation of *s* and | (6)(7) | |
| 857 | | | *t* | | |
| 858 | +--------------------------+--------------------------------+----------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 859 | | ``s * n`` or | *n* shallow copies of *s* | (2)(7) | |
| 860 | | ``n * s`` | concatenated | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 861 | +--------------------------+--------------------------------+----------+ |
| 862 | | ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) | |
| 863 | +--------------------------+--------------------------------+----------+ |
| 864 | | ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) | |
| 865 | +--------------------------+--------------------------------+----------+ |
| 866 | | ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) | |
| 867 | | | with step *k* | | |
| 868 | +--------------------------+--------------------------------+----------+ |
| 869 | | ``len(s)`` | length of *s* | | |
| 870 | +--------------------------+--------------------------------+----------+ |
| 871 | | ``min(s)`` | smallest item of *s* | | |
| 872 | +--------------------------+--------------------------------+----------+ |
| 873 | | ``max(s)`` | largest item of *s* | | |
| 874 | +--------------------------+--------------------------------+----------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 875 | | ``s.index(x[, i[, j]])`` | index of the first occurence | \(8) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 876 | | | of *x* in *s* (at or after | | |
| 877 | | | index *i* and before index *j*)| | |
| 878 | +--------------------------+--------------------------------+----------+ |
| 879 | | ``s.count(x)`` | total number of occurences of | | |
| 880 | | | *x* in *s* | | |
| 881 | +--------------------------+--------------------------------+----------+ |
| 882 | |
| 883 | Sequences of the same type also support comparisons. In particular, tuples |
| 884 | and lists are compared lexicographically by comparing corresponding elements. |
| 885 | This means that to compare equal, every element must compare equal and the |
| 886 | two sequences must be of the same type and have the same length. (For full |
| 887 | details see :ref:`comparisons` in the language reference.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 888 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 889 | Notes: |
| 890 | |
| 891 | (1) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 892 | While the ``in`` and ``not in`` operations are used only for simple |
| 893 | containment testing in the general case, some specialised sequences |
| 894 | (such as :class:`str`, :class:`bytes` and :class:`bytearray`) also use |
| 895 | them for subsequence testing:: |
| 896 | |
| 897 | >>> "gg" in "eggs" |
| 898 | True |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 899 | |
| 900 | (2) |
| 901 | Values of *n* less than ``0`` are treated as ``0`` (which yields an empty |
| 902 | sequence of the same type as *s*). Note also that the copies are shallow; |
| 903 | nested structures are not copied. This often haunts new Python programmers; |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 904 | consider:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 905 | |
| 906 | >>> lists = [[]] * 3 |
| 907 | >>> lists |
| 908 | [[], [], []] |
| 909 | >>> lists[0].append(3) |
| 910 | >>> lists |
| 911 | [[3], [3], [3]] |
| 912 | |
| 913 | What has happened is that ``[[]]`` is a one-element list containing an empty |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 914 | list, so all three elements of ``[[]] * 3`` are (pointers to) this single empty |
| 915 | list. Modifying any of the elements of ``lists`` modifies this single list. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 916 | You can create a list of different lists this way:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 917 | |
| 918 | >>> lists = [[] for i in range(3)] |
| 919 | >>> lists[0].append(3) |
| 920 | >>> lists[1].append(5) |
| 921 | >>> lists[2].append(7) |
| 922 | >>> lists |
| 923 | [[3], [5], [7]] |
| 924 | |
| 925 | (3) |
| 926 | 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] | 927 | ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is |
| 928 | still ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 929 | |
| 930 | (4) |
| 931 | The slice of *s* from *i* to *j* is defined as the sequence of items with index |
| 932 | *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use |
| 933 | ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or |
| 934 | ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is |
| 935 | empty. |
| 936 | |
| 937 | (5) |
| 938 | The slice of *s* from *i* to *j* with step *k* is defined as the sequence of |
Christian Heimes | 2c18161 | 2007-12-17 20:04:13 +0000 | [diff] [blame] | 939 | items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 940 | the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when |
| 941 | *j* is reached (but never including *j*). If *i* or *j* is greater than |
| 942 | ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become |
| 943 | "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero. |
| 944 | If *k* is ``None``, it is treated like ``1``. |
| 945 | |
| 946 | (6) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 947 | Concatenating immutable sequences always results in a new object. This |
| 948 | means that building up a sequence by repeated concatenation will have a |
| 949 | quadratic runtime cost in the total sequence length. To get a linear |
| 950 | runtime cost, you must switch to one of the alternatives below: |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | fd9ebd4 | 2011-11-25 16:33:53 +0100 | [diff] [blame] | 952 | * if concatenating :class:`str` objects, you can build a list and use |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 953 | :meth:`str.join` at the end or else write to a :class:`io.StringIO` |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 954 | instance and retrieve its value when complete |
Antoine Pitrou | fd9ebd4 | 2011-11-25 16:33:53 +0100 | [diff] [blame] | 955 | |
| 956 | * if concatenating :class:`bytes` objects, you can similarly use |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 957 | :meth:`bytes.join` or :class:`io.BytesIO`, or you can do in-place |
| 958 | concatenation with a :class:`bytearray` object. :class:`bytearray` |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 959 | objects are mutable and have an efficient overallocation mechanism |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 960 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 961 | * if concatenating :class:`tuple` objects, extend a :class:`list` instead |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 962 | |
| 963 | * for other types, investigate the relevant class documentation |
| 964 | |
| 965 | |
| 966 | (7) |
| 967 | Some sequence types (such as :class:`range`) only support item sequences |
| 968 | that follow specific patterns, and hence don't support sequence |
| 969 | concatenation or repetition. |
| 970 | |
| 971 | (8) |
| 972 | ``index`` raises :exc:`ValueError` when *x* is not found in *s*. |
| 973 | When supported, the additional arguments to the index method allow |
| 974 | efficient searching of subsections of the sequence. Passing the extra |
| 975 | arguments is roughly equivalent to using ``s[i:j].index(x)``, only |
| 976 | without copying any data and with the returned index being relative to |
| 977 | the start of the sequence rather than the start of the slice. |
| 978 | |
| 979 | |
| 980 | .. _typesseq-immutable: |
| 981 | |
| 982 | Immutable Sequence Types |
| 983 | ------------------------ |
| 984 | |
| 985 | .. index:: |
| 986 | triple: immutable; sequence; types |
| 987 | object: tuple |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 988 | builtin: hash |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 989 | |
| 990 | The only operation that immutable sequence types generally implement that is |
| 991 | not also implemented by mutable sequence types is support for the :func:`hash` |
| 992 | built-in. |
| 993 | |
| 994 | This support allows immutable sequences, such as :class:`tuple` instances, to |
| 995 | be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset` |
| 996 | instances. |
| 997 | |
| 998 | Attempting to hash an immutable sequence that contains unhashable values will |
| 999 | result in :exc:`TypeError`. |
| 1000 | |
| 1001 | |
| 1002 | .. _typesseq-mutable: |
| 1003 | |
| 1004 | Mutable Sequence Types |
| 1005 | ---------------------- |
| 1006 | |
| 1007 | .. index:: |
| 1008 | triple: mutable; sequence; types |
| 1009 | object: list |
| 1010 | object: bytearray |
| 1011 | |
| 1012 | The operations in the following table are defined on mutable sequence types. |
| 1013 | The :class:`collections.abc.MutableSequence` ABC is provided to make it |
| 1014 | easier to correctly implement these operations on custom sequence types. |
| 1015 | |
| 1016 | In the table *s* is an instance of a mutable sequence type, *t* is any |
| 1017 | iterable object and *x* is an arbitrary object that meets any type |
| 1018 | and value restrictions imposed by *s* (for example, :class:`bytearray` only |
| 1019 | accepts integers that meet the value restriction ``0 <= x <= 255``). |
| 1020 | |
| 1021 | |
| 1022 | .. index:: |
| 1023 | triple: operations on; sequence; types |
| 1024 | triple: operations on; list; type |
| 1025 | pair: subscript; assignment |
| 1026 | pair: slice; assignment |
| 1027 | statement: del |
| 1028 | single: append() (sequence method) |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1029 | single: clear() (sequence method) |
| 1030 | single: copy() (sequence method) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1031 | single: extend() (sequence method) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1032 | single: insert() (sequence method) |
| 1033 | single: pop() (sequence method) |
| 1034 | single: remove() (sequence method) |
| 1035 | single: reverse() (sequence method) |
| 1036 | |
| 1037 | +------------------------------+--------------------------------+---------------------+ |
| 1038 | | Operation | Result | Notes | |
| 1039 | +==============================+================================+=====================+ |
| 1040 | | ``s[i] = x`` | item *i* of *s* is replaced by | | |
| 1041 | | | *x* | | |
| 1042 | +------------------------------+--------------------------------+---------------------+ |
| 1043 | | ``s[i:j] = t`` | slice of *s* from *i* to *j* | | |
| 1044 | | | is replaced by the contents of | | |
| 1045 | | | the iterable *t* | | |
| 1046 | +------------------------------+--------------------------------+---------------------+ |
| 1047 | | ``del s[i:j]`` | same as ``s[i:j] = []`` | | |
| 1048 | +------------------------------+--------------------------------+---------------------+ |
| 1049 | | ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) | |
| 1050 | | | are replaced by those of *t* | | |
| 1051 | +------------------------------+--------------------------------+---------------------+ |
| 1052 | | ``del s[i:j:k]`` | removes the elements of | | |
| 1053 | | | ``s[i:j:k]`` from the list | | |
| 1054 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1055 | | ``s.append(x)`` | appends *x* to the end of the | | |
| 1056 | | | sequence (same as | | |
| 1057 | | | ``s[len(s):len(s)] = [x]``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1058 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1059 | | ``s.clear()`` | removes all items from ``s`` | \(5) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1060 | | | (same as ``del s[:]``) | | |
| 1061 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1062 | | ``s.copy()`` | creates a shallow copy of ``s``| \(5) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1063 | | | (same as ``s[:]``) | | |
| 1064 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1065 | | ``s.extend(t)`` | extends *s* with the | | |
| 1066 | | | contents of *t* (same as | | |
| 1067 | | | ``s[len(s):len(s)] = t``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1068 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1069 | | ``s.insert(i, x)`` | inserts *x* into *s* at the | | |
| 1070 | | | index given by *i* | | |
| 1071 | | | (same as ``s[i:i] = [x]``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1072 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1073 | | ``s.pop([i])`` | retrieves the item at *i* and | \(2) | |
| 1074 | | | also removes it from *s* | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1075 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1076 | | ``s.remove(x)`` | remove the first item from *s* | \(3) | |
| 1077 | | | where ``s[i] == x`` | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1078 | +------------------------------+--------------------------------+---------------------+ |
| 1079 | | ``s.reverse()`` | reverses the items of *s* in | \(4) | |
| 1080 | | | place | | |
| 1081 | +------------------------------+--------------------------------+---------------------+ |
| 1082 | |
| 1083 | |
| 1084 | Notes: |
| 1085 | |
| 1086 | (1) |
| 1087 | *t* must have the same length as the slice it is replacing. |
| 1088 | |
| 1089 | (2) |
| 1090 | The optional argument *i* defaults to ``-1``, so that by default the last |
| 1091 | item is removed and returned. |
| 1092 | |
| 1093 | (3) |
| 1094 | ``remove`` raises :exc:`ValueError` when *x* is not found in *s*. |
| 1095 | |
| 1096 | (4) |
| 1097 | The :meth:`reverse` method modifies the sequence in place for economy of |
| 1098 | space when reversing a large sequence. To remind users that it operates by |
| 1099 | side effect, it does not return the reversed sequence. |
| 1100 | |
| 1101 | (5) |
| 1102 | :meth:`clear` and :meth:`!copy` are included for consistency with the |
| 1103 | interfaces of mutable containers that don't support slicing operations |
| 1104 | (such as :class:`dict` and :class:`set`) |
| 1105 | |
| 1106 | .. versionadded:: 3.3 |
| 1107 | :meth:`clear` and :meth:`!copy` methods. |
| 1108 | |
| 1109 | |
| 1110 | .. _typesseq-list: |
| 1111 | |
| 1112 | Lists |
| 1113 | ----- |
| 1114 | |
| 1115 | .. index:: object: list |
| 1116 | |
| 1117 | Lists are mutable sequences, typically used to store collections of |
| 1118 | homogeneous items (where the precise degree of similarity will vary by |
| 1119 | application). |
| 1120 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1121 | .. class:: list([iterable]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1122 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1123 | Lists may be constructed in several ways: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1124 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1125 | * Using a pair of square brackets to denote the empty list: ``[]`` |
| 1126 | * Using square brackets, separating items with commas: ``[a]``, ``[a, b, c]`` |
| 1127 | * Using a list comprehension: ``[x for x in iterable]`` |
| 1128 | * Using the type constructor: ``list()`` or ``list(iterable)`` |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1129 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1130 | The constructor builds a list whose items are the same and in the same |
| 1131 | order as *iterable*'s items. *iterable* may be either a sequence, a |
| 1132 | container that supports iteration, or an iterator object. If *iterable* |
| 1133 | is already a list, a copy is made and returned, similar to ``iterable[:]``. |
| 1134 | For example, ``list('abc')`` returns ``['a', 'b', 'c']`` and |
| 1135 | ``list( (1, 2, 3) )`` returns ``[1, 2, 3]``. |
| 1136 | If no argument is given, the constructor creates a new empty list, ``[]``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1137 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1138 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1139 | Many other operations also produce lists, including the :func:`sorted` |
| 1140 | built-in. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1141 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1142 | Lists implement all of the :ref:`common <typesseq-common>` and |
| 1143 | :ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the |
| 1144 | following additional method: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1145 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1146 | .. method:: list.sort(*, key=None, reverse=None) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1147 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1148 | This method sorts the list in place, using only ``<`` comparisons |
| 1149 | between items. Exceptions are not suppressed - if any comparison operations |
| 1150 | fail, the entire sort operation will fail (and the list will likely be left |
| 1151 | in a partially modified state). |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1152 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1153 | *key* specifies a function of one argument that is used to extract a |
| 1154 | comparison key from each list element (for example, ``key=str.lower``). |
| 1155 | The key corresponding to each item in the list is calculated once and |
| 1156 | then used for the entire sorting process. The default value of ``None`` |
| 1157 | means that list items are sorted directly without calculating a separate |
| 1158 | key value. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1159 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1160 | The :func:`functools.cmp_to_key` utility is available to convert a 2.x |
| 1161 | style *cmp* function to a *key* function. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1162 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1163 | *reverse* is a boolean value. If set to ``True``, then the list elements |
| 1164 | are sorted as if each comparison were reversed. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1165 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1166 | This method modifies the sequence in place for economy of space when |
| 1167 | sorting a large sequence. To remind users that it operates by side |
| 1168 | effect, it does not return the sorted sequence (use :func:`sorted` to |
| 1169 | explicitly request a new sorted list instance). |
| 1170 | |
| 1171 | The :meth:`sort` method is guaranteed to be stable. A sort is stable if it |
| 1172 | guarantees not to change the relative order of elements that compare equal |
| 1173 | --- this is helpful for sorting in multiple passes (for example, sort by |
| 1174 | department, then by salary grade). |
| 1175 | |
| 1176 | .. impl-detail:: |
| 1177 | |
| 1178 | While a list is being sorted, the effect of attempting to mutate, or even |
| 1179 | inspect, the list is undefined. The C implementation of Python makes the |
| 1180 | list appear empty for the duration, and raises :exc:`ValueError` if it can |
| 1181 | detect that the list has been mutated during a sort. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1182 | |
| 1183 | |
| 1184 | .. _typesseq-tuple: |
| 1185 | |
| 1186 | Tuples |
| 1187 | ------ |
| 1188 | |
| 1189 | .. index:: object: tuple |
| 1190 | |
| 1191 | Tuples are immutable sequences, typically used to store collections of |
| 1192 | heterogeneous data (such as the 2-tuples produced by the :func:`enumerate` |
| 1193 | built-in). Tuples are also used for cases where an immutable sequence of |
| 1194 | homogeneous data is needed (such as allowing storage in a :class:`set` or |
| 1195 | :class:`dict` instance). |
| 1196 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1197 | .. class:: tuple([iterable]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1198 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1199 | Tuples may be constructed in a number of ways: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1200 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1201 | * Using a pair of parentheses to denote the empty tuple: ``()`` |
| 1202 | * Using a trailing comma for a singleton tuple: ``a,`` or ``(a,)`` |
| 1203 | * Separating items with commas: ``a, b, c`` or ``(a, b, c)`` |
| 1204 | * Using the :func:`tuple` built-in: ``tuple()`` or ``tuple(iterable)`` |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1205 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1206 | The constructor builds a tuple whose items are the same and in the same |
| 1207 | order as *iterable*'s items. *iterable* may be either a sequence, a |
| 1208 | container that supports iteration, or an iterator object. If *iterable* |
| 1209 | is already a tuple, it is returned unchanged. For example, |
| 1210 | ``tuple('abc')`` returns ``('a', 'b', 'c')`` and |
| 1211 | ``tuple( [1, 2, 3] )`` returns ``(1, 2, 3)``. |
| 1212 | If no argument is given, the constructor creates a new empty tuple, ``()``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1213 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1214 | Note that it is actually the comma which makes a tuple, not the parentheses. |
| 1215 | The parentheses are optional, except in the empty tuple case, or |
| 1216 | when they are needed to avoid syntactic ambiguity. For example, |
| 1217 | ``f(a, b, c)`` is a function call with three arguments, while |
| 1218 | ``f((a, b, c))`` is a function call with a 3-tuple as the sole argument. |
| 1219 | |
| 1220 | Tuples implement all of the :ref:`common <typesseq-common>` sequence |
| 1221 | operations. |
| 1222 | |
| 1223 | For heterogeneous collections of data where access by name is clearer than |
| 1224 | access by index, :func:`collections.namedtuple` may be a more appropriate |
| 1225 | choice than a simple tuple object. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1226 | |
| 1227 | |
| 1228 | .. _typesseq-range: |
| 1229 | |
| 1230 | Ranges |
| 1231 | ------ |
| 1232 | |
| 1233 | .. index:: object: range |
| 1234 | |
| 1235 | The :class:`range` type represents an immutable sequence of numbers and is |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1236 | commonly used for looping a specific number of times in :keyword:`for` |
| 1237 | loops. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1238 | |
Ezio Melotti | 8429b67 | 2012-09-14 06:35:09 +0300 | [diff] [blame] | 1239 | .. class:: range(stop) |
| 1240 | range(start, stop[, step]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1241 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1242 | The arguments to the range constructor must be integers (either built-in |
| 1243 | :class:`int` or any object that implements the ``__index__`` special |
| 1244 | method). If the *step* argument is omitted, it defaults to ``1``. |
| 1245 | If the *start* argument is omitted, it defaults to ``0``. |
| 1246 | If *step* is zero, :exc:`ValueError` is raised. |
| 1247 | |
| 1248 | For a positive *step*, the contents of a range ``r`` are determined by the |
| 1249 | formula ``r[i] = start + step*i`` where ``i >= 0`` and |
| 1250 | ``r[i] < stop``. |
| 1251 | |
| 1252 | For a negative *step*, the contents of the range are still determined by |
| 1253 | the formula ``r[i] = start + step*i``, but the constraints are ``i >= 0`` |
| 1254 | and ``r[i] > stop``. |
| 1255 | |
Sandro Tosi | 4c1b9f4 | 2013-01-27 00:33:04 +0100 | [diff] [blame] | 1256 | A range object will be empty if ``r[0]`` does not meet the value |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1257 | constraint. Ranges do support negative indices, but these are interpreted |
| 1258 | as indexing from the end of the sequence determined by the positive |
| 1259 | indices. |
| 1260 | |
| 1261 | Ranges containing absolute values larger than :data:`sys.maxsize` are |
| 1262 | permitted but some features (such as :func:`len`) may raise |
| 1263 | :exc:`OverflowError`. |
| 1264 | |
| 1265 | Range examples:: |
| 1266 | |
| 1267 | >>> list(range(10)) |
| 1268 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1269 | >>> list(range(1, 11)) |
| 1270 | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 1271 | >>> list(range(0, 30, 5)) |
| 1272 | [0, 5, 10, 15, 20, 25] |
| 1273 | >>> list(range(0, 10, 3)) |
| 1274 | [0, 3, 6, 9] |
| 1275 | >>> list(range(0, -10, -1)) |
| 1276 | [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] |
| 1277 | >>> list(range(0)) |
| 1278 | [] |
| 1279 | >>> list(range(1, 0)) |
| 1280 | [] |
| 1281 | |
| 1282 | Ranges implement all of the :ref:`common <typesseq-common>` sequence operations |
| 1283 | except concatenation and repetition (due to the fact that range objects can |
| 1284 | only represent sequences that follow a strict pattern and repetition and |
| 1285 | concatenation will usually violate that pattern). |
| 1286 | |
| 1287 | .. data: start |
| 1288 | |
| 1289 | The value of the *start* parameter (or ``0`` if the parameter was |
| 1290 | not supplied) |
| 1291 | |
| 1292 | .. data: stop |
| 1293 | |
| 1294 | The value of the *stop* parameter |
| 1295 | |
| 1296 | .. data: step |
| 1297 | |
| 1298 | The value of the *step* parameter (or ``1`` if the parameter was |
| 1299 | not supplied) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1300 | |
| 1301 | The advantage of the :class:`range` type over a regular :class:`list` or |
| 1302 | :class:`tuple` is that a :class:`range` object will always take the same |
| 1303 | (small) amount of memory, no matter the size of the range it represents (as it |
| 1304 | only stores the ``start``, ``stop`` and ``step`` values, calculating individual |
| 1305 | items and subranges as needed). |
| 1306 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1307 | Range objects implement the :class:`collections.Sequence` ABC, and provide |
| 1308 | features such as containment tests, element index lookup, slicing and |
| 1309 | support for negative indices (see :ref:`typesseq`): |
| 1310 | |
| 1311 | >>> r = range(0, 20, 2) |
| 1312 | >>> r |
| 1313 | range(0, 20, 2) |
| 1314 | >>> 11 in r |
| 1315 | False |
| 1316 | >>> 10 in r |
| 1317 | True |
| 1318 | >>> r.index(10) |
| 1319 | 5 |
| 1320 | >>> r[5] |
| 1321 | 10 |
| 1322 | >>> r[:5] |
| 1323 | range(0, 10, 2) |
| 1324 | >>> r[-1] |
| 1325 | 18 |
| 1326 | |
| 1327 | Testing range objects for equality with ``==`` and ``!=`` compares |
| 1328 | them as sequences. That is, two range objects are considered equal if |
| 1329 | they represent the same sequence of values. (Note that two range |
| 1330 | objects that compare equal might have different :attr:`start`, |
| 1331 | :attr:`stop` and :attr:`step` attributes, for example ``range(0) == |
| 1332 | range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.) |
| 1333 | |
| 1334 | .. versionchanged:: 3.2 |
| 1335 | Implement the Sequence ABC. |
| 1336 | Support slicing and negative indices. |
| 1337 | Test :class:`int` objects for membership in constant time instead of |
| 1338 | iterating through all items. |
| 1339 | |
| 1340 | .. versionchanged:: 3.3 |
| 1341 | Define '==' and '!=' to compare range objects based on the |
| 1342 | sequence of values they define (instead of comparing based on |
| 1343 | object identity). |
| 1344 | |
| 1345 | .. versionadded:: 3.3 |
| 1346 | The :attr:`start`, :attr:`stop` and :attr:`step` attributes. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1347 | |
| 1348 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1349 | .. index:: |
| 1350 | single: string; text sequence type |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1351 | single: str (built-in class); (see also string) |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1352 | object: string |
| 1353 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1354 | .. _textseq: |
| 1355 | |
| 1356 | Text Sequence Type --- :class:`str` |
| 1357 | =================================== |
| 1358 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1359 | Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`. |
| 1360 | Strings are immutable |
Chris Jerdonek | c33899b | 2012-10-11 18:57:48 -0700 | [diff] [blame] | 1361 | :ref:`sequences <typesseq>` of Unicode code points. String literals are |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1362 | written in a variety of ways: |
| 1363 | |
| 1364 | * Single quotes: ``'allows embedded "double" quotes'`` |
| 1365 | * Double quotes: ``"allows embedded 'single' quotes"``. |
| 1366 | * Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""`` |
| 1367 | |
| 1368 | Triple quoted strings may span multiple lines - all associated whitespace will |
| 1369 | be included in the string literal. |
| 1370 | |
| 1371 | String literals that are part of a single expression and have only whitespace |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1372 | between them will be implicitly converted to a single string literal. That |
| 1373 | is, ``("spam " "eggs") == "spam eggs"``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1374 | |
| 1375 | See :ref:`strings` for more about the various forms of string literal, |
| 1376 | including supported escape sequences, and the ``r`` ("raw") prefix that |
| 1377 | disables most escape sequence processing. |
| 1378 | |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1379 | Strings may also be created from other objects using the :class:`str` |
| 1380 | constructor. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1381 | |
| 1382 | Since there is no separate "character" type, indexing a string produces |
| 1383 | strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``. |
| 1384 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1385 | .. index:: |
| 1386 | object: io.StringIO |
| 1387 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1388 | There is also no mutable string type, but :meth:`str.join` or |
| 1389 | :class:`io.StringIO` can be used to efficiently construct strings from |
| 1390 | multiple fragments. |
| 1391 | |
| 1392 | .. versionchanged:: 3.3 |
| 1393 | For backwards compatibility with the Python 2 series, the ``u`` prefix is |
| 1394 | once again permitted on string literals. It has no effect on the meaning |
| 1395 | of string literals and cannot be combined with the ``r`` prefix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1396 | |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1397 | |
| 1398 | .. index:: |
| 1399 | single: string; str (built-in class) |
| 1400 | |
| 1401 | .. class:: str(object='') |
| 1402 | str(object=b'', encoding='utf-8', errors='strict') |
| 1403 | |
| 1404 | Return a :ref:`string <textseq>` version of *object*. If *object* is not |
| 1405 | provided, returns the empty string. Otherwise, the behavior of ``str()`` |
| 1406 | depends on whether *encoding* or *errors* is given, as follows. |
| 1407 | |
| 1408 | If neither *encoding* nor *errors* is given, ``str(object)`` returns |
| 1409 | :meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely |
| 1410 | printable string representation of *object*. For string objects, this is |
| 1411 | the string itself. If *object* does not have a :meth:`~object.__str__` |
| 1412 | method, then :func:`str` falls back to returning |
| 1413 | :meth:`repr(object) <repr>`. |
| 1414 | |
| 1415 | .. index:: |
| 1416 | single: buffer protocol; str (built-in class) |
| 1417 | single: bytes; str (built-in class) |
| 1418 | |
| 1419 | If at least one of *encoding* or *errors* is given, *object* should be a |
| 1420 | :class:`bytes` or :class:`bytearray` object, or more generally any object |
| 1421 | that supports the :ref:`buffer protocol <bufferobjects>`. In this case, if |
| 1422 | *object* is a :class:`bytes` (or :class:`bytearray`) object, then |
| 1423 | ``str(bytes, encoding, errors)`` is equivalent to |
| 1424 | :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes |
| 1425 | object underlying the buffer object is obtained before calling |
| 1426 | :meth:`bytes.decode`. See :ref:`binaryseq` and |
| 1427 | :ref:`bufferobjects` for information on buffer objects. |
| 1428 | |
| 1429 | Passing a :class:`bytes` object to :func:`str` without the *encoding* |
| 1430 | or *errors* arguments falls under the first case of returning the informal |
| 1431 | string representation (see also the :option:`-b` command-line option to |
| 1432 | Python). For example:: |
| 1433 | |
| 1434 | >>> str(b'Zoot!') |
| 1435 | "b'Zoot!'" |
| 1436 | |
| 1437 | For more information on the ``str`` class and its methods, see |
| 1438 | :ref:`textseq` and the :ref:`string-methods` section below. To output |
| 1439 | formatted strings, see the :ref:`string-formatting` section. In addition, |
| 1440 | see the :ref:`stringservices` section. |
| 1441 | |
| 1442 | |
| 1443 | .. index:: |
| 1444 | pair: string; methods |
| 1445 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1446 | .. _string-methods: |
| 1447 | |
| 1448 | String Methods |
| 1449 | -------------- |
| 1450 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1451 | .. index:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1452 | module: re |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1453 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1454 | Strings implement all of the :ref:`common <typesseq-common>` sequence |
| 1455 | operations, along with the additional methods described below. |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 1456 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1457 | Strings also support two styles of string formatting, one providing a large |
| 1458 | degree of flexibility and customization (see :meth:`str.format`, |
| 1459 | :ref:`formatstrings` and :ref:`string-formatting`) and the other based on C |
| 1460 | ``printf`` style formatting that handles a narrower range of types and is |
| 1461 | slightly harder to use correctly, but is often faster for the cases it can |
| 1462 | handle (:ref:`old-string-formatting`). |
| 1463 | |
| 1464 | The :ref:`textservices` section of the standard library covers a number of |
| 1465 | other modules that provide various text related utilities (including regular |
| 1466 | expression support in the :mod:`re` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1467 | |
| 1468 | .. method:: str.capitalize() |
| 1469 | |
Senthil Kumaran | fa89798 | 2010-07-05 11:41:42 +0000 | [diff] [blame] | 1470 | Return a copy of the string with its first character capitalized and the |
Senthil Kumaran | 37c63a3 | 2010-07-06 02:08:36 +0000 | [diff] [blame] | 1471 | rest lowercased. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1472 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1473 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 1474 | .. method:: str.casefold() |
| 1475 | |
| 1476 | Return a casefolded copy of the string. Casefolded strings may be used for |
Benjamin Peterson | 9430354 | 2012-01-18 23:09:32 -0500 | [diff] [blame] | 1477 | caseless matching. |
| 1478 | |
| 1479 | Casefolding is similar to lowercasing but more aggressive because it is |
| 1480 | intended to remove all case distinctions in a string. For example, the German |
| 1481 | lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already |
| 1482 | lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold` |
| 1483 | converts it to ``"ss"``. |
| 1484 | |
| 1485 | The casefolding algorithm is described in section 3.13 of the Unicode |
| 1486 | Standard. |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 1487 | |
| 1488 | .. versionadded:: 3.3 |
| 1489 | |
| 1490 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1491 | .. method:: str.center(width[, fillchar]) |
| 1492 | |
| 1493 | Return centered in a string of length *width*. Padding is done using the |
| 1494 | specified *fillchar* (default is a space). |
| 1495 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1496 | |
| 1497 | .. method:: str.count(sub[, start[, end]]) |
| 1498 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 1499 | Return the number of non-overlapping occurrences of substring *sub* in the |
| 1500 | range [*start*, *end*]. Optional arguments *start* and *end* are |
| 1501 | interpreted as in slice notation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1502 | |
| 1503 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 1504 | .. method:: str.encode(encoding="utf-8", errors="strict") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1505 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 1506 | Return an encoded version of the string as a bytes object. Default encoding |
| 1507 | is ``'utf-8'``. *errors* may be given to set a different error handling scheme. |
| 1508 | The default for *errors* is ``'strict'``, meaning that encoding errors raise |
| 1509 | a :exc:`UnicodeError`. Other possible |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 1510 | values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, |
| 1511 | ``'backslashreplace'`` and any other name registered via |
| 1512 | :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a |
| 1513 | list of possible encodings, see section :ref:`standard-encodings`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1514 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 1515 | .. versionchanged:: 3.1 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 1516 | Support for keyword arguments added. |
| 1517 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1518 | |
| 1519 | .. method:: str.endswith(suffix[, start[, end]]) |
| 1520 | |
| 1521 | Return ``True`` if the string ends with the specified *suffix*, otherwise return |
| 1522 | ``False``. *suffix* can also be a tuple of suffixes to look for. With optional |
| 1523 | *start*, test beginning at that position. With optional *end*, stop comparing |
| 1524 | at that position. |
| 1525 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1526 | |
| 1527 | .. method:: str.expandtabs([tabsize]) |
| 1528 | |
Eli Bendersky | c2c8960 | 2011-11-11 10:40:14 +0200 | [diff] [blame] | 1529 | Return a copy of the string where all tab characters are replaced by zero or |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1530 | more spaces, depending on the current column and the given tab size. The |
| 1531 | column number is reset to zero after each newline occurring in the string. |
| 1532 | If *tabsize* is not given, a tab size of ``8`` characters is assumed. This |
| 1533 | doesn't understand other non-printing characters or escape sequences. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1534 | |
| 1535 | |
| 1536 | .. method:: str.find(sub[, start[, end]]) |
| 1537 | |
Benjamin Peterson | d99cd81 | 2010-04-27 22:58:50 +0000 | [diff] [blame] | 1538 | Return the lowest index in the string where substring *sub* is found, such |
| 1539 | that *sub* is contained in the slice ``s[start:end]``. Optional arguments |
| 1540 | *start* and *end* are interpreted as in slice notation. Return ``-1`` if |
| 1541 | *sub* is not found. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1542 | |
Ezio Melotti | 0ed8c68 | 2011-05-09 03:54:30 +0300 | [diff] [blame] | 1543 | .. note:: |
| 1544 | |
| 1545 | The :meth:`~str.find` method should be used only if you need to know the |
| 1546 | position of *sub*. To check if *sub* is a substring or not, use the |
| 1547 | :keyword:`in` operator:: |
| 1548 | |
| 1549 | >>> 'Py' in 'Python' |
| 1550 | True |
| 1551 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1552 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 1553 | .. method:: str.format(*args, **kwargs) |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1554 | |
Georg Brandl | 1f70cdf | 2010-03-21 09:04:24 +0000 | [diff] [blame] | 1555 | Perform a string formatting operation. The string on which this method is |
| 1556 | called can contain literal text or replacement fields delimited by braces |
| 1557 | ``{}``. Each replacement field contains either the numeric index of a |
| 1558 | positional argument, or the name of a keyword argument. Returns a copy of |
| 1559 | the string where each replacement field is replaced with the string value of |
| 1560 | the corresponding argument. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1561 | |
| 1562 | >>> "The sum of 1 + 2 is {0}".format(1+2) |
| 1563 | 'The sum of 1 + 2 is 3' |
| 1564 | |
| 1565 | See :ref:`formatstrings` for a description of the various formatting options |
| 1566 | that can be specified in format strings. |
| 1567 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1568 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1569 | .. method:: str.format_map(mapping) |
| 1570 | |
Éric Araujo | 2642ad0 | 2010-11-06 04:59:27 +0000 | [diff] [blame] | 1571 | Similar to ``str.format(**mapping)``, except that ``mapping`` is |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1572 | used directly and not copied to a :class:`dict` . This is useful |
Eric Smith | 5ad85f8 | 2010-11-06 13:22:13 +0000 | [diff] [blame] | 1573 | if for example ``mapping`` is a dict subclass: |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1574 | |
Eric Smith | 5ad85f8 | 2010-11-06 13:22:13 +0000 | [diff] [blame] | 1575 | >>> class Default(dict): |
| 1576 | ... def __missing__(self, key): |
| 1577 | ... return key |
| 1578 | ... |
| 1579 | >>> '{name} was born in {country}'.format_map(Default(name='Guido')) |
| 1580 | 'Guido was born in country' |
| 1581 | |
| 1582 | .. versionadded:: 3.2 |
| 1583 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1584 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1585 | .. method:: str.index(sub[, start[, end]]) |
| 1586 | |
| 1587 | Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found. |
| 1588 | |
| 1589 | |
| 1590 | .. method:: str.isalnum() |
| 1591 | |
| 1592 | Return true if all characters in the string are alphanumeric and there is at |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1593 | least one character, false otherwise. A character ``c`` is alphanumeric if one |
| 1594 | of the following returns ``True``: ``c.isalpha()``, ``c.isdecimal()``, |
| 1595 | ``c.isdigit()``, or ``c.isnumeric()``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1596 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1597 | |
| 1598 | .. method:: str.isalpha() |
| 1599 | |
| 1600 | Return true if all characters in the string are alphabetic and there is at least |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1601 | one character, false otherwise. Alphabetic characters are those characters defined |
| 1602 | in the Unicode character database as "Letter", i.e., those with general category |
| 1603 | property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different |
| 1604 | from the "Alphabetic" property defined in the Unicode Standard. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1605 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1606 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1607 | .. method:: str.isdecimal() |
| 1608 | |
| 1609 | Return true if all characters in the string are decimal |
| 1610 | characters and there is at least one character, false |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1611 | otherwise. Decimal characters are those from general category "Nd". This category |
| 1612 | includes digit characters, and all characters |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 1613 | that can be used to form decimal-radix numbers, e.g. U+0660, |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1614 | ARABIC-INDIC DIGIT ZERO. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1615 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1616 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1617 | .. method:: str.isdigit() |
| 1618 | |
| 1619 | Return true if all characters in the string are digits and there is at least one |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1620 | character, false otherwise. Digits include decimal characters and digits that need |
| 1621 | special handling, such as the compatibility superscript digits. Formally, a digit |
| 1622 | is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1623 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1624 | |
| 1625 | .. method:: str.isidentifier() |
| 1626 | |
| 1627 | 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] | 1628 | definition, section :ref:`identifiers`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1629 | |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 1630 | Use :func:`keyword.iskeyword` to test for reserved identifiers such as |
| 1631 | :keyword:`def` and :keyword:`class`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1632 | |
| 1633 | .. method:: str.islower() |
| 1634 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1635 | Return true if all cased characters [4]_ in the string are lowercase and |
| 1636 | there is at least one cased character, false otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1637 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1638 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1639 | .. method:: str.isnumeric() |
| 1640 | |
| 1641 | Return true if all characters in the string are numeric |
| 1642 | characters, and there is at least one character, false |
| 1643 | otherwise. Numeric characters include digit characters, and all characters |
| 1644 | that have the Unicode numeric value property, e.g. U+2155, |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1645 | VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property |
| 1646 | value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric. |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1647 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1648 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1649 | .. method:: str.isprintable() |
| 1650 | |
| 1651 | Return true if all characters in the string are printable or the string is |
| 1652 | empty, false otherwise. Nonprintable characters are those characters defined |
| 1653 | in the Unicode character database as "Other" or "Separator", excepting the |
| 1654 | ASCII space (0x20) which is considered printable. (Note that printable |
| 1655 | characters in this context are those which should not be escaped when |
| 1656 | :func:`repr` is invoked on a string. It has no bearing on the handling of |
| 1657 | strings written to :data:`sys.stdout` or :data:`sys.stderr`.) |
| 1658 | |
| 1659 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1660 | .. method:: str.isspace() |
| 1661 | |
| 1662 | Return true if there are only whitespace characters in the string and there is |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1663 | at least one character, false otherwise. Whitespace characters are those |
| 1664 | characters defined in the Unicode character database as "Other" or "Separator" |
| 1665 | and those with bidirectional property being one of "WS", "B", or "S". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1666 | |
| 1667 | .. method:: str.istitle() |
| 1668 | |
| 1669 | Return true if the string is a titlecased string and there is at least one |
| 1670 | character, for example uppercase characters may only follow uncased characters |
| 1671 | and lowercase characters only cased ones. Return false otherwise. |
| 1672 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1673 | |
| 1674 | .. method:: str.isupper() |
| 1675 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1676 | Return true if all cased characters [4]_ in the string are uppercase and |
| 1677 | there is at least one cased character, false otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1678 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1679 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1680 | .. method:: str.join(iterable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1681 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1682 | Return a string which is the concatenation of the strings in the |
| 1683 | :term:`iterable` *iterable*. A :exc:`TypeError` will be raised if there are |
Terry Jan Reedy | f4ec3c5 | 2012-01-11 03:29:42 -0500 | [diff] [blame] | 1684 | any non-string values in *iterable*, including :class:`bytes` objects. The |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1685 | separator between elements is the string providing this method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1686 | |
| 1687 | |
| 1688 | .. method:: str.ljust(width[, fillchar]) |
| 1689 | |
| 1690 | Return the string left justified in a string of length *width*. Padding is done |
| 1691 | using the specified *fillchar* (default is a space). The original string is |
Terry Jan Reedy | f4ec3c5 | 2012-01-11 03:29:42 -0500 | [diff] [blame] | 1692 | returned if *width* is less than or equal to ``len(s)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1693 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1694 | |
| 1695 | .. method:: str.lower() |
| 1696 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1697 | Return a copy of the string with all the cased characters [4]_ converted to |
| 1698 | lowercase. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1699 | |
Benjamin Peterson | 9430354 | 2012-01-18 23:09:32 -0500 | [diff] [blame] | 1700 | The lowercasing algorithm used is described in section 3.13 of the Unicode |
| 1701 | Standard. |
| 1702 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1703 | |
| 1704 | .. method:: str.lstrip([chars]) |
| 1705 | |
| 1706 | Return a copy of the string with leading characters removed. The *chars* |
| 1707 | argument is a string specifying the set of characters to be removed. If omitted |
| 1708 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1709 | argument is not a prefix; rather, all combinations of its values are stripped: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1710 | |
| 1711 | >>> ' spacious '.lstrip() |
| 1712 | 'spacious ' |
| 1713 | >>> 'www.example.com'.lstrip('cmowz.') |
| 1714 | 'example.com' |
| 1715 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1716 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1717 | .. staticmethod:: str.maketrans(x[, y[, z]]) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1718 | |
| 1719 | This static method returns a translation table usable for :meth:`str.translate`. |
| 1720 | |
| 1721 | If there is only one argument, it must be a dictionary mapping Unicode |
| 1722 | ordinals (integers) or characters (strings of length 1) to Unicode ordinals, |
| 1723 | strings (of arbitrary lengths) or None. Character keys will then be |
| 1724 | converted to ordinals. |
| 1725 | |
| 1726 | If there are two arguments, they must be strings of equal length, and in the |
| 1727 | resulting dictionary, each character in x will be mapped to the character at |
| 1728 | the same position in y. If there is a third argument, it must be a string, |
| 1729 | whose characters will be mapped to None in the result. |
| 1730 | |
| 1731 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1732 | .. method:: str.partition(sep) |
| 1733 | |
| 1734 | Split the string at the first occurrence of *sep*, and return a 3-tuple |
| 1735 | containing the part before the separator, the separator itself, and the part |
| 1736 | after the separator. If the separator is not found, return a 3-tuple containing |
| 1737 | the string itself, followed by two empty strings. |
| 1738 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1739 | |
| 1740 | .. method:: str.replace(old, new[, count]) |
| 1741 | |
| 1742 | Return a copy of the string with all occurrences of substring *old* replaced by |
| 1743 | *new*. If the optional argument *count* is given, only the first *count* |
| 1744 | occurrences are replaced. |
| 1745 | |
| 1746 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1747 | .. method:: str.rfind(sub[, start[, end]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1748 | |
Benjamin Peterson | d99cd81 | 2010-04-27 22:58:50 +0000 | [diff] [blame] | 1749 | Return the highest index in the string where substring *sub* is found, such |
| 1750 | that *sub* is contained within ``s[start:end]``. Optional arguments *start* |
| 1751 | and *end* are interpreted as in slice notation. Return ``-1`` on failure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1752 | |
| 1753 | |
| 1754 | .. method:: str.rindex(sub[, start[, end]]) |
| 1755 | |
| 1756 | Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not |
| 1757 | found. |
| 1758 | |
| 1759 | |
| 1760 | .. method:: str.rjust(width[, fillchar]) |
| 1761 | |
| 1762 | Return the string right justified in a string of length *width*. Padding is done |
| 1763 | using the specified *fillchar* (default is a space). The original string is |
Terry Jan Reedy | f4ec3c5 | 2012-01-11 03:29:42 -0500 | [diff] [blame] | 1764 | returned if *width* is less than or equal to ``len(s)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1765 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1766 | |
| 1767 | .. method:: str.rpartition(sep) |
| 1768 | |
| 1769 | Split the string at the last occurrence of *sep*, and return a 3-tuple |
| 1770 | containing the part before the separator, the separator itself, and the part |
| 1771 | after the separator. If the separator is not found, return a 3-tuple containing |
| 1772 | two empty strings, followed by the string itself. |
| 1773 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1774 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1775 | .. method:: str.rsplit(sep=None, maxsplit=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1776 | |
| 1777 | Return a list of the words in the string, using *sep* as the delimiter string. |
| 1778 | If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* |
| 1779 | ones. If *sep* is not specified or ``None``, any whitespace string is a |
| 1780 | separator. Except for splitting from the right, :meth:`rsplit` behaves like |
| 1781 | :meth:`split` which is described in detail below. |
| 1782 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1783 | |
| 1784 | .. method:: str.rstrip([chars]) |
| 1785 | |
| 1786 | Return a copy of the string with trailing characters removed. The *chars* |
| 1787 | argument is a string specifying the set of characters to be removed. If omitted |
| 1788 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1789 | argument is not a suffix; rather, all combinations of its values are stripped: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1790 | |
| 1791 | >>> ' spacious '.rstrip() |
| 1792 | ' spacious' |
| 1793 | >>> 'mississippi'.rstrip('ipz') |
| 1794 | 'mississ' |
| 1795 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1796 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1797 | .. method:: str.split(sep=None, maxsplit=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1798 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1799 | Return a list of the words in the string, using *sep* as the delimiter |
| 1800 | string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, |
| 1801 | the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not |
Ezio Melotti | bf3165b | 2012-05-10 15:30:42 +0300 | [diff] [blame] | 1802 | specified or ``-1``, then there is no limit on the number of splits |
| 1803 | (all possible splits are made). |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1804 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1805 | If *sep* is given, consecutive delimiters are not grouped together and are |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1806 | deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns |
| 1807 | ``['1', '', '2']``). The *sep* argument may consist of multiple characters |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1808 | (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``). |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1809 | Splitting an empty string with a specified separator returns ``['']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1810 | |
| 1811 | 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] | 1812 | applied: runs of consecutive whitespace are regarded as a single separator, |
| 1813 | and the result will contain no empty strings at the start or end if the |
| 1814 | string has leading or trailing whitespace. Consequently, splitting an empty |
| 1815 | string or a string consisting of just whitespace with a ``None`` separator |
| 1816 | returns ``[]``. |
| 1817 | |
| 1818 | For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and |
| 1819 | ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1820 | |
| 1821 | |
R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 1822 | .. index:: |
| 1823 | single: universal newlines; str.splitlines method |
| 1824 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1825 | .. method:: str.splitlines([keepends]) |
| 1826 | |
R David Murray | 05c35a6 | 2012-08-06 16:08:09 -0400 | [diff] [blame] | 1827 | Return a list of the lines in the string, breaking at line boundaries. |
R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 1828 | This method uses the :term:`universal newlines` approach to splitting lines. |
R David Murray | 05c35a6 | 2012-08-06 16:08:09 -0400 | [diff] [blame] | 1829 | Line breaks are not included in the resulting list unless *keepends* is |
| 1830 | given and true. |
R David Murray | ae1b94b | 2012-06-01 16:19:36 -0400 | [diff] [blame] | 1831 | |
| 1832 | For example, ``'ab c\n\nde fg\rkl\r\n'.splitlines()`` returns |
R David Murray | 554b348 | 2012-06-02 11:20:29 -0400 | [diff] [blame] | 1833 | ``['ab c', '', 'de fg', 'kl']``, while the same call with ``splitlines(True)`` |
Sandro Tosi | 82a509c | 2012-08-12 12:35:14 +0200 | [diff] [blame] | 1834 | returns ``['ab c\n', '\n', 'de fg\r', 'kl\r\n']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1835 | |
R David Murray | 05c35a6 | 2012-08-06 16:08:09 -0400 | [diff] [blame] | 1836 | Unlike :meth:`~str.split` when a delimiter string *sep* is given, this |
| 1837 | method returns an empty list for the empty string, and a terminal line |
| 1838 | break does not result in an extra line. |
| 1839 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1840 | |
| 1841 | .. method:: str.startswith(prefix[, start[, end]]) |
| 1842 | |
| 1843 | Return ``True`` if string starts with the *prefix*, otherwise return ``False``. |
| 1844 | *prefix* can also be a tuple of prefixes to look for. With optional *start*, |
| 1845 | test string beginning at that position. With optional *end*, stop comparing |
| 1846 | string at that position. |
| 1847 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1848 | |
| 1849 | .. method:: str.strip([chars]) |
| 1850 | |
| 1851 | Return a copy of the string with the leading and trailing characters removed. |
| 1852 | The *chars* argument is a string specifying the set of characters to be removed. |
| 1853 | If omitted or ``None``, the *chars* argument defaults to removing whitespace. |
| 1854 | The *chars* argument is not a prefix or suffix; rather, all combinations of its |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1855 | values are stripped: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1856 | |
| 1857 | >>> ' spacious '.strip() |
| 1858 | 'spacious' |
| 1859 | >>> 'www.example.com'.strip('cmowz.') |
| 1860 | 'example' |
| 1861 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1862 | |
| 1863 | .. method:: str.swapcase() |
| 1864 | |
| 1865 | Return a copy of the string with uppercase characters converted to lowercase and |
Benjamin Peterson | b2bf01d | 2012-01-11 18:17:06 -0500 | [diff] [blame] | 1866 | vice versa. Note that it is not necessarily true that |
| 1867 | ``s.swapcase().swapcase() == s``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1868 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1869 | |
| 1870 | .. method:: str.title() |
| 1871 | |
Raymond Hettinger | b8b0ba1 | 2009-09-29 06:22:28 +0000 | [diff] [blame] | 1872 | Return a titlecased version of the string where words start with an uppercase |
| 1873 | character and the remaining characters are lowercase. |
| 1874 | |
| 1875 | The algorithm uses a simple language-independent definition of a word as |
| 1876 | groups of consecutive letters. The definition works in many contexts but |
| 1877 | it means that apostrophes in contractions and possessives form word |
| 1878 | boundaries, which may not be the desired result:: |
| 1879 | |
| 1880 | >>> "they're bill's friends from the UK".title() |
| 1881 | "They'Re Bill'S Friends From The Uk" |
| 1882 | |
| 1883 | A workaround for apostrophes can be constructed using regular expressions:: |
| 1884 | |
| 1885 | >>> import re |
| 1886 | >>> def titlecase(s): |
Andrew Svetlov | 5c90436 | 2012-11-08 17:26:53 +0200 | [diff] [blame] | 1887 | ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", |
| 1888 | ... lambda mo: mo.group(0)[0].upper() + |
| 1889 | ... mo.group(0)[1:].lower(), |
| 1890 | ... s) |
| 1891 | ... |
Raymond Hettinger | b8b0ba1 | 2009-09-29 06:22:28 +0000 | [diff] [blame] | 1892 | >>> titlecase("they're bill's friends.") |
| 1893 | "They're Bill's Friends." |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1894 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1895 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1896 | .. method:: str.translate(map) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1897 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1898 | Return a copy of the *s* where all characters have been mapped through the |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 1899 | *map* which must be a dictionary of Unicode ordinals (integers) to Unicode |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1900 | ordinals, strings or ``None``. Unmapped characters are left untouched. |
| 1901 | Characters mapped to ``None`` are deleted. |
| 1902 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 1903 | You can use :meth:`str.maketrans` to create a translation map from |
| 1904 | character-to-character mappings in different formats. |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1905 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1906 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1907 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1908 | An even more flexible approach is to create a custom character mapping |
| 1909 | codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1910 | example). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1911 | |
| 1912 | |
| 1913 | .. method:: str.upper() |
| 1914 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1915 | Return a copy of the string with all the cased characters [4]_ converted to |
| 1916 | uppercase. Note that ``str.upper().isupper()`` might be ``False`` if ``s`` |
| 1917 | contains uncased characters or if the Unicode category of the resulting |
Benjamin Peterson | 9430354 | 2012-01-18 23:09:32 -0500 | [diff] [blame] | 1918 | character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, |
| 1919 | titlecase). |
| 1920 | |
| 1921 | The uppercasing algorithm used is described in section 3.13 of the Unicode |
| 1922 | Standard. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1923 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1924 | |
| 1925 | .. method:: str.zfill(width) |
| 1926 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1927 | Return the numeric string left filled with zeros in a string of length |
| 1928 | *width*. A sign prefix is handled correctly. The original string is |
Terry Jan Reedy | f4ec3c5 | 2012-01-11 03:29:42 -0500 | [diff] [blame] | 1929 | returned if *width* is less than or equal to ``len(s)``. |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1930 | |
| 1931 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1932 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1933 | .. _old-string-formatting: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1934 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1935 | ``printf``-style String Formatting |
| 1936 | ---------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1937 | |
| 1938 | .. index:: |
| 1939 | single: formatting, string (%) |
| 1940 | single: interpolation, string (%) |
| 1941 | single: string; formatting |
| 1942 | single: string; interpolation |
| 1943 | single: printf-style formatting |
| 1944 | single: sprintf-style formatting |
| 1945 | single: % formatting |
| 1946 | single: % interpolation |
| 1947 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1948 | .. note:: |
| 1949 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1950 | The formatting operations described here exhibit a variety of quirks that |
| 1951 | lead to a number of common errors (such as failing to display tuples and |
| 1952 | dictionaries correctly). Using the newer :meth:`str.format` interface |
| 1953 | helps avoid these errors, and also provides a generally more powerful, |
| 1954 | flexible and extensible approach to formatting text. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1955 | |
| 1956 | String objects have one unique built-in operation: the ``%`` operator (modulo). |
| 1957 | This is also known as the string *formatting* or *interpolation* operator. |
| 1958 | Given ``format % values`` (where *format* is a string), ``%`` conversion |
| 1959 | specifications in *format* are replaced with zero or more elements of *values*. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1960 | The effect is similar to using the :c:func:`sprintf` in the C language. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1961 | |
| 1962 | If *format* requires a single argument, *values* may be a single non-tuple |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1963 | object. [5]_ Otherwise, *values* must be a tuple with exactly the number of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1964 | items specified by the format string, or a single mapping object (for example, a |
| 1965 | dictionary). |
| 1966 | |
| 1967 | A conversion specifier contains two or more characters and has the following |
| 1968 | components, which must occur in this order: |
| 1969 | |
| 1970 | #. The ``'%'`` character, which marks the start of the specifier. |
| 1971 | |
| 1972 | #. Mapping key (optional), consisting of a parenthesised sequence of characters |
| 1973 | (for example, ``(somename)``). |
| 1974 | |
| 1975 | #. Conversion flags (optional), which affect the result of some conversion |
| 1976 | types. |
| 1977 | |
| 1978 | #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the |
| 1979 | actual width is read from the next element of the tuple in *values*, and the |
| 1980 | object to convert comes after the minimum field width and optional precision. |
| 1981 | |
| 1982 | #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 1983 | specified as ``'*'`` (an asterisk), the actual precision is read from the next |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1984 | element of the tuple in *values*, and the value to convert comes after the |
| 1985 | precision. |
| 1986 | |
| 1987 | #. Length modifier (optional). |
| 1988 | |
| 1989 | #. Conversion type. |
| 1990 | |
| 1991 | When the right argument is a dictionary (or other mapping type), then the |
| 1992 | formats in the string *must* include a parenthesised mapping key into that |
| 1993 | dictionary inserted immediately after the ``'%'`` character. The mapping key |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 1994 | selects the value to be formatted from the mapping. For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1995 | |
Georg Brandl | edc9e7f | 2010-10-17 09:19:03 +0000 | [diff] [blame] | 1996 | >>> print('%(language)s has %(number)03d quote types.' % |
| 1997 | ... {'language': "Python", "number": 2}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1998 | Python has 002 quote types. |
| 1999 | |
| 2000 | In this case no ``*`` specifiers may occur in a format (since they require a |
| 2001 | sequential parameter list). |
| 2002 | |
| 2003 | The conversion flag characters are: |
| 2004 | |
| 2005 | +---------+---------------------------------------------------------------------+ |
| 2006 | | Flag | Meaning | |
| 2007 | +=========+=====================================================================+ |
| 2008 | | ``'#'`` | The value conversion will use the "alternate form" (where defined | |
| 2009 | | | below). | |
| 2010 | +---------+---------------------------------------------------------------------+ |
| 2011 | | ``'0'`` | The conversion will be zero padded for numeric values. | |
| 2012 | +---------+---------------------------------------------------------------------+ |
| 2013 | | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | |
| 2014 | | | conversion if both are given). | |
| 2015 | +---------+---------------------------------------------------------------------+ |
| 2016 | | ``' '`` | (a space) A blank should be left before a positive number (or empty | |
| 2017 | | | string) produced by a signed conversion. | |
| 2018 | +---------+---------------------------------------------------------------------+ |
| 2019 | | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | |
| 2020 | | | (overrides a "space" flag). | |
| 2021 | +---------+---------------------------------------------------------------------+ |
| 2022 | |
| 2023 | A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2024 | is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2025 | |
| 2026 | The conversion types are: |
| 2027 | |
| 2028 | +------------+-----------------------------------------------------+-------+ |
| 2029 | | Conversion | Meaning | Notes | |
| 2030 | +============+=====================================================+=======+ |
| 2031 | | ``'d'`` | Signed integer decimal. | | |
| 2032 | +------------+-----------------------------------------------------+-------+ |
| 2033 | | ``'i'`` | Signed integer decimal. | | |
| 2034 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2035 | | ``'o'`` | Signed octal value. | \(1) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2036 | +------------+-----------------------------------------------------+-------+ |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 2037 | | ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(7) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2038 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2039 | | ``'x'`` | Signed hexadecimal (lowercase). | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2040 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2041 | | ``'X'`` | Signed hexadecimal (uppercase). | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2042 | +------------+-----------------------------------------------------+-------+ |
| 2043 | | ``'e'`` | Floating point exponential format (lowercase). | \(3) | |
| 2044 | +------------+-----------------------------------------------------+-------+ |
| 2045 | | ``'E'`` | Floating point exponential format (uppercase). | \(3) | |
| 2046 | +------------+-----------------------------------------------------+-------+ |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 2047 | | ``'f'`` | Floating point decimal format. | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2048 | +------------+-----------------------------------------------------+-------+ |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 2049 | | ``'F'`` | Floating point decimal format. | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2050 | +------------+-----------------------------------------------------+-------+ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 2051 | | ``'g'`` | Floating point format. Uses lowercase exponential | \(4) | |
| 2052 | | | format if exponent is less than -4 or not less than | | |
| 2053 | | | precision, decimal format otherwise. | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2054 | +------------+-----------------------------------------------------+-------+ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 2055 | | ``'G'`` | Floating point format. Uses uppercase exponential | \(4) | |
| 2056 | | | format if exponent is less than -4 or not less than | | |
| 2057 | | | precision, decimal format otherwise. | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2058 | +------------+-----------------------------------------------------+-------+ |
| 2059 | | ``'c'`` | Single character (accepts integer or single | | |
| 2060 | | | character string). | | |
| 2061 | +------------+-----------------------------------------------------+-------+ |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 2062 | | ``'r'`` | String (converts any Python object using | \(5) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2063 | | | :func:`repr`). | | |
| 2064 | +------------+-----------------------------------------------------+-------+ |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2065 | | ``'s'`` | String (converts any Python object using | \(5) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2066 | | | :func:`str`). | | |
| 2067 | +------------+-----------------------------------------------------+-------+ |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2068 | | ``'a'`` | String (converts any Python object using | \(5) | |
| 2069 | | | :func:`ascii`). | | |
| 2070 | +------------+-----------------------------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2071 | | ``'%'`` | No argument is converted, results in a ``'%'`` | | |
| 2072 | | | character in the result. | | |
| 2073 | +------------+-----------------------------------------------------+-------+ |
| 2074 | |
| 2075 | Notes: |
| 2076 | |
| 2077 | (1) |
| 2078 | The alternate form causes a leading zero (``'0'``) to be inserted between |
| 2079 | left-hand padding and the formatting of the number if the leading character |
| 2080 | of the result is not already a zero. |
| 2081 | |
| 2082 | (2) |
| 2083 | The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether |
| 2084 | the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding |
| 2085 | and the formatting of the number if the leading character of the result is not |
| 2086 | already a zero. |
| 2087 | |
| 2088 | (3) |
| 2089 | The alternate form causes the result to always contain a decimal point, even if |
| 2090 | no digits follow it. |
| 2091 | |
| 2092 | The precision determines the number of digits after the decimal point and |
| 2093 | defaults to 6. |
| 2094 | |
| 2095 | (4) |
| 2096 | The alternate form causes the result to always contain a decimal point, and |
| 2097 | trailing zeroes are not removed as they would otherwise be. |
| 2098 | |
| 2099 | The precision determines the number of significant digits before and after the |
| 2100 | decimal point and defaults to 6. |
| 2101 | |
| 2102 | (5) |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2103 | If precision is ``N``, the output is truncated to ``N`` characters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2104 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2105 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2106 | (7) |
| 2107 | See :pep:`237`. |
| 2108 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2109 | Since Python strings have an explicit length, ``%s`` conversions do not assume |
| 2110 | that ``'\0'`` is the end of the string. |
| 2111 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 2112 | .. XXX Examples? |
| 2113 | |
Mark Dickinson | 33841c3 | 2009-05-01 15:37:04 +0000 | [diff] [blame] | 2114 | .. versionchanged:: 3.1 |
| 2115 | ``%f`` conversions for numbers whose absolute value is over 1e50 are no |
| 2116 | longer replaced by ``%g`` conversions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2117 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2118 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 2119 | .. index:: |
| 2120 | single: buffer protocol; binary sequence types |
| 2121 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2122 | .. _binaryseq: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2123 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2124 | Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview` |
| 2125 | ================================================================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2126 | |
| 2127 | .. index:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2128 | object: bytes |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2129 | object: bytearray |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2130 | object: memoryview |
| 2131 | module: array |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2132 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2133 | The core built-in types for manipulating binary data are :class:`bytes` and |
| 2134 | :class:`bytearray`. They are supported by :class:`memoryview` which uses |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 2135 | the :ref:`buffer protocol <bufferobjects>` to access the memory of other |
| 2136 | binary objects without needing to make a copy. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2137 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2138 | The :mod:`array` module supports efficient storage of basic data types like |
| 2139 | 32-bit integers and IEEE754 double-precision floating values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2140 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2141 | .. _typebytes: |
Senthil Kumaran | 7cafd26 | 2010-10-02 03:16:04 +0000 | [diff] [blame] | 2142 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2143 | Bytes |
| 2144 | ----- |
| 2145 | |
| 2146 | .. index:: object: bytes |
| 2147 | |
| 2148 | Bytes objects are immutable sequences of single bytes. Since many major |
| 2149 | binary protocols are based on the ASCII text encoding, bytes objects offer |
| 2150 | several methods that are only valid when working with ASCII compatible |
| 2151 | data and are closely related to string objects in a variety of other ways. |
| 2152 | |
| 2153 | Firstly, the syntax for bytes literals is largely the same as that for string |
| 2154 | literals, except that a ``b`` prefix is added: |
| 2155 | |
| 2156 | * Single quotes: ``b'still allows embedded "double" quotes'`` |
| 2157 | * Double quotes: ``b"still allows embedded 'single' quotes"``. |
| 2158 | * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""`` |
| 2159 | |
| 2160 | Only ASCII characters are permitted in bytes literals (regardless of the |
| 2161 | declared source code encoding). Any binary values over 127 must be entered |
| 2162 | into bytes literals using the appropriate escape sequence. |
| 2163 | |
| 2164 | As with string literals, bytes literals may also use a ``r`` prefix to disable |
| 2165 | processing of escape sequences. See :ref:`strings` for more about the various |
| 2166 | forms of bytes literal, including supported escape sequences. |
| 2167 | |
| 2168 | While bytes literals and representations are based on ASCII text, bytes |
| 2169 | objects actually behave like immutable sequences of integers, with each |
| 2170 | value in the sequence restricted such that ``0 <= x < 256`` (attempts to |
| 2171 | violate this restriction will trigger :exc:`ValueError`. This is done |
| 2172 | deliberately to emphasise that while many binary formats include ASCII based |
| 2173 | elements and can be usefully manipulated with some text-oriented algorithms, |
| 2174 | this is not generally the case for arbitrary binary data (blindly applying |
| 2175 | text processing algorithms to binary data formats that are not ASCII |
| 2176 | compatible will usually lead to data corruption). |
| 2177 | |
| 2178 | In addition to the literal forms, bytes objects can be created in a number of |
| 2179 | other ways: |
| 2180 | |
| 2181 | * A zero-filled bytes object of a specified length: ``bytes(10)`` |
| 2182 | * From an iterable of integers: ``bytes(range(20))`` |
| 2183 | * Copying existing binary data via the buffer protocol: ``bytes(obj)`` |
| 2184 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2185 | Also see the :ref:`bytes <func-bytes>` built-in. |
| 2186 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2187 | Since bytes objects are sequences of integers, for a bytes object *b*, |
| 2188 | ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes object of |
| 2189 | length 1. (This contrasts with text strings, where both indexing and |
| 2190 | slicing will produce a string of length 1) |
| 2191 | |
| 2192 | The representation of bytes objects uses the literal format (``b'...'``) |
| 2193 | since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can |
| 2194 | always convert a bytes object into a list of integers using ``list(b)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2195 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2196 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2197 | .. note:: |
| 2198 | For Python 2.x users: In the Python 2.x series, a variety of implicit |
| 2199 | conversions between 8-bit strings (the closest thing 2.x offers to a |
| 2200 | built-in binary data type) and Unicode strings were permitted. This was a |
| 2201 | backwards compatibility workaround to account for the fact that Python |
| 2202 | originally only supported 8-bit text, and Unicode text was a later |
| 2203 | addition. In Python 3.x, those implicit conversions are gone - conversions |
| 2204 | between 8-bit binary data and Unicode text must be explicit, and bytes and |
| 2205 | string objects will always compare unequal. |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 2206 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2207 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2208 | .. _typebytearray: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2209 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2210 | Bytearray Objects |
| 2211 | ----------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2212 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2213 | .. index:: object: bytearray |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 2214 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2215 | :class:`bytearray` objects are a mutable counterpart to :class:`bytes` |
| 2216 | objects. There is no dedicated literal syntax for bytearray objects, instead |
| 2217 | they are always created by calling the constructor: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2218 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2219 | * Creating an empty instance: ``bytearray()`` |
| 2220 | * Creating a zero-filled instance with a given length: ``bytearray(10)`` |
| 2221 | * From an iterable of integers: ``bytearray(range(20))`` |
Ezio Melotti | 971ba4c | 2012-10-27 23:25:18 +0300 | [diff] [blame] | 2222 | * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')`` |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 2223 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2224 | As bytearray objects are mutable, they support the |
| 2225 | :ref:`mutable <typesseq-mutable>` sequence operations in addition to the |
| 2226 | common bytes and bytearray operations described in :ref:`bytes-methods`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2227 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2228 | Also see the :ref:`bytearray <func-bytearray>` built-in. |
| 2229 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 2230 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2231 | .. _bytes-methods: |
| 2232 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2233 | Bytes and Bytearray Operations |
| 2234 | ------------------------------ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2235 | |
| 2236 | .. index:: pair: bytes; methods |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2237 | pair: bytearray; methods |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2238 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2239 | Both bytes and bytearray objects support the :ref:`common <typesseq-common>` |
| 2240 | sequence operations. They interoperate not just with operands of the same |
| 2241 | type, but with any object that supports the |
| 2242 | :ref:`buffer protocol <bufferobjects>`. Due to this flexibility, they can be |
| 2243 | freely mixed in operations without causing errors. However, the return type |
| 2244 | of the result may depend on the order of operands. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2245 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2246 | Due to the common use of ASCII text as the basis for binary protocols, bytes |
| 2247 | and bytearray objects provide almost all methods found on text strings, with |
| 2248 | the exceptions of: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2249 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2250 | * :meth:`str.encode` (which converts text strings to bytes objects) |
| 2251 | * :meth:`str.format` and :meth:`str.format_map` (which are used to format |
| 2252 | text for display to users) |
| 2253 | * :meth:`str.isidentifier`, :meth:`str.isnumeric`, :meth:`str.isdecimal`, |
| 2254 | :meth:`str.isprintable` (which are used to check various properties of |
| 2255 | text strings which are not typically applicable to binary protocols). |
| 2256 | |
| 2257 | All other string methods are supported, although sometimes with slight |
| 2258 | differences in functionality and semantics (as described below). |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2259 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2260 | .. note:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2261 | |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2262 | 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] | 2263 | arguments, just as the methods on strings don't accept bytes as their |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2264 | arguments. For example, you have to write:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2265 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2266 | a = "abc" |
| 2267 | b = a.replace("a", "f") |
| 2268 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2269 | and:: |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2270 | |
| 2271 | a = b"abc" |
| 2272 | b = a.replace(b"a", b"f") |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2273 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2274 | Whenever a bytes or bytearray method needs to interpret the bytes as |
| 2275 | characters (e.g. the :meth:`is...` methods, :meth:`split`, :meth:`strip`), |
| 2276 | the ASCII character set is assumed (text strings use Unicode semantics). |
| 2277 | |
| 2278 | .. note:: |
| 2279 | Using these ASCII based methods to manipulate binary data that is not |
| 2280 | stored in an ASCII based format may lead to data corruption. |
| 2281 | |
| 2282 | The search operations (:keyword:`in`, :meth:`count`, :meth:`find`, |
| 2283 | :meth:`index`, :meth:`rfind` and :meth:`rindex`) all accept both integers |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2284 | in the range 0 to 255 (inclusive) as well as bytes and byte array sequences. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2285 | |
| 2286 | .. versionchanged:: 3.3 |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2287 | All of the search methods also accept an integer in the range 0 to 255 |
| 2288 | (inclusive) as their first argument. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2289 | |
| 2290 | |
| 2291 | Each bytes and bytearray instance provides a :meth:`decode` convenience |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2292 | method that is the inverse of :meth:`str.encode`: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2293 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 2294 | .. method:: bytes.decode(encoding="utf-8", errors="strict") |
| 2295 | bytearray.decode(encoding="utf-8", errors="strict") |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 2296 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 2297 | Return a string decoded from the given bytes. Default encoding is |
| 2298 | ``'utf-8'``. *errors* may be given to set a different |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 2299 | error handling scheme. The default for *errors* is ``'strict'``, meaning |
| 2300 | that encoding errors raise a :exc:`UnicodeError`. Other possible values are |
| 2301 | ``'ignore'``, ``'replace'`` and any other name registered via |
| 2302 | :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a |
| 2303 | list of possible encodings, see section :ref:`standard-encodings`. |
| 2304 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2305 | .. versionchanged:: 3.1 |
| 2306 | Added support for keyword arguments. |
| 2307 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2308 | Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal |
| 2309 | numbers are a commonly used format for describing binary data. Accordingly, |
| 2310 | the bytes and bytearray types have an additional class method to read data in |
| 2311 | that format: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2312 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2313 | .. classmethod:: bytes.fromhex(string) |
| 2314 | bytearray.fromhex(string) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2315 | |
Georg Brandl | 18da8f0 | 2008-07-01 20:08:02 +0000 | [diff] [blame] | 2316 | This :class:`bytes` class method returns a bytes or bytearray object, |
| 2317 | decoding the given string object. The string must contain two hexadecimal |
| 2318 | digits per byte, spaces are ignored. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2319 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2320 | >>> bytes.fromhex('2Ef0 F1f2 ') |
| 2321 | b'.\xf0\xf1\xf2' |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2322 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2323 | |
| 2324 | The maketrans and translate methods differ in semantics from the versions |
| 2325 | available on strings: |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 2326 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2327 | .. method:: bytes.translate(table[, delete]) |
Georg Brandl | 751771b | 2009-05-31 21:38:37 +0000 | [diff] [blame] | 2328 | bytearray.translate(table[, delete]) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2329 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2330 | Return a copy of the bytes or bytearray object where all bytes occurring in |
| 2331 | the optional argument *delete* are removed, and the remaining bytes have been |
| 2332 | mapped through the given translation table, which must be a bytes object of |
| 2333 | length 256. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2334 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2335 | You can use the :func:`bytes.maketrans` method to create a translation table. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2336 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2337 | Set the *table* argument to ``None`` for translations that only delete |
| 2338 | characters:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2339 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2340 | >>> b'read this short text'.translate(None, b'aeiou') |
| 2341 | b'rd ths shrt txt' |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2342 | |
| 2343 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2344 | .. staticmethod:: bytes.maketrans(from, to) |
Georg Brandl | 751771b | 2009-05-31 21:38:37 +0000 | [diff] [blame] | 2345 | bytearray.maketrans(from, to) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2346 | |
| 2347 | This static method returns a translation table usable for |
| 2348 | :meth:`bytes.translate` that will map each character in *from* into the |
| 2349 | character at the same position in *to*; *from* and *to* must be bytes objects |
| 2350 | and have the same length. |
| 2351 | |
| 2352 | .. versionadded:: 3.1 |
| 2353 | |
| 2354 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2355 | .. _typememoryview: |
| 2356 | |
| 2357 | Memory Views |
| 2358 | ------------ |
| 2359 | |
| 2360 | :class:`memoryview` objects allow Python code to access the internal data |
| 2361 | of an object that supports the :ref:`buffer protocol <bufferobjects>` without |
| 2362 | copying. |
| 2363 | |
| 2364 | .. class:: memoryview(obj) |
| 2365 | |
| 2366 | Create a :class:`memoryview` that references *obj*. *obj* must support the |
| 2367 | buffer protocol. Built-in objects that support the buffer protocol include |
| 2368 | :class:`bytes` and :class:`bytearray`. |
| 2369 | |
| 2370 | A :class:`memoryview` has the notion of an *element*, which is the |
| 2371 | atomic memory unit handled by the originating object *obj*. For many |
| 2372 | simple types such as :class:`bytes` and :class:`bytearray`, an element |
| 2373 | is a single byte, but other types such as :class:`array.array` may have |
| 2374 | bigger elements. |
| 2375 | |
| 2376 | ``len(view)`` is equal to the length of :class:`~memoryview.tolist`. |
| 2377 | If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length |
| 2378 | is equal to the number of elements in the view. For higher dimensions, |
| 2379 | the length is equal to the length of the nested list representation of |
| 2380 | the view. The :class:`~memoryview.itemsize` attribute will give you the |
| 2381 | number of bytes in a single element. |
| 2382 | |
| 2383 | A :class:`memoryview` supports slicing to expose its data. If |
| 2384 | :class:`~memoryview.format` is one of the native format specifiers |
| 2385 | from the :mod:`struct` module, indexing will return a single element |
| 2386 | with the correct type. Full slicing will result in a subview:: |
| 2387 | |
| 2388 | >>> v = memoryview(b'abcefg') |
| 2389 | >>> v[1] |
| 2390 | 98 |
| 2391 | >>> v[-1] |
| 2392 | 103 |
| 2393 | >>> v[1:4] |
| 2394 | <memory at 0x7f3ddc9f4350> |
| 2395 | >>> bytes(v[1:4]) |
| 2396 | b'bce' |
| 2397 | |
| 2398 | Other native formats:: |
| 2399 | |
| 2400 | >>> import array |
| 2401 | >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444]) |
| 2402 | >>> a[0] |
| 2403 | -11111111 |
| 2404 | >>> a[-1] |
| 2405 | 44444444 |
| 2406 | >>> a[2:3].tolist() |
| 2407 | [-33333333] |
| 2408 | >>> a[::2].tolist() |
| 2409 | [-11111111, -33333333] |
| 2410 | >>> a[::-1].tolist() |
| 2411 | [44444444, -33333333, 22222222, -11111111] |
| 2412 | |
| 2413 | .. versionadded:: 3.3 |
| 2414 | |
| 2415 | If the underlying object is writable, the memoryview supports slice |
| 2416 | assignment. Resizing is not allowed:: |
| 2417 | |
| 2418 | >>> data = bytearray(b'abcefg') |
| 2419 | >>> v = memoryview(data) |
| 2420 | >>> v.readonly |
| 2421 | False |
| 2422 | >>> v[0] = ord(b'z') |
| 2423 | >>> data |
| 2424 | bytearray(b'zbcefg') |
| 2425 | >>> v[1:4] = b'123' |
| 2426 | >>> data |
| 2427 | bytearray(b'z123fg') |
| 2428 | >>> v[2:3] = b'spam' |
| 2429 | Traceback (most recent call last): |
| 2430 | File "<stdin>", line 1, in <module> |
| 2431 | ValueError: memoryview assignment: lvalue and rvalue have different structures |
| 2432 | >>> v[2:6] = b'spam' |
| 2433 | >>> data |
| 2434 | bytearray(b'z1spam') |
| 2435 | |
Stefan Krah | a3b84fb | 2012-09-02 14:50:56 +0200 | [diff] [blame] | 2436 | One-dimensional memoryviews of hashable (read-only) types with formats |
| 2437 | 'B', 'b' or 'c' are also hashable. The hash is defined as |
| 2438 | ``hash(m) == hash(m.tobytes())``:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2439 | |
| 2440 | >>> v = memoryview(b'abcefg') |
| 2441 | >>> hash(v) == hash(b'abcefg') |
| 2442 | True |
| 2443 | >>> hash(v[2:4]) == hash(b'ce') |
| 2444 | True |
| 2445 | >>> hash(v[::-2]) == hash(b'abcefg'[::-2]) |
| 2446 | True |
| 2447 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2448 | .. versionchanged:: 3.3 |
Stefan Krah | a3b84fb | 2012-09-02 14:50:56 +0200 | [diff] [blame] | 2449 | One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2450 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2451 | :class:`memoryview` has several methods: |
| 2452 | |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2453 | .. method:: __eq__(exporter) |
| 2454 | |
| 2455 | A memoryview and a :pep:`3118` exporter are equal if their shapes are |
| 2456 | equivalent and if all corresponding values are equal when the operands' |
| 2457 | respective format codes are interpreted using :mod:`struct` syntax. |
| 2458 | |
| 2459 | For the subset of :mod:`struct` format strings currently supported by |
| 2460 | :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``:: |
| 2461 | |
| 2462 | >>> import array |
| 2463 | >>> a = array.array('I', [1, 2, 3, 4, 5]) |
| 2464 | >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0]) |
| 2465 | >>> c = array.array('b', [5, 3, 1]) |
| 2466 | >>> x = memoryview(a) |
| 2467 | >>> y = memoryview(b) |
| 2468 | >>> x == a == y == b |
| 2469 | True |
| 2470 | >>> x.tolist() == a.tolist() == y.tolist() == b.tolist() |
| 2471 | True |
| 2472 | >>> z = y[::-2] |
| 2473 | >>> z == c |
| 2474 | True |
| 2475 | >>> z.tolist() == c.tolist() |
| 2476 | True |
| 2477 | |
| 2478 | If either format string is not supported by the :mod:`struct` module, |
| 2479 | then the objects will always compare as unequal (even if the format |
| 2480 | strings and buffer contents are identical):: |
| 2481 | |
| 2482 | >>> from ctypes import BigEndianStructure, c_long |
| 2483 | >>> class BEPoint(BigEndianStructure): |
| 2484 | ... _fields_ = [("x", c_long), ("y", c_long)] |
| 2485 | ... |
| 2486 | >>> point = BEPoint(100, 200) |
| 2487 | >>> a = memoryview(point) |
| 2488 | >>> b = memoryview(point) |
| 2489 | >>> a == point |
| 2490 | False |
| 2491 | >>> a == b |
| 2492 | False |
| 2493 | |
| 2494 | Note that, as with floating point numbers, ``v is w`` does *not* imply |
| 2495 | ``v == w`` for memoryview objects. |
| 2496 | |
| 2497 | .. versionchanged:: 3.3 |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 2498 | Previous versions compared the raw memory disregarding the item format |
| 2499 | and the logical array structure. |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2500 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2501 | .. method:: tobytes() |
| 2502 | |
| 2503 | Return the data in the buffer as a bytestring. This is equivalent to |
| 2504 | calling the :class:`bytes` constructor on the memoryview. :: |
| 2505 | |
| 2506 | >>> m = memoryview(b"abc") |
| 2507 | >>> m.tobytes() |
| 2508 | b'abc' |
| 2509 | >>> bytes(m) |
| 2510 | b'abc' |
| 2511 | |
| 2512 | For non-contiguous arrays the result is equal to the flattened list |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2513 | representation with all elements converted to bytes. :meth:`tobytes` |
| 2514 | supports all format strings, including those that are not in |
| 2515 | :mod:`struct` module syntax. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2516 | |
| 2517 | .. method:: tolist() |
| 2518 | |
| 2519 | Return the data in the buffer as a list of elements. :: |
| 2520 | |
| 2521 | >>> memoryview(b'abc').tolist() |
| 2522 | [97, 98, 99] |
| 2523 | >>> import array |
| 2524 | >>> a = array.array('d', [1.1, 2.2, 3.3]) |
| 2525 | >>> m = memoryview(a) |
| 2526 | >>> m.tolist() |
| 2527 | [1.1, 2.2, 3.3] |
| 2528 | |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 2529 | .. versionchanged:: 3.3 |
| 2530 | :meth:`tolist` now supports all single character native formats in |
| 2531 | :mod:`struct` module syntax as well as multi-dimensional |
| 2532 | representations. |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2533 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2534 | .. method:: release() |
| 2535 | |
| 2536 | Release the underlying buffer exposed by the memoryview object. Many |
| 2537 | objects take special actions when a view is held on them (for example, |
| 2538 | a :class:`bytearray` would temporarily forbid resizing); therefore, |
| 2539 | calling release() is handy to remove these restrictions (and free any |
| 2540 | dangling resources) as soon as possible. |
| 2541 | |
| 2542 | After this method has been called, any further operation on the view |
| 2543 | raises a :class:`ValueError` (except :meth:`release()` itself which can |
| 2544 | be called multiple times):: |
| 2545 | |
| 2546 | >>> m = memoryview(b'abc') |
| 2547 | >>> m.release() |
| 2548 | >>> m[0] |
| 2549 | Traceback (most recent call last): |
| 2550 | File "<stdin>", line 1, in <module> |
| 2551 | ValueError: operation forbidden on released memoryview object |
| 2552 | |
| 2553 | The context management protocol can be used for a similar effect, |
| 2554 | using the ``with`` statement:: |
| 2555 | |
| 2556 | >>> with memoryview(b'abc') as m: |
| 2557 | ... m[0] |
| 2558 | ... |
| 2559 | 97 |
| 2560 | >>> m[0] |
| 2561 | Traceback (most recent call last): |
| 2562 | File "<stdin>", line 1, in <module> |
| 2563 | ValueError: operation forbidden on released memoryview object |
| 2564 | |
| 2565 | .. versionadded:: 3.2 |
| 2566 | |
| 2567 | .. method:: cast(format[, shape]) |
| 2568 | |
| 2569 | Cast a memoryview to a new format or shape. *shape* defaults to |
| 2570 | ``[byte_length//new_itemsize]``, which means that the result view |
| 2571 | will be one-dimensional. The return value is a new memoryview, but |
| 2572 | the buffer itself is not copied. Supported casts are 1D -> C-contiguous |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2573 | and C-contiguous -> 1D. |
| 2574 | |
| 2575 | Both formats are restricted to single element native formats in |
| 2576 | :mod:`struct` syntax. One of the formats must be a byte format |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2577 | ('B', 'b' or 'c'). The byte length of the result must be the same |
| 2578 | as the original length. |
| 2579 | |
| 2580 | Cast 1D/long to 1D/unsigned bytes:: |
| 2581 | |
| 2582 | >>> import array |
| 2583 | >>> a = array.array('l', [1,2,3]) |
| 2584 | >>> x = memoryview(a) |
| 2585 | >>> x.format |
| 2586 | 'l' |
| 2587 | >>> x.itemsize |
| 2588 | 8 |
| 2589 | >>> len(x) |
| 2590 | 3 |
| 2591 | >>> x.nbytes |
| 2592 | 24 |
| 2593 | >>> y = x.cast('B') |
| 2594 | >>> y.format |
| 2595 | 'B' |
| 2596 | >>> y.itemsize |
| 2597 | 1 |
| 2598 | >>> len(y) |
| 2599 | 24 |
| 2600 | >>> y.nbytes |
| 2601 | 24 |
| 2602 | |
| 2603 | Cast 1D/unsigned bytes to 1D/char:: |
| 2604 | |
| 2605 | >>> b = bytearray(b'zyz') |
| 2606 | >>> x = memoryview(b) |
| 2607 | >>> x[0] = b'a' |
| 2608 | Traceback (most recent call last): |
| 2609 | File "<stdin>", line 1, in <module> |
| 2610 | ValueError: memoryview: invalid value for format "B" |
| 2611 | >>> y = x.cast('c') |
| 2612 | >>> y[0] = b'a' |
| 2613 | >>> b |
| 2614 | bytearray(b'ayz') |
| 2615 | |
| 2616 | Cast 1D/bytes to 3D/ints to 1D/signed char:: |
| 2617 | |
| 2618 | >>> import struct |
| 2619 | >>> buf = struct.pack("i"*12, *list(range(12))) |
| 2620 | >>> x = memoryview(buf) |
| 2621 | >>> y = x.cast('i', shape=[2,2,3]) |
| 2622 | >>> y.tolist() |
| 2623 | [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]] |
| 2624 | >>> y.format |
| 2625 | 'i' |
| 2626 | >>> y.itemsize |
| 2627 | 4 |
| 2628 | >>> len(y) |
| 2629 | 2 |
| 2630 | >>> y.nbytes |
| 2631 | 48 |
| 2632 | >>> z = y.cast('b') |
| 2633 | >>> z.format |
| 2634 | 'b' |
| 2635 | >>> z.itemsize |
| 2636 | 1 |
| 2637 | >>> len(z) |
| 2638 | 48 |
| 2639 | >>> z.nbytes |
| 2640 | 48 |
| 2641 | |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 2642 | Cast 1D/unsigned char to 2D/unsigned long:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2643 | |
| 2644 | >>> buf = struct.pack("L"*6, *list(range(6))) |
| 2645 | >>> x = memoryview(buf) |
| 2646 | >>> y = x.cast('L', shape=[2,3]) |
| 2647 | >>> len(y) |
| 2648 | 2 |
| 2649 | >>> y.nbytes |
| 2650 | 48 |
| 2651 | >>> y.tolist() |
| 2652 | [[0, 1, 2], [3, 4, 5]] |
| 2653 | |
| 2654 | .. versionadded:: 3.3 |
| 2655 | |
| 2656 | There are also several readonly attributes available: |
| 2657 | |
| 2658 | .. attribute:: obj |
| 2659 | |
| 2660 | The underlying object of the memoryview:: |
| 2661 | |
| 2662 | >>> b = bytearray(b'xyz') |
| 2663 | >>> m = memoryview(b) |
| 2664 | >>> m.obj is b |
| 2665 | True |
| 2666 | |
| 2667 | .. versionadded:: 3.3 |
| 2668 | |
| 2669 | .. attribute:: nbytes |
| 2670 | |
| 2671 | ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is |
| 2672 | the amount of space in bytes that the array would use in a contiguous |
| 2673 | representation. It is not necessarily equal to len(m):: |
| 2674 | |
| 2675 | >>> import array |
| 2676 | >>> a = array.array('i', [1,2,3,4,5]) |
| 2677 | >>> m = memoryview(a) |
| 2678 | >>> len(m) |
| 2679 | 5 |
| 2680 | >>> m.nbytes |
| 2681 | 20 |
| 2682 | >>> y = m[::2] |
| 2683 | >>> len(y) |
| 2684 | 3 |
| 2685 | >>> y.nbytes |
| 2686 | 12 |
| 2687 | >>> len(y.tobytes()) |
| 2688 | 12 |
| 2689 | |
| 2690 | Multi-dimensional arrays:: |
| 2691 | |
| 2692 | >>> import struct |
| 2693 | >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)]) |
| 2694 | >>> x = memoryview(buf) |
| 2695 | >>> y = x.cast('d', shape=[3,4]) |
| 2696 | >>> y.tolist() |
| 2697 | [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]] |
| 2698 | >>> len(y) |
| 2699 | 3 |
| 2700 | >>> y.nbytes |
| 2701 | 96 |
| 2702 | |
| 2703 | .. versionadded:: 3.3 |
| 2704 | |
| 2705 | .. attribute:: readonly |
| 2706 | |
| 2707 | A bool indicating whether the memory is read only. |
| 2708 | |
| 2709 | .. attribute:: format |
| 2710 | |
| 2711 | A string containing the format (in :mod:`struct` module style) for each |
| 2712 | element in the view. A memoryview can be created from exporters with |
| 2713 | arbitrary format strings, but some methods (e.g. :meth:`tolist`) are |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 2714 | restricted to native single element formats. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2715 | |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 2716 | .. versionchanged:: 3.3 |
| 2717 | format ``'B'`` is now handled according to the struct module syntax. |
| 2718 | This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``. |
| 2719 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2720 | .. attribute:: itemsize |
| 2721 | |
| 2722 | The size in bytes of each element of the memoryview:: |
| 2723 | |
| 2724 | >>> import array, struct |
| 2725 | >>> m = memoryview(array.array('H', [32000, 32001, 32002])) |
| 2726 | >>> m.itemsize |
| 2727 | 2 |
| 2728 | >>> m[0] |
| 2729 | 32000 |
| 2730 | >>> struct.calcsize('H') == m.itemsize |
| 2731 | True |
| 2732 | |
| 2733 | .. attribute:: ndim |
| 2734 | |
| 2735 | An integer indicating how many dimensions of a multi-dimensional array the |
| 2736 | memory represents. |
| 2737 | |
| 2738 | .. attribute:: shape |
| 2739 | |
| 2740 | A tuple of integers the length of :attr:`ndim` giving the shape of the |
Alexander Belopolsky | e8677c0 | 2012-09-03 17:29:22 -0400 | [diff] [blame] | 2741 | memory as an N-dimensional array. |
| 2742 | |
| 2743 | .. versionchanged:: 3.3 |
| 2744 | An empty tuple instead of None when ndim = 0. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2745 | |
| 2746 | .. attribute:: strides |
| 2747 | |
| 2748 | A tuple of integers the length of :attr:`ndim` giving the size in bytes to |
| 2749 | access each element for each dimension of the array. |
| 2750 | |
Alexander Belopolsky | e8677c0 | 2012-09-03 17:29:22 -0400 | [diff] [blame] | 2751 | .. versionchanged:: 3.3 |
| 2752 | An empty tuple instead of None when ndim = 0. |
| 2753 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2754 | .. attribute:: suboffsets |
| 2755 | |
| 2756 | Used internally for PIL-style arrays. The value is informational only. |
| 2757 | |
| 2758 | .. attribute:: c_contiguous |
| 2759 | |
| 2760 | A bool indicating whether the memory is C-contiguous. |
| 2761 | |
| 2762 | .. versionadded:: 3.3 |
| 2763 | |
| 2764 | .. attribute:: f_contiguous |
| 2765 | |
| 2766 | A bool indicating whether the memory is Fortran contiguous. |
| 2767 | |
| 2768 | .. versionadded:: 3.3 |
| 2769 | |
| 2770 | .. attribute:: contiguous |
| 2771 | |
| 2772 | A bool indicating whether the memory is contiguous. |
| 2773 | |
| 2774 | .. versionadded:: 3.3 |
| 2775 | |
| 2776 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2777 | .. _types-set: |
| 2778 | |
| 2779 | Set Types --- :class:`set`, :class:`frozenset` |
| 2780 | ============================================== |
| 2781 | |
| 2782 | .. index:: object: set |
| 2783 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 2784 | 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] | 2785 | Common uses include membership testing, removing duplicates from a sequence, and |
| 2786 | computing mathematical operations such as intersection, union, difference, and |
| 2787 | symmetric difference. |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2788 | (For other containers see the built-in :class:`dict`, :class:`list`, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2789 | and :class:`tuple` classes, and the :mod:`collections` module.) |
| 2790 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2791 | Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in |
| 2792 | set``. Being an unordered collection, sets do not record element position or |
| 2793 | order of insertion. Accordingly, sets do not support indexing, slicing, or |
| 2794 | other sequence-like behavior. |
| 2795 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 2796 | There are currently two built-in set types, :class:`set` and :class:`frozenset`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2797 | The :class:`set` type is mutable --- the contents can be changed using methods |
| 2798 | like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value |
| 2799 | 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] | 2800 | 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] | 2801 | altered after it is created; it can therefore be used as a dictionary key or as |
| 2802 | an element of another set. |
| 2803 | |
Georg Brandl | 99cd957 | 2010-03-21 09:10:32 +0000 | [diff] [blame] | 2804 | Non-empty sets (not frozensets) can be created by placing a comma-separated list |
Georg Brandl | 53b95e7 | 2010-03-21 11:53:50 +0000 | [diff] [blame] | 2805 | of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the |
| 2806 | :class:`set` constructor. |
Georg Brandl | 99cd957 | 2010-03-21 09:10:32 +0000 | [diff] [blame] | 2807 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2808 | The constructors for both classes work the same: |
| 2809 | |
| 2810 | .. class:: set([iterable]) |
| 2811 | frozenset([iterable]) |
| 2812 | |
| 2813 | Return a new set or frozenset object whose elements are taken from |
| 2814 | *iterable*. The elements of a set must be hashable. To represent sets of |
| 2815 | sets, the inner sets must be :class:`frozenset` objects. If *iterable* is |
| 2816 | not specified, a new empty set is returned. |
| 2817 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2818 | Instances of :class:`set` and :class:`frozenset` provide the following |
| 2819 | operations: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2820 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2821 | .. describe:: len(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2822 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2823 | Return the cardinality of set *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2824 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2825 | .. describe:: x in s |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2826 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2827 | Test *x* for membership in *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2828 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2829 | .. describe:: x not in s |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2830 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2831 | Test *x* for non-membership in *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2832 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2833 | .. method:: isdisjoint(other) |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2834 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2835 | Return True if the set has no elements in common with *other*. Sets are |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 2836 | disjoint if and only if their intersection is the empty set. |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 2837 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2838 | .. method:: issubset(other) |
| 2839 | set <= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2840 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2841 | Test whether every element in the set is in *other*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2842 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2843 | .. method:: set < other |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 2844 | |
Andrew Svetlov | 5bb4207 | 2012-11-01 21:47:54 +0200 | [diff] [blame] | 2845 | Test whether the set is a proper subset of *other*, that is, |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2846 | ``set <= other and set != other``. |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 2847 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2848 | .. method:: issuperset(other) |
| 2849 | set >= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2850 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2851 | Test whether every element in *other* is in the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2852 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2853 | .. method:: set > other |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 2854 | |
Andrew Svetlov | 5bb4207 | 2012-11-01 21:47:54 +0200 | [diff] [blame] | 2855 | Test whether the set is a proper superset of *other*, that is, ``set >= |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2856 | other and set != other``. |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 2857 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 2858 | .. method:: union(other, ...) |
| 2859 | set | other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2860 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 2861 | Return a new set with elements from the set and all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2862 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 2863 | .. method:: intersection(other, ...) |
| 2864 | set & other & ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2865 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 2866 | Return a new set with elements common to the set and all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2867 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 2868 | .. method:: difference(other, ...) |
| 2869 | set - other - ... |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 2870 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 2871 | Return a new set with elements in the set that are not in the others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2872 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2873 | .. method:: symmetric_difference(other) |
| 2874 | set ^ other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2875 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2876 | Return a new set with elements in either the set or *other* but not both. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2877 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2878 | .. method:: copy() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2879 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2880 | Return a new set with a shallow copy of *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2881 | |
| 2882 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2883 | Note, the non-operator versions of :meth:`union`, :meth:`intersection`, |
| 2884 | :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and |
| 2885 | :meth:`issuperset` methods will accept any iterable as an argument. In |
| 2886 | contrast, their operator based counterparts require their arguments to be |
| 2887 | sets. This precludes error-prone constructions like ``set('abc') & 'cbs'`` |
| 2888 | in favor of the more readable ``set('abc').intersection('cbs')``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2889 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2890 | Both :class:`set` and :class:`frozenset` support set to set comparisons. Two |
| 2891 | sets are equal if and only if every element of each set is contained in the |
| 2892 | other (each is a subset of the other). A set is less than another set if and |
| 2893 | only if the first set is a proper subset of the second set (is a subset, but |
| 2894 | is not equal). A set is greater than another set if and only if the first set |
| 2895 | is a proper superset of the second set (is a superset, but is not equal). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2896 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2897 | Instances of :class:`set` are compared to instances of :class:`frozenset` |
| 2898 | based on their members. For example, ``set('abc') == frozenset('abc')`` |
| 2899 | returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2900 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2901 | The subset and equality comparisons do not generalize to a complete ordering |
| 2902 | function. For example, any two disjoint sets are not equal and are not |
| 2903 | subsets of each other, so *all* of the following return ``False``: ``a<b``, |
Georg Brandl | 05f5ab7 | 2008-09-24 09:11:47 +0000 | [diff] [blame] | 2904 | ``a==b``, or ``a>b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2905 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2906 | Since sets only define partial ordering (subset relationships), the output of |
| 2907 | the :meth:`list.sort` method is undefined for lists of sets. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2908 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2909 | Set elements, like dictionary keys, must be :term:`hashable`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2910 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2911 | Binary operations that mix :class:`set` instances with :class:`frozenset` |
| 2912 | return the type of the first operand. For example: ``frozenset('ab') | |
| 2913 | set('bc')`` returns an instance of :class:`frozenset`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2914 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2915 | The following table lists operations available for :class:`set` that do not |
| 2916 | apply to immutable instances of :class:`frozenset`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2917 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 2918 | .. method:: update(other, ...) |
| 2919 | set |= other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2920 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 2921 | Update the set, adding elements from all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2922 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 2923 | .. method:: intersection_update(other, ...) |
| 2924 | set &= other & ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2925 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 2926 | Update the set, keeping only elements found in it and all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2927 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 2928 | .. method:: difference_update(other, ...) |
| 2929 | set -= other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2930 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 2931 | Update the set, removing elements found in others. |
| 2932 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2933 | .. method:: symmetric_difference_update(other) |
| 2934 | set ^= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2935 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2936 | Update the set, keeping only elements found in either set, but not in both. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2937 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2938 | .. method:: add(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2939 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2940 | Add element *elem* to the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2941 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2942 | .. method:: remove(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2943 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2944 | Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is |
| 2945 | not contained in the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2946 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2947 | .. method:: discard(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2948 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2949 | Remove element *elem* from the set if it is present. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2950 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2951 | .. method:: pop() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2952 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2953 | Remove and return an arbitrary element from the set. Raises |
| 2954 | :exc:`KeyError` if the set is empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2955 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2956 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2957 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2958 | Remove all elements from the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2959 | |
| 2960 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2961 | Note, the non-operator versions of the :meth:`update`, |
| 2962 | :meth:`intersection_update`, :meth:`difference_update`, and |
| 2963 | :meth:`symmetric_difference_update` methods will accept any iterable as an |
| 2964 | argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2965 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 2966 | Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and |
| 2967 | :meth:`discard` methods may be a set. To support searching for an equivalent |
| 2968 | frozenset, the *elem* set is temporarily mutated during the search and then |
| 2969 | restored. During the search, the *elem* set should not be read or mutated |
| 2970 | since it does not have a meaningful value. |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 2971 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2972 | |
| 2973 | .. _typesmapping: |
| 2974 | |
| 2975 | Mapping Types --- :class:`dict` |
| 2976 | =============================== |
| 2977 | |
| 2978 | .. index:: |
| 2979 | object: mapping |
| 2980 | object: dictionary |
| 2981 | triple: operations on; mapping; types |
| 2982 | triple: operations on; dictionary; type |
| 2983 | statement: del |
| 2984 | builtin: len |
| 2985 | |
Chris Jerdonek | 11f3f17 | 2012-11-03 12:05:55 -0700 | [diff] [blame] | 2986 | A :term:`mapping` object maps :term:`hashable` values to arbitrary objects. |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 2987 | Mappings are mutable objects. There is currently only one standard mapping |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2988 | type, the :dfn:`dictionary`. (For other containers see the built-in |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 2989 | :class:`list`, :class:`set`, and :class:`tuple` classes, and the |
| 2990 | :mod:`collections` module.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2991 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 2992 | A dictionary's keys are *almost* arbitrary values. Values that are not |
| 2993 | :term:`hashable`, that is, values containing lists, dictionaries or other |
| 2994 | mutable types (that are compared by value rather than by object identity) may |
| 2995 | not be used as keys. Numeric types used for keys obey the normal rules for |
| 2996 | numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``) |
| 2997 | then they can be used interchangeably to index the same dictionary entry. (Note |
| 2998 | however, that since computers store floating-point numbers as approximations it |
| 2999 | is usually unwise to use them as dictionary keys.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3000 | |
| 3001 | Dictionaries can be created by placing a comma-separated list of ``key: value`` |
| 3002 | pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: |
| 3003 | 'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. |
| 3004 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 3005 | .. class:: dict(**kwarg) |
| 3006 | dict(mapping, **kwarg) |
| 3007 | dict(iterable, **kwarg) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3008 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 3009 | Return a new dictionary initialized from an optional positional argument |
| 3010 | and a possibly empty set of keyword arguments. |
| 3011 | |
| 3012 | If no positional argument is given, an empty dictionary is created. |
| 3013 | If a positional argument is given and it is a mapping object, a dictionary |
| 3014 | is created with the same key-value pairs as the mapping object. Otherwise, |
| 3015 | the positional argument must be an :term:`iterator` object. Each item in |
| 3016 | the iterable must itself be an iterator with exactly two objects. The |
| 3017 | first object of each item becomes a key in the new dictionary, and the |
| 3018 | second object the corresponding value. If a key occurs more than once, the |
| 3019 | last value for that key becomes the corresponding value in the new |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 3020 | dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3021 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 3022 | If keyword arguments are given, the keyword arguments and their values are |
| 3023 | added to the dictionary created from the positional argument. If a key |
| 3024 | being added is already present, the value from the keyword argument |
| 3025 | replaces the value from the positional argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3026 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 3027 | To illustrate, the following examples all return a dictionary equal to |
Ezio Melotti | a20879f | 2012-10-26 19:14:16 +0300 | [diff] [blame] | 3028 | ``{"one": 1, "two": 2, "three": 3}``:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3029 | |
Ezio Melotti | a20879f | 2012-10-26 19:14:16 +0300 | [diff] [blame] | 3030 | >>> a = dict(one=1, two=2, three=3) |
| 3031 | >>> b = {'one': 1, 'two': 2, 'three': 3} |
| 3032 | >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) |
| 3033 | >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) |
| 3034 | >>> e = dict({'three': 3, 'one': 1, 'two': 2}) |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 3035 | >>> a == b == c == d == e |
| 3036 | True |
| 3037 | |
| 3038 | Providing keyword arguments as in the first example only works for keys that |
| 3039 | are valid Python identifiers. Otherwise, any valid keys can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3040 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3041 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3042 | These are the operations that dictionaries support (and therefore, custom |
| 3043 | mapping types should support too): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3044 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3045 | .. describe:: len(d) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3046 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3047 | Return the number of items in the dictionary *d*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3048 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3049 | .. describe:: d[key] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3050 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3051 | Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is |
| 3052 | not in the map. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 3053 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3054 | If a subclass of dict defines a method :meth:`__missing__`, if the key *key* |
| 3055 | is not present, the ``d[key]`` operation calls that method with the key *key* |
| 3056 | as argument. The ``d[key]`` operation then returns or raises whatever is |
| 3057 | returned or raised by the ``__missing__(key)`` call if the key is not |
| 3058 | present. No other operations or methods invoke :meth:`__missing__`. If |
| 3059 | :meth:`__missing__` is not defined, :exc:`KeyError` is raised. |
Raymond Hettinger | 5254e97 | 2011-01-08 09:35:38 +0000 | [diff] [blame] | 3060 | :meth:`__missing__` must be a method; it cannot be an instance variable:: |
| 3061 | |
| 3062 | >>> class Counter(dict): |
| 3063 | ... def __missing__(self, key): |
| 3064 | ... return 0 |
| 3065 | >>> c = Counter() |
| 3066 | >>> c['red'] |
| 3067 | 0 |
| 3068 | >>> c['red'] += 1 |
| 3069 | >>> c['red'] |
| 3070 | 1 |
| 3071 | |
| 3072 | See :class:`collections.Counter` for a complete implementation including |
| 3073 | other methods helpful for accumulating and managing tallies. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3074 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3075 | .. describe:: d[key] = value |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3076 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3077 | Set ``d[key]`` to *value*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3078 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3079 | .. describe:: del d[key] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3080 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3081 | Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the |
| 3082 | map. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3083 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3084 | .. describe:: key in d |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3085 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3086 | Return ``True`` if *d* has a key *key*, else ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3087 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3088 | .. describe:: key not in d |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3089 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3090 | Equivalent to ``not key in d``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3091 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 3092 | .. describe:: iter(d) |
| 3093 | |
| 3094 | Return an iterator over the keys of the dictionary. This is a shortcut |
Georg Brandl | ede6c2a | 2010-01-05 10:22:04 +0000 | [diff] [blame] | 3095 | for ``iter(d.keys())``. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 3096 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3097 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3098 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3099 | Remove all items from the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3100 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3101 | .. method:: copy() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3102 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3103 | Return a shallow copy of the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3104 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 3105 | .. classmethod:: fromkeys(seq[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3106 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3107 | Create a new dictionary with keys from *seq* and values set to *value*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3108 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3109 | :meth:`fromkeys` is a class method that returns a new dictionary. *value* |
| 3110 | defaults to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3111 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3112 | .. method:: get(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3113 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3114 | Return the value for *key* if *key* is in the dictionary, else *default*. |
| 3115 | If *default* is not given, it defaults to ``None``, so that this method |
| 3116 | never raises a :exc:`KeyError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3117 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3118 | .. method:: items() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3119 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 3120 | Return a new view of the dictionary's items (``(key, value)`` pairs). |
| 3121 | See the :ref:`documentation of view objects <dict-views>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3122 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3123 | .. method:: keys() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3124 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 3125 | Return a new view of the dictionary's keys. See the :ref:`documentation |
| 3126 | of view objects <dict-views>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3127 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3128 | .. method:: pop(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3129 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3130 | If *key* is in the dictionary, remove it and return its value, else return |
| 3131 | *default*. If *default* is not given and *key* is not in the dictionary, |
| 3132 | a :exc:`KeyError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3133 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3134 | .. method:: popitem() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3135 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3136 | Remove and return an arbitrary ``(key, value)`` pair from the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3137 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3138 | :meth:`popitem` is useful to destructively iterate over a dictionary, as |
| 3139 | often used in set algorithms. If the dictionary is empty, calling |
| 3140 | :meth:`popitem` raises a :exc:`KeyError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3141 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3142 | .. method:: setdefault(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3143 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3144 | If *key* is in the dictionary, return its value. If not, insert *key* |
| 3145 | with a value of *default* and return *default*. *default* defaults to |
| 3146 | ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3147 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3148 | .. method:: update([other]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3149 | |
Éric Araujo | 0fc86b8 | 2010-08-18 22:29:54 +0000 | [diff] [blame] | 3150 | Update the dictionary with the key/value pairs from *other*, overwriting |
| 3151 | existing keys. Return ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3152 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3153 | :meth:`update` accepts either another dictionary object or an iterable of |
Georg Brandl | fda2106 | 2010-09-25 16:56:36 +0000 | [diff] [blame] | 3154 | key/value pairs (as tuples or other iterables of length two). If keyword |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 3155 | arguments are specified, the dictionary is then updated with those |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3156 | key/value pairs: ``d.update(red=1, blue=2)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3157 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3158 | .. method:: values() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3159 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 3160 | Return a new view of the dictionary's values. See the |
| 3161 | :ref:`documentation of view objects <dict-views>`. |
| 3162 | |
| 3163 | .. seealso:: |
| 3164 | :class:`types.MappingProxyType` can be used to create a read-only view |
| 3165 | of a :class:`dict`. |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 3166 | |
| 3167 | |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 3168 | .. _dict-views: |
| 3169 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 3170 | Dictionary view objects |
| 3171 | ----------------------- |
| 3172 | |
| 3173 | The objects returned by :meth:`dict.keys`, :meth:`dict.values` and |
| 3174 | :meth:`dict.items` are *view objects*. They provide a dynamic view on the |
| 3175 | dictionary's entries, which means that when the dictionary changes, the view |
Benjamin Peterson | ce0506c | 2008-11-17 21:47:41 +0000 | [diff] [blame] | 3176 | reflects these changes. |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 3177 | |
| 3178 | Dictionary views can be iterated over to yield their respective data, and |
| 3179 | support membership tests: |
| 3180 | |
| 3181 | .. describe:: len(dictview) |
| 3182 | |
| 3183 | Return the number of entries in the dictionary. |
| 3184 | |
| 3185 | .. describe:: iter(dictview) |
| 3186 | |
| 3187 | Return an iterator over the keys, values or items (represented as tuples of |
| 3188 | ``(key, value)``) in the dictionary. |
| 3189 | |
| 3190 | Keys and values are iterated over in an arbitrary order which is non-random, |
| 3191 | varies across Python implementations, and depends on the dictionary's history |
| 3192 | of insertions and deletions. If keys, values and items views are iterated |
| 3193 | over with no intervening modifications to the dictionary, the order of items |
| 3194 | will directly correspond. This allows the creation of ``(value, key)`` pairs |
| 3195 | using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to |
| 3196 | create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``. |
| 3197 | |
Georg Brandl | 8126914 | 2009-05-17 08:31:29 +0000 | [diff] [blame] | 3198 | Iterating views while adding or deleting entries in the dictionary may raise |
| 3199 | a :exc:`RuntimeError` or fail to iterate over all entries. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 3200 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 3201 | .. describe:: x in dictview |
| 3202 | |
| 3203 | Return ``True`` if *x* is in the underlying dictionary's keys, values or |
| 3204 | items (in the latter case, *x* should be a ``(key, value)`` tuple). |
| 3205 | |
| 3206 | |
Benjamin Peterson | ce0506c | 2008-11-17 21:47:41 +0000 | [diff] [blame] | 3207 | Keys views are set-like since their entries are unique and hashable. If all |
Georg Brandl | f74cf77 | 2010-10-15 16:03:02 +0000 | [diff] [blame] | 3208 | values are hashable, so that ``(key, value)`` pairs are unique and hashable, |
| 3209 | then the items view is also set-like. (Values views are not treated as set-like |
| 3210 | since the entries are generally not unique.) For set-like views, all of the |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3211 | operations defined for the abstract base class :class:`collections.abc.Set` are |
Georg Brandl | f74cf77 | 2010-10-15 16:03:02 +0000 | [diff] [blame] | 3212 | available (for example, ``==``, ``<``, or ``^``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3213 | |
Georg Brandl | c53c966 | 2007-09-04 17:58:02 +0000 | [diff] [blame] | 3214 | An example of dictionary view usage:: |
| 3215 | |
| 3216 | >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} |
| 3217 | >>> keys = dishes.keys() |
| 3218 | >>> values = dishes.values() |
| 3219 | |
| 3220 | >>> # iteration |
| 3221 | >>> n = 0 |
| 3222 | >>> for val in values: |
| 3223 | ... n += val |
| 3224 | >>> print(n) |
| 3225 | 504 |
| 3226 | |
| 3227 | >>> # keys and values are iterated over in the same order |
| 3228 | >>> list(keys) |
| 3229 | ['eggs', 'bacon', 'sausage', 'spam'] |
| 3230 | >>> list(values) |
| 3231 | [2, 1, 1, 500] |
| 3232 | |
| 3233 | >>> # view objects are dynamic and reflect dict changes |
| 3234 | >>> del dishes['eggs'] |
| 3235 | >>> del dishes['sausage'] |
| 3236 | >>> list(keys) |
| 3237 | ['spam', 'bacon'] |
| 3238 | |
| 3239 | >>> # set operations |
| 3240 | >>> keys & {'eggs', 'bacon', 'salad'} |
Gregory P. Smith | e838812 | 2008-09-04 04:18:09 +0000 | [diff] [blame] | 3241 | {'bacon'} |
Georg Brandl | f74cf77 | 2010-10-15 16:03:02 +0000 | [diff] [blame] | 3242 | >>> keys ^ {'sausage', 'juice'} |
Sandro Tosi | 2a8d195 | 2011-08-02 18:42:04 +0200 | [diff] [blame] | 3243 | {'juice', 'sausage', 'bacon', 'spam'} |
Georg Brandl | c53c966 | 2007-09-04 17:58:02 +0000 | [diff] [blame] | 3244 | |
| 3245 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3246 | .. _typecontextmanager: |
| 3247 | |
| 3248 | Context Manager Types |
| 3249 | ===================== |
| 3250 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3251 | .. index:: |
| 3252 | single: context manager |
| 3253 | single: context management protocol |
| 3254 | single: protocol; context management |
| 3255 | |
| 3256 | Python's :keyword:`with` statement supports the concept of a runtime context |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 3257 | defined by a context manager. This is implemented using a pair of methods |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3258 | that allow user-defined classes to define a runtime context that is entered |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 3259 | before the statement body is executed and exited when the statement ends: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3260 | |
| 3261 | |
| 3262 | .. method:: contextmanager.__enter__() |
| 3263 | |
| 3264 | Enter the runtime context and return either this object or another object |
| 3265 | related to the runtime context. The value returned by this method is bound to |
| 3266 | the identifier in the :keyword:`as` clause of :keyword:`with` statements using |
| 3267 | this context manager. |
| 3268 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 3269 | An example of a context manager that returns itself is a :term:`file object`. |
| 3270 | File objects return themselves from __enter__() to allow :func:`open` to be |
| 3271 | used as the context expression in a :keyword:`with` statement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3272 | |
| 3273 | An example of a context manager that returns a related object is the one |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 3274 | returned by :func:`decimal.localcontext`. These managers set the active |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3275 | decimal context to a copy of the original decimal context and then return the |
| 3276 | copy. This allows changes to be made to the current decimal context in the body |
| 3277 | of the :keyword:`with` statement without affecting code outside the |
| 3278 | :keyword:`with` statement. |
| 3279 | |
| 3280 | |
| 3281 | .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb) |
| 3282 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 3283 | 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] | 3284 | that occurred should be suppressed. If an exception occurred while executing the |
| 3285 | body of the :keyword:`with` statement, the arguments contain the exception type, |
| 3286 | value and traceback information. Otherwise, all three arguments are ``None``. |
| 3287 | |
| 3288 | Returning a true value from this method will cause the :keyword:`with` statement |
| 3289 | to suppress the exception and continue execution with the statement immediately |
| 3290 | following the :keyword:`with` statement. Otherwise the exception continues |
| 3291 | propagating after this method has finished executing. Exceptions that occur |
| 3292 | during execution of this method will replace any exception that occurred in the |
| 3293 | body of the :keyword:`with` statement. |
| 3294 | |
| 3295 | The exception passed in should never be reraised explicitly - instead, this |
| 3296 | method should return a false value to indicate that the method completed |
| 3297 | successfully and does not want to suppress the raised exception. This allows |
| 3298 | context management code (such as ``contextlib.nested``) to easily detect whether |
| 3299 | or not an :meth:`__exit__` method has actually failed. |
| 3300 | |
| 3301 | Python defines several context managers to support easy thread synchronisation, |
| 3302 | prompt closure of files or other objects, and simpler manipulation of the active |
| 3303 | decimal arithmetic context. The specific types are not treated specially beyond |
| 3304 | their implementation of the context management protocol. See the |
| 3305 | :mod:`contextlib` module for some examples. |
| 3306 | |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 3307 | Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 3308 | provide a convenient way to implement these protocols. If a generator function is |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 3309 | decorated with the :class:`contextlib.contextmanager` decorator, it will return a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3310 | context manager implementing the necessary :meth:`__enter__` and |
| 3311 | :meth:`__exit__` methods, rather than the iterator produced by an undecorated |
| 3312 | generator function. |
| 3313 | |
| 3314 | Note that there is no specific slot for any of these methods in the type |
| 3315 | structure for Python objects in the Python/C API. Extension types wanting to |
| 3316 | define these methods must provide them as a normal Python accessible method. |
| 3317 | Compared to the overhead of setting up the runtime context, the overhead of a |
| 3318 | single class dictionary lookup is negligible. |
| 3319 | |
| 3320 | |
| 3321 | .. _typesother: |
| 3322 | |
| 3323 | Other Built-in Types |
| 3324 | ==================== |
| 3325 | |
| 3326 | The interpreter supports several other kinds of objects. Most of these support |
| 3327 | only one or two operations. |
| 3328 | |
| 3329 | |
| 3330 | .. _typesmodules: |
| 3331 | |
| 3332 | Modules |
| 3333 | ------- |
| 3334 | |
| 3335 | The only special operation on a module is attribute access: ``m.name``, where |
| 3336 | *m* is a module and *name* accesses a name defined in *m*'s symbol table. |
| 3337 | Module attributes can be assigned to. (Note that the :keyword:`import` |
| 3338 | statement is not, strictly speaking, an operation on a module object; ``import |
| 3339 | foo`` does not require a module object named *foo* to exist, rather it requires |
| 3340 | an (external) *definition* for a module named *foo* somewhere.) |
| 3341 | |
Senthil Kumaran | a6bac95 | 2011-07-04 11:28:30 -0700 | [diff] [blame] | 3342 | A special attribute of every module is :attr:`__dict__`. This is the dictionary |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3343 | containing the module's symbol table. Modifying this dictionary will actually |
| 3344 | change the module's symbol table, but direct assignment to the :attr:`__dict__` |
| 3345 | attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines |
| 3346 | ``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying |
| 3347 | :attr:`__dict__` directly is not recommended. |
| 3348 | |
| 3349 | Modules built into the interpreter are written like this: ``<module 'sys' |
| 3350 | (built-in)>``. If loaded from a file, they are written as ``<module 'os' from |
| 3351 | '/usr/local/lib/pythonX.Y/os.pyc'>``. |
| 3352 | |
| 3353 | |
| 3354 | .. _typesobjects: |
| 3355 | |
| 3356 | Classes and Class Instances |
| 3357 | --------------------------- |
| 3358 | |
| 3359 | See :ref:`objects` and :ref:`class` for these. |
| 3360 | |
| 3361 | |
| 3362 | .. _typesfunctions: |
| 3363 | |
| 3364 | Functions |
| 3365 | --------- |
| 3366 | |
| 3367 | Function objects are created by function definitions. The only operation on a |
| 3368 | function object is to call it: ``func(argument-list)``. |
| 3369 | |
| 3370 | There are really two flavors of function objects: built-in functions and |
| 3371 | user-defined functions. Both support the same operation (to call the function), |
| 3372 | but the implementation is different, hence the different object types. |
| 3373 | |
| 3374 | See :ref:`function` for more information. |
| 3375 | |
| 3376 | |
| 3377 | .. _typesmethods: |
| 3378 | |
| 3379 | Methods |
| 3380 | ------- |
| 3381 | |
| 3382 | .. index:: object: method |
| 3383 | |
| 3384 | Methods are functions that are called using the attribute notation. There are |
| 3385 | two flavors: built-in methods (such as :meth:`append` on lists) and class |
| 3386 | instance methods. Built-in methods are described with the types that support |
| 3387 | them. |
| 3388 | |
Georg Brandl | 2e0b755 | 2007-11-27 12:43:08 +0000 | [diff] [blame] | 3389 | If you access a method (a function defined in a class namespace) through an |
| 3390 | instance, you get a special object: a :dfn:`bound method` (also called |
| 3391 | :dfn:`instance method`) object. When called, it will add the ``self`` argument |
| 3392 | to the argument list. Bound methods have two special read-only attributes: |
| 3393 | ``m.__self__`` is the object on which the method operates, and ``m.__func__`` is |
| 3394 | the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)`` |
| 3395 | is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ..., |
| 3396 | arg-n)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3397 | |
Georg Brandl | 2e0b755 | 2007-11-27 12:43:08 +0000 | [diff] [blame] | 3398 | Like function objects, bound method objects support getting arbitrary |
| 3399 | attributes. However, since method attributes are actually stored on the |
| 3400 | underlying function object (``meth.__func__``), setting method attributes on |
Ezio Melotti | 8b6b176 | 2012-11-09 01:08:25 +0200 | [diff] [blame] | 3401 | bound methods is disallowed. Attempting to set an attribute on a method |
| 3402 | results in an :exc:`AttributeError` being raised. In order to set a method |
| 3403 | attribute, you need to explicitly set it on the underlying function object:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3404 | |
Ezio Melotti | 8b6b176 | 2012-11-09 01:08:25 +0200 | [diff] [blame] | 3405 | >>> class C: |
| 3406 | ... def method(self): |
| 3407 | ... pass |
| 3408 | ... |
| 3409 | >>> c = C() |
| 3410 | >>> c.method.whoami = 'my name is method' # can't set on the method |
| 3411 | Traceback (most recent call last): |
| 3412 | File "<stdin>", line 1, in <module> |
| 3413 | AttributeError: 'method' object has no attribute 'whoami' |
| 3414 | >>> c.method.__func__.whoami = 'my name is method' |
| 3415 | >>> c.method.whoami |
| 3416 | 'my name is method' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3417 | |
| 3418 | See :ref:`types` for more information. |
| 3419 | |
| 3420 | |
| 3421 | .. _bltin-code-objects: |
| 3422 | |
| 3423 | Code Objects |
| 3424 | ------------ |
| 3425 | |
| 3426 | .. index:: object: code |
| 3427 | |
| 3428 | .. index:: |
| 3429 | builtin: compile |
| 3430 | single: __code__ (function object attribute) |
| 3431 | |
| 3432 | Code objects are used by the implementation to represent "pseudo-compiled" |
| 3433 | executable Python code such as a function body. They differ from function |
| 3434 | objects because they don't contain a reference to their global execution |
| 3435 | environment. Code objects are returned by the built-in :func:`compile` function |
| 3436 | and can be extracted from function objects through their :attr:`__code__` |
| 3437 | attribute. See also the :mod:`code` module. |
| 3438 | |
| 3439 | .. index:: |
| 3440 | builtin: exec |
| 3441 | builtin: eval |
| 3442 | |
| 3443 | A code object can be executed or evaluated by passing it (instead of a source |
| 3444 | string) to the :func:`exec` or :func:`eval` built-in functions. |
| 3445 | |
| 3446 | See :ref:`types` for more information. |
| 3447 | |
| 3448 | |
| 3449 | .. _bltin-type-objects: |
| 3450 | |
| 3451 | Type Objects |
| 3452 | ------------ |
| 3453 | |
| 3454 | .. index:: |
| 3455 | builtin: type |
| 3456 | module: types |
| 3457 | |
| 3458 | Type objects represent the various object types. An object's type is accessed |
| 3459 | by the built-in function :func:`type`. There are no special operations on |
| 3460 | types. The standard module :mod:`types` defines names for all standard built-in |
| 3461 | types. |
| 3462 | |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 3463 | Types are written like this: ``<class 'int'>``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3464 | |
| 3465 | |
| 3466 | .. _bltin-null-object: |
| 3467 | |
| 3468 | The Null Object |
| 3469 | --------------- |
| 3470 | |
| 3471 | This object is returned by functions that don't explicitly return a value. It |
| 3472 | supports no special operations. There is exactly one null object, named |
Benjamin Peterson | 98f2b9b | 2011-07-30 12:26:27 -0500 | [diff] [blame] | 3473 | ``None`` (a built-in name). ``type(None)()`` produces the same singleton. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3474 | |
| 3475 | It is written as ``None``. |
| 3476 | |
| 3477 | |
| 3478 | .. _bltin-ellipsis-object: |
| 3479 | |
| 3480 | The Ellipsis Object |
| 3481 | ------------------- |
| 3482 | |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 3483 | This object is commonly used by slicing (see :ref:`slicings`). It supports no |
| 3484 | special operations. There is exactly one ellipsis object, named |
| 3485 | :const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the |
| 3486 | :const:`Ellipsis` singleton. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3487 | |
| 3488 | It is written as ``Ellipsis`` or ``...``. |
| 3489 | |
| 3490 | |
Éric Araujo | 18ddf82 | 2011-09-01 23:10:36 +0200 | [diff] [blame] | 3491 | .. _bltin-notimplemented-object: |
| 3492 | |
Benjamin Peterson | 50211fa | 2011-07-30 09:57:24 -0500 | [diff] [blame] | 3493 | The NotImplemented Object |
| 3494 | ------------------------- |
| 3495 | |
| 3496 | This object is returned from comparisons and binary operations when they are |
| 3497 | asked to operate on types they don't support. See :ref:`comparisons` for more |
Benjamin Peterson | 98f2b9b | 2011-07-30 12:26:27 -0500 | [diff] [blame] | 3498 | information. There is exactly one ``NotImplemented`` object. |
| 3499 | ``type(NotImplemented)()`` produces the singleton instance. |
Benjamin Peterson | 50211fa | 2011-07-30 09:57:24 -0500 | [diff] [blame] | 3500 | |
| 3501 | It is written as ``NotImplemented``. |
| 3502 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3503 | |
Éric Araujo | 18ddf82 | 2011-09-01 23:10:36 +0200 | [diff] [blame] | 3504 | .. _bltin-boolean-values: |
| 3505 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3506 | Boolean Values |
| 3507 | -------------- |
| 3508 | |
| 3509 | Boolean values are the two constant objects ``False`` and ``True``. They are |
| 3510 | used to represent truth values (although other values can also be considered |
| 3511 | false or true). In numeric contexts (for example when used as the argument to |
| 3512 | an arithmetic operator), they behave like the integers 0 and 1, respectively. |
Ezio Melotti | c1f26f6 | 2011-12-02 19:47:24 +0200 | [diff] [blame] | 3513 | The built-in function :func:`bool` can be used to convert any value to a |
| 3514 | Boolean, if the value can be interpreted as a truth value (see section |
| 3515 | :ref:`truth` above). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3516 | |
| 3517 | .. index:: |
| 3518 | single: False |
| 3519 | single: True |
| 3520 | pair: Boolean; values |
| 3521 | |
| 3522 | They are written as ``False`` and ``True``, respectively. |
| 3523 | |
| 3524 | |
| 3525 | .. _typesinternal: |
| 3526 | |
| 3527 | Internal Objects |
| 3528 | ---------------- |
| 3529 | |
| 3530 | See :ref:`types` for this information. It describes stack frame objects, |
| 3531 | traceback objects, and slice objects. |
| 3532 | |
| 3533 | |
| 3534 | .. _specialattrs: |
| 3535 | |
| 3536 | Special Attributes |
| 3537 | ================== |
| 3538 | |
| 3539 | The implementation adds a few special read-only attributes to several object |
| 3540 | types, where they are relevant. Some of these are not reported by the |
| 3541 | :func:`dir` built-in function. |
| 3542 | |
| 3543 | |
| 3544 | .. attribute:: object.__dict__ |
| 3545 | |
| 3546 | A dictionary or other mapping object used to store an object's (writable) |
| 3547 | attributes. |
| 3548 | |
| 3549 | |
| 3550 | .. attribute:: instance.__class__ |
| 3551 | |
| 3552 | The class to which a class instance belongs. |
| 3553 | |
| 3554 | |
| 3555 | .. attribute:: class.__bases__ |
| 3556 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 3557 | The tuple of base classes of a class object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3558 | |
| 3559 | |
| 3560 | .. attribute:: class.__name__ |
| 3561 | |
| 3562 | The name of the class or type. |
| 3563 | |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 3564 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3565 | .. attribute:: class.__qualname__ |
| 3566 | |
| 3567 | The :term:`qualified name` of the class or type. |
| 3568 | |
| 3569 | .. versionadded:: 3.3 |
| 3570 | |
| 3571 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 3572 | .. attribute:: class.__mro__ |
| 3573 | |
| 3574 | This attribute is a tuple of classes that are considered when looking for |
| 3575 | base classes during method resolution. |
| 3576 | |
| 3577 | |
| 3578 | .. method:: class.mro() |
| 3579 | |
| 3580 | This method can be overridden by a metaclass to customize the method |
| 3581 | resolution order for its instances. It is called at class instantiation, and |
| 3582 | its result is stored in :attr:`__mro__`. |
| 3583 | |
| 3584 | |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 3585 | .. method:: class.__subclasses__ |
| 3586 | |
Florent Xicluna | 74e6495 | 2011-10-28 11:21:19 +0200 | [diff] [blame] | 3587 | Each class keeps a list of weak references to its immediate subclasses. This |
| 3588 | method returns a list of all those references still alive. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 3589 | Example:: |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 3590 | |
| 3591 | >>> int.__subclasses__() |
Florent Xicluna | 74e6495 | 2011-10-28 11:21:19 +0200 | [diff] [blame] | 3592 | [<class 'bool'>] |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 3593 | |
| 3594 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3595 | .. rubric:: Footnotes |
| 3596 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 3597 | .. [1] Additional information on these special methods may be found in the Python |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3598 | Reference Manual (:ref:`customization`). |
| 3599 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 3600 | .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3601 | similarly for tuples. |
| 3602 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 3603 | .. [3] They must have since the parser can't tell the type of the operands. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3604 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 3605 | .. [4] Cased characters are those with general category property being one of |
| 3606 | "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase). |
| 3607 | |
| 3608 | .. [5] To format only a tuple you should therefore provide a singleton tuple whose only |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3609 | element is the tuple to be formatted. |