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 |
Mariatta | 1936ba9 | 2017-03-03 13:24:13 -0800 | [diff] [blame^] | 111 | argument if the first one is false. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | (2) |
| 114 | This is a short-circuit operator, so it only evaluates the second |
Mariatta | 1936ba9 | 2017-03-03 13:24:13 -0800 | [diff] [blame^] | 115 | argument if the first one is true. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 |
Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 272 | ascending priority (all numeric operations have a higher priority than |
| 273 | comparison operations): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 342 | as in C; see functions :func:`math.floor` and :func:`math.ceil` for |
| 343 | well-defined conversions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 4801383 | 2015-06-27 15:45:56 -0500 | [diff] [blame] | 357 | See http://www.unicode.org/Public/8.0.0/ucd/extracted/DerivedNumericType.txt |
Raymond Hettinger | c706dbf | 2011-03-22 17:33:17 -0700 | [diff] [blame] | 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 | |
Martin Panter | 129fe04 | 2016-05-08 12:22:37 +0000 | [diff] [blame] | 364 | +--------------------+---------------------------------------------+ |
| 365 | | Operation | Result | |
| 366 | +====================+=============================================+ |
| 367 | | :func:`math.trunc(\| *x* truncated to :class:`~numbers.Integral` | |
| 368 | | x) <math.trunc>` | | |
| 369 | +--------------------+---------------------------------------------+ |
| 370 | | :func:`round(x[, | *x* rounded to *n* digits, | |
| 371 | | n]) <round>` | rounding half to even. If *n* is | |
| 372 | | | omitted, it defaults to 0. | |
| 373 | +--------------------+---------------------------------------------+ |
| 374 | | :func:`math.floor(\| the greatest :class:`~numbers.Integral` | |
| 375 | | x) <math.floor>` | <= *x* | |
| 376 | +--------------------+---------------------------------------------+ |
| 377 | | :func:`math.ceil(x)| the least :class:`~numbers.Integral` >= *x* | |
| 378 | | <math.ceil>` | | |
| 379 | +--------------------+---------------------------------------------+ |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 380 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 381 | For additional numeric operations see the :mod:`math` and :mod:`cmath` |
| 382 | modules. |
| 383 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 384 | .. XXXJH exceptions: overflow (when? what operations?) zerodivision |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
| 386 | |
| 387 | .. _bitstring-ops: |
| 388 | |
Benjamin Peterson | e9fca25 | 2012-01-25 16:29:03 -0500 | [diff] [blame] | 389 | Bitwise Operations on Integer Types |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | -------------------------------------- |
| 391 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 392 | .. index:: |
| 393 | triple: operations on; integer; types |
Benjamin Peterson | e9fca25 | 2012-01-25 16:29:03 -0500 | [diff] [blame] | 394 | pair: bitwise; operations |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 395 | pair: shifting; operations |
| 396 | pair: masking; operations |
| 397 | operator: ^ |
| 398 | operator: & |
| 399 | operator: << |
| 400 | operator: >> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | b4b0b35 | 2012-01-25 16:30:18 -0500 | [diff] [blame] | 402 | Bitwise operations only make sense for integers. Negative numbers are treated |
Raymond Hettinger | 4d02896 | 2016-03-12 22:58:24 -0800 | [diff] [blame] | 403 | as their 2's complement value (this assumes that there are enough bits so that |
| 404 | no overflow occurs during the operation). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 406 | 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] | 407 | operations and higher than the comparisons; the unary operation ``~`` has the |
| 408 | same priority as the other unary numeric operations (``+`` and ``-``). |
| 409 | |
Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 410 | This table lists the bitwise operations sorted in ascending priority: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 411 | |
| 412 | +------------+--------------------------------+----------+ |
| 413 | | Operation | Result | Notes | |
| 414 | +============+================================+==========+ |
| 415 | | ``x | y`` | bitwise :dfn:`or` of *x* and | | |
| 416 | | | *y* | | |
| 417 | +------------+--------------------------------+----------+ |
| 418 | | ``x ^ y`` | bitwise :dfn:`exclusive or` of | | |
| 419 | | | *x* and *y* | | |
| 420 | +------------+--------------------------------+----------+ |
| 421 | | ``x & y`` | bitwise :dfn:`and` of *x* and | | |
| 422 | | | *y* | | |
| 423 | +------------+--------------------------------+----------+ |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 424 | | ``x << n`` | *x* shifted left by *n* bits | (1)(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | +------------+--------------------------------+----------+ |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 426 | | ``x >> n`` | *x* shifted right by *n* bits | (1)(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | +------------+--------------------------------+----------+ |
| 428 | | ``~x`` | the bits of *x* inverted | | |
| 429 | +------------+--------------------------------+----------+ |
| 430 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | Notes: |
| 432 | |
| 433 | (1) |
| 434 | Negative shift counts are illegal and cause a :exc:`ValueError` to be raised. |
| 435 | |
| 436 | (2) |
| 437 | A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)`` |
| 438 | without overflow check. |
| 439 | |
| 440 | (3) |
| 441 | A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without |
| 442 | overflow check. |
| 443 | |
| 444 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 445 | Additional Methods on Integer Types |
| 446 | ----------------------------------- |
| 447 | |
Raymond Hettinger | 9b2fd32 | 2011-05-01 18:14:49 -0700 | [diff] [blame] | 448 | The int type implements the :class:`numbers.Integral` :term:`abstract base |
Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 449 | class`. In addition, it provides a few more methods: |
Benjamin Peterson | 10116d4 | 2011-05-01 17:38:17 -0500 | [diff] [blame] | 450 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 451 | .. method:: int.bit_length() |
| 452 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 453 | Return the number of bits necessary to represent an integer in binary, |
| 454 | excluding the sign and leading zeros:: |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 455 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 456 | >>> n = -37 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 457 | >>> bin(n) |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 458 | '-0b100101' |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 459 | >>> n.bit_length() |
| 460 | 6 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 461 | |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 462 | More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the |
| 463 | unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``. |
| 464 | Equivalently, when ``abs(x)`` is small enough to have a correctly |
| 465 | rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``. |
| 466 | If ``x`` is zero, then ``x.bit_length()`` returns ``0``. |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 467 | |
| 468 | Equivalent to:: |
| 469 | |
| 470 | def bit_length(self): |
Senthil Kumaran | 0aae6dc | 2010-06-22 02:57:23 +0000 | [diff] [blame] | 471 | s = bin(self) # binary representation: bin(-37) --> '-0b100101' |
Raymond Hettinger | d3e18b7 | 2008-12-19 09:11:49 +0000 | [diff] [blame] | 472 | s = s.lstrip('-0b') # remove leading zeros and minus sign |
| 473 | return len(s) # len('100101') --> 6 |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 474 | |
| 475 | .. versionadded:: 3.1 |
| 476 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 477 | .. method:: int.to_bytes(length, byteorder, \*, signed=False) |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 478 | |
| 479 | Return an array of bytes representing an integer. |
| 480 | |
| 481 | >>> (1024).to_bytes(2, byteorder='big') |
| 482 | b'\x04\x00' |
| 483 | >>> (1024).to_bytes(10, byteorder='big') |
| 484 | b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00' |
| 485 | >>> (-1024).to_bytes(10, byteorder='big', signed=True) |
| 486 | b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00' |
| 487 | >>> x = 1000 |
INADA Naoki | 3e3e9f3 | 2016-10-31 17:41:47 +0900 | [diff] [blame] | 488 | >>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little') |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 489 | b'\xe8\x03' |
| 490 | |
| 491 | The integer is represented using *length* bytes. An :exc:`OverflowError` |
| 492 | is raised if the integer is not representable with the given number of |
| 493 | bytes. |
| 494 | |
| 495 | The *byteorder* argument determines the byte order used to represent the |
| 496 | integer. If *byteorder* is ``"big"``, the most significant byte is at the |
| 497 | beginning of the byte array. If *byteorder* is ``"little"``, the most |
| 498 | significant byte is at the end of the byte array. To request the native |
| 499 | byte order of the host system, use :data:`sys.byteorder` as the byte order |
| 500 | value. |
| 501 | |
| 502 | The *signed* argument determines whether two's complement is used to |
| 503 | represent the integer. If *signed* is ``False`` and a negative integer is |
| 504 | given, an :exc:`OverflowError` is raised. The default value for *signed* |
| 505 | is ``False``. |
| 506 | |
| 507 | .. versionadded:: 3.2 |
| 508 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 509 | .. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False) |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 510 | |
| 511 | Return the integer represented by the given array of bytes. |
| 512 | |
| 513 | >>> int.from_bytes(b'\x00\x10', byteorder='big') |
| 514 | 16 |
| 515 | >>> int.from_bytes(b'\x00\x10', byteorder='little') |
| 516 | 4096 |
| 517 | >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) |
| 518 | -1024 |
| 519 | >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False) |
| 520 | 64512 |
| 521 | >>> int.from_bytes([255, 0, 0], byteorder='big') |
| 522 | 16711680 |
| 523 | |
Ezio Melotti | c228e96 | 2013-05-04 18:06:34 +0300 | [diff] [blame] | 524 | The argument *bytes* must either be a :term:`bytes-like object` or an |
| 525 | iterable producing bytes. |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 526 | |
| 527 | The *byteorder* argument determines the byte order used to represent the |
| 528 | integer. If *byteorder* is ``"big"``, the most significant byte is at the |
| 529 | beginning of the byte array. If *byteorder* is ``"little"``, the most |
| 530 | significant byte is at the end of the byte array. To request the native |
| 531 | byte order of the host system, use :data:`sys.byteorder` as the byte order |
| 532 | value. |
| 533 | |
| 534 | The *signed* argument indicates whether two's complement is used to |
| 535 | represent the integer. |
| 536 | |
| 537 | .. versionadded:: 3.2 |
| 538 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 539 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 540 | Additional Methods on Float |
| 541 | --------------------------- |
| 542 | |
Benjamin Peterson | 10116d4 | 2011-05-01 17:38:17 -0500 | [diff] [blame] | 543 | The float type implements the :class:`numbers.Real` :term:`abstract base |
| 544 | class`. float also has the following additional methods. |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 545 | |
| 546 | .. method:: float.as_integer_ratio() |
| 547 | |
Mark Dickinson | 4a3c7c4 | 2010-11-07 12:48:18 +0000 | [diff] [blame] | 548 | Return a pair of integers whose ratio is exactly equal to the |
| 549 | original float and with a positive denominator. Raises |
| 550 | :exc:`OverflowError` on infinities and a :exc:`ValueError` on |
| 551 | NaNs. |
| 552 | |
| 553 | .. method:: float.is_integer() |
| 554 | |
| 555 | Return ``True`` if the float instance is finite with integral |
| 556 | value, and ``False`` otherwise:: |
| 557 | |
| 558 | >>> (-2.0).is_integer() |
| 559 | True |
| 560 | >>> (3.2).is_integer() |
| 561 | False |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 562 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 563 | Two methods support conversion to |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 564 | and from hexadecimal strings. Since Python's floats are stored |
| 565 | internally as binary numbers, converting a float to or from a |
| 566 | *decimal* string usually involves a small rounding error. In |
| 567 | contrast, hexadecimal strings allow exact representation and |
| 568 | specification of floating-point numbers. This can be useful when |
| 569 | debugging, and in numerical work. |
| 570 | |
| 571 | |
| 572 | .. method:: float.hex() |
| 573 | |
| 574 | Return a representation of a floating-point number as a hexadecimal |
| 575 | string. For finite floating-point numbers, this representation |
| 576 | will always include a leading ``0x`` and a trailing ``p`` and |
| 577 | exponent. |
| 578 | |
| 579 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 580 | .. classmethod:: float.fromhex(s) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 581 | |
| 582 | Class method to return the float represented by a hexadecimal |
| 583 | string *s*. The string *s* may have leading and trailing |
| 584 | whitespace. |
| 585 | |
| 586 | |
| 587 | Note that :meth:`float.hex` is an instance method, while |
| 588 | :meth:`float.fromhex` is a class method. |
| 589 | |
| 590 | A hexadecimal string takes the form:: |
| 591 | |
| 592 | [sign] ['0x'] integer ['.' fraction] ['p' exponent] |
| 593 | |
| 594 | where the optional ``sign`` may by either ``+`` or ``-``, ``integer`` |
| 595 | and ``fraction`` are strings of hexadecimal digits, and ``exponent`` |
| 596 | is a decimal integer with an optional leading sign. Case is not |
| 597 | significant, and there must be at least one hexadecimal digit in |
| 598 | either the integer or the fraction. This syntax is similar to the |
| 599 | syntax specified in section 6.4.4.2 of the C99 standard, and also to |
| 600 | the syntax used in Java 1.5 onwards. In particular, the output of |
| 601 | :meth:`float.hex` is usable as a hexadecimal floating-point literal in |
| 602 | C or Java code, and hexadecimal strings produced by C's ``%a`` format |
| 603 | character or Java's ``Double.toHexString`` are accepted by |
| 604 | :meth:`float.fromhex`. |
| 605 | |
| 606 | |
| 607 | Note that the exponent is written in decimal rather than hexadecimal, |
| 608 | and that it gives the power of 2 by which to multiply the coefficient. |
| 609 | For example, the hexadecimal string ``0x3.a7p10`` represents the |
| 610 | floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or |
| 611 | ``3740.0``:: |
| 612 | |
| 613 | >>> float.fromhex('0x3.a7p10') |
| 614 | 3740.0 |
| 615 | |
| 616 | |
| 617 | Applying the reverse conversion to ``3740.0`` gives a different |
| 618 | hexadecimal string representing the same number:: |
| 619 | |
| 620 | >>> float.hex(3740.0) |
| 621 | '0x1.d380000000000p+11' |
| 622 | |
| 623 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 624 | .. _numeric-hash: |
| 625 | |
| 626 | Hashing of numeric types |
| 627 | ------------------------ |
| 628 | |
| 629 | For numbers ``x`` and ``y``, possibly of different types, it's a requirement |
| 630 | that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__` |
| 631 | method documentation for more details). For ease of implementation and |
| 632 | efficiency across a variety of numeric types (including :class:`int`, |
| 633 | :class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`) |
| 634 | Python's hash for numeric types is based on a single mathematical function |
| 635 | that's defined for any rational number, and hence applies to all instances of |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 636 | :class:`int` and :class:`fractions.Fraction`, and all finite instances of |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 637 | :class:`float` and :class:`decimal.Decimal`. Essentially, this function is |
| 638 | given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is |
| 639 | made available to Python as the :attr:`modulus` attribute of |
| 640 | :data:`sys.hash_info`. |
| 641 | |
| 642 | .. impl-detail:: |
| 643 | |
| 644 | Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C |
| 645 | longs and ``P = 2**61 - 1`` on machines with 64-bit C longs. |
| 646 | |
| 647 | Here are the rules in detail: |
| 648 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 649 | - If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible |
| 650 | by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n, |
| 651 | P)`` gives the inverse of ``n`` modulo ``P``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 652 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 653 | - If ``x = m / n`` is a nonnegative rational number and ``n`` is |
| 654 | divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse |
| 655 | modulo ``P`` and the rule above doesn't apply; in this case define |
| 656 | ``hash(x)`` to be the constant value ``sys.hash_info.inf``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 657 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 658 | - If ``x = m / n`` is a negative rational number define ``hash(x)`` |
| 659 | as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with |
| 660 | ``-2``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 661 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 662 | - The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf`` |
| 663 | and ``sys.hash_info.nan`` are used as hash values for positive |
| 664 | infinity, negative infinity, or nans (respectively). (All hashable |
| 665 | nans have the same hash value.) |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 666 | |
Georg Brandl | 226ed7e | 2012-03-24 08:12:41 +0100 | [diff] [blame] | 667 | - For a :class:`complex` number ``z``, the hash values of the real |
| 668 | and imaginary parts are combined by computing ``hash(z.real) + |
| 669 | sys.hash_info.imag * hash(z.imag)``, reduced modulo |
| 670 | ``2**sys.hash_info.width`` so that it lies in |
| 671 | ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - |
| 672 | 1))``. Again, if the result is ``-1``, it's replaced with ``-2``. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 673 | |
| 674 | |
| 675 | To clarify the above rules, here's some example Python code, |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 676 | 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] | 677 | number, :class:`float`, or :class:`complex`:: |
| 678 | |
| 679 | |
| 680 | import sys, math |
| 681 | |
| 682 | def hash_fraction(m, n): |
| 683 | """Compute the hash of a rational number m / n. |
| 684 | |
| 685 | Assumes m and n are integers, with n positive. |
| 686 | Equivalent to hash(fractions.Fraction(m, n)). |
| 687 | |
| 688 | """ |
| 689 | P = sys.hash_info.modulus |
| 690 | # Remove common factors of P. (Unnecessary if m and n already coprime.) |
| 691 | while m % P == n % P == 0: |
| 692 | m, n = m // P, n // P |
| 693 | |
| 694 | if n % P == 0: |
Berker Peksag | aa46bd4 | 2016-07-25 04:55:51 +0300 | [diff] [blame] | 695 | hash_value = sys.hash_info.inf |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 696 | else: |
| 697 | # Fermat's Little Theorem: pow(n, P-1, P) is 1, so |
| 698 | # pow(n, P-2, P) gives the inverse of n modulo P. |
Berker Peksag | aa46bd4 | 2016-07-25 04:55:51 +0300 | [diff] [blame] | 699 | hash_value = (abs(m) % P) * pow(n, P - 2, P) % P |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 700 | if m < 0: |
Berker Peksag | aa46bd4 | 2016-07-25 04:55:51 +0300 | [diff] [blame] | 701 | hash_value = -hash_value |
| 702 | if hash_value == -1: |
| 703 | hash_value = -2 |
| 704 | return hash_value |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 705 | |
| 706 | def hash_float(x): |
| 707 | """Compute the hash of a float x.""" |
| 708 | |
| 709 | if math.isnan(x): |
| 710 | return sys.hash_info.nan |
| 711 | elif math.isinf(x): |
| 712 | return sys.hash_info.inf if x > 0 else -sys.hash_info.inf |
| 713 | else: |
| 714 | return hash_fraction(*x.as_integer_ratio()) |
| 715 | |
| 716 | def hash_complex(z): |
| 717 | """Compute the hash of a complex number z.""" |
| 718 | |
Berker Peksag | aa46bd4 | 2016-07-25 04:55:51 +0300 | [diff] [blame] | 719 | hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag) |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 720 | # do a signed reduction modulo 2**sys.hash_info.width |
| 721 | M = 2**(sys.hash_info.width - 1) |
Berker Peksag | aa46bd4 | 2016-07-25 04:55:51 +0300 | [diff] [blame] | 722 | hash_value = (hash_value & (M - 1)) - (hash_value & M) |
| 723 | if hash_value == -1: |
| 724 | hash_value = -2 |
| 725 | return hash_value |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 726 | |
Georg Brandl | 6ea420b | 2008-07-16 12:58:29 +0000 | [diff] [blame] | 727 | .. _typeiter: |
| 728 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 729 | Iterator Types |
| 730 | ============== |
| 731 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 732 | .. index:: |
| 733 | single: iterator protocol |
| 734 | single: protocol; iterator |
| 735 | single: sequence; iteration |
| 736 | single: container; iteration over |
| 737 | |
| 738 | Python supports a concept of iteration over containers. This is implemented |
| 739 | using two distinct methods; these are used to allow user-defined classes to |
| 740 | support iteration. Sequences, described below in more detail, always support |
| 741 | the iteration methods. |
| 742 | |
| 743 | One method needs to be defined for container objects to provide iteration |
| 744 | support: |
| 745 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 746 | .. XXX duplicated in reference/datamodel! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 747 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 748 | .. method:: container.__iter__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | |
| 750 | Return an iterator object. The object is required to support the iterator |
| 751 | protocol described below. If a container supports different types of |
| 752 | iteration, additional methods can be provided to specifically request |
| 753 | iterators for those iteration types. (An example of an object supporting |
| 754 | multiple forms of iteration would be a tree structure which supports both |
| 755 | breadth-first and depth-first traversal.) This method corresponds to the |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 756 | :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 757 | API. |
| 758 | |
| 759 | The iterator objects themselves are required to support the following two |
| 760 | methods, which together form the :dfn:`iterator protocol`: |
| 761 | |
| 762 | |
| 763 | .. method:: iterator.__iter__() |
| 764 | |
| 765 | Return the iterator object itself. This is required to allow both containers |
| 766 | and iterators to be used with the :keyword:`for` and :keyword:`in` statements. |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 767 | This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 768 | Python objects in the Python/C API. |
| 769 | |
| 770 | |
Georg Brandl | 905ec32 | 2007-09-28 13:39:25 +0000 | [diff] [blame] | 771 | .. method:: iterator.__next__() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 772 | |
| 773 | Return the next item from the container. If there are no further items, raise |
| 774 | the :exc:`StopIteration` exception. This method corresponds to the |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 775 | :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 776 | Python/C API. |
| 777 | |
| 778 | Python defines several iterator objects to support iteration over general and |
| 779 | specific sequence types, dictionaries, and other more specialized forms. The |
| 780 | specific types are not important beyond their implementation of the iterator |
| 781 | protocol. |
| 782 | |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 783 | Once an iterator's :meth:`~iterator.__next__` method raises |
| 784 | :exc:`StopIteration`, it must continue to do so on subsequent calls. |
| 785 | Implementations that do not obey this property are deemed broken. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 786 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 787 | |
| 788 | .. _generator-types: |
| 789 | |
| 790 | Generator Types |
| 791 | --------------- |
| 792 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 793 | Python's :term:`generator`\s provide a convenient way to implement the iterator |
| 794 | protocol. If a container object's :meth:`__iter__` method is implemented as a |
| 795 | generator, it will automatically return an iterator object (technically, a |
Ezio Melotti | 7fa8222 | 2012-10-12 13:42:08 +0300 | [diff] [blame] | 796 | generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__` |
| 797 | methods. |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 798 | More information about generators can be found in :ref:`the documentation for |
| 799 | the yield expression <yieldexpr>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 800 | |
| 801 | |
| 802 | .. _typesseq: |
| 803 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 804 | Sequence Types --- :class:`list`, :class:`tuple`, :class:`range` |
| 805 | ================================================================ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 806 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 807 | There are three basic sequence types: lists, tuples, and range objects. |
| 808 | Additional sequence types tailored for processing of |
| 809 | :ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are |
| 810 | described in dedicated sections. |
Georg Brandl | e17d586 | 2009-01-18 10:40:25 +0000 | [diff] [blame] | 811 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 812 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 813 | .. _typesseq-common: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 814 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 815 | Common Sequence Operations |
| 816 | -------------------------- |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 817 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 818 | .. index:: object: sequence |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 819 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 820 | The operations in the following table are supported by most sequence types, |
| 821 | both mutable and immutable. The :class:`collections.abc.Sequence` ABC is |
| 822 | provided to make it easier to correctly implement these operations on |
| 823 | custom sequence types. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 824 | |
Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 825 | This table lists the sequence operations sorted in ascending priority. In the |
| 826 | table, *s* and *t* are sequences of the same type, *n*, *i*, *j* and *k* are |
| 827 | integers and *x* is an arbitrary object that meets any type and value |
| 828 | restrictions imposed by *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 829 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 830 | The ``in`` and ``not in`` operations have the same priorities as the |
| 831 | comparison operations. The ``+`` (concatenation) and ``*`` (repetition) |
| 832 | operations have the same priority as the corresponding numeric operations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 833 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 834 | .. index:: |
| 835 | triple: operations on; sequence; types |
| 836 | builtin: len |
| 837 | builtin: min |
| 838 | builtin: max |
| 839 | pair: concatenation; operation |
| 840 | pair: repetition; operation |
| 841 | pair: subscript; operation |
| 842 | pair: slice; operation |
| 843 | operator: in |
| 844 | operator: not in |
| 845 | single: count() (sequence method) |
| 846 | single: index() (sequence method) |
| 847 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 848 | +--------------------------+--------------------------------+----------+ |
| 849 | | Operation | Result | Notes | |
| 850 | +==========================+================================+==========+ |
| 851 | | ``x in s`` | ``True`` if an item of *s* is | \(1) | |
| 852 | | | equal to *x*, else ``False`` | | |
| 853 | +--------------------------+--------------------------------+----------+ |
| 854 | | ``x not in s`` | ``False`` if an item of *s* is | \(1) | |
| 855 | | | equal to *x*, else ``True`` | | |
| 856 | +--------------------------+--------------------------------+----------+ |
| 857 | | ``s + t`` | the concatenation of *s* and | (6)(7) | |
| 858 | | | *t* | | |
| 859 | +--------------------------+--------------------------------+----------+ |
Martin Panter | 7f02d6d | 2015-09-07 02:08:55 +0000 | [diff] [blame] | 860 | | ``s * n`` or | equivalent to adding *s* to | (2)(7) | |
| 861 | | ``n * s`` | itself *n* times | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 862 | +--------------------------+--------------------------------+----------+ |
| 863 | | ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) | |
| 864 | +--------------------------+--------------------------------+----------+ |
| 865 | | ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) | |
| 866 | +--------------------------+--------------------------------+----------+ |
| 867 | | ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) | |
| 868 | | | with step *k* | | |
| 869 | +--------------------------+--------------------------------+----------+ |
| 870 | | ``len(s)`` | length of *s* | | |
| 871 | +--------------------------+--------------------------------+----------+ |
| 872 | | ``min(s)`` | smallest item of *s* | | |
| 873 | +--------------------------+--------------------------------+----------+ |
| 874 | | ``max(s)`` | largest item of *s* | | |
| 875 | +--------------------------+--------------------------------+----------+ |
Ned Deily | 0995c47 | 2013-07-14 12:43:16 -0700 | [diff] [blame] | 876 | | ``s.index(x[, i[, j]])`` | index of the first occurrence | \(8) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 877 | | | of *x* in *s* (at or after | | |
| 878 | | | index *i* and before index *j*)| | |
| 879 | +--------------------------+--------------------------------+----------+ |
Ned Deily | 0995c47 | 2013-07-14 12:43:16 -0700 | [diff] [blame] | 880 | | ``s.count(x)`` | total number of occurrences of | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 881 | | | *x* in *s* | | |
| 882 | +--------------------------+--------------------------------+----------+ |
| 883 | |
| 884 | Sequences of the same type also support comparisons. In particular, tuples |
| 885 | and lists are compared lexicographically by comparing corresponding elements. |
| 886 | This means that to compare equal, every element must compare equal and the |
| 887 | two sequences must be of the same type and have the same length. (For full |
| 888 | details see :ref:`comparisons` in the language reference.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 889 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 890 | Notes: |
| 891 | |
| 892 | (1) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 893 | While the ``in`` and ``not in`` operations are used only for simple |
| 894 | containment testing in the general case, some specialised sequences |
| 895 | (such as :class:`str`, :class:`bytes` and :class:`bytearray`) also use |
| 896 | them for subsequence testing:: |
| 897 | |
| 898 | >>> "gg" in "eggs" |
| 899 | True |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 900 | |
| 901 | (2) |
| 902 | Values of *n* less than ``0`` are treated as ``0`` (which yields an empty |
Martin Panter | 7f02d6d | 2015-09-07 02:08:55 +0000 | [diff] [blame] | 903 | sequence of the same type as *s*). Note that items in the sequence *s* |
| 904 | are not copied; they are referenced multiple times. This often haunts |
| 905 | new Python programmers; consider:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 906 | |
| 907 | >>> lists = [[]] * 3 |
| 908 | >>> lists |
| 909 | [[], [], []] |
| 910 | >>> lists[0].append(3) |
| 911 | >>> lists |
| 912 | [[3], [3], [3]] |
| 913 | |
| 914 | What has happened is that ``[[]]`` is a one-element list containing an empty |
Martin Panter | 7f02d6d | 2015-09-07 02:08:55 +0000 | [diff] [blame] | 915 | list, so all three elements of ``[[]] * 3`` are references to this single empty |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 916 | list. Modifying any of the elements of ``lists`` modifies this single list. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 917 | You can create a list of different lists this way:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 918 | |
| 919 | >>> lists = [[] for i in range(3)] |
| 920 | >>> lists[0].append(3) |
| 921 | >>> lists[1].append(5) |
| 922 | >>> lists[2].append(7) |
| 923 | >>> lists |
| 924 | [[3], [5], [7]] |
| 925 | |
Martin Panter | 7f02d6d | 2015-09-07 02:08:55 +0000 | [diff] [blame] | 926 | Further explanation is available in the FAQ entry |
| 927 | :ref:`faq-multidimensional-list`. |
| 928 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 929 | (3) |
Xiang Zhang | cea904f | 2016-12-30 11:57:09 +0800 | [diff] [blame] | 930 | If *i* or *j* is negative, the index is relative to the end of sequence *s*: |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 931 | ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is |
| 932 | still ``0``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 933 | |
| 934 | (4) |
| 935 | The slice of *s* from *i* to *j* is defined as the sequence of items with index |
| 936 | *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use |
| 937 | ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or |
| 938 | ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is |
| 939 | empty. |
| 940 | |
| 941 | (5) |
| 942 | 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] | 943 | 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] | 944 | the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when |
Martin Panter | 3dbd87f | 2016-12-24 08:25:15 +0000 | [diff] [blame] | 945 | *j* is reached (but never including *j*). When *k* is positive, |
| 946 | *i* and *j* are reduced to ``len(s)`` if they are greater. |
| 947 | When *k* is negative, *i* and *j* are reduced to ``len(s) - 1`` if |
| 948 | they are greater. If *i* or *j* are omitted or ``None``, they become |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 949 | "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero. |
| 950 | If *k* is ``None``, it is treated like ``1``. |
| 951 | |
| 952 | (6) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 953 | Concatenating immutable sequences always results in a new object. This |
| 954 | means that building up a sequence by repeated concatenation will have a |
| 955 | quadratic runtime cost in the total sequence length. To get a linear |
| 956 | runtime cost, you must switch to one of the alternatives below: |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | fd9ebd4 | 2011-11-25 16:33:53 +0100 | [diff] [blame] | 958 | * if concatenating :class:`str` objects, you can build a list and use |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 959 | :meth:`str.join` at the end or else write to an :class:`io.StringIO` |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 960 | instance and retrieve its value when complete |
Antoine Pitrou | fd9ebd4 | 2011-11-25 16:33:53 +0100 | [diff] [blame] | 961 | |
| 962 | * if concatenating :class:`bytes` objects, you can similarly use |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 963 | :meth:`bytes.join` or :class:`io.BytesIO`, or you can do in-place |
| 964 | concatenation with a :class:`bytearray` object. :class:`bytearray` |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 965 | objects are mutable and have an efficient overallocation mechanism |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 966 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 967 | * if concatenating :class:`tuple` objects, extend a :class:`list` instead |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 968 | |
| 969 | * for other types, investigate the relevant class documentation |
| 970 | |
| 971 | |
| 972 | (7) |
| 973 | Some sequence types (such as :class:`range`) only support item sequences |
| 974 | that follow specific patterns, and hence don't support sequence |
| 975 | concatenation or repetition. |
| 976 | |
| 977 | (8) |
| 978 | ``index`` raises :exc:`ValueError` when *x* is not found in *s*. |
| 979 | When supported, the additional arguments to the index method allow |
| 980 | efficient searching of subsections of the sequence. Passing the extra |
| 981 | arguments is roughly equivalent to using ``s[i:j].index(x)``, only |
| 982 | without copying any data and with the returned index being relative to |
| 983 | the start of the sequence rather than the start of the slice. |
| 984 | |
| 985 | |
| 986 | .. _typesseq-immutable: |
| 987 | |
| 988 | Immutable Sequence Types |
| 989 | ------------------------ |
| 990 | |
| 991 | .. index:: |
| 992 | triple: immutable; sequence; types |
| 993 | object: tuple |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 994 | builtin: hash |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 995 | |
| 996 | The only operation that immutable sequence types generally implement that is |
| 997 | not also implemented by mutable sequence types is support for the :func:`hash` |
| 998 | built-in. |
| 999 | |
| 1000 | This support allows immutable sequences, such as :class:`tuple` instances, to |
| 1001 | be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset` |
| 1002 | instances. |
| 1003 | |
| 1004 | Attempting to hash an immutable sequence that contains unhashable values will |
| 1005 | result in :exc:`TypeError`. |
| 1006 | |
| 1007 | |
| 1008 | .. _typesseq-mutable: |
| 1009 | |
| 1010 | Mutable Sequence Types |
| 1011 | ---------------------- |
| 1012 | |
| 1013 | .. index:: |
| 1014 | triple: mutable; sequence; types |
| 1015 | object: list |
| 1016 | object: bytearray |
| 1017 | |
| 1018 | The operations in the following table are defined on mutable sequence types. |
| 1019 | The :class:`collections.abc.MutableSequence` ABC is provided to make it |
| 1020 | easier to correctly implement these operations on custom sequence types. |
| 1021 | |
| 1022 | In the table *s* is an instance of a mutable sequence type, *t* is any |
| 1023 | iterable object and *x* is an arbitrary object that meets any type |
| 1024 | and value restrictions imposed by *s* (for example, :class:`bytearray` only |
| 1025 | accepts integers that meet the value restriction ``0 <= x <= 255``). |
| 1026 | |
| 1027 | |
| 1028 | .. index:: |
| 1029 | triple: operations on; sequence; types |
| 1030 | triple: operations on; list; type |
| 1031 | pair: subscript; assignment |
| 1032 | pair: slice; assignment |
| 1033 | statement: del |
| 1034 | single: append() (sequence method) |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1035 | single: clear() (sequence method) |
| 1036 | single: copy() (sequence method) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1037 | single: extend() (sequence method) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1038 | single: insert() (sequence method) |
| 1039 | single: pop() (sequence method) |
| 1040 | single: remove() (sequence method) |
| 1041 | single: reverse() (sequence method) |
| 1042 | |
| 1043 | +------------------------------+--------------------------------+---------------------+ |
| 1044 | | Operation | Result | Notes | |
| 1045 | +==============================+================================+=====================+ |
| 1046 | | ``s[i] = x`` | item *i* of *s* is replaced by | | |
| 1047 | | | *x* | | |
| 1048 | +------------------------------+--------------------------------+---------------------+ |
| 1049 | | ``s[i:j] = t`` | slice of *s* from *i* to *j* | | |
| 1050 | | | is replaced by the contents of | | |
| 1051 | | | the iterable *t* | | |
| 1052 | +------------------------------+--------------------------------+---------------------+ |
| 1053 | | ``del s[i:j]`` | same as ``s[i:j] = []`` | | |
| 1054 | +------------------------------+--------------------------------+---------------------+ |
| 1055 | | ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) | |
| 1056 | | | are replaced by those of *t* | | |
| 1057 | +------------------------------+--------------------------------+---------------------+ |
| 1058 | | ``del s[i:j:k]`` | removes the elements of | | |
| 1059 | | | ``s[i:j:k]`` from the list | | |
| 1060 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1061 | | ``s.append(x)`` | appends *x* to the end of the | | |
| 1062 | | | sequence (same as | | |
| 1063 | | | ``s[len(s):len(s)] = [x]``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1064 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1065 | | ``s.clear()`` | removes all items from ``s`` | \(5) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1066 | | | (same as ``del s[:]``) | | |
| 1067 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1068 | | ``s.copy()`` | creates a shallow copy of ``s``| \(5) | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1069 | | | (same as ``s[:]``) | | |
| 1070 | +------------------------------+--------------------------------+---------------------+ |
Martin Panter | 3795d12 | 2015-10-03 07:46:04 +0000 | [diff] [blame] | 1071 | | ``s.extend(t)`` or | extends *s* with the | | |
| 1072 | | ``s += t`` | contents of *t* (for the | | |
| 1073 | | | most part the same as | | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1074 | | | ``s[len(s):len(s)] = t``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1075 | +------------------------------+--------------------------------+---------------------+ |
Martin Panter | 3795d12 | 2015-10-03 07:46:04 +0000 | [diff] [blame] | 1076 | | ``s *= n`` | updates *s* with its contents | \(6) | |
| 1077 | | | repeated *n* times | | |
| 1078 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1079 | | ``s.insert(i, x)`` | inserts *x* into *s* at the | | |
| 1080 | | | index given by *i* | | |
| 1081 | | | (same as ``s[i:i] = [x]``) | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1082 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1083 | | ``s.pop([i])`` | retrieves the item at *i* and | \(2) | |
| 1084 | | | also removes it from *s* | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1085 | +------------------------------+--------------------------------+---------------------+ |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1086 | | ``s.remove(x)`` | remove the first item from *s* | \(3) | |
| 1087 | | | where ``s[i] == x`` | | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1088 | +------------------------------+--------------------------------+---------------------+ |
| 1089 | | ``s.reverse()`` | reverses the items of *s* in | \(4) | |
| 1090 | | | place | | |
| 1091 | +------------------------------+--------------------------------+---------------------+ |
| 1092 | |
| 1093 | |
| 1094 | Notes: |
| 1095 | |
| 1096 | (1) |
| 1097 | *t* must have the same length as the slice it is replacing. |
| 1098 | |
| 1099 | (2) |
| 1100 | The optional argument *i* defaults to ``-1``, so that by default the last |
| 1101 | item is removed and returned. |
| 1102 | |
| 1103 | (3) |
| 1104 | ``remove`` raises :exc:`ValueError` when *x* is not found in *s*. |
| 1105 | |
| 1106 | (4) |
| 1107 | The :meth:`reverse` method modifies the sequence in place for economy of |
| 1108 | space when reversing a large sequence. To remind users that it operates by |
| 1109 | side effect, it does not return the reversed sequence. |
| 1110 | |
| 1111 | (5) |
| 1112 | :meth:`clear` and :meth:`!copy` are included for consistency with the |
| 1113 | interfaces of mutable containers that don't support slicing operations |
| 1114 | (such as :class:`dict` and :class:`set`) |
| 1115 | |
| 1116 | .. versionadded:: 3.3 |
| 1117 | :meth:`clear` and :meth:`!copy` methods. |
| 1118 | |
Martin Panter | 3795d12 | 2015-10-03 07:46:04 +0000 | [diff] [blame] | 1119 | (6) |
| 1120 | The value *n* is an integer, or an object implementing |
| 1121 | :meth:`~object.__index__`. Zero and negative values of *n* clear |
| 1122 | the sequence. Items in the sequence are not copied; they are referenced |
| 1123 | multiple times, as explained for ``s * n`` under :ref:`typesseq-common`. |
| 1124 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1125 | |
| 1126 | .. _typesseq-list: |
| 1127 | |
| 1128 | Lists |
| 1129 | ----- |
| 1130 | |
| 1131 | .. index:: object: list |
| 1132 | |
| 1133 | Lists are mutable sequences, typically used to store collections of |
| 1134 | homogeneous items (where the precise degree of similarity will vary by |
| 1135 | application). |
| 1136 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1137 | .. class:: list([iterable]) |
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 | Lists may be constructed in several ways: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1140 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1141 | * Using a pair of square brackets to denote the empty list: ``[]`` |
| 1142 | * Using square brackets, separating items with commas: ``[a]``, ``[a, b, c]`` |
| 1143 | * Using a list comprehension: ``[x for x in iterable]`` |
| 1144 | * Using the type constructor: ``list()`` or ``list(iterable)`` |
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 | The constructor builds a list whose items are the same and in the same |
| 1147 | order as *iterable*'s items. *iterable* may be either a sequence, a |
| 1148 | container that supports iteration, or an iterator object. If *iterable* |
| 1149 | is already a list, a copy is made and returned, similar to ``iterable[:]``. |
| 1150 | For example, ``list('abc')`` returns ``['a', 'b', 'c']`` and |
| 1151 | ``list( (1, 2, 3) )`` returns ``[1, 2, 3]``. |
| 1152 | If no argument is given, the constructor creates a new empty list, ``[]``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1153 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1154 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1155 | Many other operations also produce lists, including the :func:`sorted` |
| 1156 | built-in. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1157 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1158 | Lists implement all of the :ref:`common <typesseq-common>` and |
| 1159 | :ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the |
| 1160 | following additional method: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1161 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1162 | .. method:: list.sort(*, key=None, reverse=None) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1163 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1164 | This method sorts the list in place, using only ``<`` comparisons |
| 1165 | between items. Exceptions are not suppressed - if any comparison operations |
| 1166 | fail, the entire sort operation will fail (and the list will likely be left |
| 1167 | in a partially modified state). |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1168 | |
Zachary Ware | e1391a0 | 2013-11-22 13:58:34 -0600 | [diff] [blame] | 1169 | :meth:`sort` accepts two arguments that can only be passed by keyword |
| 1170 | (:ref:`keyword-only arguments <keyword-only_parameter>`): |
| 1171 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1172 | *key* specifies a function of one argument that is used to extract a |
| 1173 | comparison key from each list element (for example, ``key=str.lower``). |
| 1174 | The key corresponding to each item in the list is calculated once and |
| 1175 | then used for the entire sorting process. The default value of ``None`` |
| 1176 | means that list items are sorted directly without calculating a separate |
| 1177 | key value. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1178 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1179 | The :func:`functools.cmp_to_key` utility is available to convert a 2.x |
| 1180 | style *cmp* function to a *key* function. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1181 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1182 | *reverse* is a boolean value. If set to ``True``, then the list elements |
| 1183 | are sorted as if each comparison were reversed. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1184 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1185 | This method modifies the sequence in place for economy of space when |
| 1186 | sorting a large sequence. To remind users that it operates by side |
| 1187 | effect, it does not return the sorted sequence (use :func:`sorted` to |
| 1188 | explicitly request a new sorted list instance). |
| 1189 | |
| 1190 | The :meth:`sort` method is guaranteed to be stable. A sort is stable if it |
| 1191 | guarantees not to change the relative order of elements that compare equal |
| 1192 | --- this is helpful for sorting in multiple passes (for example, sort by |
| 1193 | department, then by salary grade). |
| 1194 | |
| 1195 | .. impl-detail:: |
| 1196 | |
| 1197 | While a list is being sorted, the effect of attempting to mutate, or even |
| 1198 | inspect, the list is undefined. The C implementation of Python makes the |
| 1199 | list appear empty for the duration, and raises :exc:`ValueError` if it can |
| 1200 | detect that the list has been mutated during a sort. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1201 | |
| 1202 | |
| 1203 | .. _typesseq-tuple: |
| 1204 | |
| 1205 | Tuples |
| 1206 | ------ |
| 1207 | |
| 1208 | .. index:: object: tuple |
| 1209 | |
| 1210 | Tuples are immutable sequences, typically used to store collections of |
| 1211 | heterogeneous data (such as the 2-tuples produced by the :func:`enumerate` |
| 1212 | built-in). Tuples are also used for cases where an immutable sequence of |
| 1213 | homogeneous data is needed (such as allowing storage in a :class:`set` or |
| 1214 | :class:`dict` instance). |
| 1215 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1216 | .. class:: tuple([iterable]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1217 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1218 | Tuples may be constructed in a number of ways: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1219 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1220 | * Using a pair of parentheses to denote the empty tuple: ``()`` |
| 1221 | * Using a trailing comma for a singleton tuple: ``a,`` or ``(a,)`` |
| 1222 | * Separating items with commas: ``a, b, c`` or ``(a, b, c)`` |
| 1223 | * Using the :func:`tuple` built-in: ``tuple()`` or ``tuple(iterable)`` |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1224 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1225 | The constructor builds a tuple whose items are the same and in the same |
| 1226 | order as *iterable*'s items. *iterable* may be either a sequence, a |
| 1227 | container that supports iteration, or an iterator object. If *iterable* |
| 1228 | is already a tuple, it is returned unchanged. For example, |
| 1229 | ``tuple('abc')`` returns ``('a', 'b', 'c')`` and |
| 1230 | ``tuple( [1, 2, 3] )`` returns ``(1, 2, 3)``. |
| 1231 | If no argument is given, the constructor creates a new empty tuple, ``()``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1232 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1233 | Note that it is actually the comma which makes a tuple, not the parentheses. |
| 1234 | The parentheses are optional, except in the empty tuple case, or |
| 1235 | when they are needed to avoid syntactic ambiguity. For example, |
| 1236 | ``f(a, b, c)`` is a function call with three arguments, while |
| 1237 | ``f((a, b, c))`` is a function call with a 3-tuple as the sole argument. |
| 1238 | |
| 1239 | Tuples implement all of the :ref:`common <typesseq-common>` sequence |
| 1240 | operations. |
| 1241 | |
| 1242 | For heterogeneous collections of data where access by name is clearer than |
| 1243 | access by index, :func:`collections.namedtuple` may be a more appropriate |
| 1244 | choice than a simple tuple object. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1245 | |
| 1246 | |
| 1247 | .. _typesseq-range: |
| 1248 | |
| 1249 | Ranges |
| 1250 | ------ |
| 1251 | |
| 1252 | .. index:: object: range |
| 1253 | |
| 1254 | The :class:`range` type represents an immutable sequence of numbers and is |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1255 | commonly used for looping a specific number of times in :keyword:`for` |
| 1256 | loops. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1257 | |
Ezio Melotti | 8429b67 | 2012-09-14 06:35:09 +0300 | [diff] [blame] | 1258 | .. class:: range(stop) |
| 1259 | range(start, stop[, step]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1260 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1261 | The arguments to the range constructor must be integers (either built-in |
| 1262 | :class:`int` or any object that implements the ``__index__`` special |
| 1263 | method). If the *step* argument is omitted, it defaults to ``1``. |
| 1264 | If the *start* argument is omitted, it defaults to ``0``. |
| 1265 | If *step* is zero, :exc:`ValueError` is raised. |
| 1266 | |
| 1267 | For a positive *step*, the contents of a range ``r`` are determined by the |
| 1268 | formula ``r[i] = start + step*i`` where ``i >= 0`` and |
| 1269 | ``r[i] < stop``. |
| 1270 | |
| 1271 | For a negative *step*, the contents of the range are still determined by |
| 1272 | the formula ``r[i] = start + step*i``, but the constraints are ``i >= 0`` |
| 1273 | and ``r[i] > stop``. |
| 1274 | |
Sandro Tosi | 4c1b9f4 | 2013-01-27 00:33:04 +0100 | [diff] [blame] | 1275 | 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] | 1276 | constraint. Ranges do support negative indices, but these are interpreted |
| 1277 | as indexing from the end of the sequence determined by the positive |
| 1278 | indices. |
| 1279 | |
| 1280 | Ranges containing absolute values larger than :data:`sys.maxsize` are |
| 1281 | permitted but some features (such as :func:`len`) may raise |
| 1282 | :exc:`OverflowError`. |
| 1283 | |
| 1284 | Range examples:: |
| 1285 | |
| 1286 | >>> list(range(10)) |
| 1287 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1288 | >>> list(range(1, 11)) |
| 1289 | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 1290 | >>> list(range(0, 30, 5)) |
| 1291 | [0, 5, 10, 15, 20, 25] |
| 1292 | >>> list(range(0, 10, 3)) |
| 1293 | [0, 3, 6, 9] |
| 1294 | >>> list(range(0, -10, -1)) |
| 1295 | [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] |
| 1296 | >>> list(range(0)) |
| 1297 | [] |
| 1298 | >>> list(range(1, 0)) |
| 1299 | [] |
| 1300 | |
| 1301 | Ranges implement all of the :ref:`common <typesseq-common>` sequence operations |
| 1302 | except concatenation and repetition (due to the fact that range objects can |
| 1303 | only represent sequences that follow a strict pattern and repetition and |
| 1304 | concatenation will usually violate that pattern). |
| 1305 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 1306 | .. attribute:: start |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1307 | |
| 1308 | The value of the *start* parameter (or ``0`` if the parameter was |
| 1309 | not supplied) |
| 1310 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 1311 | .. attribute:: stop |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1312 | |
| 1313 | The value of the *stop* parameter |
| 1314 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 1315 | .. attribute:: step |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1316 | |
| 1317 | The value of the *step* parameter (or ``1`` if the parameter was |
| 1318 | not supplied) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1319 | |
| 1320 | The advantage of the :class:`range` type over a regular :class:`list` or |
| 1321 | :class:`tuple` is that a :class:`range` object will always take the same |
| 1322 | (small) amount of memory, no matter the size of the range it represents (as it |
| 1323 | only stores the ``start``, ``stop`` and ``step`` values, calculating individual |
| 1324 | items and subranges as needed). |
| 1325 | |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 1326 | Range objects implement the :class:`collections.abc.Sequence` ABC, and provide |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1327 | features such as containment tests, element index lookup, slicing and |
| 1328 | support for negative indices (see :ref:`typesseq`): |
| 1329 | |
| 1330 | >>> r = range(0, 20, 2) |
| 1331 | >>> r |
| 1332 | range(0, 20, 2) |
| 1333 | >>> 11 in r |
| 1334 | False |
| 1335 | >>> 10 in r |
| 1336 | True |
| 1337 | >>> r.index(10) |
| 1338 | 5 |
| 1339 | >>> r[5] |
| 1340 | 10 |
| 1341 | >>> r[:5] |
| 1342 | range(0, 10, 2) |
| 1343 | >>> r[-1] |
| 1344 | 18 |
| 1345 | |
| 1346 | Testing range objects for equality with ``==`` and ``!=`` compares |
| 1347 | them as sequences. That is, two range objects are considered equal if |
| 1348 | they represent the same sequence of values. (Note that two range |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 1349 | objects that compare equal might have different :attr:`~range.start`, |
| 1350 | :attr:`~range.stop` and :attr:`~range.step` attributes, for example |
| 1351 | ``range(0) == range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.) |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 1352 | |
| 1353 | .. versionchanged:: 3.2 |
| 1354 | Implement the Sequence ABC. |
| 1355 | Support slicing and negative indices. |
| 1356 | Test :class:`int` objects for membership in constant time instead of |
| 1357 | iterating through all items. |
| 1358 | |
| 1359 | .. versionchanged:: 3.3 |
| 1360 | Define '==' and '!=' to compare range objects based on the |
| 1361 | sequence of values they define (instead of comparing based on |
| 1362 | object identity). |
| 1363 | |
| 1364 | .. versionadded:: 3.3 |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 1365 | The :attr:`~range.start`, :attr:`~range.stop` and :attr:`~range.step` |
| 1366 | attributes. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1367 | |
Raymond Hettinger | e256acc | 2016-09-06 16:35:34 -0700 | [diff] [blame] | 1368 | .. seealso:: |
| 1369 | |
| 1370 | * The `linspace recipe <http://code.activestate.com/recipes/579000/>`_ |
| 1371 | shows how to implement a lazy version of range that suitable for floating |
| 1372 | point applications. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1373 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1374 | .. index:: |
| 1375 | single: string; text sequence type |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1376 | single: str (built-in class); (see also string) |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1377 | object: string |
| 1378 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1379 | .. _textseq: |
| 1380 | |
| 1381 | Text Sequence Type --- :class:`str` |
| 1382 | =================================== |
| 1383 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1384 | Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`. |
| 1385 | Strings are immutable |
Chris Jerdonek | c33899b | 2012-10-11 18:57:48 -0700 | [diff] [blame] | 1386 | :ref:`sequences <typesseq>` of Unicode code points. String literals are |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1387 | written in a variety of ways: |
| 1388 | |
| 1389 | * Single quotes: ``'allows embedded "double" quotes'`` |
| 1390 | * Double quotes: ``"allows embedded 'single' quotes"``. |
| 1391 | * Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""`` |
| 1392 | |
| 1393 | Triple quoted strings may span multiple lines - all associated whitespace will |
| 1394 | be included in the string literal. |
| 1395 | |
| 1396 | 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] | 1397 | between them will be implicitly converted to a single string literal. That |
| 1398 | is, ``("spam " "eggs") == "spam eggs"``. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1399 | |
| 1400 | See :ref:`strings` for more about the various forms of string literal, |
| 1401 | including supported escape sequences, and the ``r`` ("raw") prefix that |
| 1402 | disables most escape sequence processing. |
| 1403 | |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1404 | Strings may also be created from other objects using the :class:`str` |
| 1405 | constructor. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1406 | |
| 1407 | Since there is no separate "character" type, indexing a string produces |
| 1408 | strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``. |
| 1409 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 1410 | .. index:: |
| 1411 | object: io.StringIO |
| 1412 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1413 | There is also no mutable string type, but :meth:`str.join` or |
| 1414 | :class:`io.StringIO` can be used to efficiently construct strings from |
| 1415 | multiple fragments. |
| 1416 | |
| 1417 | .. versionchanged:: 3.3 |
| 1418 | For backwards compatibility with the Python 2 series, the ``u`` prefix is |
| 1419 | once again permitted on string literals. It has no effect on the meaning |
| 1420 | of string literals and cannot be combined with the ``r`` prefix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1421 | |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1422 | |
| 1423 | .. index:: |
| 1424 | single: string; str (built-in class) |
| 1425 | |
| 1426 | .. class:: str(object='') |
| 1427 | str(object=b'', encoding='utf-8', errors='strict') |
| 1428 | |
| 1429 | Return a :ref:`string <textseq>` version of *object*. If *object* is not |
| 1430 | provided, returns the empty string. Otherwise, the behavior of ``str()`` |
| 1431 | depends on whether *encoding* or *errors* is given, as follows. |
| 1432 | |
| 1433 | If neither *encoding* nor *errors* is given, ``str(object)`` returns |
| 1434 | :meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely |
| 1435 | printable string representation of *object*. For string objects, this is |
| 1436 | the string itself. If *object* does not have a :meth:`~object.__str__` |
| 1437 | method, then :func:`str` falls back to returning |
| 1438 | :meth:`repr(object) <repr>`. |
| 1439 | |
| 1440 | .. index:: |
| 1441 | single: buffer protocol; str (built-in class) |
| 1442 | single: bytes; str (built-in class) |
| 1443 | |
| 1444 | If at least one of *encoding* or *errors* is given, *object* should be a |
Ezio Melotti | c228e96 | 2013-05-04 18:06:34 +0300 | [diff] [blame] | 1445 | :term:`bytes-like object` (e.g. :class:`bytes` or :class:`bytearray`). In |
| 1446 | this case, if *object* is a :class:`bytes` (or :class:`bytearray`) object, |
| 1447 | then ``str(bytes, encoding, errors)`` is equivalent to |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1448 | :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes |
| 1449 | object underlying the buffer object is obtained before calling |
| 1450 | :meth:`bytes.decode`. See :ref:`binaryseq` and |
| 1451 | :ref:`bufferobjects` for information on buffer objects. |
| 1452 | |
| 1453 | Passing a :class:`bytes` object to :func:`str` without the *encoding* |
| 1454 | or *errors* arguments falls under the first case of returning the informal |
| 1455 | string representation (see also the :option:`-b` command-line option to |
| 1456 | Python). For example:: |
| 1457 | |
| 1458 | >>> str(b'Zoot!') |
| 1459 | "b'Zoot!'" |
| 1460 | |
| 1461 | For more information on the ``str`` class and its methods, see |
| 1462 | :ref:`textseq` and the :ref:`string-methods` section below. To output |
Martin Panter | bc1ee46 | 2016-02-13 00:41:37 +0000 | [diff] [blame] | 1463 | formatted strings, see the :ref:`f-strings` and :ref:`formatstrings` |
| 1464 | sections. In addition, see the :ref:`stringservices` section. |
Chris Jerdonek | bb4e941 | 2012-11-28 01:38:40 -0800 | [diff] [blame] | 1465 | |
| 1466 | |
| 1467 | .. index:: |
| 1468 | pair: string; methods |
| 1469 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1470 | .. _string-methods: |
| 1471 | |
| 1472 | String Methods |
| 1473 | -------------- |
| 1474 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1475 | .. index:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1476 | module: re |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1477 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1478 | Strings implement all of the :ref:`common <typesseq-common>` sequence |
| 1479 | operations, along with the additional methods described below. |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 1480 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 1481 | Strings also support two styles of string formatting, one providing a large |
| 1482 | degree of flexibility and customization (see :meth:`str.format`, |
| 1483 | :ref:`formatstrings` and :ref:`string-formatting`) and the other based on C |
| 1484 | ``printf`` style formatting that handles a narrower range of types and is |
| 1485 | slightly harder to use correctly, but is often faster for the cases it can |
| 1486 | handle (:ref:`old-string-formatting`). |
| 1487 | |
| 1488 | The :ref:`textservices` section of the standard library covers a number of |
| 1489 | other modules that provide various text related utilities (including regular |
| 1490 | expression support in the :mod:`re` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1491 | |
| 1492 | .. method:: str.capitalize() |
| 1493 | |
Senthil Kumaran | fa89798 | 2010-07-05 11:41:42 +0000 | [diff] [blame] | 1494 | 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] | 1495 | rest lowercased. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1496 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1497 | |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 1498 | .. method:: str.casefold() |
| 1499 | |
| 1500 | 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] | 1501 | caseless matching. |
| 1502 | |
| 1503 | Casefolding is similar to lowercasing but more aggressive because it is |
| 1504 | intended to remove all case distinctions in a string. For example, the German |
| 1505 | lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already |
| 1506 | lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold` |
| 1507 | converts it to ``"ss"``. |
| 1508 | |
| 1509 | The casefolding algorithm is described in section 3.13 of the Unicode |
| 1510 | Standard. |
Benjamin Peterson | d5890c8 | 2012-01-14 13:23:30 -0500 | [diff] [blame] | 1511 | |
| 1512 | .. versionadded:: 3.3 |
| 1513 | |
| 1514 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1515 | .. method:: str.center(width[, fillchar]) |
| 1516 | |
| 1517 | Return centered in a string of length *width*. Padding is done using the |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1518 | specified *fillchar* (default is an ASCII space). The original string is |
| 1519 | returned if *width* is less than or equal to ``len(s)``. |
| 1520 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1521 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1522 | |
| 1523 | .. method:: str.count(sub[, start[, end]]) |
| 1524 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 1525 | Return the number of non-overlapping occurrences of substring *sub* in the |
| 1526 | range [*start*, *end*]. Optional arguments *start* and *end* are |
| 1527 | interpreted as in slice notation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1528 | |
| 1529 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 1530 | .. method:: str.encode(encoding="utf-8", errors="strict") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1531 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 1532 | Return an encoded version of the string as a bytes object. Default encoding |
| 1533 | is ``'utf-8'``. *errors* may be given to set a different error handling scheme. |
| 1534 | The default for *errors* is ``'strict'``, meaning that encoding errors raise |
| 1535 | a :exc:`UnicodeError`. Other possible |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 1536 | values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, |
| 1537 | ``'backslashreplace'`` and any other name registered via |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1538 | :func:`codecs.register_error`, see section :ref:`error-handlers`. For a |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 1539 | list of possible encodings, see section :ref:`standard-encodings`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1540 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 1541 | .. versionchanged:: 3.1 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 1542 | Support for keyword arguments added. |
| 1543 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1544 | |
| 1545 | .. method:: str.endswith(suffix[, start[, end]]) |
| 1546 | |
| 1547 | Return ``True`` if the string ends with the specified *suffix*, otherwise return |
| 1548 | ``False``. *suffix* can also be a tuple of suffixes to look for. With optional |
| 1549 | *start*, test beginning at that position. With optional *end*, stop comparing |
| 1550 | at that position. |
| 1551 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1552 | |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 1553 | .. method:: str.expandtabs(tabsize=8) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1554 | |
Ned Deily | bebe91a | 2013-04-21 13:05:21 -0700 | [diff] [blame] | 1555 | Return a copy of the string where all tab characters are replaced by one or |
| 1556 | more spaces, depending on the current column and the given tab size. Tab |
| 1557 | positions occur every *tabsize* characters (default is 8, giving tab |
| 1558 | positions at columns 0, 8, 16 and so on). To expand the string, the current |
| 1559 | column is set to zero and the string is examined character by character. If |
| 1560 | the character is a tab (``\t``), one or more space characters are inserted |
| 1561 | in the result until the current column is equal to the next tab position. |
| 1562 | (The tab character itself is not copied.) If the character is a newline |
| 1563 | (``\n``) or return (``\r``), it is copied and the current column is reset to |
| 1564 | zero. Any other character is copied unchanged and the current column is |
| 1565 | incremented by one regardless of how the character is represented when |
| 1566 | printed. |
| 1567 | |
| 1568 | >>> '01\t012\t0123\t01234'.expandtabs() |
| 1569 | '01 012 0123 01234' |
| 1570 | >>> '01\t012\t0123\t01234'.expandtabs(4) |
| 1571 | '01 012 0123 01234' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1572 | |
| 1573 | |
| 1574 | .. method:: str.find(sub[, start[, end]]) |
| 1575 | |
Senthil Kumaran | 114a1d6 | 2016-01-03 17:57:10 -0800 | [diff] [blame] | 1576 | Return the lowest index in the string where substring *sub* is found within |
| 1577 | the slice ``s[start:end]``. Optional arguments *start* and *end* are |
| 1578 | interpreted as in slice notation. Return ``-1`` if *sub* is not found. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1579 | |
Ezio Melotti | 0ed8c68 | 2011-05-09 03:54:30 +0300 | [diff] [blame] | 1580 | .. note:: |
| 1581 | |
| 1582 | The :meth:`~str.find` method should be used only if you need to know the |
| 1583 | position of *sub*. To check if *sub* is a substring or not, use the |
| 1584 | :keyword:`in` operator:: |
| 1585 | |
| 1586 | >>> 'Py' in 'Python' |
| 1587 | True |
| 1588 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1589 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 1590 | .. method:: str.format(*args, **kwargs) |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1591 | |
Georg Brandl | 1f70cdf | 2010-03-21 09:04:24 +0000 | [diff] [blame] | 1592 | Perform a string formatting operation. The string on which this method is |
| 1593 | called can contain literal text or replacement fields delimited by braces |
| 1594 | ``{}``. Each replacement field contains either the numeric index of a |
| 1595 | positional argument, or the name of a keyword argument. Returns a copy of |
| 1596 | the string where each replacement field is replaced with the string value of |
| 1597 | the corresponding argument. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1598 | |
| 1599 | >>> "The sum of 1 + 2 is {0}".format(1+2) |
| 1600 | 'The sum of 1 + 2 is 3' |
| 1601 | |
| 1602 | See :ref:`formatstrings` for a description of the various formatting options |
| 1603 | that can be specified in format strings. |
| 1604 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 1605 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1606 | .. method:: str.format_map(mapping) |
| 1607 | |
Éric Araujo | 2642ad0 | 2010-11-06 04:59:27 +0000 | [diff] [blame] | 1608 | Similar to ``str.format(**mapping)``, except that ``mapping`` is |
Serhiy Storchaka | a4d170d | 2013-12-23 18:20:51 +0200 | [diff] [blame] | 1609 | used directly and not copied to a :class:`dict`. This is useful |
Eric Smith | 5ad85f8 | 2010-11-06 13:22:13 +0000 | [diff] [blame] | 1610 | if for example ``mapping`` is a dict subclass: |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1611 | |
Eric Smith | 5ad85f8 | 2010-11-06 13:22:13 +0000 | [diff] [blame] | 1612 | >>> class Default(dict): |
| 1613 | ... def __missing__(self, key): |
| 1614 | ... return key |
| 1615 | ... |
| 1616 | >>> '{name} was born in {country}'.format_map(Default(name='Guido')) |
| 1617 | 'Guido was born in country' |
| 1618 | |
| 1619 | .. versionadded:: 3.2 |
| 1620 | |
Eric Smith | 27bbca6 | 2010-11-04 17:06:58 +0000 | [diff] [blame] | 1621 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1622 | .. method:: str.index(sub[, start[, end]]) |
| 1623 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1624 | Like :meth:`~str.find`, but raise :exc:`ValueError` when the substring is |
| 1625 | not found. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1626 | |
| 1627 | |
| 1628 | .. method:: str.isalnum() |
| 1629 | |
| 1630 | 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] | 1631 | least one character, false otherwise. A character ``c`` is alphanumeric if one |
| 1632 | of the following returns ``True``: ``c.isalpha()``, ``c.isdecimal()``, |
| 1633 | ``c.isdigit()``, or ``c.isnumeric()``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1634 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1635 | |
| 1636 | .. method:: str.isalpha() |
| 1637 | |
| 1638 | 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] | 1639 | one character, false otherwise. Alphabetic characters are those characters defined |
| 1640 | in the Unicode character database as "Letter", i.e., those with general category |
| 1641 | property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different |
| 1642 | from the "Alphabetic" property defined in the Unicode Standard. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1643 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1644 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1645 | .. method:: str.isdecimal() |
| 1646 | |
| 1647 | Return true if all characters in the string are decimal |
| 1648 | characters and there is at least one character, false |
Martin Panter | 49c14d8 | 2016-12-11 01:08:25 +0000 | [diff] [blame] | 1649 | otherwise. Decimal characters are those that can be used to form |
| 1650 | numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT |
| 1651 | ZERO. Formally a decimal character is a character in the Unicode |
| 1652 | General Category "Nd". |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1653 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1654 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1655 | .. method:: str.isdigit() |
| 1656 | |
| 1657 | 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] | 1658 | character, false otherwise. Digits include decimal characters and digits that need |
Martin Panter | 49c14d8 | 2016-12-11 01:08:25 +0000 | [diff] [blame] | 1659 | special handling, such as the compatibility superscript digits. |
| 1660 | This covers digits which cannot be used to form numbers in base 10, |
| 1661 | like the Kharosthi numbers. Formally, a digit is a character that has the |
| 1662 | property value Numeric_Type=Digit or Numeric_Type=Decimal. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1663 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1664 | |
| 1665 | .. method:: str.isidentifier() |
| 1666 | |
| 1667 | 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] | 1668 | definition, section :ref:`identifiers`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1669 | |
Raymond Hettinger | 378170d | 2013-03-23 08:21:12 -0700 | [diff] [blame] | 1670 | Use :func:`keyword.iskeyword` to test for reserved identifiers such as |
| 1671 | :keyword:`def` and :keyword:`class`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1672 | |
| 1673 | .. method:: str.islower() |
| 1674 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1675 | Return true if all cased characters [4]_ in the string are lowercase and |
| 1676 | there is at least one cased character, false otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1677 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1678 | |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1679 | .. method:: str.isnumeric() |
| 1680 | |
| 1681 | Return true if all characters in the string are numeric |
| 1682 | characters, and there is at least one character, false |
| 1683 | otherwise. Numeric characters include digit characters, and all characters |
| 1684 | that have the Unicode numeric value property, e.g. U+2155, |
Alexander Belopolsky | 0d26798 | 2010-12-23 02:58:25 +0000 | [diff] [blame] | 1685 | VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property |
| 1686 | value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric. |
Mark Summerfield | bbfd71d | 2008-07-01 15:50:04 +0000 | [diff] [blame] | 1687 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1688 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 1689 | .. method:: str.isprintable() |
| 1690 | |
| 1691 | Return true if all characters in the string are printable or the string is |
| 1692 | empty, false otherwise. Nonprintable characters are those characters defined |
| 1693 | in the Unicode character database as "Other" or "Separator", excepting the |
| 1694 | ASCII space (0x20) which is considered printable. (Note that printable |
| 1695 | characters in this context are those which should not be escaped when |
| 1696 | :func:`repr` is invoked on a string. It has no bearing on the handling of |
| 1697 | strings written to :data:`sys.stdout` or :data:`sys.stderr`.) |
| 1698 | |
| 1699 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1700 | .. method:: str.isspace() |
| 1701 | |
| 1702 | 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] | 1703 | at least one character, false otherwise. Whitespace characters are those |
| 1704 | characters defined in the Unicode character database as "Other" or "Separator" |
| 1705 | and those with bidirectional property being one of "WS", "B", or "S". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1706 | |
| 1707 | .. method:: str.istitle() |
| 1708 | |
| 1709 | Return true if the string is a titlecased string and there is at least one |
| 1710 | character, for example uppercase characters may only follow uncased characters |
| 1711 | and lowercase characters only cased ones. Return false otherwise. |
| 1712 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1713 | |
| 1714 | .. method:: str.isupper() |
| 1715 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1716 | Return true if all cased characters [4]_ in the string are uppercase and |
| 1717 | there is at least one cased character, false otherwise. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1718 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1719 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1720 | .. method:: str.join(iterable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1721 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1722 | Return a string which is the concatenation of the strings in the |
| 1723 | :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] | 1724 | any non-string values in *iterable*, including :class:`bytes` objects. The |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 1725 | separator between elements is the string providing this method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1726 | |
| 1727 | |
| 1728 | .. method:: str.ljust(width[, fillchar]) |
| 1729 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1730 | Return the string left justified in a string of length *width*. Padding is |
| 1731 | done using the specified *fillchar* (default is an ASCII space). The |
| 1732 | original string is returned if *width* is less than or equal to ``len(s)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1733 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1734 | |
| 1735 | .. method:: str.lower() |
| 1736 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 1737 | Return a copy of the string with all the cased characters [4]_ converted to |
| 1738 | lowercase. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1739 | |
Benjamin Peterson | 9430354 | 2012-01-18 23:09:32 -0500 | [diff] [blame] | 1740 | The lowercasing algorithm used is described in section 3.13 of the Unicode |
| 1741 | Standard. |
| 1742 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1743 | |
| 1744 | .. method:: str.lstrip([chars]) |
| 1745 | |
| 1746 | Return a copy of the string with leading characters removed. The *chars* |
| 1747 | argument is a string specifying the set of characters to be removed. If omitted |
| 1748 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1749 | 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] | 1750 | |
| 1751 | >>> ' spacious '.lstrip() |
| 1752 | 'spacious ' |
| 1753 | >>> 'www.example.com'.lstrip('cmowz.') |
| 1754 | 'example.com' |
| 1755 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1756 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1757 | .. staticmethod:: str.maketrans(x[, y[, z]]) |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1758 | |
| 1759 | This static method returns a translation table usable for :meth:`str.translate`. |
| 1760 | |
| 1761 | If there is only one argument, it must be a dictionary mapping Unicode |
| 1762 | ordinals (integers) or characters (strings of length 1) to Unicode ordinals, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 1763 | strings (of arbitrary lengths) or ``None``. Character keys will then be |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1764 | converted to ordinals. |
| 1765 | |
| 1766 | If there are two arguments, they must be strings of equal length, and in the |
| 1767 | resulting dictionary, each character in x will be mapped to the character at |
| 1768 | the same position in y. If there is a third argument, it must be a string, |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 1769 | whose characters will be mapped to ``None`` in the result. |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 1770 | |
| 1771 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1772 | .. method:: str.partition(sep) |
| 1773 | |
| 1774 | Split the string at the first occurrence of *sep*, and return a 3-tuple |
| 1775 | containing the part before the separator, the separator itself, and the part |
| 1776 | after the separator. If the separator is not found, return a 3-tuple containing |
| 1777 | the string itself, followed by two empty strings. |
| 1778 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1779 | |
| 1780 | .. method:: str.replace(old, new[, count]) |
| 1781 | |
| 1782 | Return a copy of the string with all occurrences of substring *old* replaced by |
| 1783 | *new*. If the optional argument *count* is given, only the first *count* |
| 1784 | occurrences are replaced. |
| 1785 | |
| 1786 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1787 | .. method:: str.rfind(sub[, start[, end]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1788 | |
Benjamin Peterson | d99cd81 | 2010-04-27 22:58:50 +0000 | [diff] [blame] | 1789 | Return the highest index in the string where substring *sub* is found, such |
| 1790 | that *sub* is contained within ``s[start:end]``. Optional arguments *start* |
| 1791 | and *end* are interpreted as in slice notation. Return ``-1`` on failure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1792 | |
| 1793 | |
| 1794 | .. method:: str.rindex(sub[, start[, end]]) |
| 1795 | |
| 1796 | Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not |
| 1797 | found. |
| 1798 | |
| 1799 | |
| 1800 | .. method:: str.rjust(width[, fillchar]) |
| 1801 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1802 | Return the string right justified in a string of length *width*. Padding is |
| 1803 | done using the specified *fillchar* (default is an ASCII space). The |
| 1804 | original string is returned if *width* is less than or equal to ``len(s)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1805 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1806 | |
| 1807 | .. method:: str.rpartition(sep) |
| 1808 | |
| 1809 | Split the string at the last occurrence of *sep*, and return a 3-tuple |
| 1810 | containing the part before the separator, the separator itself, and the part |
| 1811 | after the separator. If the separator is not found, return a 3-tuple containing |
| 1812 | two empty strings, followed by the string itself. |
| 1813 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1814 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1815 | .. method:: str.rsplit(sep=None, maxsplit=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1816 | |
| 1817 | Return a list of the words in the string, using *sep* as the delimiter string. |
| 1818 | If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost* |
| 1819 | ones. If *sep* is not specified or ``None``, any whitespace string is a |
| 1820 | separator. Except for splitting from the right, :meth:`rsplit` behaves like |
| 1821 | :meth:`split` which is described in detail below. |
| 1822 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1823 | |
| 1824 | .. method:: str.rstrip([chars]) |
| 1825 | |
| 1826 | Return a copy of the string with trailing characters removed. The *chars* |
| 1827 | argument is a string specifying the set of characters to be removed. If omitted |
| 1828 | or ``None``, the *chars* argument defaults to removing whitespace. The *chars* |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1829 | 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] | 1830 | |
| 1831 | >>> ' spacious '.rstrip() |
| 1832 | ' spacious' |
| 1833 | >>> 'mississippi'.rstrip('ipz') |
| 1834 | 'mississ' |
| 1835 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1836 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1837 | .. method:: str.split(sep=None, maxsplit=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1838 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1839 | Return a list of the words in the string, using *sep* as the delimiter |
| 1840 | string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, |
| 1841 | 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] | 1842 | specified or ``-1``, then there is no limit on the number of splits |
| 1843 | (all possible splits are made). |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1844 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 1845 | If *sep* is given, consecutive delimiters are not grouped together and are |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1846 | deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns |
| 1847 | ``['1', '', '2']``). The *sep* argument may consist of multiple characters |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 1848 | (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``). |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 1849 | Splitting an empty string with a specified separator returns ``['']``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1850 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1851 | For example:: |
| 1852 | |
| 1853 | >>> '1,2,3'.split(',') |
| 1854 | ['1', '2', '3'] |
| 1855 | >>> '1,2,3'.split(',', maxsplit=1) |
Benjamin Peterson | eb83ffe | 2014-09-22 22:43:50 -0400 | [diff] [blame] | 1856 | ['1', '2,3'] |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1857 | >>> '1,2,,3,'.split(',') |
| 1858 | ['1', '2', '', '3', ''] |
| 1859 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1860 | 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] | 1861 | applied: runs of consecutive whitespace are regarded as a single separator, |
| 1862 | and the result will contain no empty strings at the start or end if the |
| 1863 | string has leading or trailing whitespace. Consequently, splitting an empty |
| 1864 | string or a string consisting of just whitespace with a ``None`` separator |
| 1865 | returns ``[]``. |
| 1866 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1867 | For example:: |
| 1868 | |
| 1869 | >>> '1 2 3'.split() |
| 1870 | ['1', '2', '3'] |
| 1871 | >>> '1 2 3'.split(maxsplit=1) |
| 1872 | ['1', '2 3'] |
| 1873 | >>> ' 1 2 3 '.split() |
| 1874 | ['1', '2', '3'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1875 | |
| 1876 | |
R David Murray | 1b00f25 | 2012-08-15 10:43:58 -0400 | [diff] [blame] | 1877 | .. index:: |
| 1878 | single: universal newlines; str.splitlines method |
| 1879 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1880 | .. method:: str.splitlines([keepends]) |
| 1881 | |
Benjamin Peterson | 8218bd4 | 2015-03-31 21:20:36 -0400 | [diff] [blame] | 1882 | Return a list of the lines in the string, breaking at line boundaries. Line |
| 1883 | breaks are not included in the resulting list unless *keepends* is given and |
| 1884 | true. |
| 1885 | |
| 1886 | This method splits on the following line boundaries. In particular, the |
| 1887 | boundaries are a superset of :term:`universal newlines`. |
| 1888 | |
| 1889 | +-----------------------+-----------------------------+ |
| 1890 | | Representation | Description | |
| 1891 | +=======================+=============================+ |
| 1892 | | ``\n`` | Line Feed | |
| 1893 | +-----------------------+-----------------------------+ |
| 1894 | | ``\r`` | Carriage Return | |
| 1895 | +-----------------------+-----------------------------+ |
| 1896 | | ``\r\n`` | Carriage Return + Line Feed | |
| 1897 | +-----------------------+-----------------------------+ |
| 1898 | | ``\v`` or ``\x0b`` | Line Tabulation | |
| 1899 | +-----------------------+-----------------------------+ |
| 1900 | | ``\f`` or ``\x0c`` | Form Feed | |
| 1901 | +-----------------------+-----------------------------+ |
| 1902 | | ``\x1c`` | File Separator | |
| 1903 | +-----------------------+-----------------------------+ |
| 1904 | | ``\x1d`` | Group Separator | |
| 1905 | +-----------------------+-----------------------------+ |
| 1906 | | ``\x1e`` | Record Separator | |
| 1907 | +-----------------------+-----------------------------+ |
| 1908 | | ``\x85`` | Next Line (C1 Control Code) | |
| 1909 | +-----------------------+-----------------------------+ |
| 1910 | | ``\u2028`` | Line Separator | |
| 1911 | +-----------------------+-----------------------------+ |
| 1912 | | ``\u2029`` | Paragraph Separator | |
| 1913 | +-----------------------+-----------------------------+ |
| 1914 | |
| 1915 | .. versionchanged:: 3.2 |
| 1916 | |
| 1917 | ``\v`` and ``\f`` added to list of line boundaries. |
R David Murray | ae1b94b | 2012-06-01 16:19:36 -0400 | [diff] [blame] | 1918 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1919 | For example:: |
| 1920 | |
| 1921 | >>> 'ab c\n\nde fg\rkl\r\n'.splitlines() |
Larry Hastings | c6256e5 | 2014-10-05 19:03:48 -0700 | [diff] [blame] | 1922 | ['ab c', '', 'de fg', 'kl'] |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1923 | >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) |
| 1924 | ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1925 | |
R David Murray | 05c35a6 | 2012-08-06 16:08:09 -0400 | [diff] [blame] | 1926 | Unlike :meth:`~str.split` when a delimiter string *sep* is given, this |
| 1927 | method returns an empty list for the empty string, and a terminal line |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1928 | break does not result in an extra line:: |
| 1929 | |
| 1930 | >>> "".splitlines() |
| 1931 | [] |
| 1932 | >>> "One line\n".splitlines() |
| 1933 | ['One line'] |
| 1934 | |
| 1935 | For comparison, ``split('\n')`` gives:: |
| 1936 | |
| 1937 | >>> ''.split('\n') |
| 1938 | [''] |
| 1939 | >>> 'Two lines\n'.split('\n') |
| 1940 | ['Two lines', ''] |
R David Murray | 05c35a6 | 2012-08-06 16:08:09 -0400 | [diff] [blame] | 1941 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1942 | |
| 1943 | .. method:: str.startswith(prefix[, start[, end]]) |
| 1944 | |
| 1945 | Return ``True`` if string starts with the *prefix*, otherwise return ``False``. |
| 1946 | *prefix* can also be a tuple of prefixes to look for. With optional *start*, |
| 1947 | test string beginning at that position. With optional *end*, stop comparing |
| 1948 | string at that position. |
| 1949 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1950 | |
| 1951 | .. method:: str.strip([chars]) |
| 1952 | |
| 1953 | Return a copy of the string with the leading and trailing characters removed. |
| 1954 | The *chars* argument is a string specifying the set of characters to be removed. |
| 1955 | If omitted or ``None``, the *chars* argument defaults to removing whitespace. |
| 1956 | The *chars* argument is not a prefix or suffix; rather, all combinations of its |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1957 | values are stripped:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1958 | |
| 1959 | >>> ' spacious '.strip() |
| 1960 | 'spacious' |
| 1961 | >>> 'www.example.com'.strip('cmowz.') |
| 1962 | 'example' |
| 1963 | |
Raymond Hettinger | 19cfb57 | 2015-05-23 09:11:55 -0700 | [diff] [blame] | 1964 | The outermost leading and trailing *chars* argument values are stripped |
| 1965 | from the string. Characters are removed from the leading end until |
| 1966 | reaching a string character that is not contained in the set of |
| 1967 | characters in *chars*. A similar action takes place on the trailing end. |
| 1968 | For example:: |
| 1969 | |
| 1970 | >>> comment_string = '#....... Section 3.2.1 Issue #32 .......' |
| 1971 | >>> comment_string.strip('.#! ') |
| 1972 | 'Section 3.2.1 Issue #32' |
| 1973 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1974 | |
| 1975 | .. method:: str.swapcase() |
| 1976 | |
| 1977 | 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] | 1978 | vice versa. Note that it is not necessarily true that |
| 1979 | ``s.swapcase().swapcase() == s``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1980 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1981 | |
| 1982 | .. method:: str.title() |
| 1983 | |
Raymond Hettinger | b8b0ba1 | 2009-09-29 06:22:28 +0000 | [diff] [blame] | 1984 | Return a titlecased version of the string where words start with an uppercase |
| 1985 | character and the remaining characters are lowercase. |
| 1986 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 1987 | For example:: |
| 1988 | |
| 1989 | >>> 'Hello world'.title() |
| 1990 | 'Hello World' |
| 1991 | |
Raymond Hettinger | b8b0ba1 | 2009-09-29 06:22:28 +0000 | [diff] [blame] | 1992 | The algorithm uses a simple language-independent definition of a word as |
| 1993 | groups of consecutive letters. The definition works in many contexts but |
| 1994 | it means that apostrophes in contractions and possessives form word |
| 1995 | boundaries, which may not be the desired result:: |
| 1996 | |
| 1997 | >>> "they're bill's friends from the UK".title() |
| 1998 | "They'Re Bill'S Friends From The Uk" |
| 1999 | |
| 2000 | A workaround for apostrophes can be constructed using regular expressions:: |
| 2001 | |
| 2002 | >>> import re |
| 2003 | >>> def titlecase(s): |
Andrew Svetlov | 5c90436 | 2012-11-08 17:26:53 +0200 | [diff] [blame] | 2004 | ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", |
| 2005 | ... lambda mo: mo.group(0)[0].upper() + |
| 2006 | ... mo.group(0)[1:].lower(), |
| 2007 | ... s) |
| 2008 | ... |
Raymond Hettinger | b8b0ba1 | 2009-09-29 06:22:28 +0000 | [diff] [blame] | 2009 | >>> titlecase("they're bill's friends.") |
| 2010 | "They're Bill's Friends." |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2011 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2012 | |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 2013 | .. method:: str.translate(table) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2014 | |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 2015 | Return a copy of the string in which each character has been mapped through |
| 2016 | the given translation table. The table must be an object that implements |
| 2017 | indexing via :meth:`__getitem__`, typically a :term:`mapping` or |
| 2018 | :term:`sequence`. When indexed by a Unicode ordinal (an integer), the |
| 2019 | table object can do any of the following: return a Unicode ordinal or a |
| 2020 | string, to map the character to one or more other characters; return |
| 2021 | ``None``, to delete the character from the return string; or raise a |
| 2022 | :exc:`LookupError` exception, to map the character to itself. |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 2023 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2024 | You can use :meth:`str.maketrans` to create a translation map from |
| 2025 | character-to-character mappings in different formats. |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 2026 | |
Zachary Ware | 79b98df | 2015-08-05 23:54:15 -0500 | [diff] [blame] | 2027 | See also the :mod:`codecs` module for a more flexible approach to custom |
| 2028 | character mappings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2029 | |
| 2030 | |
| 2031 | .. method:: str.upper() |
| 2032 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 2033 | Return a copy of the string with all the cased characters [4]_ converted to |
| 2034 | uppercase. Note that ``str.upper().isupper()`` might be ``False`` if ``s`` |
| 2035 | contains uncased characters or if the Unicode category of the resulting |
Benjamin Peterson | 9430354 | 2012-01-18 23:09:32 -0500 | [diff] [blame] | 2036 | character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, |
| 2037 | titlecase). |
| 2038 | |
| 2039 | The uppercasing algorithm used is described in section 3.13 of the Unicode |
| 2040 | Standard. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2041 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2042 | |
| 2043 | .. method:: str.zfill(width) |
| 2044 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2045 | Return a copy of the string left filled with ASCII ``'0'`` digits to |
Tim Golden | 42c235e | 2015-04-06 11:04:49 +0100 | [diff] [blame] | 2046 | make a string of length *width*. A leading sign prefix (``'+'``/``'-'``) |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2047 | is handled by inserting the padding *after* the sign character rather |
| 2048 | than before. The original string is returned if *width* is less than |
| 2049 | or equal to ``len(s)``. |
| 2050 | |
| 2051 | For example:: |
| 2052 | |
| 2053 | >>> "42".zfill(5) |
| 2054 | '00042' |
| 2055 | >>> "-42".zfill(5) |
| 2056 | '-0042' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 2057 | |
| 2058 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2059 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 2060 | .. _old-string-formatting: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2061 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2062 | ``printf``-style String Formatting |
| 2063 | ---------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2064 | |
| 2065 | .. index:: |
| 2066 | single: formatting, string (%) |
| 2067 | single: interpolation, string (%) |
Martin Panter | bc1ee46 | 2016-02-13 00:41:37 +0000 | [diff] [blame] | 2068 | single: string; formatting, printf |
| 2069 | single: string; interpolation, printf |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2070 | single: printf-style formatting |
| 2071 | single: sprintf-style formatting |
| 2072 | single: % formatting |
| 2073 | single: % interpolation |
| 2074 | |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 2075 | .. note:: |
| 2076 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2077 | The formatting operations described here exhibit a variety of quirks that |
| 2078 | lead to a number of common errors (such as failing to display tuples and |
Martin Panter | bc1ee46 | 2016-02-13 00:41:37 +0000 | [diff] [blame] | 2079 | dictionaries correctly). Using the newer :ref:`formatted |
| 2080 | string literals <f-strings>` or the :meth:`str.format` interface |
| 2081 | helps avoid these errors. These alternatives also provide more powerful, |
| 2082 | flexible and extensible approaches to formatting text. |
Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 2083 | |
| 2084 | String objects have one unique built-in operation: the ``%`` operator (modulo). |
| 2085 | This is also known as the string *formatting* or *interpolation* operator. |
| 2086 | Given ``format % values`` (where *format* is a string), ``%`` conversion |
| 2087 | specifications in *format* are replaced with zero or more elements of *values*. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2088 | 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] | 2089 | |
| 2090 | 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] | 2091 | 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] | 2092 | items specified by the format string, or a single mapping object (for example, a |
| 2093 | dictionary). |
| 2094 | |
| 2095 | A conversion specifier contains two or more characters and has the following |
| 2096 | components, which must occur in this order: |
| 2097 | |
| 2098 | #. The ``'%'`` character, which marks the start of the specifier. |
| 2099 | |
| 2100 | #. Mapping key (optional), consisting of a parenthesised sequence of characters |
| 2101 | (for example, ``(somename)``). |
| 2102 | |
| 2103 | #. Conversion flags (optional), which affect the result of some conversion |
| 2104 | types. |
| 2105 | |
| 2106 | #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the |
| 2107 | actual width is read from the next element of the tuple in *values*, and the |
| 2108 | object to convert comes after the minimum field width and optional precision. |
| 2109 | |
| 2110 | #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2111 | specified as ``'*'`` (an asterisk), the actual precision is read from the next |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2112 | element of the tuple in *values*, and the value to convert comes after the |
| 2113 | precision. |
| 2114 | |
| 2115 | #. Length modifier (optional). |
| 2116 | |
| 2117 | #. Conversion type. |
| 2118 | |
| 2119 | When the right argument is a dictionary (or other mapping type), then the |
| 2120 | formats in the string *must* include a parenthesised mapping key into that |
| 2121 | dictionary inserted immediately after the ``'%'`` character. The mapping key |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 2122 | selects the value to be formatted from the mapping. For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2123 | |
Georg Brandl | edc9e7f | 2010-10-17 09:19:03 +0000 | [diff] [blame] | 2124 | >>> print('%(language)s has %(number)03d quote types.' % |
| 2125 | ... {'language': "Python", "number": 2}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2126 | Python has 002 quote types. |
| 2127 | |
| 2128 | In this case no ``*`` specifiers may occur in a format (since they require a |
| 2129 | sequential parameter list). |
| 2130 | |
| 2131 | The conversion flag characters are: |
| 2132 | |
| 2133 | +---------+---------------------------------------------------------------------+ |
| 2134 | | Flag | Meaning | |
| 2135 | +=========+=====================================================================+ |
| 2136 | | ``'#'`` | The value conversion will use the "alternate form" (where defined | |
| 2137 | | | below). | |
| 2138 | +---------+---------------------------------------------------------------------+ |
| 2139 | | ``'0'`` | The conversion will be zero padded for numeric values. | |
| 2140 | +---------+---------------------------------------------------------------------+ |
| 2141 | | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | |
| 2142 | | | conversion if both are given). | |
| 2143 | +---------+---------------------------------------------------------------------+ |
| 2144 | | ``' '`` | (a space) A blank should be left before a positive number (or empty | |
| 2145 | | | string) produced by a signed conversion. | |
| 2146 | +---------+---------------------------------------------------------------------+ |
| 2147 | | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | |
| 2148 | | | (overrides a "space" flag). | |
| 2149 | +---------+---------------------------------------------------------------------+ |
| 2150 | |
| 2151 | 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] | 2152 | 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] | 2153 | |
| 2154 | The conversion types are: |
| 2155 | |
| 2156 | +------------+-----------------------------------------------------+-------+ |
| 2157 | | Conversion | Meaning | Notes | |
| 2158 | +============+=====================================================+=======+ |
| 2159 | | ``'d'`` | Signed integer decimal. | | |
| 2160 | +------------+-----------------------------------------------------+-------+ |
| 2161 | | ``'i'`` | Signed integer decimal. | | |
| 2162 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2163 | | ``'o'`` | Signed octal value. | \(1) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2164 | +------------+-----------------------------------------------------+-------+ |
Berker Peksag | 7b440df | 2016-12-15 05:37:56 +0300 | [diff] [blame] | 2165 | | ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(6) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2166 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2167 | | ``'x'`` | Signed hexadecimal (lowercase). | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2168 | +------------+-----------------------------------------------------+-------+ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2169 | | ``'X'`` | Signed hexadecimal (uppercase). | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2170 | +------------+-----------------------------------------------------+-------+ |
| 2171 | | ``'e'`` | Floating point exponential format (lowercase). | \(3) | |
| 2172 | +------------+-----------------------------------------------------+-------+ |
| 2173 | | ``'E'`` | Floating point exponential format (uppercase). | \(3) | |
| 2174 | +------------+-----------------------------------------------------+-------+ |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 2175 | | ``'f'`` | Floating point decimal format. | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2176 | +------------+-----------------------------------------------------+-------+ |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 2177 | | ``'F'`` | Floating point decimal format. | \(3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2178 | +------------+-----------------------------------------------------+-------+ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 2179 | | ``'g'`` | Floating point format. Uses lowercase exponential | \(4) | |
| 2180 | | | format if exponent is less than -4 or not less than | | |
| 2181 | | | precision, decimal format otherwise. | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2182 | +------------+-----------------------------------------------------+-------+ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 2183 | | ``'G'`` | Floating point format. Uses uppercase exponential | \(4) | |
| 2184 | | | format if exponent is less than -4 or not less than | | |
| 2185 | | | precision, decimal format otherwise. | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2186 | +------------+-----------------------------------------------------+-------+ |
| 2187 | | ``'c'`` | Single character (accepts integer or single | | |
| 2188 | | | character string). | | |
| 2189 | +------------+-----------------------------------------------------+-------+ |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 2190 | | ``'r'`` | String (converts any Python object using | \(5) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2191 | | | :func:`repr`). | | |
| 2192 | +------------+-----------------------------------------------------+-------+ |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2193 | | ``'s'`` | String (converts any Python object using | \(5) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2194 | | | :func:`str`). | | |
| 2195 | +------------+-----------------------------------------------------+-------+ |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2196 | | ``'a'`` | String (converts any Python object using | \(5) | |
| 2197 | | | :func:`ascii`). | | |
| 2198 | +------------+-----------------------------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2199 | | ``'%'`` | No argument is converted, results in a ``'%'`` | | |
| 2200 | | | character in the result. | | |
| 2201 | +------------+-----------------------------------------------------+-------+ |
| 2202 | |
| 2203 | Notes: |
| 2204 | |
| 2205 | (1) |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 2206 | The alternate form causes a leading octal specifier (``'0o'``) to be |
| 2207 | inserted before the first digit. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2208 | |
| 2209 | (2) |
| 2210 | The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 2211 | the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2212 | |
| 2213 | (3) |
| 2214 | The alternate form causes the result to always contain a decimal point, even if |
| 2215 | no digits follow it. |
| 2216 | |
| 2217 | The precision determines the number of digits after the decimal point and |
| 2218 | defaults to 6. |
| 2219 | |
| 2220 | (4) |
| 2221 | The alternate form causes the result to always contain a decimal point, and |
| 2222 | trailing zeroes are not removed as they would otherwise be. |
| 2223 | |
| 2224 | The precision determines the number of significant digits before and after the |
| 2225 | decimal point and defaults to 6. |
| 2226 | |
| 2227 | (5) |
Eli Bendersky | ef4902a | 2011-07-29 09:30:42 +0300 | [diff] [blame] | 2228 | If precision is ``N``, the output is truncated to ``N`` characters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2229 | |
Berker Peksag | 7b440df | 2016-12-15 05:37:56 +0300 | [diff] [blame] | 2230 | (6) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 2231 | See :pep:`237`. |
| 2232 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2233 | Since Python strings have an explicit length, ``%s`` conversions do not assume |
| 2234 | that ``'\0'`` is the end of the string. |
| 2235 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 2236 | .. XXX Examples? |
| 2237 | |
Mark Dickinson | 33841c3 | 2009-05-01 15:37:04 +0000 | [diff] [blame] | 2238 | .. versionchanged:: 3.1 |
| 2239 | ``%f`` conversions for numbers whose absolute value is over 1e50 are no |
| 2240 | longer replaced by ``%g`` conversions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2241 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2242 | |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 2243 | .. index:: |
| 2244 | single: buffer protocol; binary sequence types |
| 2245 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2246 | .. _binaryseq: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2247 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2248 | Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview` |
| 2249 | ================================================================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2250 | |
| 2251 | .. index:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2252 | object: bytes |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2253 | object: bytearray |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2254 | object: memoryview |
| 2255 | module: array |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2256 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2257 | The core built-in types for manipulating binary data are :class:`bytes` and |
| 2258 | :class:`bytearray`. They are supported by :class:`memoryview` which uses |
Chris Jerdonek | 5fae0e5 | 2012-11-20 17:45:51 -0800 | [diff] [blame] | 2259 | the :ref:`buffer protocol <bufferobjects>` to access the memory of other |
| 2260 | binary objects without needing to make a copy. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2261 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2262 | The :mod:`array` module supports efficient storage of basic data types like |
| 2263 | 32-bit integers and IEEE754 double-precision floating values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2264 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2265 | .. _typebytes: |
Senthil Kumaran | 7cafd26 | 2010-10-02 03:16:04 +0000 | [diff] [blame] | 2266 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2267 | Bytes |
| 2268 | ----- |
| 2269 | |
| 2270 | .. index:: object: bytes |
| 2271 | |
| 2272 | Bytes objects are immutable sequences of single bytes. Since many major |
| 2273 | binary protocols are based on the ASCII text encoding, bytes objects offer |
| 2274 | several methods that are only valid when working with ASCII compatible |
| 2275 | data and are closely related to string objects in a variety of other ways. |
| 2276 | |
| 2277 | Firstly, the syntax for bytes literals is largely the same as that for string |
| 2278 | literals, except that a ``b`` prefix is added: |
| 2279 | |
| 2280 | * Single quotes: ``b'still allows embedded "double" quotes'`` |
| 2281 | * Double quotes: ``b"still allows embedded 'single' quotes"``. |
| 2282 | * Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""`` |
| 2283 | |
| 2284 | Only ASCII characters are permitted in bytes literals (regardless of the |
| 2285 | declared source code encoding). Any binary values over 127 must be entered |
| 2286 | into bytes literals using the appropriate escape sequence. |
| 2287 | |
| 2288 | As with string literals, bytes literals may also use a ``r`` prefix to disable |
| 2289 | processing of escape sequences. See :ref:`strings` for more about the various |
| 2290 | forms of bytes literal, including supported escape sequences. |
| 2291 | |
| 2292 | While bytes literals and representations are based on ASCII text, bytes |
| 2293 | objects actually behave like immutable sequences of integers, with each |
| 2294 | value in the sequence restricted such that ``0 <= x < 256`` (attempts to |
| 2295 | violate this restriction will trigger :exc:`ValueError`. This is done |
| 2296 | deliberately to emphasise that while many binary formats include ASCII based |
| 2297 | elements and can be usefully manipulated with some text-oriented algorithms, |
| 2298 | this is not generally the case for arbitrary binary data (blindly applying |
| 2299 | text processing algorithms to binary data formats that are not ASCII |
| 2300 | compatible will usually lead to data corruption). |
| 2301 | |
| 2302 | In addition to the literal forms, bytes objects can be created in a number of |
| 2303 | other ways: |
| 2304 | |
| 2305 | * A zero-filled bytes object of a specified length: ``bytes(10)`` |
| 2306 | * From an iterable of integers: ``bytes(range(20))`` |
| 2307 | * Copying existing binary data via the buffer protocol: ``bytes(obj)`` |
| 2308 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2309 | Also see the :ref:`bytes <func-bytes>` built-in. |
| 2310 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2311 | Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal |
| 2312 | numbers are a commonly used format for describing binary data. Accordingly, |
| 2313 | the bytes type has an additional class method to read data in that format: |
| 2314 | |
| 2315 | .. classmethod:: bytes.fromhex(string) |
| 2316 | |
| 2317 | This :class:`bytes` class method returns a bytes object, decoding the |
| 2318 | given string object. The string must contain two hexadecimal digits per |
| 2319 | byte, with ASCII spaces being ignored. |
| 2320 | |
| 2321 | >>> bytes.fromhex('2Ef0 F1f2 ') |
| 2322 | b'.\xf0\xf1\xf2' |
| 2323 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2324 | A reverse conversion function exists to transform a bytes object into its |
| 2325 | hexadecimal representation. |
| 2326 | |
| 2327 | .. method:: bytes.hex() |
| 2328 | |
| 2329 | Return a string object containing two hexadecimal digits for each |
| 2330 | byte in the instance. |
| 2331 | |
| 2332 | >>> b'\xf0\xf1\xf2'.hex() |
| 2333 | 'f0f1f2' |
| 2334 | |
| 2335 | .. versionadded:: 3.5 |
| 2336 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2337 | Since bytes objects are sequences of integers (akin to a tuple), for a bytes |
| 2338 | object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes |
| 2339 | object of length 1. (This contrasts with text strings, where both indexing |
| 2340 | and slicing will produce a string of length 1) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2341 | |
| 2342 | The representation of bytes objects uses the literal format (``b'...'``) |
| 2343 | since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can |
| 2344 | 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] | 2345 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2346 | .. note:: |
| 2347 | For Python 2.x users: In the Python 2.x series, a variety of implicit |
| 2348 | conversions between 8-bit strings (the closest thing 2.x offers to a |
| 2349 | built-in binary data type) and Unicode strings were permitted. This was a |
| 2350 | backwards compatibility workaround to account for the fact that Python |
| 2351 | originally only supported 8-bit text, and Unicode text was a later |
| 2352 | addition. In Python 3.x, those implicit conversions are gone - conversions |
| 2353 | between 8-bit binary data and Unicode text must be explicit, and bytes and |
| 2354 | string objects will always compare unequal. |
Raymond Hettinger | c50846a | 2010-04-05 18:56:31 +0000 | [diff] [blame] | 2355 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2356 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2357 | .. _typebytearray: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2358 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2359 | Bytearray Objects |
| 2360 | ----------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2361 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2362 | .. index:: object: bytearray |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 2363 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2364 | :class:`bytearray` objects are a mutable counterpart to :class:`bytes` |
| 2365 | objects. There is no dedicated literal syntax for bytearray objects, instead |
| 2366 | they are always created by calling the constructor: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2367 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2368 | * Creating an empty instance: ``bytearray()`` |
| 2369 | * Creating a zero-filled instance with a given length: ``bytearray(10)`` |
| 2370 | * From an iterable of integers: ``bytearray(range(20))`` |
Ezio Melotti | 971ba4c | 2012-10-27 23:25:18 +0300 | [diff] [blame] | 2371 | * Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')`` |
Eli Bendersky | cbbaa96 | 2011-02-25 05:47:53 +0000 | [diff] [blame] | 2372 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2373 | As bytearray objects are mutable, they support the |
| 2374 | :ref:`mutable <typesseq-mutable>` sequence operations in addition to the |
| 2375 | common bytes and bytearray operations described in :ref:`bytes-methods`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2376 | |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 2377 | Also see the :ref:`bytearray <func-bytearray>` built-in. |
| 2378 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2379 | Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal |
| 2380 | numbers are a commonly used format for describing binary data. Accordingly, |
| 2381 | the bytearray type has an additional class method to read data in that format: |
| 2382 | |
| 2383 | .. classmethod:: bytearray.fromhex(string) |
| 2384 | |
| 2385 | This :class:`bytearray` class method returns bytearray object, decoding |
| 2386 | the given string object. The string must contain two hexadecimal digits |
| 2387 | per byte, with ASCII spaces being ignored. |
| 2388 | |
| 2389 | >>> bytearray.fromhex('2Ef0 F1f2 ') |
| 2390 | bytearray(b'.\xf0\xf1\xf2') |
| 2391 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 2392 | A reverse conversion function exists to transform a bytearray object into its |
| 2393 | hexadecimal representation. |
| 2394 | |
| 2395 | .. method:: bytearray.hex() |
| 2396 | |
| 2397 | Return a string object containing two hexadecimal digits for each |
| 2398 | byte in the instance. |
| 2399 | |
| 2400 | >>> bytearray(b'\xf0\xf1\xf2').hex() |
| 2401 | 'f0f1f2' |
| 2402 | |
| 2403 | .. versionadded:: 3.5 |
| 2404 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2405 | Since bytearray objects are sequences of integers (akin to a list), for a |
| 2406 | bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be |
| 2407 | a bytearray object of length 1. (This contrasts with text strings, where |
| 2408 | both indexing and slicing will produce a string of length 1) |
| 2409 | |
| 2410 | The representation of bytearray objects uses the bytes literal format |
| 2411 | (``bytearray(b'...')``) since it is often more useful than e.g. |
| 2412 | ``bytearray([46, 46, 46])``. You can always convert a bytearray object into |
| 2413 | a list of integers using ``list(b)``. |
| 2414 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 2415 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2416 | .. _bytes-methods: |
| 2417 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2418 | Bytes and Bytearray Operations |
| 2419 | ------------------------------ |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2420 | |
| 2421 | .. index:: pair: bytes; methods |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2422 | pair: bytearray; methods |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2423 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2424 | Both bytes and bytearray objects support the :ref:`common <typesseq-common>` |
| 2425 | sequence operations. They interoperate not just with operands of the same |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2426 | type, but with any :term:`bytes-like object`. Due to this flexibility, they can be |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2427 | freely mixed in operations without causing errors. However, the return type |
| 2428 | of the result may depend on the order of operands. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2429 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2430 | .. note:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2431 | |
Georg Brandl | 9541463 | 2007-11-22 11:00:28 +0000 | [diff] [blame] | 2432 | 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] | 2433 | 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] | 2434 | arguments. For example, you have to write:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2435 | |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2436 | a = "abc" |
| 2437 | b = a.replace("a", "f") |
| 2438 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2439 | and:: |
Georg Brandl | 7c67613 | 2007-10-23 18:17:00 +0000 | [diff] [blame] | 2440 | |
| 2441 | a = b"abc" |
| 2442 | b = a.replace(b"a", b"f") |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2443 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2444 | Some bytes and bytearray operations assume the use of ASCII compatible |
| 2445 | binary formats, and hence should be avoided when working with arbitrary |
| 2446 | binary data. These restrictions are covered below. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2447 | |
| 2448 | .. note:: |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2449 | Using these ASCII based operations to manipulate binary data that is not |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2450 | stored in an ASCII based format may lead to data corruption. |
| 2451 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2452 | The following methods on bytes and bytearray objects can be used with |
| 2453 | arbitrary binary data. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2454 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2455 | .. method:: bytes.count(sub[, start[, end]]) |
| 2456 | bytearray.count(sub[, start[, end]]) |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2457 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2458 | Return the number of non-overlapping occurrences of subsequence *sub* in |
| 2459 | the range [*start*, *end*]. Optional arguments *start* and *end* are |
| 2460 | interpreted as in slice notation. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 2461 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2462 | The subsequence to search for may be any :term:`bytes-like object` or an |
| 2463 | integer in the range 0 to 255. |
| 2464 | |
| 2465 | .. versionchanged:: 3.3 |
| 2466 | Also accept an integer in the range 0 to 255 as the subsequence. |
| 2467 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2468 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 2469 | .. method:: bytes.decode(encoding="utf-8", errors="strict") |
| 2470 | bytearray.decode(encoding="utf-8", errors="strict") |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 2471 | |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 2472 | Return a string decoded from the given bytes. Default encoding is |
| 2473 | ``'utf-8'``. *errors* may be given to set a different |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 2474 | error handling scheme. The default for *errors* is ``'strict'``, meaning |
| 2475 | that encoding errors raise a :exc:`UnicodeError`. Other possible values are |
| 2476 | ``'ignore'``, ``'replace'`` and any other name registered via |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 2477 | :func:`codecs.register_error`, see section :ref:`error-handlers`. For a |
Georg Brandl | 4f5f98d | 2009-05-04 21:01:20 +0000 | [diff] [blame] | 2478 | list of possible encodings, see section :ref:`standard-encodings`. |
| 2479 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2480 | .. note:: |
| 2481 | |
| 2482 | Passing the *encoding* argument to :class:`str` allows decoding any |
| 2483 | :term:`bytes-like object` directly, without needing to make a temporary |
| 2484 | bytes or bytearray object. |
| 2485 | |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2486 | .. versionchanged:: 3.1 |
| 2487 | Added support for keyword arguments. |
| 2488 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2489 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2490 | .. method:: bytes.endswith(suffix[, start[, end]]) |
| 2491 | bytearray.endswith(suffix[, start[, end]]) |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2492 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2493 | Return ``True`` if the binary data ends with the specified *suffix*, |
| 2494 | otherwise return ``False``. *suffix* can also be a tuple of suffixes to |
| 2495 | look for. With optional *start*, test beginning at that position. With |
| 2496 | optional *end*, stop comparing at that position. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2497 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2498 | The suffix(es) to search for may be any :term:`bytes-like object`. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2499 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2500 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2501 | .. method:: bytes.find(sub[, start[, end]]) |
| 2502 | bytearray.find(sub[, start[, end]]) |
| 2503 | |
| 2504 | Return the lowest index in the data where the subsequence *sub* is found, |
| 2505 | such that *sub* is contained in the slice ``s[start:end]``. Optional |
| 2506 | arguments *start* and *end* are interpreted as in slice notation. Return |
| 2507 | ``-1`` if *sub* is not found. |
| 2508 | |
| 2509 | The subsequence to search for may be any :term:`bytes-like object` or an |
| 2510 | integer in the range 0 to 255. |
| 2511 | |
| 2512 | .. note:: |
| 2513 | |
| 2514 | The :meth:`~bytes.find` method should be used only if you need to know the |
| 2515 | position of *sub*. To check if *sub* is a substring or not, use the |
| 2516 | :keyword:`in` operator:: |
| 2517 | |
| 2518 | >>> b'Py' in b'Python' |
| 2519 | True |
| 2520 | |
| 2521 | .. versionchanged:: 3.3 |
| 2522 | Also accept an integer in the range 0 to 255 as the subsequence. |
| 2523 | |
| 2524 | |
| 2525 | .. method:: bytes.index(sub[, start[, end]]) |
| 2526 | bytearray.index(sub[, start[, end]]) |
| 2527 | |
| 2528 | Like :meth:`~bytes.find`, but raise :exc:`ValueError` when the |
| 2529 | subsequence is not found. |
| 2530 | |
| 2531 | The subsequence to search for may be any :term:`bytes-like object` or an |
| 2532 | integer in the range 0 to 255. |
| 2533 | |
| 2534 | .. versionchanged:: 3.3 |
| 2535 | Also accept an integer in the range 0 to 255 as the subsequence. |
| 2536 | |
| 2537 | |
| 2538 | .. method:: bytes.join(iterable) |
| 2539 | bytearray.join(iterable) |
| 2540 | |
| 2541 | Return a bytes or bytearray object which is the concatenation of the |
| 2542 | binary data sequences in the :term:`iterable` *iterable*. A |
| 2543 | :exc:`TypeError` will be raised if there are any values in *iterable* |
R David Murray | 0e8168c | 2015-05-17 10:16:37 -0400 | [diff] [blame] | 2544 | that are not :term:`bytes-like objects <bytes-like object>`, including |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2545 | :class:`str` objects. The separator between elements is the contents |
| 2546 | of the bytes or bytearray object providing this method. |
| 2547 | |
| 2548 | |
| 2549 | .. staticmethod:: bytes.maketrans(from, to) |
| 2550 | bytearray.maketrans(from, to) |
| 2551 | |
| 2552 | This static method returns a translation table usable for |
| 2553 | :meth:`bytes.translate` that will map each character in *from* into the |
| 2554 | character at the same position in *to*; *from* and *to* must both be |
| 2555 | :term:`bytes-like objects <bytes-like object>` and have the same length. |
| 2556 | |
| 2557 | .. versionadded:: 3.1 |
| 2558 | |
| 2559 | |
| 2560 | .. method:: bytes.partition(sep) |
| 2561 | bytearray.partition(sep) |
| 2562 | |
| 2563 | Split the sequence at the first occurrence of *sep*, and return a 3-tuple |
| 2564 | containing the part before the separator, the separator, and the part |
| 2565 | after the separator. If the separator is not found, return a 3-tuple |
| 2566 | containing a copy of the original sequence, followed by two empty bytes or |
| 2567 | bytearray objects. |
| 2568 | |
| 2569 | The separator to search for may be any :term:`bytes-like object`. |
| 2570 | |
| 2571 | |
| 2572 | .. method:: bytes.replace(old, new[, count]) |
| 2573 | bytearray.replace(old, new[, count]) |
| 2574 | |
| 2575 | Return a copy of the sequence with all occurrences of subsequence *old* |
| 2576 | replaced by *new*. If the optional argument *count* is given, only the |
| 2577 | first *count* occurrences are replaced. |
| 2578 | |
| 2579 | The subsequence to search for and its replacement may be any |
| 2580 | :term:`bytes-like object`. |
| 2581 | |
| 2582 | .. note:: |
| 2583 | |
| 2584 | The bytearray version of this method does *not* operate in place - it |
| 2585 | always produces a new object, even if no changes were made. |
| 2586 | |
| 2587 | |
| 2588 | .. method:: bytes.rfind(sub[, start[, end]]) |
| 2589 | bytearray.rfind(sub[, start[, end]]) |
| 2590 | |
| 2591 | Return the highest index in the sequence where the subsequence *sub* is |
| 2592 | found, such that *sub* is contained within ``s[start:end]``. Optional |
| 2593 | arguments *start* and *end* are interpreted as in slice notation. Return |
| 2594 | ``-1`` on failure. |
| 2595 | |
| 2596 | The subsequence to search for may be any :term:`bytes-like object` or an |
| 2597 | integer in the range 0 to 255. |
| 2598 | |
| 2599 | .. versionchanged:: 3.3 |
| 2600 | Also accept an integer in the range 0 to 255 as the subsequence. |
| 2601 | |
| 2602 | |
| 2603 | .. method:: bytes.rindex(sub[, start[, end]]) |
| 2604 | bytearray.rindex(sub[, start[, end]]) |
| 2605 | |
| 2606 | Like :meth:`~bytes.rfind` but raises :exc:`ValueError` when the |
| 2607 | subsequence *sub* is not found. |
| 2608 | |
| 2609 | The subsequence to search for may be any :term:`bytes-like object` or an |
| 2610 | integer in the range 0 to 255. |
| 2611 | |
| 2612 | .. versionchanged:: 3.3 |
| 2613 | Also accept an integer in the range 0 to 255 as the subsequence. |
| 2614 | |
| 2615 | |
| 2616 | .. method:: bytes.rpartition(sep) |
| 2617 | bytearray.rpartition(sep) |
| 2618 | |
| 2619 | Split the sequence at the last occurrence of *sep*, and return a 3-tuple |
| 2620 | containing the part before the separator, the separator, and the part |
| 2621 | after the separator. If the separator is not found, return a 3-tuple |
| 2622 | containing a copy of the original sequence, followed by two empty bytes or |
| 2623 | bytearray objects. |
| 2624 | |
| 2625 | The separator to search for may be any :term:`bytes-like object`. |
| 2626 | |
| 2627 | |
| 2628 | .. method:: bytes.startswith(prefix[, start[, end]]) |
| 2629 | bytearray.startswith(prefix[, start[, end]]) |
| 2630 | |
| 2631 | Return ``True`` if the binary data starts with the specified *prefix*, |
| 2632 | otherwise return ``False``. *prefix* can also be a tuple of prefixes to |
| 2633 | look for. With optional *start*, test beginning at that position. With |
| 2634 | optional *end*, stop comparing at that position. |
| 2635 | |
| 2636 | The prefix(es) to search for may be any :term:`bytes-like object`. |
| 2637 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 2638 | |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 2639 | .. method:: bytes.translate(table, delete=b'') |
| 2640 | bytearray.translate(table, delete=b'') |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2641 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2642 | Return a copy of the bytes or bytearray object where all bytes occurring in |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2643 | the optional argument *delete* are removed, and the remaining bytes have |
| 2644 | been mapped through the given translation table, which must be a bytes |
| 2645 | object of length 256. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2646 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2647 | You can use the :func:`bytes.maketrans` method to create a translation |
| 2648 | table. |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2649 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2650 | Set the *table* argument to ``None`` for translations that only delete |
| 2651 | characters:: |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2652 | |
Georg Brandl | 454636f | 2008-12-27 23:33:20 +0000 | [diff] [blame] | 2653 | >>> b'read this short text'.translate(None, b'aeiou') |
| 2654 | b'rd ths shrt txt' |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2655 | |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 2656 | .. versionchanged:: 3.6 |
| 2657 | *delete* is now supported as a keyword argument. |
| 2658 | |
Georg Brandl | 226878c | 2007-08-31 10:15:37 +0000 | [diff] [blame] | 2659 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2660 | The following methods on bytes and bytearray objects have default behaviours |
| 2661 | that assume the use of ASCII compatible binary formats, but can still be used |
| 2662 | with arbitrary binary data by passing appropriate arguments. Note that all of |
| 2663 | the bytearray methods in this section do *not* operate in place, and instead |
| 2664 | produce new objects. |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2665 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2666 | .. method:: bytes.center(width[, fillbyte]) |
| 2667 | bytearray.center(width[, fillbyte]) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2668 | |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2669 | Return a copy of the object centered in a sequence of length *width*. |
| 2670 | Padding is done using the specified *fillbyte* (default is an ASCII |
| 2671 | space). For :class:`bytes` objects, the original sequence is returned if |
| 2672 | *width* is less than or equal to ``len(s)``. |
| 2673 | |
| 2674 | .. note:: |
| 2675 | |
| 2676 | The bytearray version of this method does *not* operate in place - |
| 2677 | it always produces a new object, even if no changes were made. |
| 2678 | |
| 2679 | |
| 2680 | .. method:: bytes.ljust(width[, fillbyte]) |
| 2681 | bytearray.ljust(width[, fillbyte]) |
| 2682 | |
| 2683 | Return a copy of the object left justified in a sequence of length *width*. |
| 2684 | Padding is done using the specified *fillbyte* (default is an ASCII |
| 2685 | space). For :class:`bytes` objects, the original sequence is returned if |
| 2686 | *width* is less than or equal to ``len(s)``. |
| 2687 | |
| 2688 | .. note:: |
| 2689 | |
| 2690 | The bytearray version of this method does *not* operate in place - |
| 2691 | it always produces a new object, even if no changes were made. |
| 2692 | |
| 2693 | |
| 2694 | .. method:: bytes.lstrip([chars]) |
| 2695 | bytearray.lstrip([chars]) |
| 2696 | |
| 2697 | Return a copy of the sequence with specified leading bytes removed. The |
| 2698 | *chars* argument is a binary sequence specifying the set of byte values to |
| 2699 | be removed - the name refers to the fact this method is usually used with |
| 2700 | ASCII characters. If omitted or ``None``, the *chars* argument defaults |
| 2701 | to removing ASCII whitespace. The *chars* argument is not a prefix; |
| 2702 | rather, all combinations of its values are stripped:: |
| 2703 | |
| 2704 | >>> b' spacious '.lstrip() |
| 2705 | b'spacious ' |
| 2706 | >>> b'www.example.com'.lstrip(b'cmowz.') |
| 2707 | b'example.com' |
| 2708 | |
| 2709 | The binary sequence of byte values to remove may be any |
| 2710 | :term:`bytes-like object`. |
| 2711 | |
| 2712 | .. note:: |
| 2713 | |
| 2714 | The bytearray version of this method does *not* operate in place - |
| 2715 | it always produces a new object, even if no changes were made. |
| 2716 | |
| 2717 | |
| 2718 | .. method:: bytes.rjust(width[, fillbyte]) |
| 2719 | bytearray.rjust(width[, fillbyte]) |
| 2720 | |
| 2721 | Return a copy of the object right justified in a sequence of length *width*. |
| 2722 | Padding is done using the specified *fillbyte* (default is an ASCII |
| 2723 | space). For :class:`bytes` objects, the original sequence is returned if |
| 2724 | *width* is less than or equal to ``len(s)``. |
| 2725 | |
| 2726 | .. note:: |
| 2727 | |
| 2728 | The bytearray version of this method does *not* operate in place - |
| 2729 | it always produces a new object, even if no changes were made. |
| 2730 | |
| 2731 | |
| 2732 | .. method:: bytes.rsplit(sep=None, maxsplit=-1) |
| 2733 | bytearray.rsplit(sep=None, maxsplit=-1) |
| 2734 | |
| 2735 | Split the binary sequence into subsequences of the same type, using *sep* |
| 2736 | as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits |
| 2737 | are done, the *rightmost* ones. If *sep* is not specified or ``None``, |
| 2738 | any subsequence consisting solely of ASCII whitespace is a separator. |
| 2739 | Except for splitting from the right, :meth:`rsplit` behaves like |
| 2740 | :meth:`split` which is described in detail below. |
| 2741 | |
| 2742 | |
| 2743 | .. method:: bytes.rstrip([chars]) |
| 2744 | bytearray.rstrip([chars]) |
| 2745 | |
| 2746 | Return a copy of the sequence with specified trailing bytes removed. The |
| 2747 | *chars* argument is a binary sequence specifying the set of byte values to |
| 2748 | be removed - the name refers to the fact this method is usually used with |
| 2749 | ASCII characters. If omitted or ``None``, the *chars* argument defaults to |
| 2750 | removing ASCII whitespace. The *chars* argument is not a suffix; rather, |
| 2751 | all combinations of its values are stripped:: |
| 2752 | |
| 2753 | >>> b' spacious '.rstrip() |
| 2754 | b' spacious' |
| 2755 | >>> b'mississippi'.rstrip(b'ipz') |
| 2756 | b'mississ' |
| 2757 | |
| 2758 | The binary sequence of byte values to remove may be any |
| 2759 | :term:`bytes-like object`. |
| 2760 | |
| 2761 | .. note:: |
| 2762 | |
| 2763 | The bytearray version of this method does *not* operate in place - |
| 2764 | it always produces a new object, even if no changes were made. |
| 2765 | |
| 2766 | |
| 2767 | .. method:: bytes.split(sep=None, maxsplit=-1) |
| 2768 | bytearray.split(sep=None, maxsplit=-1) |
| 2769 | |
| 2770 | Split the binary sequence into subsequences of the same type, using *sep* |
| 2771 | as the delimiter string. If *maxsplit* is given and non-negative, at most |
| 2772 | *maxsplit* splits are done (thus, the list will have at most ``maxsplit+1`` |
| 2773 | elements). If *maxsplit* is not specified or is ``-1``, then there is no |
| 2774 | limit on the number of splits (all possible splits are made). |
| 2775 | |
| 2776 | If *sep* is given, consecutive delimiters are not grouped together and are |
| 2777 | deemed to delimit empty subsequences (for example, ``b'1,,2'.split(b',')`` |
| 2778 | returns ``[b'1', b'', b'2']``). The *sep* argument may consist of a |
| 2779 | multibyte sequence (for example, ``b'1<>2<>3'.split(b'<>')`` returns |
| 2780 | ``[b'1', b'2', b'3']``). Splitting an empty sequence with a specified |
| 2781 | separator returns ``[b'']`` or ``[bytearray(b'')]`` depending on the type |
| 2782 | of object being split. The *sep* argument may be any |
| 2783 | :term:`bytes-like object`. |
| 2784 | |
| 2785 | For example:: |
| 2786 | |
| 2787 | >>> b'1,2,3'.split(b',') |
| 2788 | [b'1', b'2', b'3'] |
| 2789 | >>> b'1,2,3'.split(b',', maxsplit=1) |
Benjamin Peterson | eb83ffe | 2014-09-22 22:43:50 -0400 | [diff] [blame] | 2790 | [b'1', b'2,3'] |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2791 | >>> b'1,2,,3,'.split(b',') |
| 2792 | [b'1', b'2', b'', b'3', b''] |
| 2793 | |
| 2794 | If *sep* is not specified or is ``None``, a different splitting algorithm |
| 2795 | is applied: runs of consecutive ASCII whitespace are regarded as a single |
| 2796 | separator, and the result will contain no empty strings at the start or |
| 2797 | end if the sequence has leading or trailing whitespace. Consequently, |
| 2798 | splitting an empty sequence or a sequence consisting solely of ASCII |
| 2799 | whitespace without a specified separator returns ``[]``. |
| 2800 | |
| 2801 | For example:: |
| 2802 | |
| 2803 | |
| 2804 | >>> b'1 2 3'.split() |
| 2805 | [b'1', b'2', b'3'] |
| 2806 | >>> b'1 2 3'.split(maxsplit=1) |
| 2807 | [b'1', b'2 3'] |
| 2808 | >>> b' 1 2 3 '.split() |
| 2809 | [b'1', b'2', b'3'] |
| 2810 | |
| 2811 | |
| 2812 | .. method:: bytes.strip([chars]) |
| 2813 | bytearray.strip([chars]) |
| 2814 | |
| 2815 | Return a copy of the sequence with specified leading and trailing bytes |
| 2816 | removed. The *chars* argument is a binary sequence specifying the set of |
| 2817 | byte values to be removed - the name refers to the fact this method is |
| 2818 | usually used with ASCII characters. If omitted or ``None``, the *chars* |
| 2819 | argument defaults to removing ASCII whitespace. The *chars* argument is |
| 2820 | not a prefix or suffix; rather, all combinations of its values are |
| 2821 | stripped:: |
| 2822 | |
| 2823 | >>> b' spacious '.strip() |
| 2824 | b'spacious' |
| 2825 | >>> b'www.example.com'.strip(b'cmowz.') |
| 2826 | b'example' |
| 2827 | |
| 2828 | The binary sequence of byte values to remove may be any |
| 2829 | :term:`bytes-like object`. |
| 2830 | |
| 2831 | .. note:: |
| 2832 | |
| 2833 | The bytearray version of this method does *not* operate in place - |
| 2834 | it always produces a new object, even if no changes were made. |
| 2835 | |
| 2836 | |
| 2837 | The following methods on bytes and bytearray objects assume the use of ASCII |
| 2838 | compatible binary formats and should not be applied to arbitrary binary data. |
| 2839 | Note that all of the bytearray methods in this section do *not* operate in |
| 2840 | place, and instead produce new objects. |
| 2841 | |
| 2842 | .. method:: bytes.capitalize() |
| 2843 | bytearray.capitalize() |
| 2844 | |
| 2845 | Return a copy of the sequence with each byte interpreted as an ASCII |
| 2846 | character, and the first byte capitalized and the rest lowercased. |
| 2847 | Non-ASCII byte values are passed through unchanged. |
| 2848 | |
| 2849 | .. note:: |
| 2850 | |
| 2851 | The bytearray version of this method does *not* operate in place - it |
| 2852 | always produces a new object, even if no changes were made. |
| 2853 | |
| 2854 | |
| 2855 | .. method:: bytes.expandtabs(tabsize=8) |
| 2856 | bytearray.expandtabs(tabsize=8) |
| 2857 | |
| 2858 | Return a copy of the sequence where all ASCII tab characters are replaced |
| 2859 | by one or more ASCII spaces, depending on the current column and the given |
| 2860 | tab size. Tab positions occur every *tabsize* bytes (default is 8, |
| 2861 | giving tab positions at columns 0, 8, 16 and so on). To expand the |
| 2862 | sequence, the current column is set to zero and the sequence is examined |
| 2863 | byte by byte. If the byte is an ASCII tab character (``b'\t'``), one or |
| 2864 | more space characters are inserted in the result until the current column |
| 2865 | is equal to the next tab position. (The tab character itself is not |
| 2866 | copied.) If the current byte is an ASCII newline (``b'\n'``) or |
| 2867 | carriage return (``b'\r'``), it is copied and the current column is reset |
| 2868 | to zero. Any other byte value is copied unchanged and the current column |
| 2869 | is incremented by one regardless of how the byte value is represented when |
| 2870 | printed:: |
| 2871 | |
| 2872 | >>> b'01\t012\t0123\t01234'.expandtabs() |
| 2873 | b'01 012 0123 01234' |
| 2874 | >>> b'01\t012\t0123\t01234'.expandtabs(4) |
| 2875 | b'01 012 0123 01234' |
| 2876 | |
| 2877 | .. note:: |
| 2878 | |
| 2879 | The bytearray version of this method does *not* operate in place - it |
| 2880 | always produces a new object, even if no changes were made. |
| 2881 | |
| 2882 | |
| 2883 | .. method:: bytes.isalnum() |
| 2884 | bytearray.isalnum() |
| 2885 | |
| 2886 | Return true if all bytes in the sequence are alphabetical ASCII characters |
| 2887 | or ASCII decimal digits and the sequence is not empty, false otherwise. |
| 2888 | Alphabetic ASCII characters are those byte values in the sequence |
| 2889 | ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. ASCII decimal |
| 2890 | digits are those byte values in the sequence ``b'0123456789'``. |
| 2891 | |
| 2892 | For example:: |
| 2893 | |
| 2894 | >>> b'ABCabc1'.isalnum() |
| 2895 | True |
| 2896 | >>> b'ABC abc1'.isalnum() |
| 2897 | False |
| 2898 | |
| 2899 | |
| 2900 | .. method:: bytes.isalpha() |
| 2901 | bytearray.isalpha() |
| 2902 | |
| 2903 | Return true if all bytes in the sequence are alphabetic ASCII characters |
| 2904 | and the sequence is not empty, false otherwise. Alphabetic ASCII |
| 2905 | characters are those byte values in the sequence |
| 2906 | ``b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 2907 | |
| 2908 | For example:: |
| 2909 | |
| 2910 | >>> b'ABCabc'.isalpha() |
| 2911 | True |
| 2912 | >>> b'ABCabc1'.isalpha() |
| 2913 | False |
| 2914 | |
| 2915 | |
| 2916 | .. method:: bytes.isdigit() |
| 2917 | bytearray.isdigit() |
| 2918 | |
| 2919 | Return true if all bytes in the sequence are ASCII decimal digits |
| 2920 | and the sequence is not empty, false otherwise. ASCII decimal digits are |
| 2921 | those byte values in the sequence ``b'0123456789'``. |
| 2922 | |
| 2923 | For example:: |
| 2924 | |
| 2925 | >>> b'1234'.isdigit() |
| 2926 | True |
| 2927 | >>> b'1.23'.isdigit() |
| 2928 | False |
| 2929 | |
| 2930 | |
| 2931 | .. method:: bytes.islower() |
| 2932 | bytearray.islower() |
| 2933 | |
| 2934 | Return true if there is at least one lowercase ASCII character |
| 2935 | in the sequence and no uppercase ASCII characters, false otherwise. |
| 2936 | |
| 2937 | For example:: |
| 2938 | |
| 2939 | >>> b'hello world'.islower() |
| 2940 | True |
| 2941 | >>> b'Hello world'.islower() |
| 2942 | False |
| 2943 | |
| 2944 | Lowercase ASCII characters are those byte values in the sequence |
| 2945 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 2946 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 2947 | |
| 2948 | |
| 2949 | .. method:: bytes.isspace() |
| 2950 | bytearray.isspace() |
| 2951 | |
| 2952 | Return true if all bytes in the sequence are ASCII whitespace and the |
| 2953 | sequence is not empty, false otherwise. ASCII whitespace characters are |
Serhiy Storchaka | bf7b9ed | 2015-11-23 16:43:05 +0200 | [diff] [blame] | 2954 | those byte values in the sequence ``b' \t\n\r\x0b\f'`` (space, tab, newline, |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2955 | carriage return, vertical tab, form feed). |
| 2956 | |
| 2957 | |
| 2958 | .. method:: bytes.istitle() |
| 2959 | bytearray.istitle() |
| 2960 | |
| 2961 | Return true if the sequence is ASCII titlecase and the sequence is not |
| 2962 | empty, false otherwise. See :meth:`bytes.title` for more details on the |
| 2963 | definition of "titlecase". |
| 2964 | |
| 2965 | For example:: |
| 2966 | |
| 2967 | >>> b'Hello World'.istitle() |
| 2968 | True |
| 2969 | >>> b'Hello world'.istitle() |
| 2970 | False |
| 2971 | |
| 2972 | |
| 2973 | .. method:: bytes.isupper() |
| 2974 | bytearray.isupper() |
| 2975 | |
Zachary Ware | 0b49637 | 2015-02-27 01:40:22 -0600 | [diff] [blame] | 2976 | Return true if there is at least one uppercase alphabetic ASCII character |
| 2977 | in the sequence and no lowercase ASCII characters, false otherwise. |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 2978 | |
| 2979 | For example:: |
| 2980 | |
| 2981 | >>> b'HELLO WORLD'.isupper() |
| 2982 | True |
| 2983 | >>> b'Hello world'.isupper() |
| 2984 | False |
| 2985 | |
| 2986 | Lowercase ASCII characters are those byte values in the sequence |
| 2987 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 2988 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 2989 | |
| 2990 | |
| 2991 | .. method:: bytes.lower() |
| 2992 | bytearray.lower() |
| 2993 | |
| 2994 | Return a copy of the sequence with all the uppercase ASCII characters |
| 2995 | converted to their corresponding lowercase counterpart. |
| 2996 | |
| 2997 | For example:: |
| 2998 | |
| 2999 | >>> b'Hello World'.lower() |
| 3000 | b'hello world' |
| 3001 | |
| 3002 | Lowercase ASCII characters are those byte values in the sequence |
| 3003 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 3004 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 3005 | |
| 3006 | .. note:: |
| 3007 | |
| 3008 | The bytearray version of this method does *not* operate in place - it |
| 3009 | always produces a new object, even if no changes were made. |
| 3010 | |
| 3011 | |
| 3012 | .. index:: |
| 3013 | single: universal newlines; bytes.splitlines method |
| 3014 | single: universal newlines; bytearray.splitlines method |
| 3015 | |
| 3016 | .. method:: bytes.splitlines(keepends=False) |
| 3017 | bytearray.splitlines(keepends=False) |
| 3018 | |
| 3019 | Return a list of the lines in the binary sequence, breaking at ASCII |
| 3020 | line boundaries. This method uses the :term:`universal newlines` approach |
| 3021 | to splitting lines. Line breaks are not included in the resulting list |
| 3022 | unless *keepends* is given and true. |
| 3023 | |
| 3024 | For example:: |
| 3025 | |
| 3026 | >>> b'ab c\n\nde fg\rkl\r\n'.splitlines() |
Larry Hastings | c6256e5 | 2014-10-05 19:03:48 -0700 | [diff] [blame] | 3027 | [b'ab c', b'', b'de fg', b'kl'] |
Nick Coghlan | e4936b8 | 2014-08-09 16:14:04 +1000 | [diff] [blame] | 3028 | >>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) |
| 3029 | [b'ab c\n', b'\n', b'de fg\r', b'kl\r\n'] |
| 3030 | |
| 3031 | Unlike :meth:`~bytes.split` when a delimiter string *sep* is given, this |
| 3032 | method returns an empty list for the empty string, and a terminal line |
| 3033 | break does not result in an extra line:: |
| 3034 | |
| 3035 | >>> b"".split(b'\n'), b"Two lines\n".split(b'\n') |
| 3036 | ([b''], [b'Two lines', b'']) |
| 3037 | >>> b"".splitlines(), b"One line\n".splitlines() |
| 3038 | ([], [b'One line']) |
| 3039 | |
| 3040 | |
| 3041 | .. method:: bytes.swapcase() |
| 3042 | bytearray.swapcase() |
| 3043 | |
| 3044 | Return a copy of the sequence with all the lowercase ASCII characters |
| 3045 | converted to their corresponding uppercase counterpart and vice-versa. |
| 3046 | |
| 3047 | For example:: |
| 3048 | |
| 3049 | >>> b'Hello World'.swapcase() |
| 3050 | b'hELLO wORLD' |
| 3051 | |
| 3052 | Lowercase ASCII characters are those byte values in the sequence |
| 3053 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 3054 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 3055 | |
| 3056 | Unlike :func:`str.swapcase()`, it is always the case that |
| 3057 | ``bin.swapcase().swapcase() == bin`` for the binary versions. Case |
| 3058 | conversions are symmetrical in ASCII, even though that is not generally |
| 3059 | true for arbitrary Unicode code points. |
| 3060 | |
| 3061 | .. note:: |
| 3062 | |
| 3063 | The bytearray version of this method does *not* operate in place - it |
| 3064 | always produces a new object, even if no changes were made. |
| 3065 | |
| 3066 | |
| 3067 | .. method:: bytes.title() |
| 3068 | bytearray.title() |
| 3069 | |
| 3070 | Return a titlecased version of the binary sequence where words start with |
| 3071 | an uppercase ASCII character and the remaining characters are lowercase. |
| 3072 | Uncased byte values are left unmodified. |
| 3073 | |
| 3074 | For example:: |
| 3075 | |
| 3076 | >>> b'Hello world'.title() |
| 3077 | b'Hello World' |
| 3078 | |
| 3079 | Lowercase ASCII characters are those byte values in the sequence |
| 3080 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 3081 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 3082 | All other byte values are uncased. |
| 3083 | |
| 3084 | The algorithm uses a simple language-independent definition of a word as |
| 3085 | groups of consecutive letters. The definition works in many contexts but |
| 3086 | it means that apostrophes in contractions and possessives form word |
| 3087 | boundaries, which may not be the desired result:: |
| 3088 | |
| 3089 | >>> b"they're bill's friends from the UK".title() |
| 3090 | b"They'Re Bill'S Friends From The Uk" |
| 3091 | |
| 3092 | A workaround for apostrophes can be constructed using regular expressions:: |
| 3093 | |
| 3094 | >>> import re |
| 3095 | >>> def titlecase(s): |
| 3096 | ... return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?", |
| 3097 | ... lambda mo: mo.group(0)[0:1].upper() + |
| 3098 | ... mo.group(0)[1:].lower(), |
| 3099 | ... s) |
| 3100 | ... |
| 3101 | >>> titlecase(b"they're bill's friends.") |
| 3102 | b"They're Bill's Friends." |
| 3103 | |
| 3104 | .. note:: |
| 3105 | |
| 3106 | The bytearray version of this method does *not* operate in place - it |
| 3107 | always produces a new object, even if no changes were made. |
| 3108 | |
| 3109 | |
| 3110 | .. method:: bytes.upper() |
| 3111 | bytearray.upper() |
| 3112 | |
| 3113 | Return a copy of the sequence with all the lowercase ASCII characters |
| 3114 | converted to their corresponding uppercase counterpart. |
| 3115 | |
| 3116 | For example:: |
| 3117 | |
| 3118 | >>> b'Hello World'.upper() |
| 3119 | b'HELLO WORLD' |
| 3120 | |
| 3121 | Lowercase ASCII characters are those byte values in the sequence |
| 3122 | ``b'abcdefghijklmnopqrstuvwxyz'``. Uppercase ASCII characters |
| 3123 | are those byte values in the sequence ``b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``. |
| 3124 | |
| 3125 | .. note:: |
| 3126 | |
| 3127 | The bytearray version of this method does *not* operate in place - it |
| 3128 | always produces a new object, even if no changes were made. |
| 3129 | |
| 3130 | |
| 3131 | .. method:: bytes.zfill(width) |
| 3132 | bytearray.zfill(width) |
| 3133 | |
| 3134 | Return a copy of the sequence left filled with ASCII ``b'0'`` digits to |
| 3135 | make a sequence of length *width*. A leading sign prefix (``b'+'``/ |
| 3136 | ``b'-'`` is handled by inserting the padding *after* the sign character |
| 3137 | rather than before. For :class:`bytes` objects, the original sequence is |
| 3138 | returned if *width* is less than or equal to ``len(seq)``. |
| 3139 | |
| 3140 | For example:: |
| 3141 | |
| 3142 | >>> b"42".zfill(5) |
| 3143 | b'00042' |
| 3144 | >>> b"-42".zfill(5) |
| 3145 | b'-0042' |
| 3146 | |
| 3147 | .. note:: |
| 3148 | |
| 3149 | The bytearray version of this method does *not* operate in place - it |
| 3150 | always produces a new object, even if no changes were made. |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 3151 | |
| 3152 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3153 | .. _bytes-formatting: |
| 3154 | |
| 3155 | ``printf``-style Bytes Formatting |
| 3156 | ---------------------------------- |
| 3157 | |
| 3158 | .. index:: |
| 3159 | single: formatting, bytes (%) |
| 3160 | single: formatting, bytearray (%) |
| 3161 | single: interpolation, bytes (%) |
| 3162 | single: interpolation, bytearray (%) |
| 3163 | single: bytes; formatting |
| 3164 | single: bytearray; formatting |
| 3165 | single: bytes; interpolation |
| 3166 | single: bytearray; interpolation |
| 3167 | single: printf-style formatting |
| 3168 | single: sprintf-style formatting |
| 3169 | single: % formatting |
| 3170 | single: % interpolation |
| 3171 | |
| 3172 | .. note:: |
| 3173 | |
| 3174 | The formatting operations described here exhibit a variety of quirks that |
| 3175 | lead to a number of common errors (such as failing to display tuples and |
| 3176 | dictionaries correctly). If the value being printed may be a tuple or |
| 3177 | dictionary, wrap it in a tuple. |
| 3178 | |
| 3179 | Bytes objects (``bytes``/``bytearray``) have one unique built-in operation: |
| 3180 | the ``%`` operator (modulo). |
| 3181 | This is also known as the bytes *formatting* or *interpolation* operator. |
| 3182 | Given ``format % values`` (where *format* is a bytes object), ``%`` conversion |
| 3183 | specifications in *format* are replaced with zero or more elements of *values*. |
| 3184 | The effect is similar to using the :c:func:`sprintf` in the C language. |
| 3185 | |
| 3186 | If *format* requires a single argument, *values* may be a single non-tuple |
| 3187 | object. [5]_ Otherwise, *values* must be a tuple with exactly the number of |
| 3188 | items specified by the format bytes object, or a single mapping object (for |
| 3189 | example, a dictionary). |
| 3190 | |
| 3191 | A conversion specifier contains two or more characters and has the following |
| 3192 | components, which must occur in this order: |
| 3193 | |
| 3194 | #. The ``'%'`` character, which marks the start of the specifier. |
| 3195 | |
| 3196 | #. Mapping key (optional), consisting of a parenthesised sequence of characters |
| 3197 | (for example, ``(somename)``). |
| 3198 | |
| 3199 | #. Conversion flags (optional), which affect the result of some conversion |
| 3200 | types. |
| 3201 | |
| 3202 | #. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the |
| 3203 | actual width is read from the next element of the tuple in *values*, and the |
| 3204 | object to convert comes after the minimum field width and optional precision. |
| 3205 | |
| 3206 | #. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If |
| 3207 | specified as ``'*'`` (an asterisk), the actual precision is read from the next |
| 3208 | element of the tuple in *values*, and the value to convert comes after the |
| 3209 | precision. |
| 3210 | |
| 3211 | #. Length modifier (optional). |
| 3212 | |
| 3213 | #. Conversion type. |
| 3214 | |
| 3215 | When the right argument is a dictionary (or other mapping type), then the |
| 3216 | formats in the bytes object *must* include a parenthesised mapping key into that |
| 3217 | dictionary inserted immediately after the ``'%'`` character. The mapping key |
| 3218 | selects the value to be formatted from the mapping. For example: |
| 3219 | |
| 3220 | >>> print(b'%(language)s has %(number)03d quote types.' % |
| 3221 | ... {b'language': b"Python", b"number": 2}) |
| 3222 | b'Python has 002 quote types.' |
| 3223 | |
| 3224 | In this case no ``*`` specifiers may occur in a format (since they require a |
| 3225 | sequential parameter list). |
| 3226 | |
| 3227 | The conversion flag characters are: |
| 3228 | |
| 3229 | +---------+---------------------------------------------------------------------+ |
| 3230 | | Flag | Meaning | |
| 3231 | +=========+=====================================================================+ |
| 3232 | | ``'#'`` | The value conversion will use the "alternate form" (where defined | |
| 3233 | | | below). | |
| 3234 | +---------+---------------------------------------------------------------------+ |
| 3235 | | ``'0'`` | The conversion will be zero padded for numeric values. | |
| 3236 | +---------+---------------------------------------------------------------------+ |
| 3237 | | ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` | |
| 3238 | | | conversion if both are given). | |
| 3239 | +---------+---------------------------------------------------------------------+ |
| 3240 | | ``' '`` | (a space) A blank should be left before a positive number (or empty | |
| 3241 | | | string) produced by a signed conversion. | |
| 3242 | +---------+---------------------------------------------------------------------+ |
| 3243 | | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion | |
| 3244 | | | (overrides a "space" flag). | |
| 3245 | +---------+---------------------------------------------------------------------+ |
| 3246 | |
| 3247 | A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it |
| 3248 | is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``. |
| 3249 | |
| 3250 | The conversion types are: |
| 3251 | |
| 3252 | +------------+-----------------------------------------------------+-------+ |
| 3253 | | Conversion | Meaning | Notes | |
| 3254 | +============+=====================================================+=======+ |
| 3255 | | ``'d'`` | Signed integer decimal. | | |
| 3256 | +------------+-----------------------------------------------------+-------+ |
| 3257 | | ``'i'`` | Signed integer decimal. | | |
| 3258 | +------------+-----------------------------------------------------+-------+ |
| 3259 | | ``'o'`` | Signed octal value. | \(1) | |
| 3260 | +------------+-----------------------------------------------------+-------+ |
Ethan Furman | 62e977f | 2015-03-11 08:17:00 -0700 | [diff] [blame] | 3261 | | ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(8) | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3262 | +------------+-----------------------------------------------------+-------+ |
| 3263 | | ``'x'`` | Signed hexadecimal (lowercase). | \(2) | |
| 3264 | +------------+-----------------------------------------------------+-------+ |
| 3265 | | ``'X'`` | Signed hexadecimal (uppercase). | \(2) | |
| 3266 | +------------+-----------------------------------------------------+-------+ |
| 3267 | | ``'e'`` | Floating point exponential format (lowercase). | \(3) | |
| 3268 | +------------+-----------------------------------------------------+-------+ |
| 3269 | | ``'E'`` | Floating point exponential format (uppercase). | \(3) | |
| 3270 | +------------+-----------------------------------------------------+-------+ |
| 3271 | | ``'f'`` | Floating point decimal format. | \(3) | |
| 3272 | +------------+-----------------------------------------------------+-------+ |
| 3273 | | ``'F'`` | Floating point decimal format. | \(3) | |
| 3274 | +------------+-----------------------------------------------------+-------+ |
| 3275 | | ``'g'`` | Floating point format. Uses lowercase exponential | \(4) | |
| 3276 | | | format if exponent is less than -4 or not less than | | |
| 3277 | | | precision, decimal format otherwise. | | |
| 3278 | +------------+-----------------------------------------------------+-------+ |
| 3279 | | ``'G'`` | Floating point format. Uses uppercase exponential | \(4) | |
| 3280 | | | format if exponent is less than -4 or not less than | | |
| 3281 | | | precision, decimal format otherwise. | | |
| 3282 | +------------+-----------------------------------------------------+-------+ |
| 3283 | | ``'c'`` | Single byte (accepts integer or single | | |
| 3284 | | | byte objects). | | |
| 3285 | +------------+-----------------------------------------------------+-------+ |
| 3286 | | ``'b'`` | Bytes (any object that follows the | \(5) | |
| 3287 | | | :ref:`buffer protocol <bufferobjects>` or has | | |
| 3288 | | | :meth:`__bytes__`). | | |
| 3289 | +------------+-----------------------------------------------------+-------+ |
| 3290 | | ``'s'`` | ``'s'`` is an alias for ``'b'`` and should only | \(6) | |
| 3291 | | | be used for Python2/3 code bases. | | |
| 3292 | +------------+-----------------------------------------------------+-------+ |
| 3293 | | ``'a'`` | Bytes (converts any Python object using | \(5) | |
| 3294 | | | ``repr(obj).encode('ascii','backslashreplace)``). | | |
| 3295 | +------------+-----------------------------------------------------+-------+ |
Ethan Furman | 62e977f | 2015-03-11 08:17:00 -0700 | [diff] [blame] | 3296 | | ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) | |
| 3297 | | | be used for Python2/3 code bases. | | |
| 3298 | +------------+-----------------------------------------------------+-------+ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3299 | | ``'%'`` | No argument is converted, results in a ``'%'`` | | |
| 3300 | | | character in the result. | | |
| 3301 | +------------+-----------------------------------------------------+-------+ |
| 3302 | |
| 3303 | Notes: |
| 3304 | |
| 3305 | (1) |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 3306 | The alternate form causes a leading octal specifier (``'0o'``) to be |
| 3307 | inserted before the first digit. |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3308 | |
| 3309 | (2) |
| 3310 | The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether |
Martin Panter | 41176ae | 2016-12-11 01:07:29 +0000 | [diff] [blame] | 3311 | the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit. |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3312 | |
| 3313 | (3) |
| 3314 | The alternate form causes the result to always contain a decimal point, even if |
| 3315 | no digits follow it. |
| 3316 | |
| 3317 | The precision determines the number of digits after the decimal point and |
| 3318 | defaults to 6. |
| 3319 | |
| 3320 | (4) |
| 3321 | The alternate form causes the result to always contain a decimal point, and |
| 3322 | trailing zeroes are not removed as they would otherwise be. |
| 3323 | |
| 3324 | The precision determines the number of significant digits before and after the |
| 3325 | decimal point and defaults to 6. |
| 3326 | |
| 3327 | (5) |
| 3328 | If precision is ``N``, the output is truncated to ``N`` characters. |
| 3329 | |
| 3330 | (6) |
| 3331 | ``b'%s'`` is deprecated, but will not be removed during the 3.x series. |
| 3332 | |
| 3333 | (7) |
Ethan Furman | 62e977f | 2015-03-11 08:17:00 -0700 | [diff] [blame] | 3334 | ``b'%r'`` is deprecated, but will not be removed during the 3.x series. |
| 3335 | |
| 3336 | (8) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3337 | See :pep:`237`. |
| 3338 | |
| 3339 | .. note:: |
| 3340 | |
| 3341 | The bytearray version of this method does *not* operate in place - it |
| 3342 | always produces a new object, even if no changes were made. |
| 3343 | |
| 3344 | .. seealso:: :pep:`461`. |
| 3345 | .. versionadded:: 3.5 |
| 3346 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3347 | .. _typememoryview: |
| 3348 | |
| 3349 | Memory Views |
| 3350 | ------------ |
| 3351 | |
| 3352 | :class:`memoryview` objects allow Python code to access the internal data |
| 3353 | of an object that supports the :ref:`buffer protocol <bufferobjects>` without |
| 3354 | copying. |
| 3355 | |
| 3356 | .. class:: memoryview(obj) |
| 3357 | |
| 3358 | Create a :class:`memoryview` that references *obj*. *obj* must support the |
| 3359 | buffer protocol. Built-in objects that support the buffer protocol include |
| 3360 | :class:`bytes` and :class:`bytearray`. |
| 3361 | |
| 3362 | A :class:`memoryview` has the notion of an *element*, which is the |
| 3363 | atomic memory unit handled by the originating object *obj*. For many |
| 3364 | simple types such as :class:`bytes` and :class:`bytearray`, an element |
| 3365 | is a single byte, but other types such as :class:`array.array` may have |
| 3366 | bigger elements. |
| 3367 | |
| 3368 | ``len(view)`` is equal to the length of :class:`~memoryview.tolist`. |
| 3369 | If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length |
| 3370 | is equal to the number of elements in the view. For higher dimensions, |
| 3371 | the length is equal to the length of the nested list representation of |
| 3372 | the view. The :class:`~memoryview.itemsize` attribute will give you the |
| 3373 | number of bytes in a single element. |
| 3374 | |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3375 | A :class:`memoryview` supports slicing and indexing to expose its data. |
| 3376 | One-dimensional slicing will result in a subview:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3377 | |
| 3378 | >>> v = memoryview(b'abcefg') |
| 3379 | >>> v[1] |
| 3380 | 98 |
| 3381 | >>> v[-1] |
| 3382 | 103 |
| 3383 | >>> v[1:4] |
| 3384 | <memory at 0x7f3ddc9f4350> |
| 3385 | >>> bytes(v[1:4]) |
| 3386 | b'bce' |
| 3387 | |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3388 | If :class:`~memoryview.format` is one of the native format specifiers |
| 3389 | from the :mod:`struct` module, indexing with an integer or a tuple of |
| 3390 | integers is also supported and returns a single *element* with |
| 3391 | the correct type. One-dimensional memoryviews can be indexed |
| 3392 | with an integer or a one-integer tuple. Multi-dimensional memoryviews |
| 3393 | can be indexed with tuples of exactly *ndim* integers where *ndim* is |
| 3394 | the number of dimensions. Zero-dimensional memoryviews can be indexed |
| 3395 | with the empty tuple. |
| 3396 | |
| 3397 | Here is an example with a non-byte format:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3398 | |
| 3399 | >>> import array |
| 3400 | >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444]) |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3401 | >>> m = memoryview(a) |
| 3402 | >>> m[0] |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3403 | -11111111 |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3404 | >>> m[-1] |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3405 | 44444444 |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3406 | >>> m[::2].tolist() |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3407 | [-11111111, -33333333] |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3408 | |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3409 | If the underlying object is writable, the memoryview supports |
| 3410 | one-dimensional slice assignment. Resizing is not allowed:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3411 | |
| 3412 | >>> data = bytearray(b'abcefg') |
| 3413 | >>> v = memoryview(data) |
| 3414 | >>> v.readonly |
| 3415 | False |
| 3416 | >>> v[0] = ord(b'z') |
| 3417 | >>> data |
| 3418 | bytearray(b'zbcefg') |
| 3419 | >>> v[1:4] = b'123' |
| 3420 | >>> data |
| 3421 | bytearray(b'z123fg') |
| 3422 | >>> v[2:3] = b'spam' |
| 3423 | Traceback (most recent call last): |
| 3424 | File "<stdin>", line 1, in <module> |
| 3425 | ValueError: memoryview assignment: lvalue and rvalue have different structures |
| 3426 | >>> v[2:6] = b'spam' |
| 3427 | >>> data |
| 3428 | bytearray(b'z1spam') |
| 3429 | |
Stefan Krah | a3b84fb | 2012-09-02 14:50:56 +0200 | [diff] [blame] | 3430 | One-dimensional memoryviews of hashable (read-only) types with formats |
| 3431 | 'B', 'b' or 'c' are also hashable. The hash is defined as |
| 3432 | ``hash(m) == hash(m.tobytes())``:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3433 | |
| 3434 | >>> v = memoryview(b'abcefg') |
| 3435 | >>> hash(v) == hash(b'abcefg') |
| 3436 | True |
| 3437 | >>> hash(v[2:4]) == hash(b'ce') |
| 3438 | True |
| 3439 | >>> hash(v[::-2]) == hash(b'abcefg'[::-2]) |
| 3440 | True |
| 3441 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3442 | .. versionchanged:: 3.3 |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3443 | One-dimensional memoryviews can now be sliced. |
Stefan Krah | a3b84fb | 2012-09-02 14:50:56 +0200 | [diff] [blame] | 3444 | One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3445 | |
Nick Coghlan | 45163cc | 2013-10-02 22:31:47 +1000 | [diff] [blame] | 3446 | .. versionchanged:: 3.4 |
| 3447 | memoryview is now registered automatically with |
| 3448 | :class:`collections.abc.Sequence` |
| 3449 | |
Antoine Pitrou | 31084ba | 2015-03-19 23:29:36 +0100 | [diff] [blame] | 3450 | .. versionchanged:: 3.5 |
| 3451 | memoryviews can now be indexed with tuple of integers. |
| 3452 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3453 | :class:`memoryview` has several methods: |
| 3454 | |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3455 | .. method:: __eq__(exporter) |
| 3456 | |
| 3457 | A memoryview and a :pep:`3118` exporter are equal if their shapes are |
| 3458 | equivalent and if all corresponding values are equal when the operands' |
| 3459 | respective format codes are interpreted using :mod:`struct` syntax. |
| 3460 | |
| 3461 | For the subset of :mod:`struct` format strings currently supported by |
| 3462 | :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``:: |
| 3463 | |
| 3464 | >>> import array |
| 3465 | >>> a = array.array('I', [1, 2, 3, 4, 5]) |
| 3466 | >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0]) |
| 3467 | >>> c = array.array('b', [5, 3, 1]) |
| 3468 | >>> x = memoryview(a) |
| 3469 | >>> y = memoryview(b) |
| 3470 | >>> x == a == y == b |
| 3471 | True |
| 3472 | >>> x.tolist() == a.tolist() == y.tolist() == b.tolist() |
| 3473 | True |
| 3474 | >>> z = y[::-2] |
| 3475 | >>> z == c |
| 3476 | True |
| 3477 | >>> z.tolist() == c.tolist() |
| 3478 | True |
| 3479 | |
| 3480 | If either format string is not supported by the :mod:`struct` module, |
| 3481 | then the objects will always compare as unequal (even if the format |
| 3482 | strings and buffer contents are identical):: |
| 3483 | |
| 3484 | >>> from ctypes import BigEndianStructure, c_long |
| 3485 | >>> class BEPoint(BigEndianStructure): |
| 3486 | ... _fields_ = [("x", c_long), ("y", c_long)] |
| 3487 | ... |
| 3488 | >>> point = BEPoint(100, 200) |
| 3489 | >>> a = memoryview(point) |
| 3490 | >>> b = memoryview(point) |
| 3491 | >>> a == point |
| 3492 | False |
| 3493 | >>> a == b |
| 3494 | False |
| 3495 | |
| 3496 | Note that, as with floating point numbers, ``v is w`` does *not* imply |
| 3497 | ``v == w`` for memoryview objects. |
| 3498 | |
| 3499 | .. versionchanged:: 3.3 |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 3500 | Previous versions compared the raw memory disregarding the item format |
| 3501 | and the logical array structure. |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3502 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3503 | .. method:: tobytes() |
| 3504 | |
| 3505 | Return the data in the buffer as a bytestring. This is equivalent to |
| 3506 | calling the :class:`bytes` constructor on the memoryview. :: |
| 3507 | |
| 3508 | >>> m = memoryview(b"abc") |
| 3509 | >>> m.tobytes() |
| 3510 | b'abc' |
| 3511 | >>> bytes(m) |
| 3512 | b'abc' |
| 3513 | |
| 3514 | For non-contiguous arrays the result is equal to the flattened list |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3515 | representation with all elements converted to bytes. :meth:`tobytes` |
| 3516 | supports all format strings, including those that are not in |
| 3517 | :mod:`struct` module syntax. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3518 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 3519 | .. method:: hex() |
| 3520 | |
| 3521 | Return a string object containing two hexadecimal digits for each |
| 3522 | byte in the buffer. :: |
| 3523 | |
| 3524 | >>> m = memoryview(b"abc") |
| 3525 | >>> m.hex() |
| 3526 | '616263' |
| 3527 | |
| 3528 | .. versionadded:: 3.5 |
| 3529 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3530 | .. method:: tolist() |
| 3531 | |
| 3532 | Return the data in the buffer as a list of elements. :: |
| 3533 | |
| 3534 | >>> memoryview(b'abc').tolist() |
| 3535 | [97, 98, 99] |
| 3536 | >>> import array |
| 3537 | >>> a = array.array('d', [1.1, 2.2, 3.3]) |
| 3538 | >>> m = memoryview(a) |
| 3539 | >>> m.tolist() |
| 3540 | [1.1, 2.2, 3.3] |
| 3541 | |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 3542 | .. versionchanged:: 3.3 |
| 3543 | :meth:`tolist` now supports all single character native formats in |
| 3544 | :mod:`struct` module syntax as well as multi-dimensional |
| 3545 | representations. |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3546 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3547 | .. method:: release() |
| 3548 | |
| 3549 | Release the underlying buffer exposed by the memoryview object. Many |
| 3550 | objects take special actions when a view is held on them (for example, |
| 3551 | a :class:`bytearray` would temporarily forbid resizing); therefore, |
| 3552 | calling release() is handy to remove these restrictions (and free any |
| 3553 | dangling resources) as soon as possible. |
| 3554 | |
| 3555 | After this method has been called, any further operation on the view |
| 3556 | raises a :class:`ValueError` (except :meth:`release()` itself which can |
| 3557 | be called multiple times):: |
| 3558 | |
| 3559 | >>> m = memoryview(b'abc') |
| 3560 | >>> m.release() |
| 3561 | >>> m[0] |
| 3562 | Traceback (most recent call last): |
| 3563 | File "<stdin>", line 1, in <module> |
| 3564 | ValueError: operation forbidden on released memoryview object |
| 3565 | |
| 3566 | The context management protocol can be used for a similar effect, |
| 3567 | using the ``with`` statement:: |
| 3568 | |
| 3569 | >>> with memoryview(b'abc') as m: |
| 3570 | ... m[0] |
| 3571 | ... |
| 3572 | 97 |
| 3573 | >>> m[0] |
| 3574 | Traceback (most recent call last): |
| 3575 | File "<stdin>", line 1, in <module> |
| 3576 | ValueError: operation forbidden on released memoryview object |
| 3577 | |
| 3578 | .. versionadded:: 3.2 |
| 3579 | |
| 3580 | .. method:: cast(format[, shape]) |
| 3581 | |
| 3582 | Cast a memoryview to a new format or shape. *shape* defaults to |
| 3583 | ``[byte_length//new_itemsize]``, which means that the result view |
| 3584 | will be one-dimensional. The return value is a new memoryview, but |
Stefan Krah | 70e543b | 2015-08-08 14:33:28 +0200 | [diff] [blame] | 3585 | the buffer itself is not copied. Supported casts are 1D -> C-:term:`contiguous` |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3586 | and C-contiguous -> 1D. |
| 3587 | |
Stefan Krah | 0c51595 | 2015-08-08 13:38:10 +0200 | [diff] [blame] | 3588 | The destination format is restricted to a single element native format in |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3589 | :mod:`struct` syntax. One of the formats must be a byte format |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3590 | ('B', 'b' or 'c'). The byte length of the result must be the same |
| 3591 | as the original length. |
| 3592 | |
| 3593 | Cast 1D/long to 1D/unsigned bytes:: |
| 3594 | |
| 3595 | >>> import array |
| 3596 | >>> a = array.array('l', [1,2,3]) |
| 3597 | >>> x = memoryview(a) |
| 3598 | >>> x.format |
| 3599 | 'l' |
| 3600 | >>> x.itemsize |
| 3601 | 8 |
| 3602 | >>> len(x) |
| 3603 | 3 |
| 3604 | >>> x.nbytes |
| 3605 | 24 |
| 3606 | >>> y = x.cast('B') |
| 3607 | >>> y.format |
| 3608 | 'B' |
| 3609 | >>> y.itemsize |
| 3610 | 1 |
| 3611 | >>> len(y) |
| 3612 | 24 |
| 3613 | >>> y.nbytes |
| 3614 | 24 |
| 3615 | |
| 3616 | Cast 1D/unsigned bytes to 1D/char:: |
| 3617 | |
| 3618 | >>> b = bytearray(b'zyz') |
| 3619 | >>> x = memoryview(b) |
| 3620 | >>> x[0] = b'a' |
| 3621 | Traceback (most recent call last): |
| 3622 | File "<stdin>", line 1, in <module> |
| 3623 | ValueError: memoryview: invalid value for format "B" |
| 3624 | >>> y = x.cast('c') |
| 3625 | >>> y[0] = b'a' |
| 3626 | >>> b |
| 3627 | bytearray(b'ayz') |
| 3628 | |
| 3629 | Cast 1D/bytes to 3D/ints to 1D/signed char:: |
| 3630 | |
| 3631 | >>> import struct |
| 3632 | >>> buf = struct.pack("i"*12, *list(range(12))) |
| 3633 | >>> x = memoryview(buf) |
| 3634 | >>> y = x.cast('i', shape=[2,2,3]) |
| 3635 | >>> y.tolist() |
| 3636 | [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]] |
| 3637 | >>> y.format |
| 3638 | 'i' |
| 3639 | >>> y.itemsize |
| 3640 | 4 |
| 3641 | >>> len(y) |
| 3642 | 2 |
| 3643 | >>> y.nbytes |
| 3644 | 48 |
| 3645 | >>> z = y.cast('b') |
| 3646 | >>> z.format |
| 3647 | 'b' |
| 3648 | >>> z.itemsize |
| 3649 | 1 |
| 3650 | >>> len(z) |
| 3651 | 48 |
| 3652 | >>> z.nbytes |
| 3653 | 48 |
| 3654 | |
Terry Jan Reedy | 0f84764 | 2013-03-11 18:34:00 -0400 | [diff] [blame] | 3655 | Cast 1D/unsigned char to 2D/unsigned long:: |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3656 | |
| 3657 | >>> buf = struct.pack("L"*6, *list(range(6))) |
| 3658 | >>> x = memoryview(buf) |
| 3659 | >>> y = x.cast('L', shape=[2,3]) |
| 3660 | >>> len(y) |
| 3661 | 2 |
| 3662 | >>> y.nbytes |
| 3663 | 48 |
| 3664 | >>> y.tolist() |
| 3665 | [[0, 1, 2], [3, 4, 5]] |
| 3666 | |
| 3667 | .. versionadded:: 3.3 |
| 3668 | |
Stefan Krah | 0c51595 | 2015-08-08 13:38:10 +0200 | [diff] [blame] | 3669 | .. versionchanged:: 3.5 |
| 3670 | The source format is no longer restricted when casting to a byte view. |
| 3671 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3672 | There are also several readonly attributes available: |
| 3673 | |
| 3674 | .. attribute:: obj |
| 3675 | |
| 3676 | The underlying object of the memoryview:: |
| 3677 | |
| 3678 | >>> b = bytearray(b'xyz') |
| 3679 | >>> m = memoryview(b) |
| 3680 | >>> m.obj is b |
| 3681 | True |
| 3682 | |
| 3683 | .. versionadded:: 3.3 |
| 3684 | |
| 3685 | .. attribute:: nbytes |
| 3686 | |
| 3687 | ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is |
| 3688 | the amount of space in bytes that the array would use in a contiguous |
| 3689 | representation. It is not necessarily equal to len(m):: |
| 3690 | |
| 3691 | >>> import array |
| 3692 | >>> a = array.array('i', [1,2,3,4,5]) |
| 3693 | >>> m = memoryview(a) |
| 3694 | >>> len(m) |
| 3695 | 5 |
| 3696 | >>> m.nbytes |
| 3697 | 20 |
| 3698 | >>> y = m[::2] |
| 3699 | >>> len(y) |
| 3700 | 3 |
| 3701 | >>> y.nbytes |
| 3702 | 12 |
| 3703 | >>> len(y.tobytes()) |
| 3704 | 12 |
| 3705 | |
| 3706 | Multi-dimensional arrays:: |
| 3707 | |
| 3708 | >>> import struct |
| 3709 | >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)]) |
| 3710 | >>> x = memoryview(buf) |
| 3711 | >>> y = x.cast('d', shape=[3,4]) |
| 3712 | >>> y.tolist() |
| 3713 | [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]] |
| 3714 | >>> len(y) |
| 3715 | 3 |
| 3716 | >>> y.nbytes |
| 3717 | 96 |
| 3718 | |
| 3719 | .. versionadded:: 3.3 |
| 3720 | |
| 3721 | .. attribute:: readonly |
| 3722 | |
| 3723 | A bool indicating whether the memory is read only. |
| 3724 | |
| 3725 | .. attribute:: format |
| 3726 | |
| 3727 | A string containing the format (in :mod:`struct` module style) for each |
| 3728 | element in the view. A memoryview can be created from exporters with |
| 3729 | arbitrary format strings, but some methods (e.g. :meth:`tolist`) are |
Nick Coghlan | 06e1ab0 | 2012-08-25 17:59:50 +1000 | [diff] [blame] | 3730 | restricted to native single element formats. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3731 | |
Stefan Krah | ab0c3c7 | 2012-08-30 12:09:09 +0200 | [diff] [blame] | 3732 | .. versionchanged:: 3.3 |
| 3733 | format ``'B'`` is now handled according to the struct module syntax. |
| 3734 | This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``. |
| 3735 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3736 | .. attribute:: itemsize |
| 3737 | |
| 3738 | The size in bytes of each element of the memoryview:: |
| 3739 | |
| 3740 | >>> import array, struct |
| 3741 | >>> m = memoryview(array.array('H', [32000, 32001, 32002])) |
| 3742 | >>> m.itemsize |
| 3743 | 2 |
| 3744 | >>> m[0] |
| 3745 | 32000 |
| 3746 | >>> struct.calcsize('H') == m.itemsize |
| 3747 | True |
| 3748 | |
| 3749 | .. attribute:: ndim |
| 3750 | |
| 3751 | An integer indicating how many dimensions of a multi-dimensional array the |
| 3752 | memory represents. |
| 3753 | |
| 3754 | .. attribute:: shape |
| 3755 | |
| 3756 | 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] | 3757 | memory as an N-dimensional array. |
| 3758 | |
| 3759 | .. versionchanged:: 3.3 |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 3760 | An empty tuple instead of ``None`` when ndim = 0. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3761 | |
| 3762 | .. attribute:: strides |
| 3763 | |
| 3764 | A tuple of integers the length of :attr:`ndim` giving the size in bytes to |
| 3765 | access each element for each dimension of the array. |
| 3766 | |
Alexander Belopolsky | e8677c0 | 2012-09-03 17:29:22 -0400 | [diff] [blame] | 3767 | .. versionchanged:: 3.3 |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 3768 | An empty tuple instead of ``None`` when ndim = 0. |
Alexander Belopolsky | e8677c0 | 2012-09-03 17:29:22 -0400 | [diff] [blame] | 3769 | |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3770 | .. attribute:: suboffsets |
| 3771 | |
| 3772 | Used internally for PIL-style arrays. The value is informational only. |
| 3773 | |
| 3774 | .. attribute:: c_contiguous |
| 3775 | |
Stefan Krah | 70e543b | 2015-08-08 14:33:28 +0200 | [diff] [blame] | 3776 | A bool indicating whether the memory is C-:term:`contiguous`. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3777 | |
| 3778 | .. versionadded:: 3.3 |
| 3779 | |
| 3780 | .. attribute:: f_contiguous |
| 3781 | |
Stefan Krah | 70e543b | 2015-08-08 14:33:28 +0200 | [diff] [blame] | 3782 | A bool indicating whether the memory is Fortran :term:`contiguous`. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3783 | |
| 3784 | .. versionadded:: 3.3 |
| 3785 | |
| 3786 | .. attribute:: contiguous |
| 3787 | |
Stefan Krah | 70e543b | 2015-08-08 14:33:28 +0200 | [diff] [blame] | 3788 | A bool indicating whether the memory is :term:`contiguous`. |
Nick Coghlan | 273069c | 2012-08-20 17:14:07 +1000 | [diff] [blame] | 3789 | |
| 3790 | .. versionadded:: 3.3 |
| 3791 | |
| 3792 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3793 | .. _types-set: |
| 3794 | |
| 3795 | Set Types --- :class:`set`, :class:`frozenset` |
| 3796 | ============================================== |
| 3797 | |
| 3798 | .. index:: object: set |
| 3799 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 3800 | 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] | 3801 | Common uses include membership testing, removing duplicates from a sequence, and |
| 3802 | computing mathematical operations such as intersection, union, difference, and |
| 3803 | symmetric difference. |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 3804 | (For other containers see the built-in :class:`dict`, :class:`list`, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3805 | and :class:`tuple` classes, and the :mod:`collections` module.) |
| 3806 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3807 | Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in |
| 3808 | set``. Being an unordered collection, sets do not record element position or |
| 3809 | order of insertion. Accordingly, sets do not support indexing, slicing, or |
| 3810 | other sequence-like behavior. |
| 3811 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 3812 | 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] | 3813 | The :class:`set` type is mutable --- the contents can be changed using methods |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 3814 | like :meth:`~set.add` and :meth:`~set.remove`. Since it is mutable, it has no |
| 3815 | hash value and cannot be used as either a dictionary key or as an element of |
| 3816 | another set. The :class:`frozenset` type is immutable and :term:`hashable` --- |
| 3817 | its contents cannot be altered after it is created; it can therefore be used as |
| 3818 | a dictionary key or as an element of another set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3819 | |
Georg Brandl | 99cd957 | 2010-03-21 09:10:32 +0000 | [diff] [blame] | 3820 | 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] | 3821 | of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the |
| 3822 | :class:`set` constructor. |
Georg Brandl | 99cd957 | 2010-03-21 09:10:32 +0000 | [diff] [blame] | 3823 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3824 | The constructors for both classes work the same: |
| 3825 | |
| 3826 | .. class:: set([iterable]) |
| 3827 | frozenset([iterable]) |
| 3828 | |
| 3829 | Return a new set or frozenset object whose elements are taken from |
Andrew Svetlov | 9a411ce | 2013-04-05 16:21:50 +0300 | [diff] [blame] | 3830 | *iterable*. The elements of a set must be :term:`hashable`. To |
| 3831 | represent sets of sets, the inner sets must be :class:`frozenset` |
| 3832 | objects. If *iterable* is not specified, a new empty set is |
| 3833 | returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3834 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3835 | Instances of :class:`set` and :class:`frozenset` provide the following |
| 3836 | operations: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3837 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3838 | .. describe:: len(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3839 | |
Gregory P. Smith | e27403b | 2016-02-08 09:58:40 -0800 | [diff] [blame] | 3840 | Return the number of elements in set *s* (cardinality of *s*). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3841 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3842 | .. describe:: x in s |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3843 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3844 | Test *x* for membership in *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3845 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3846 | .. describe:: x not in s |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3847 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3848 | Test *x* for non-membership in *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3849 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3850 | .. method:: isdisjoint(other) |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3851 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 3852 | 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] | 3853 | disjoint if and only if their intersection is the empty set. |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 3854 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3855 | .. method:: issubset(other) |
| 3856 | set <= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3857 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3858 | Test whether every element in the set is in *other*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3859 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3860 | .. method:: set < other |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 3861 | |
Andrew Svetlov | 5bb4207 | 2012-11-01 21:47:54 +0200 | [diff] [blame] | 3862 | Test whether the set is a proper subset of *other*, that is, |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3863 | ``set <= other and set != other``. |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 3864 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3865 | .. method:: issuperset(other) |
| 3866 | set >= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3867 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3868 | Test whether every element in *other* is in the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3869 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3870 | .. method:: set > other |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 3871 | |
Andrew Svetlov | 5bb4207 | 2012-11-01 21:47:54 +0200 | [diff] [blame] | 3872 | 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] | 3873 | other and set != other``. |
Georg Brandl | a6f5278 | 2007-09-01 15:49:30 +0000 | [diff] [blame] | 3874 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3875 | .. method:: union(*others) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 3876 | set | other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3877 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 3878 | Return a new set with elements from the set and all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3879 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3880 | .. method:: intersection(*others) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 3881 | set & other & ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3882 | |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 3883 | 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] | 3884 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3885 | .. method:: difference(*others) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 3886 | set - other - ... |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 3887 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 3888 | 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] | 3889 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3890 | .. method:: symmetric_difference(other) |
| 3891 | set ^ other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3892 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3893 | 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] | 3894 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3895 | .. method:: copy() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3896 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3897 | Return a new set with a shallow copy of *s*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3898 | |
| 3899 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3900 | Note, the non-operator versions of :meth:`union`, :meth:`intersection`, |
| 3901 | :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and |
| 3902 | :meth:`issuperset` methods will accept any iterable as an argument. In |
| 3903 | contrast, their operator based counterparts require their arguments to be |
| 3904 | sets. This precludes error-prone constructions like ``set('abc') & 'cbs'`` |
| 3905 | in favor of the more readable ``set('abc').intersection('cbs')``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3906 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3907 | Both :class:`set` and :class:`frozenset` support set to set comparisons. Two |
| 3908 | sets are equal if and only if every element of each set is contained in the |
| 3909 | other (each is a subset of the other). A set is less than another set if and |
| 3910 | only if the first set is a proper subset of the second set (is a subset, but |
| 3911 | is not equal). A set is greater than another set if and only if the first set |
| 3912 | 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] | 3913 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3914 | Instances of :class:`set` are compared to instances of :class:`frozenset` |
| 3915 | based on their members. For example, ``set('abc') == frozenset('abc')`` |
| 3916 | returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3917 | |
Raymond Hettinger | 12f588a | 2013-05-06 18:22:43 -0700 | [diff] [blame] | 3918 | The subset and equality comparisons do not generalize to a total ordering |
| 3919 | function. For example, any two nonempty disjoint sets are not equal and are not |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3920 | 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] | 3921 | ``a==b``, or ``a>b``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3922 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3923 | Since sets only define partial ordering (subset relationships), the output of |
| 3924 | the :meth:`list.sort` method is undefined for lists of sets. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3925 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3926 | Set elements, like dictionary keys, must be :term:`hashable`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3927 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3928 | Binary operations that mix :class:`set` instances with :class:`frozenset` |
| 3929 | return the type of the first operand. For example: ``frozenset('ab') | |
| 3930 | set('bc')`` returns an instance of :class:`frozenset`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3931 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3932 | The following table lists operations available for :class:`set` that do not |
| 3933 | apply to immutable instances of :class:`frozenset`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3934 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3935 | .. method:: update(*others) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 3936 | set |= other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3937 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 3938 | Update the set, adding elements from all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3939 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3940 | .. method:: intersection_update(*others) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 3941 | set &= other & ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3942 | |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 3943 | Update the set, keeping only elements found in it and all others. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3944 | |
Raymond Hettinger | a33e9f7 | 2016-09-12 23:38:50 -0700 | [diff] [blame] | 3945 | .. method:: difference_update(*others) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 3946 | set -= other | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3947 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 3948 | Update the set, removing elements found in others. |
| 3949 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3950 | .. method:: symmetric_difference_update(other) |
| 3951 | set ^= other |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3952 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3953 | 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] | 3954 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3955 | .. method:: add(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3956 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3957 | Add element *elem* to the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3958 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3959 | .. method:: remove(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3960 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3961 | Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is |
| 3962 | not contained in the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3963 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3964 | .. method:: discard(elem) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3965 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3966 | Remove element *elem* from the set if it is present. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3967 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3968 | .. method:: pop() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3969 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3970 | Remove and return an arbitrary element from the set. Raises |
| 3971 | :exc:`KeyError` if the set is empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3972 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3973 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3974 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3975 | Remove all elements from the set. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3976 | |
| 3977 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3978 | Note, the non-operator versions of the :meth:`update`, |
| 3979 | :meth:`intersection_update`, :meth:`difference_update`, and |
| 3980 | :meth:`symmetric_difference_update` methods will accept any iterable as an |
| 3981 | argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3982 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 3983 | Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and |
| 3984 | :meth:`discard` methods may be a set. To support searching for an equivalent |
| 3985 | frozenset, the *elem* set is temporarily mutated during the search and then |
| 3986 | restored. During the search, the *elem* set should not be read or mutated |
| 3987 | since it does not have a meaningful value. |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 3988 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3989 | |
| 3990 | .. _typesmapping: |
| 3991 | |
| 3992 | Mapping Types --- :class:`dict` |
| 3993 | =============================== |
| 3994 | |
| 3995 | .. index:: |
| 3996 | object: mapping |
| 3997 | object: dictionary |
| 3998 | triple: operations on; mapping; types |
| 3999 | triple: operations on; dictionary; type |
| 4000 | statement: del |
| 4001 | builtin: len |
| 4002 | |
Chris Jerdonek | 11f3f17 | 2012-11-03 12:05:55 -0700 | [diff] [blame] | 4003 | A :term:`mapping` object maps :term:`hashable` values to arbitrary objects. |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 4004 | Mappings are mutable objects. There is currently only one standard mapping |
Nick Coghlan | 83c0ae5 | 2012-08-21 17:42:52 +1000 | [diff] [blame] | 4005 | type, the :dfn:`dictionary`. (For other containers see the built-in |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 4006 | :class:`list`, :class:`set`, and :class:`tuple` classes, and the |
| 4007 | :mod:`collections` module.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4008 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 4009 | A dictionary's keys are *almost* arbitrary values. Values that are not |
| 4010 | :term:`hashable`, that is, values containing lists, dictionaries or other |
| 4011 | mutable types (that are compared by value rather than by object identity) may |
| 4012 | not be used as keys. Numeric types used for keys obey the normal rules for |
| 4013 | numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``) |
| 4014 | then they can be used interchangeably to index the same dictionary entry. (Note |
| 4015 | however, that since computers store floating-point numbers as approximations it |
| 4016 | is usually unwise to use them as dictionary keys.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4017 | |
| 4018 | Dictionaries can be created by placing a comma-separated list of ``key: value`` |
| 4019 | pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: |
| 4020 | 'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. |
| 4021 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4022 | .. class:: dict(**kwarg) |
| 4023 | dict(mapping, **kwarg) |
| 4024 | dict(iterable, **kwarg) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4025 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4026 | Return a new dictionary initialized from an optional positional argument |
| 4027 | and a possibly empty set of keyword arguments. |
| 4028 | |
| 4029 | If no positional argument is given, an empty dictionary is created. |
| 4030 | If a positional argument is given and it is a mapping object, a dictionary |
| 4031 | is created with the same key-value pairs as the mapping object. Otherwise, |
Terry Jan Reedy | b52f876 | 2014-06-02 20:42:56 -0400 | [diff] [blame] | 4032 | the positional argument must be an :term:`iterable` object. Each item in |
| 4033 | the iterable must itself be an iterable with exactly two objects. The |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4034 | first object of each item becomes a key in the new dictionary, and the |
| 4035 | second object the corresponding value. If a key occurs more than once, the |
| 4036 | last value for that key becomes the corresponding value in the new |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 4037 | dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4038 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4039 | If keyword arguments are given, the keyword arguments and their values are |
| 4040 | added to the dictionary created from the positional argument. If a key |
| 4041 | being added is already present, the value from the keyword argument |
| 4042 | replaces the value from the positional argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4043 | |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4044 | To illustrate, the following examples all return a dictionary equal to |
Ezio Melotti | a20879f | 2012-10-26 19:14:16 +0300 | [diff] [blame] | 4045 | ``{"one": 1, "two": 2, "three": 3}``:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4046 | |
Ezio Melotti | a20879f | 2012-10-26 19:14:16 +0300 | [diff] [blame] | 4047 | >>> a = dict(one=1, two=2, three=3) |
| 4048 | >>> b = {'one': 1, 'two': 2, 'three': 3} |
| 4049 | >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) |
| 4050 | >>> d = dict([('two', 2), ('one', 1), ('three', 3)]) |
| 4051 | >>> e = dict({'three': 3, 'one': 1, 'two': 2}) |
Chris Jerdonek | f341317 | 2012-10-13 03:22:33 -0700 | [diff] [blame] | 4052 | >>> a == b == c == d == e |
| 4053 | True |
| 4054 | |
| 4055 | Providing keyword arguments as in the first example only works for keys that |
| 4056 | are valid Python identifiers. Otherwise, any valid keys can be used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4057 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4058 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4059 | These are the operations that dictionaries support (and therefore, custom |
| 4060 | mapping types should support too): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4061 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4062 | .. describe:: len(d) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4063 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4064 | Return the number of items in the dictionary *d*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4065 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4066 | .. describe:: d[key] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4067 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4068 | Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is |
| 4069 | not in the map. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 4070 | |
Terry Jan Reedy | 06c6218 | 2014-12-10 18:48:23 -0500 | [diff] [blame] | 4071 | .. index:: __missing__() |
Terry Jan Reedy | e40031d | 2014-12-10 18:49:58 -0500 | [diff] [blame] | 4072 | |
Terry Jan Reedy | b67f6e2 | 2014-12-10 18:38:19 -0500 | [diff] [blame] | 4073 | If a subclass of dict defines a method :meth:`__missing__` and *key* |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4074 | is not present, the ``d[key]`` operation calls that method with the key *key* |
| 4075 | as argument. The ``d[key]`` operation then returns or raises whatever is |
Terry Jan Reedy | b67f6e2 | 2014-12-10 18:38:19 -0500 | [diff] [blame] | 4076 | returned or raised by the ``__missing__(key)`` call. |
| 4077 | No other operations or methods invoke :meth:`__missing__`. If |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4078 | :meth:`__missing__` is not defined, :exc:`KeyError` is raised. |
Raymond Hettinger | 5254e97 | 2011-01-08 09:35:38 +0000 | [diff] [blame] | 4079 | :meth:`__missing__` must be a method; it cannot be an instance variable:: |
| 4080 | |
| 4081 | >>> class Counter(dict): |
| 4082 | ... def __missing__(self, key): |
| 4083 | ... return 0 |
| 4084 | >>> c = Counter() |
| 4085 | >>> c['red'] |
| 4086 | 0 |
| 4087 | >>> c['red'] += 1 |
| 4088 | >>> c['red'] |
| 4089 | 1 |
| 4090 | |
Terry Jan Reedy | b67f6e2 | 2014-12-10 18:38:19 -0500 | [diff] [blame] | 4091 | The example above shows part of the implementation of |
| 4092 | :class:`collections.Counter`. A different ``__missing__`` method is used |
| 4093 | by :class:`collections.defaultdict`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4094 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4095 | .. describe:: d[key] = value |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4096 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4097 | Set ``d[key]`` to *value*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4098 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4099 | .. describe:: del d[key] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4100 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4101 | Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the |
| 4102 | map. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4103 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4104 | .. describe:: key in d |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4105 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4106 | Return ``True`` if *d* has a key *key*, else ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4107 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4108 | .. describe:: key not in d |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4109 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4110 | Equivalent to ``not key in d``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4111 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4112 | .. describe:: iter(d) |
| 4113 | |
| 4114 | 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] | 4115 | for ``iter(d.keys())``. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4116 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4117 | .. method:: clear() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4118 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4119 | Remove all items from the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4120 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4121 | .. method:: copy() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4122 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4123 | Return a shallow copy of the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4124 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 4125 | .. classmethod:: fromkeys(seq[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4126 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4127 | 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] | 4128 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4129 | :meth:`fromkeys` is a class method that returns a new dictionary. *value* |
| 4130 | defaults to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4131 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4132 | .. method:: get(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4133 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4134 | Return the value for *key* if *key* is in the dictionary, else *default*. |
| 4135 | If *default* is not given, it defaults to ``None``, so that this method |
| 4136 | never raises a :exc:`KeyError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4137 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4138 | .. method:: items() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4139 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 4140 | Return a new view of the dictionary's items (``(key, value)`` pairs). |
| 4141 | See the :ref:`documentation of view objects <dict-views>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4142 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4143 | .. method:: keys() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4144 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 4145 | Return a new view of the dictionary's keys. See the :ref:`documentation |
| 4146 | of view objects <dict-views>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4147 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4148 | .. method:: pop(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4149 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4150 | If *key* is in the dictionary, remove it and return its value, else return |
| 4151 | *default*. If *default* is not given and *key* is not in the dictionary, |
| 4152 | a :exc:`KeyError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4153 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4154 | .. method:: popitem() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4155 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4156 | Remove and return an arbitrary ``(key, value)`` pair from the dictionary. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4157 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4158 | :meth:`popitem` is useful to destructively iterate over a dictionary, as |
| 4159 | often used in set algorithms. If the dictionary is empty, calling |
| 4160 | :meth:`popitem` raises a :exc:`KeyError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4161 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4162 | .. method:: setdefault(key[, default]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4163 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4164 | If *key* is in the dictionary, return its value. If not, insert *key* |
| 4165 | with a value of *default* and return *default*. *default* defaults to |
| 4166 | ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4167 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4168 | .. method:: update([other]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4169 | |
Éric Araujo | 0fc86b8 | 2010-08-18 22:29:54 +0000 | [diff] [blame] | 4170 | Update the dictionary with the key/value pairs from *other*, overwriting |
| 4171 | existing keys. Return ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4172 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4173 | :meth:`update` accepts either another dictionary object or an iterable of |
Georg Brandl | fda2106 | 2010-09-25 16:56:36 +0000 | [diff] [blame] | 4174 | 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] | 4175 | arguments are specified, the dictionary is then updated with those |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4176 | key/value pairs: ``d.update(red=1, blue=2)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4177 | |
Alexandre Vassalotti | a79e33e | 2008-05-15 22:51:26 +0000 | [diff] [blame] | 4178 | .. method:: values() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4179 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 4180 | Return a new view of the dictionary's values. See the |
| 4181 | :ref:`documentation of view objects <dict-views>`. |
| 4182 | |
Terry Jan Reedy | fe63c9a | 2015-06-12 16:38:57 -0400 | [diff] [blame] | 4183 | Dictionaries compare equal if and only if they have the same ``(key, |
| 4184 | value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise |
| 4185 | :exc:`TypeError`. |
Terry Jan Reedy | 6ac5cc1 | 2015-06-12 16:47:44 -0400 | [diff] [blame] | 4186 | |
Victor Stinner | 0db176f | 2012-04-16 00:16:30 +0200 | [diff] [blame] | 4187 | .. seealso:: |
| 4188 | :class:`types.MappingProxyType` can be used to create a read-only view |
| 4189 | of a :class:`dict`. |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 4190 | |
| 4191 | |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 4192 | .. _dict-views: |
| 4193 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 4194 | Dictionary view objects |
| 4195 | ----------------------- |
| 4196 | |
| 4197 | The objects returned by :meth:`dict.keys`, :meth:`dict.values` and |
| 4198 | :meth:`dict.items` are *view objects*. They provide a dynamic view on the |
| 4199 | dictionary's entries, which means that when the dictionary changes, the view |
Benjamin Peterson | ce0506c | 2008-11-17 21:47:41 +0000 | [diff] [blame] | 4200 | reflects these changes. |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 4201 | |
| 4202 | Dictionary views can be iterated over to yield their respective data, and |
| 4203 | support membership tests: |
| 4204 | |
| 4205 | .. describe:: len(dictview) |
| 4206 | |
| 4207 | Return the number of entries in the dictionary. |
| 4208 | |
| 4209 | .. describe:: iter(dictview) |
| 4210 | |
| 4211 | Return an iterator over the keys, values or items (represented as tuples of |
| 4212 | ``(key, value)``) in the dictionary. |
| 4213 | |
| 4214 | Keys and values are iterated over in an arbitrary order which is non-random, |
| 4215 | varies across Python implementations, and depends on the dictionary's history |
| 4216 | of insertions and deletions. If keys, values and items views are iterated |
| 4217 | over with no intervening modifications to the dictionary, the order of items |
| 4218 | will directly correspond. This allows the creation of ``(value, key)`` pairs |
| 4219 | using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to |
| 4220 | create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``. |
| 4221 | |
Georg Brandl | 8126914 | 2009-05-17 08:31:29 +0000 | [diff] [blame] | 4222 | Iterating views while adding or deleting entries in the dictionary may raise |
| 4223 | a :exc:`RuntimeError` or fail to iterate over all entries. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4224 | |
Georg Brandl | d22a815 | 2007-09-04 17:43:37 +0000 | [diff] [blame] | 4225 | .. describe:: x in dictview |
| 4226 | |
| 4227 | Return ``True`` if *x* is in the underlying dictionary's keys, values or |
| 4228 | items (in the latter case, *x* should be a ``(key, value)`` tuple). |
| 4229 | |
| 4230 | |
Benjamin Peterson | ce0506c | 2008-11-17 21:47:41 +0000 | [diff] [blame] | 4231 | 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] | 4232 | values are hashable, so that ``(key, value)`` pairs are unique and hashable, |
| 4233 | then the items view is also set-like. (Values views are not treated as set-like |
| 4234 | 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] | 4235 | operations defined for the abstract base class :class:`collections.abc.Set` are |
Georg Brandl | f74cf77 | 2010-10-15 16:03:02 +0000 | [diff] [blame] | 4236 | available (for example, ``==``, ``<``, or ``^``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4237 | |
Georg Brandl | c53c966 | 2007-09-04 17:58:02 +0000 | [diff] [blame] | 4238 | An example of dictionary view usage:: |
| 4239 | |
| 4240 | >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} |
| 4241 | >>> keys = dishes.keys() |
| 4242 | >>> values = dishes.values() |
| 4243 | |
| 4244 | >>> # iteration |
| 4245 | >>> n = 0 |
| 4246 | >>> for val in values: |
| 4247 | ... n += val |
| 4248 | >>> print(n) |
| 4249 | 504 |
| 4250 | |
| 4251 | >>> # keys and values are iterated over in the same order |
| 4252 | >>> list(keys) |
| 4253 | ['eggs', 'bacon', 'sausage', 'spam'] |
| 4254 | >>> list(values) |
| 4255 | [2, 1, 1, 500] |
| 4256 | |
| 4257 | >>> # view objects are dynamic and reflect dict changes |
| 4258 | >>> del dishes['eggs'] |
| 4259 | >>> del dishes['sausage'] |
| 4260 | >>> list(keys) |
| 4261 | ['spam', 'bacon'] |
| 4262 | |
| 4263 | >>> # set operations |
| 4264 | >>> keys & {'eggs', 'bacon', 'salad'} |
Gregory P. Smith | e838812 | 2008-09-04 04:18:09 +0000 | [diff] [blame] | 4265 | {'bacon'} |
Georg Brandl | f74cf77 | 2010-10-15 16:03:02 +0000 | [diff] [blame] | 4266 | >>> keys ^ {'sausage', 'juice'} |
Sandro Tosi | 2a8d195 | 2011-08-02 18:42:04 +0200 | [diff] [blame] | 4267 | {'juice', 'sausage', 'bacon', 'spam'} |
Georg Brandl | c53c966 | 2007-09-04 17:58:02 +0000 | [diff] [blame] | 4268 | |
| 4269 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4270 | .. _typecontextmanager: |
| 4271 | |
| 4272 | Context Manager Types |
| 4273 | ===================== |
| 4274 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4275 | .. index:: |
| 4276 | single: context manager |
| 4277 | single: context management protocol |
| 4278 | single: protocol; context management |
| 4279 | |
| 4280 | Python's :keyword:`with` statement supports the concept of a runtime context |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 4281 | 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] | 4282 | 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] | 4283 | before the statement body is executed and exited when the statement ends: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4284 | |
| 4285 | |
| 4286 | .. method:: contextmanager.__enter__() |
| 4287 | |
| 4288 | Enter the runtime context and return either this object or another object |
| 4289 | related to the runtime context. The value returned by this method is bound to |
| 4290 | the identifier in the :keyword:`as` clause of :keyword:`with` statements using |
| 4291 | this context manager. |
| 4292 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 4293 | An example of a context manager that returns itself is a :term:`file object`. |
| 4294 | File objects return themselves from __enter__() to allow :func:`open` to be |
| 4295 | used as the context expression in a :keyword:`with` statement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4296 | |
| 4297 | 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] | 4298 | returned by :func:`decimal.localcontext`. These managers set the active |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4299 | decimal context to a copy of the original decimal context and then return the |
| 4300 | copy. This allows changes to be made to the current decimal context in the body |
| 4301 | of the :keyword:`with` statement without affecting code outside the |
| 4302 | :keyword:`with` statement. |
| 4303 | |
| 4304 | |
| 4305 | .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb) |
| 4306 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 4307 | 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] | 4308 | that occurred should be suppressed. If an exception occurred while executing the |
| 4309 | body of the :keyword:`with` statement, the arguments contain the exception type, |
| 4310 | value and traceback information. Otherwise, all three arguments are ``None``. |
| 4311 | |
| 4312 | Returning a true value from this method will cause the :keyword:`with` statement |
| 4313 | to suppress the exception and continue execution with the statement immediately |
| 4314 | following the :keyword:`with` statement. Otherwise the exception continues |
| 4315 | propagating after this method has finished executing. Exceptions that occur |
| 4316 | during execution of this method will replace any exception that occurred in the |
| 4317 | body of the :keyword:`with` statement. |
| 4318 | |
| 4319 | The exception passed in should never be reraised explicitly - instead, this |
| 4320 | method should return a false value to indicate that the method completed |
| 4321 | successfully and does not want to suppress the raised exception. This allows |
Georg Brandl | e4196d3 | 2014-10-31 09:41:46 +0100 | [diff] [blame] | 4322 | context management code to easily detect whether or not an :meth:`__exit__` |
| 4323 | method has actually failed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4324 | |
| 4325 | Python defines several context managers to support easy thread synchronisation, |
| 4326 | prompt closure of files or other objects, and simpler manipulation of the active |
| 4327 | decimal arithmetic context. The specific types are not treated specially beyond |
| 4328 | their implementation of the context management protocol. See the |
| 4329 | :mod:`contextlib` module for some examples. |
| 4330 | |
Antoine Pitrou | a654090 | 2010-12-12 20:09:18 +0000 | [diff] [blame] | 4331 | Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 4332 | 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] | 4333 | decorated with the :class:`contextlib.contextmanager` decorator, it will return a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4334 | context manager implementing the necessary :meth:`__enter__` and |
| 4335 | :meth:`__exit__` methods, rather than the iterator produced by an undecorated |
| 4336 | generator function. |
| 4337 | |
| 4338 | Note that there is no specific slot for any of these methods in the type |
| 4339 | structure for Python objects in the Python/C API. Extension types wanting to |
| 4340 | define these methods must provide them as a normal Python accessible method. |
| 4341 | Compared to the overhead of setting up the runtime context, the overhead of a |
| 4342 | single class dictionary lookup is negligible. |
| 4343 | |
| 4344 | |
| 4345 | .. _typesother: |
| 4346 | |
| 4347 | Other Built-in Types |
| 4348 | ==================== |
| 4349 | |
| 4350 | The interpreter supports several other kinds of objects. Most of these support |
| 4351 | only one or two operations. |
| 4352 | |
| 4353 | |
| 4354 | .. _typesmodules: |
| 4355 | |
| 4356 | Modules |
| 4357 | ------- |
| 4358 | |
| 4359 | The only special operation on a module is attribute access: ``m.name``, where |
| 4360 | *m* is a module and *name* accesses a name defined in *m*'s symbol table. |
| 4361 | Module attributes can be assigned to. (Note that the :keyword:`import` |
| 4362 | statement is not, strictly speaking, an operation on a module object; ``import |
| 4363 | foo`` does not require a module object named *foo* to exist, rather it requires |
| 4364 | an (external) *definition* for a module named *foo* somewhere.) |
| 4365 | |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 4366 | A special attribute of every module is :attr:`~object.__dict__`. This is the |
| 4367 | dictionary containing the module's symbol table. Modifying this dictionary will |
| 4368 | actually change the module's symbol table, but direct assignment to the |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4369 | :attr:`~object.__dict__` attribute is not possible (you can write |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 4370 | ``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but you can't write |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4371 | ``m.__dict__ = {}``). Modifying :attr:`~object.__dict__` directly is |
| 4372 | not recommended. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4373 | |
| 4374 | Modules built into the interpreter are written like this: ``<module 'sys' |
| 4375 | (built-in)>``. If loaded from a file, they are written as ``<module 'os' from |
| 4376 | '/usr/local/lib/pythonX.Y/os.pyc'>``. |
| 4377 | |
| 4378 | |
| 4379 | .. _typesobjects: |
| 4380 | |
| 4381 | Classes and Class Instances |
| 4382 | --------------------------- |
| 4383 | |
| 4384 | See :ref:`objects` and :ref:`class` for these. |
| 4385 | |
| 4386 | |
| 4387 | .. _typesfunctions: |
| 4388 | |
| 4389 | Functions |
| 4390 | --------- |
| 4391 | |
| 4392 | Function objects are created by function definitions. The only operation on a |
| 4393 | function object is to call it: ``func(argument-list)``. |
| 4394 | |
| 4395 | There are really two flavors of function objects: built-in functions and |
| 4396 | user-defined functions. Both support the same operation (to call the function), |
| 4397 | but the implementation is different, hence the different object types. |
| 4398 | |
| 4399 | See :ref:`function` for more information. |
| 4400 | |
| 4401 | |
| 4402 | .. _typesmethods: |
| 4403 | |
| 4404 | Methods |
| 4405 | ------- |
| 4406 | |
| 4407 | .. index:: object: method |
| 4408 | |
| 4409 | Methods are functions that are called using the attribute notation. There are |
| 4410 | two flavors: built-in methods (such as :meth:`append` on lists) and class |
| 4411 | instance methods. Built-in methods are described with the types that support |
| 4412 | them. |
| 4413 | |
Georg Brandl | 2e0b755 | 2007-11-27 12:43:08 +0000 | [diff] [blame] | 4414 | If you access a method (a function defined in a class namespace) through an |
| 4415 | instance, you get a special object: a :dfn:`bound method` (also called |
| 4416 | :dfn:`instance method`) object. When called, it will add the ``self`` argument |
| 4417 | to the argument list. Bound methods have two special read-only attributes: |
| 4418 | ``m.__self__`` is the object on which the method operates, and ``m.__func__`` is |
| 4419 | the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)`` |
| 4420 | is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ..., |
| 4421 | arg-n)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4422 | |
Georg Brandl | 2e0b755 | 2007-11-27 12:43:08 +0000 | [diff] [blame] | 4423 | Like function objects, bound method objects support getting arbitrary |
| 4424 | attributes. However, since method attributes are actually stored on the |
| 4425 | underlying function object (``meth.__func__``), setting method attributes on |
Ezio Melotti | 8b6b176 | 2012-11-09 01:08:25 +0200 | [diff] [blame] | 4426 | bound methods is disallowed. Attempting to set an attribute on a method |
| 4427 | results in an :exc:`AttributeError` being raised. In order to set a method |
| 4428 | attribute, you need to explicitly set it on the underlying function object:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4429 | |
Ezio Melotti | 8b6b176 | 2012-11-09 01:08:25 +0200 | [diff] [blame] | 4430 | >>> class C: |
| 4431 | ... def method(self): |
| 4432 | ... pass |
| 4433 | ... |
| 4434 | >>> c = C() |
| 4435 | >>> c.method.whoami = 'my name is method' # can't set on the method |
| 4436 | Traceback (most recent call last): |
| 4437 | File "<stdin>", line 1, in <module> |
| 4438 | AttributeError: 'method' object has no attribute 'whoami' |
| 4439 | >>> c.method.__func__.whoami = 'my name is method' |
| 4440 | >>> c.method.whoami |
| 4441 | 'my name is method' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4442 | |
| 4443 | See :ref:`types` for more information. |
| 4444 | |
| 4445 | |
Tommy Beadle | e9b8403 | 2016-06-02 19:26:51 -0400 | [diff] [blame] | 4446 | .. index:: object; code, code object |
| 4447 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4448 | .. _bltin-code-objects: |
| 4449 | |
| 4450 | Code Objects |
| 4451 | ------------ |
| 4452 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4453 | .. index:: |
| 4454 | builtin: compile |
| 4455 | single: __code__ (function object attribute) |
| 4456 | |
| 4457 | Code objects are used by the implementation to represent "pseudo-compiled" |
| 4458 | executable Python code such as a function body. They differ from function |
| 4459 | objects because they don't contain a reference to their global execution |
| 4460 | environment. Code objects are returned by the built-in :func:`compile` function |
| 4461 | and can be extracted from function objects through their :attr:`__code__` |
| 4462 | attribute. See also the :mod:`code` module. |
| 4463 | |
| 4464 | .. index:: |
| 4465 | builtin: exec |
| 4466 | builtin: eval |
| 4467 | |
| 4468 | A code object can be executed or evaluated by passing it (instead of a source |
| 4469 | string) to the :func:`exec` or :func:`eval` built-in functions. |
| 4470 | |
| 4471 | See :ref:`types` for more information. |
| 4472 | |
| 4473 | |
| 4474 | .. _bltin-type-objects: |
| 4475 | |
| 4476 | Type Objects |
| 4477 | ------------ |
| 4478 | |
| 4479 | .. index:: |
| 4480 | builtin: type |
| 4481 | module: types |
| 4482 | |
| 4483 | Type objects represent the various object types. An object's type is accessed |
| 4484 | by the built-in function :func:`type`. There are no special operations on |
| 4485 | types. The standard module :mod:`types` defines names for all standard built-in |
| 4486 | types. |
| 4487 | |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 4488 | Types are written like this: ``<class 'int'>``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4489 | |
| 4490 | |
| 4491 | .. _bltin-null-object: |
| 4492 | |
| 4493 | The Null Object |
| 4494 | --------------- |
| 4495 | |
| 4496 | This object is returned by functions that don't explicitly return a value. It |
| 4497 | supports no special operations. There is exactly one null object, named |
Benjamin Peterson | 98f2b9b | 2011-07-30 12:26:27 -0500 | [diff] [blame] | 4498 | ``None`` (a built-in name). ``type(None)()`` produces the same singleton. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4499 | |
| 4500 | It is written as ``None``. |
| 4501 | |
| 4502 | |
| 4503 | .. _bltin-ellipsis-object: |
| 4504 | |
| 4505 | The Ellipsis Object |
| 4506 | ------------------- |
| 4507 | |
Benjamin Peterson | d5a1c44 | 2012-05-14 22:09:31 -0700 | [diff] [blame] | 4508 | This object is commonly used by slicing (see :ref:`slicings`). It supports no |
| 4509 | special operations. There is exactly one ellipsis object, named |
| 4510 | :const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the |
| 4511 | :const:`Ellipsis` singleton. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4512 | |
| 4513 | It is written as ``Ellipsis`` or ``...``. |
| 4514 | |
| 4515 | |
Éric Araujo | 18ddf82 | 2011-09-01 23:10:36 +0200 | [diff] [blame] | 4516 | .. _bltin-notimplemented-object: |
| 4517 | |
Benjamin Peterson | 50211fa | 2011-07-30 09:57:24 -0500 | [diff] [blame] | 4518 | The NotImplemented Object |
| 4519 | ------------------------- |
| 4520 | |
| 4521 | This object is returned from comparisons and binary operations when they are |
| 4522 | 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] | 4523 | information. There is exactly one ``NotImplemented`` object. |
| 4524 | ``type(NotImplemented)()`` produces the singleton instance. |
Benjamin Peterson | 50211fa | 2011-07-30 09:57:24 -0500 | [diff] [blame] | 4525 | |
| 4526 | It is written as ``NotImplemented``. |
| 4527 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4528 | |
Éric Araujo | 18ddf82 | 2011-09-01 23:10:36 +0200 | [diff] [blame] | 4529 | .. _bltin-boolean-values: |
| 4530 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4531 | Boolean Values |
| 4532 | -------------- |
| 4533 | |
| 4534 | Boolean values are the two constant objects ``False`` and ``True``. They are |
| 4535 | used to represent truth values (although other values can also be considered |
| 4536 | false or true). In numeric contexts (for example when used as the argument to |
| 4537 | an arithmetic operator), they behave like the integers 0 and 1, respectively. |
Ezio Melotti | c1f26f6 | 2011-12-02 19:47:24 +0200 | [diff] [blame] | 4538 | The built-in function :func:`bool` can be used to convert any value to a |
| 4539 | Boolean, if the value can be interpreted as a truth value (see section |
| 4540 | :ref:`truth` above). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4541 | |
| 4542 | .. index:: |
| 4543 | single: False |
| 4544 | single: True |
| 4545 | pair: Boolean; values |
| 4546 | |
| 4547 | They are written as ``False`` and ``True``, respectively. |
| 4548 | |
| 4549 | |
| 4550 | .. _typesinternal: |
| 4551 | |
| 4552 | Internal Objects |
| 4553 | ---------------- |
| 4554 | |
| 4555 | See :ref:`types` for this information. It describes stack frame objects, |
| 4556 | traceback objects, and slice objects. |
| 4557 | |
| 4558 | |
| 4559 | .. _specialattrs: |
| 4560 | |
| 4561 | Special Attributes |
| 4562 | ================== |
| 4563 | |
| 4564 | The implementation adds a few special read-only attributes to several object |
| 4565 | types, where they are relevant. Some of these are not reported by the |
| 4566 | :func:`dir` built-in function. |
| 4567 | |
| 4568 | |
| 4569 | .. attribute:: object.__dict__ |
| 4570 | |
| 4571 | A dictionary or other mapping object used to store an object's (writable) |
| 4572 | attributes. |
| 4573 | |
| 4574 | |
| 4575 | .. attribute:: instance.__class__ |
| 4576 | |
| 4577 | The class to which a class instance belongs. |
| 4578 | |
| 4579 | |
| 4580 | .. attribute:: class.__bases__ |
| 4581 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 4582 | The tuple of base classes of a class object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4583 | |
| 4584 | |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4585 | .. attribute:: definition.__name__ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4586 | |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4587 | The name of the class, function, method, descriptor, or |
| 4588 | generator instance. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4589 | |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 4590 | |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4591 | .. attribute:: definition.__qualname__ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4592 | |
Martin Panter | bae5d81 | 2016-06-18 03:57:31 +0000 | [diff] [blame] | 4593 | The :term:`qualified name` of the class, function, method, descriptor, |
| 4594 | or generator instance. |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4595 | |
| 4596 | .. versionadded:: 3.3 |
| 4597 | |
| 4598 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4599 | .. attribute:: class.__mro__ |
| 4600 | |
| 4601 | This attribute is a tuple of classes that are considered when looking for |
| 4602 | base classes during method resolution. |
| 4603 | |
| 4604 | |
| 4605 | .. method:: class.mro() |
| 4606 | |
| 4607 | This method can be overridden by a metaclass to customize the method |
| 4608 | resolution order for its instances. It is called at class instantiation, and |
Serhiy Storchaka | 0d196ed | 2013-10-09 14:02:31 +0300 | [diff] [blame] | 4609 | its result is stored in :attr:`~class.__mro__`. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4610 | |
| 4611 | |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 4612 | .. method:: class.__subclasses__ |
| 4613 | |
Florent Xicluna | 74e6495 | 2011-10-28 11:21:19 +0200 | [diff] [blame] | 4614 | Each class keeps a list of weak references to its immediate subclasses. This |
| 4615 | method returns a list of all those references still alive. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 4616 | Example:: |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 4617 | |
| 4618 | >>> int.__subclasses__() |
Florent Xicluna | 74e6495 | 2011-10-28 11:21:19 +0200 | [diff] [blame] | 4619 | [<class 'bool'>] |
Georg Brandl | 7a51e58 | 2009-03-28 19:13:21 +0000 | [diff] [blame] | 4620 | |
| 4621 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4622 | .. rubric:: Footnotes |
| 4623 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 4624 | .. [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] | 4625 | Reference Manual (:ref:`customization`). |
| 4626 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 4627 | .. [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] | 4628 | similarly for tuples. |
| 4629 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 4630 | .. [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] | 4631 | |
Ezio Melotti | 0656a56 | 2011-08-15 14:27:19 +0300 | [diff] [blame] | 4632 | .. [4] Cased characters are those with general category property being one of |
| 4633 | "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase). |
| 4634 | |
| 4635 | .. [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] | 4636 | element is the tuple to be formatted. |