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