blob: 60df11d130c92ad0f62a831457cc77e8dd92f77f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. XXX: reference/datamodel and this have quite a few overlaps!
2
3
4.. _bltin-types:
5
6**************
7Built-in Types
8**************
9
10The following sections describe the standard types that are built into the
11interpreter.
12
Georg Brandl116aa622007-08-15 14:28:22 +000013.. index:: pair: built-in; types
14
Antoine Pitroue231e392009-12-19 18:22:15 +000015The principal built-in types are numerics, sequences, mappings, classes,
Georg Brandl116aa622007-08-15 14:28:22 +000016instances and exceptions.
17
Georg Brandl388349a2011-10-08 18:32:40 +020018Some collection classes are mutable. The methods that add, subtract, or
19rearrange their members in place, and don't return a specific item, never return
20the collection instance itself but ``None``.
21
Georg Brandl116aa622007-08-15 14:28:22 +000022Some operations are supported by several object types; in particular,
23practically all objects can be compared, tested for truth value, and converted
24to 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
26written by the :func:`print` function.
27
28
29.. _truth:
30
31Truth Value Testing
32===================
33
34.. index::
35 statement: if
36 statement: while
37 pair: truth; value
38 pair: Boolean; operations
39 single: false
40
41Any object can be tested for truth value, for use in an :keyword:`if` or
42:keyword:`while` condition or as operand of the Boolean operations below. The
43following values are considered false:
44
45 .. index:: single: None (Built-in object)
46
47* ``None``
48
49 .. index:: single: False (Built-in object)
50
51* ``False``
52
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +000053* zero of any numeric type, for example, ``0``, ``0.0``, ``0j``.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55* any empty sequence, for example, ``''``, ``()``, ``[]``.
56
57* any empty mapping, for example, ``{}``.
58
59* instances of user-defined classes, if the class defines a :meth:`__bool__` or
60 :meth:`__len__` method, when that method returns the integer zero or
Ezio Melotti0656a562011-08-15 14:27:19 +030061 :class:`bool` value ``False``. [1]_
Georg Brandl116aa622007-08-15 14:28:22 +000062
63.. index:: single: true
64
65All other values are considered true --- so objects of many types are always
66true.
67
68.. index::
69 operator: or
70 operator: and
71 single: False
72 single: True
73
74Operations and built-in functions that have a Boolean result always return ``0``
75or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
76(Important exception: the Boolean operations ``or`` and ``and`` always return
77one of their operands.)
78
79
80.. _boolean:
81
82Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not`
83====================================================================
84
85.. index:: pair: Boolean; operations
86
87These are the Boolean operations, ordered by ascending priority:
88
89+-------------+---------------------------------+-------+
90| Operation | Result | Notes |
91+=============+=================================+=======+
92| ``x or y`` | if *x* is false, then *y*, else | \(1) |
93| | *x* | |
94+-------------+---------------------------------+-------+
95| ``x and y`` | if *x* is false, then *x*, else | \(2) |
96| | *y* | |
97+-------------+---------------------------------+-------+
98| ``not x`` | if *x* is false, then ``True``, | \(3) |
99| | else ``False`` | |
100+-------------+---------------------------------+-------+
101
102.. index::
103 operator: and
104 operator: or
105 operator: not
106
107Notes:
108
109(1)
110 This is a short-circuit operator, so it only evaluates the second
111 argument if the first one is :const:`False`.
112
113(2)
114 This is a short-circuit operator, so it only evaluates the second
115 argument if the first one is :const:`True`.
116
117(3)
118 ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
119 interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
120
121
122.. _stdcomparisons:
123
124Comparisons
125===========
126
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000127.. index::
128 pair: chaining; comparisons
129 pair: operator; comparison
130 operator: ==
131 operator: <
132 operator: <=
133 operator: >
134 operator: >=
135 operator: !=
136 operator: is
137 operator: is not
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl905ec322007-09-28 13:39:25 +0000139There are eight comparison operations in Python. They all have the same
140priority (which is higher than that of the Boolean operations). Comparisons can
Georg Brandl116aa622007-08-15 14:28:22 +0000141be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
142y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
143evaluated at all when ``x < y`` is found to be false).
144
145This table summarizes the comparison operations:
146
Georg Brandlfd855162008-01-07 09:13:03 +0000147+------------+-------------------------+
148| Operation | Meaning |
149+============+=========================+
150| ``<`` | strictly less than |
151+------------+-------------------------+
152| ``<=`` | less than or equal |
153+------------+-------------------------+
154| ``>`` | strictly greater than |
155+------------+-------------------------+
156| ``>=`` | greater than or equal |
157+------------+-------------------------+
158| ``==`` | equal |
159+------------+-------------------------+
160| ``!=`` | not equal |
161+------------+-------------------------+
162| ``is`` | object identity |
163+------------+-------------------------+
164| ``is not`` | negated object identity |
165+------------+-------------------------+
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000166
167.. index::
Georg Brandl116aa622007-08-15 14:28:22 +0000168 pair: object; numeric
169 pair: objects; comparing
170
Georg Brandl905ec322007-09-28 13:39:25 +0000171Objects of different types, except different numeric types, never compare equal.
Antoine Pitroue231e392009-12-19 18:22:15 +0000172Furthermore, some types (for example, function objects) support only a degenerate
Georg Brandl905ec322007-09-28 13:39:25 +0000173notion of comparison where any two objects of that type are unequal. The ``<``,
174``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
Mark Dickinsonf673f0c2010-03-13 09:48:39 +0000175comparing a complex number with another built-in numeric type, when the objects
176are of different types that cannot be compared, or in other cases where there is
177no defined ordering.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Georg Brandl48310cd2009-01-03 21:18:54 +0000179.. index::
Georg Brandl905ec322007-09-28 13:39:25 +0000180 single: __eq__() (instance method)
181 single: __ne__() (instance method)
182 single: __lt__() (instance method)
183 single: __le__() (instance method)
184 single: __gt__() (instance method)
185 single: __ge__() (instance method)
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandl05f5ab72008-09-24 09:11:47 +0000187Non-identical instances of a class normally compare as non-equal unless the
188class defines the :meth:`__eq__` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Georg Brandl905ec322007-09-28 13:39:25 +0000190Instances of a class cannot be ordered with respect to other instances of the
191same class, or other types of object, unless the class defines enough of the
Georg Brandl05f5ab72008-09-24 09:11:47 +0000192methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
193general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the
194conventional meanings of the comparison operators).
Georg Brandl905ec322007-09-28 13:39:25 +0000195
196The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
197customized; also they can be applied to any two objects and never raise an
198exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200.. index::
201 operator: in
202 operator: not in
203
Georg Brandl375aec22011-01-15 17:03:02 +0000204Two more operations with the same syntactic priority, :keyword:`in` and
205:keyword:`not in`, are supported only by sequence types (below).
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
208.. _typesnumeric:
209
Georg Brandl905ec322007-09-28 13:39:25 +0000210Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
211================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213.. index::
214 object: numeric
215 object: Boolean
216 object: integer
Georg Brandl116aa622007-08-15 14:28:22 +0000217 object: floating point
218 object: complex number
219 pair: C; language
220
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000221There are three distinct numeric types: :dfn:`integers`, :dfn:`floating
222point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a
223subtype of integers. Integers have unlimited precision. Floating point
Georg Brandl60203b42010-10-06 10:11:56 +0000224numbers are usually implemented using :c:type:`double` in C; information
Mark Dickinson74f59022010-08-04 18:42:43 +0000225about the precision and internal representation of floating point
226numbers for the machine on which your program is running is available
227in :data:`sys.float_info`. Complex numbers have a real and imaginary
228part, which are each a floating point number. To extract these parts
229from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard
230library includes additional numeric types, :mod:`fractions` that hold
231rationals, and :mod:`decimal` that hold floating-point numbers with
232user-definable precision.)
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234.. index::
235 pair: numeric; literals
236 pair: integer; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000237 pair: floating point; literals
238 pair: complex number; literals
239 pair: hexadecimal; literals
240 pair: octal; literals
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000241 pair: binary; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243Numbers are created by numeric literals or as the result of built-in functions
Georg Brandl905ec322007-09-28 13:39:25 +0000244and operators. Unadorned integer literals (including hex, octal and binary
245numbers) yield integers. Numeric literals containing a decimal point or an
246exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
247numeric literal yields an imaginary number (a complex number with a zero real
248part) which you can add to an integer or float to get a complex number with real
249and imaginary parts.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251.. index::
252 single: arithmetic
253 builtin: int
Georg Brandl116aa622007-08-15 14:28:22 +0000254 builtin: float
255 builtin: complex
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000256 operator: +
257 operator: -
258 operator: *
259 operator: /
260 operator: //
261 operator: %
262 operator: **
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264Python fully supports mixed arithmetic: when a binary arithmetic operator has
265operands of different numeric types, the operand with the "narrower" type is
Georg Brandl905ec322007-09-28 13:39:25 +0000266widened to that of the other, where integer is narrower than floating point,
267which is narrower than complex. Comparisons between numbers of mixed type use
Ezio Melotti0656a562011-08-15 14:27:19 +0300268the same rule. [2]_ The constructors :func:`int`, :func:`float`, and
Georg Brandl905ec322007-09-28 13:39:25 +0000269:func:`complex` can be used to produce numbers of a specific type.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271All numeric types (except complex) support the following operations, sorted by
272ascending priority (operations in the same box have the same priority; all
273numeric operations have a higher priority than comparison operations):
274
Raymond Hettingerc706dbf2011-03-22 17:33:17 -0700275+---------------------+---------------------------------+---------+--------------------+
276| Operation | Result | Notes | Full documentation |
277+=====================+=================================+=========+====================+
278| ``x + y`` | sum of *x* and *y* | | |
279+---------------------+---------------------------------+---------+--------------------+
280| ``x - y`` | difference of *x* and *y* | | |
281+---------------------+---------------------------------+---------+--------------------+
282| ``x * y`` | product of *x* and *y* | | |
283+---------------------+---------------------------------+---------+--------------------+
284| ``x / y`` | quotient of *x* and *y* | | |
285+---------------------+---------------------------------+---------+--------------------+
286| ``x // y`` | floored quotient of *x* and | \(1) | |
287| | *y* | | |
288+---------------------+---------------------------------+---------+--------------------+
289| ``x % y`` | remainder of ``x / y`` | \(2) | |
290+---------------------+---------------------------------+---------+--------------------+
291| ``-x`` | *x* negated | | |
292+---------------------+---------------------------------+---------+--------------------+
293| ``+x`` | *x* unchanged | | |
294+---------------------+---------------------------------+---------+--------------------+
295| ``abs(x)`` | absolute value or magnitude of | | :func:`abs` |
296| | *x* | | |
297+---------------------+---------------------------------+---------+--------------------+
298| ``int(x)`` | *x* converted to integer | \(3)\(6)| :func:`int` |
299+---------------------+---------------------------------+---------+--------------------+
300| ``float(x)`` | *x* converted to floating point | \(4)\(6)| :func:`float` |
301+---------------------+---------------------------------+---------+--------------------+
302| ``complex(re, im)`` | a complex number with real part | \(6) | :func:`complex` |
303| | *re*, imaginary part *im*. | | |
304| | *im* defaults to zero. | | |
305+---------------------+---------------------------------+---------+--------------------+
306| ``c.conjugate()`` | conjugate of the complex number | | |
307| | *c* | | |
308+---------------------+---------------------------------+---------+--------------------+
309| ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` |
310+---------------------+---------------------------------+---------+--------------------+
311| ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` |
312+---------------------+---------------------------------+---------+--------------------+
313| ``x ** y`` | *x* to the power *y* | \(5) | |
314+---------------------+---------------------------------+---------+--------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316.. index::
317 triple: operations on; numeric; types
318 single: conjugate() (complex number method)
319
320Notes:
321
322(1)
Georg Brandl905ec322007-09-28 13:39:25 +0000323 Also referred to as integer division. The resultant value is a whole
324 integer, though the result's type is not necessarily int. The result is
325 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is
326 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328(2)
Georg Brandl905ec322007-09-28 13:39:25 +0000329 Not for complex numbers. Instead convert to floats using :func:`abs` if
330 appropriate.
331
332(3)
Georg Brandl116aa622007-08-15 14:28:22 +0000333 .. index::
334 module: math
335 single: floor() (in module math)
336 single: ceil() (in module math)
Benjamin Peterson28d88b42009-01-09 03:03:23 +0000337 single: trunc() (in module math)
Georg Brandl116aa622007-08-15 14:28:22 +0000338 pair: numeric; conversions
339 pair: C; language
340
Georg Brandlba956ae2007-11-29 17:24:34 +0000341 Conversion from floating point to integer may round or truncate
Georg Brandl116aa622007-08-15 14:28:22 +0000342 as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module
343 for well-defined conversions.
344
Georg Brandl74f36692008-01-06 17:39:49 +0000345(4)
Georg Brandl48310cd2009-01-03 21:18:54 +0000346 float also accepts the strings "nan" and "inf" with an optional prefix "+"
Christian Heimes99170a52007-12-19 02:07:34 +0000347 or "-" for Not a Number (NaN) and positive or negative infinity.
Christian Heimes7f044312008-01-06 17:05:40 +0000348
Georg Brandl74f36692008-01-06 17:39:49 +0000349(5)
Christian Heimes7f044312008-01-06 17:05:40 +0000350 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
351 programming languages.
352
Raymond Hettingerc706dbf2011-03-22 17:33:17 -0700353(6)
354 The numeric literals accepted include the digits ``0`` to ``9`` or any
355 Unicode equivalent (code points with the ``Nd`` property).
356
357 See http://www.unicode.org/Public/6.0.0/ucd/extracted/DerivedNumericType.txt
358 for a complete list of code points with the ``Nd`` property.
Georg Brandl48310cd2009-01-03 21:18:54 +0000359
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000360
Benjamin Peterson10116d42011-05-01 17:38:17 -0500361All :class:`numbers.Real` types (:class:`int` and :class:`float`) also include
362the following operations:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000363
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000364+--------------------+------------------------------------+--------+
365| Operation | Result | Notes |
366+====================+====================================+========+
367| ``math.trunc(x)`` | *x* truncated to Integral | |
368+--------------------+------------------------------------+--------+
369| ``round(x[, n])`` | *x* rounded to n digits, | |
370| | rounding half to even. If n is | |
371| | omitted, it defaults to 0. | |
372+--------------------+------------------------------------+--------+
373| ``math.floor(x)`` | the greatest integral float <= *x* | |
374+--------------------+------------------------------------+--------+
375| ``math.ceil(x)`` | the least integral float >= *x* | |
376+--------------------+------------------------------------+--------+
Christian Heimesfaf2f632008-01-06 16:59:19 +0000377
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000378For additional numeric operations see the :mod:`math` and :mod:`cmath`
379modules.
380
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000381.. XXXJH exceptions: overflow (when? what operations?) zerodivision
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383
384.. _bitstring-ops:
385
Benjamin Petersone9fca252012-01-25 16:29:03 -0500386Bitwise Operations on Integer Types
Georg Brandl116aa622007-08-15 14:28:22 +0000387--------------------------------------
388
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000389.. index::
390 triple: operations on; integer; types
Benjamin Petersone9fca252012-01-25 16:29:03 -0500391 pair: bitwise; operations
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000392 pair: shifting; operations
393 pair: masking; operations
394 operator: ^
395 operator: &
396 operator: <<
397 operator: >>
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Benjamin Petersonb4b0b352012-01-25 16:30:18 -0500399Bitwise operations only make sense for integers. Negative numbers are treated
400as their 2's complement value (this assumes a sufficiently large number of bits
401that no overflow occurs during the operation).
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Christian Heimesfaf2f632008-01-06 16:59:19 +0000403The priorities of the binary bitwise operations are all lower than the numeric
Georg Brandl116aa622007-08-15 14:28:22 +0000404operations and higher than the comparisons; the unary operation ``~`` has the
405same priority as the other unary numeric operations (``+`` and ``-``).
406
Benjamin Petersone9fca252012-01-25 16:29:03 -0500407This table lists the bitwise operations sorted in ascending priority
Georg Brandl116aa622007-08-15 14:28:22 +0000408(operations in the same box have the same priority):
409
410+------------+--------------------------------+----------+
411| Operation | Result | Notes |
412+============+================================+==========+
413| ``x | y`` | bitwise :dfn:`or` of *x* and | |
414| | *y* | |
415+------------+--------------------------------+----------+
416| ``x ^ y`` | bitwise :dfn:`exclusive or` of | |
417| | *x* and *y* | |
418+------------+--------------------------------+----------+
419| ``x & y`` | bitwise :dfn:`and` of *x* and | |
420| | *y* | |
421+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000422| ``x << n`` | *x* shifted left by *n* bits | (1)(2) |
Georg Brandl116aa622007-08-15 14:28:22 +0000423+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000424| ``x >> n`` | *x* shifted right by *n* bits | (1)(3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000425+------------+--------------------------------+----------+
426| ``~x`` | the bits of *x* inverted | |
427+------------+--------------------------------+----------+
428
Georg Brandl116aa622007-08-15 14:28:22 +0000429Notes:
430
431(1)
432 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
433
434(2)
435 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
436 without overflow check.
437
438(3)
439 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
440 overflow check.
441
442
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000443Additional Methods on Integer Types
444-----------------------------------
445
Raymond Hettinger9b2fd322011-05-01 18:14:49 -0700446The int type implements the :class:`numbers.Integral` :term:`abstract base
Éric Araujob79c2342011-05-02 13:10:18 +0200447class`. In addition, it provides one more method:
Benjamin Peterson10116d42011-05-01 17:38:17 -0500448
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000449.. method:: int.bit_length()
450
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000451 Return the number of bits necessary to represent an integer in binary,
452 excluding the sign and leading zeros::
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000453
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000454 >>> n = -37
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000455 >>> bin(n)
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000456 '-0b100101'
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000457 >>> n.bit_length()
458 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000459
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000460 More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
461 unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
462 Equivalently, when ``abs(x)`` is small enough to have a correctly
463 rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
464 If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000465
466 Equivalent to::
467
468 def bit_length(self):
Senthil Kumaran0aae6dc2010-06-22 02:57:23 +0000469 s = bin(self) # binary representation: bin(-37) --> '-0b100101'
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000470 s = s.lstrip('-0b') # remove leading zeros and minus sign
471 return len(s) # len('100101') --> 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000472
473 .. versionadded:: 3.1
474
Georg Brandl67b21b72010-08-17 15:07:14 +0000475.. method:: int.to_bytes(length, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000476
477 Return an array of bytes representing an integer.
478
479 >>> (1024).to_bytes(2, byteorder='big')
480 b'\x04\x00'
481 >>> (1024).to_bytes(10, byteorder='big')
482 b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
483 >>> (-1024).to_bytes(10, byteorder='big', signed=True)
484 b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
485 >>> x = 1000
486 >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder='little')
487 b'\xe8\x03'
488
489 The integer is represented using *length* bytes. An :exc:`OverflowError`
490 is raised if the integer is not representable with the given number of
491 bytes.
492
493 The *byteorder* argument determines the byte order used to represent the
494 integer. If *byteorder* is ``"big"``, the most significant byte is at the
495 beginning of the byte array. If *byteorder* is ``"little"``, the most
496 significant byte is at the end of the byte array. To request the native
497 byte order of the host system, use :data:`sys.byteorder` as the byte order
498 value.
499
500 The *signed* argument determines whether two's complement is used to
501 represent the integer. If *signed* is ``False`` and a negative integer is
502 given, an :exc:`OverflowError` is raised. The default value for *signed*
503 is ``False``.
504
505 .. versionadded:: 3.2
506
Georg Brandl67b21b72010-08-17 15:07:14 +0000507.. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000508
509 Return the integer represented by the given array of bytes.
510
511 >>> int.from_bytes(b'\x00\x10', byteorder='big')
512 16
513 >>> int.from_bytes(b'\x00\x10', byteorder='little')
514 4096
515 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
516 -1024
517 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
518 64512
519 >>> int.from_bytes([255, 0, 0], byteorder='big')
520 16711680
521
522 The argument *bytes* must either support the buffer protocol or be an
523 iterable producing bytes. :class:`bytes` and :class:`bytearray` are
524 examples of built-in objects that support the buffer protocol.
525
526 The *byteorder* argument determines the byte order used to represent the
527 integer. If *byteorder* is ``"big"``, the most significant byte is at the
528 beginning of the byte array. If *byteorder* is ``"little"``, the most
529 significant byte is at the end of the byte array. To request the native
530 byte order of the host system, use :data:`sys.byteorder` as the byte order
531 value.
532
533 The *signed* argument indicates whether two's complement is used to
534 represent the integer.
535
536 .. versionadded:: 3.2
537
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000538
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000539Additional Methods on Float
540---------------------------
541
Benjamin Peterson10116d42011-05-01 17:38:17 -0500542The float type implements the :class:`numbers.Real` :term:`abstract base
543class`. float also has the following additional methods.
Benjamin Petersond7b03282008-09-13 15:58:53 +0000544
545.. method:: float.as_integer_ratio()
546
Mark Dickinson4a3c7c42010-11-07 12:48:18 +0000547 Return a pair of integers whose ratio is exactly equal to the
548 original float and with a positive denominator. Raises
549 :exc:`OverflowError` on infinities and a :exc:`ValueError` on
550 NaNs.
551
552.. method:: float.is_integer()
553
554 Return ``True`` if the float instance is finite with integral
555 value, and ``False`` otherwise::
556
557 >>> (-2.0).is_integer()
558 True
559 >>> (3.2).is_integer()
560 False
Georg Brandl48310cd2009-01-03 21:18:54 +0000561
Benjamin Petersond7b03282008-09-13 15:58:53 +0000562Two methods support conversion to
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000563and from hexadecimal strings. Since Python's floats are stored
564internally as binary numbers, converting a float to or from a
565*decimal* string usually involves a small rounding error. In
566contrast, hexadecimal strings allow exact representation and
567specification of floating-point numbers. This can be useful when
568debugging, and in numerical work.
569
570
571.. method:: float.hex()
572
573 Return a representation of a floating-point number as a hexadecimal
574 string. For finite floating-point numbers, this representation
575 will always include a leading ``0x`` and a trailing ``p`` and
576 exponent.
577
578
Georg Brandlabc38772009-04-12 15:51:51 +0000579.. classmethod:: float.fromhex(s)
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000580
581 Class method to return the float represented by a hexadecimal
582 string *s*. The string *s* may have leading and trailing
583 whitespace.
584
585
586Note that :meth:`float.hex` is an instance method, while
587:meth:`float.fromhex` is a class method.
588
589A hexadecimal string takes the form::
590
591 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
592
593where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
594and ``fraction`` are strings of hexadecimal digits, and ``exponent``
595is a decimal integer with an optional leading sign. Case is not
596significant, and there must be at least one hexadecimal digit in
597either the integer or the fraction. This syntax is similar to the
598syntax specified in section 6.4.4.2 of the C99 standard, and also to
599the syntax used in Java 1.5 onwards. In particular, the output of
600:meth:`float.hex` is usable as a hexadecimal floating-point literal in
601C or Java code, and hexadecimal strings produced by C's ``%a`` format
602character or Java's ``Double.toHexString`` are accepted by
603:meth:`float.fromhex`.
604
605
606Note that the exponent is written in decimal rather than hexadecimal,
607and that it gives the power of 2 by which to multiply the coefficient.
608For example, the hexadecimal string ``0x3.a7p10`` represents the
609floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
610``3740.0``::
611
612 >>> float.fromhex('0x3.a7p10')
613 3740.0
614
615
616Applying the reverse conversion to ``3740.0`` gives a different
617hexadecimal string representing the same number::
618
619 >>> float.hex(3740.0)
620 '0x1.d380000000000p+11'
621
622
Mark Dickinsondc787d22010-05-23 13:33:13 +0000623.. _numeric-hash:
624
625Hashing of numeric types
626------------------------
627
628For numbers ``x`` and ``y``, possibly of different types, it's a requirement
629that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__`
630method documentation for more details). For ease of implementation and
631efficiency across a variety of numeric types (including :class:`int`,
632:class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`)
633Python's hash for numeric types is based on a single mathematical function
634that's defined for any rational number, and hence applies to all instances of
635:class:`int` and :class:`fraction.Fraction`, and all finite instances of
636:class:`float` and :class:`decimal.Decimal`. Essentially, this function is
637given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is
638made available to Python as the :attr:`modulus` attribute of
639:data:`sys.hash_info`.
640
641.. impl-detail::
642
643 Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C
644 longs and ``P = 2**61 - 1`` on machines with 64-bit C longs.
645
646Here are the rules in detail:
647
Georg Brandl226ed7e2012-03-24 08:12:41 +0100648- If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible
649 by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n,
650 P)`` gives the inverse of ``n`` modulo ``P``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000651
Georg Brandl226ed7e2012-03-24 08:12:41 +0100652- If ``x = m / n`` is a nonnegative rational number and ``n`` is
653 divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse
654 modulo ``P`` and the rule above doesn't apply; in this case define
655 ``hash(x)`` to be the constant value ``sys.hash_info.inf``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000656
Georg Brandl226ed7e2012-03-24 08:12:41 +0100657- If ``x = m / n`` is a negative rational number define ``hash(x)``
658 as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with
659 ``-2``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000660
Georg Brandl226ed7e2012-03-24 08:12:41 +0100661- The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf``
662 and ``sys.hash_info.nan`` are used as hash values for positive
663 infinity, negative infinity, or nans (respectively). (All hashable
664 nans have the same hash value.)
Mark Dickinsondc787d22010-05-23 13:33:13 +0000665
Georg Brandl226ed7e2012-03-24 08:12:41 +0100666- For a :class:`complex` number ``z``, the hash values of the real
667 and imaginary parts are combined by computing ``hash(z.real) +
668 sys.hash_info.imag * hash(z.imag)``, reduced modulo
669 ``2**sys.hash_info.width`` so that it lies in
670 ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width -
671 1))``. Again, if the result is ``-1``, it's replaced with ``-2``.
Mark Dickinsondc787d22010-05-23 13:33:13 +0000672
673
674To clarify the above rules, here's some example Python code,
Nick Coghlan273069c2012-08-20 17:14:07 +1000675equivalent to the built-in hash, for computing the hash of a rational
Mark Dickinsondc787d22010-05-23 13:33:13 +0000676number, :class:`float`, or :class:`complex`::
677
678
679 import sys, math
680
681 def hash_fraction(m, n):
682 """Compute the hash of a rational number m / n.
683
684 Assumes m and n are integers, with n positive.
685 Equivalent to hash(fractions.Fraction(m, n)).
686
687 """
688 P = sys.hash_info.modulus
689 # Remove common factors of P. (Unnecessary if m and n already coprime.)
690 while m % P == n % P == 0:
691 m, n = m // P, n // P
692
693 if n % P == 0:
694 hash_ = sys.hash_info.inf
695 else:
696 # Fermat's Little Theorem: pow(n, P-1, P) is 1, so
697 # pow(n, P-2, P) gives the inverse of n modulo P.
698 hash_ = (abs(m) % P) * pow(n, P - 2, P) % P
699 if m < 0:
700 hash_ = -hash_
701 if hash_ == -1:
702 hash_ = -2
703 return hash_
704
705 def hash_float(x):
706 """Compute the hash of a float x."""
707
708 if math.isnan(x):
709 return sys.hash_info.nan
710 elif math.isinf(x):
711 return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
712 else:
713 return hash_fraction(*x.as_integer_ratio())
714
715 def hash_complex(z):
716 """Compute the hash of a complex number z."""
717
718 hash_ = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
719 # do a signed reduction modulo 2**sys.hash_info.width
720 M = 2**(sys.hash_info.width - 1)
721 hash_ = (hash_ & (M - 1)) - (hash & M)
722 if hash_ == -1:
723 hash_ == -2
724 return hash_
725
Georg Brandl6ea420b2008-07-16 12:58:29 +0000726.. _typeiter:
727
Georg Brandl116aa622007-08-15 14:28:22 +0000728Iterator Types
729==============
730
Georg Brandl116aa622007-08-15 14:28:22 +0000731.. index::
732 single: iterator protocol
733 single: protocol; iterator
734 single: sequence; iteration
735 single: container; iteration over
736
737Python supports a concept of iteration over containers. This is implemented
738using two distinct methods; these are used to allow user-defined classes to
739support iteration. Sequences, described below in more detail, always support
740the iteration methods.
741
742One method needs to be defined for container objects to provide iteration
743support:
744
Christian Heimes790c8232008-01-07 21:14:23 +0000745.. XXX duplicated in reference/datamodel!
Georg Brandl116aa622007-08-15 14:28:22 +0000746
Christian Heimes790c8232008-01-07 21:14:23 +0000747.. method:: container.__iter__()
Georg Brandl116aa622007-08-15 14:28:22 +0000748
749 Return an iterator object. The object is required to support the iterator
750 protocol described below. If a container supports different types of
751 iteration, additional methods can be provided to specifically request
752 iterators for those iteration types. (An example of an object supporting
753 multiple forms of iteration would be a tree structure which supports both
754 breadth-first and depth-first traversal.) This method corresponds to the
755 :attr:`tp_iter` slot of the type structure for Python objects in the Python/C
756 API.
757
758The iterator objects themselves are required to support the following two
759methods, which together form the :dfn:`iterator protocol`:
760
761
762.. method:: iterator.__iter__()
763
764 Return the iterator object itself. This is required to allow both containers
765 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
766 This method corresponds to the :attr:`tp_iter` slot of the type structure for
767 Python objects in the Python/C API.
768
769
Georg Brandl905ec322007-09-28 13:39:25 +0000770.. method:: iterator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000771
772 Return the next item from the container. If there are no further items, raise
773 the :exc:`StopIteration` exception. This method corresponds to the
774 :attr:`tp_iternext` slot of the type structure for Python objects in the
775 Python/C API.
776
777Python defines several iterator objects to support iteration over general and
778specific sequence types, dictionaries, and other more specialized forms. The
779specific types are not important beyond their implementation of the iterator
780protocol.
781
Ezio Melotti7fa82222012-10-12 13:42:08 +0300782Once an iterator's :meth:`~iterator.__next__` method raises
783:exc:`StopIteration`, it must continue to do so on subsequent calls.
784Implementations that do not obey this property are deemed broken.
Georg Brandl116aa622007-08-15 14:28:22 +0000785
Benjamin Peterson0289b152009-06-28 17:22:03 +0000786
787.. _generator-types:
788
789Generator Types
790---------------
791
Georg Brandl9afde1c2007-11-01 20:32:30 +0000792Python's :term:`generator`\s provide a convenient way to implement the iterator
793protocol. If a container object's :meth:`__iter__` method is implemented as a
794generator, it will automatically return an iterator object (technically, a
Ezio Melotti7fa82222012-10-12 13:42:08 +0300795generator object) supplying the :meth:`__iter__` and :meth:`~generator.__next__`
796methods.
Benjamin Peterson0289b152009-06-28 17:22:03 +0000797More information about generators can be found in :ref:`the documentation for
798the yield expression <yieldexpr>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000799
800
801.. _typesseq:
802
Nick Coghlan273069c2012-08-20 17:14:07 +1000803Sequence Types --- :class:`list`, :class:`tuple`, :class:`range`
804================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000805
Nick Coghlan273069c2012-08-20 17:14:07 +1000806There are three basic sequence types: lists, tuples, and range objects.
807Additional sequence types tailored for processing of
808:ref:`binary data <binaryseq>` and :ref:`text strings <textseq>` are
809described in dedicated sections.
Georg Brandle17d5862009-01-18 10:40:25 +0000810
Georg Brandl116aa622007-08-15 14:28:22 +0000811
Nick Coghlan273069c2012-08-20 17:14:07 +1000812.. _typesseq-common:
Georg Brandl116aa622007-08-15 14:28:22 +0000813
Nick Coghlan273069c2012-08-20 17:14:07 +1000814Common Sequence Operations
815--------------------------
Georg Brandl7c676132007-10-23 18:17:00 +0000816
Nick Coghlan273069c2012-08-20 17:14:07 +1000817.. index:: object: sequence
Georg Brandl4b491312007-08-31 09:22:56 +0000818
Nick Coghlan273069c2012-08-20 17:14:07 +1000819The operations in the following table are supported by most sequence types,
820both mutable and immutable. The :class:`collections.abc.Sequence` ABC is
821provided to make it easier to correctly implement these operations on
822custom sequence types.
Georg Brandl116aa622007-08-15 14:28:22 +0000823
824This table lists the sequence operations sorted in ascending priority
825(operations in the same box have the same priority). In the table, *s* and *t*
Nick Coghlan273069c2012-08-20 17:14:07 +1000826are sequences of the same type, *n*, *i*, *j* and *k* are integers and *x* is
827an arbitrary object that meets any type and value restrictions imposed by *s*.
Georg Brandl116aa622007-08-15 14:28:22 +0000828
Nick Coghlan273069c2012-08-20 17:14:07 +1000829The ``in`` and ``not in`` operations have the same priorities as the
830comparison operations. The ``+`` (concatenation) and ``*`` (repetition)
831operations have the same priority as the corresponding numeric operations.
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000833.. index::
834 triple: operations on; sequence; types
835 builtin: len
836 builtin: min
837 builtin: max
838 pair: concatenation; operation
839 pair: repetition; operation
840 pair: subscript; operation
841 pair: slice; operation
842 operator: in
843 operator: not in
844 single: count() (sequence method)
845 single: index() (sequence method)
846
Nick Coghlan273069c2012-08-20 17:14:07 +1000847+--------------------------+--------------------------------+----------+
848| Operation | Result | Notes |
849+==========================+================================+==========+
850| ``x in s`` | ``True`` if an item of *s* is | \(1) |
851| | equal to *x*, else ``False`` | |
852+--------------------------+--------------------------------+----------+
853| ``x not in s`` | ``False`` if an item of *s* is | \(1) |
854| | equal to *x*, else ``True`` | |
855+--------------------------+--------------------------------+----------+
856| ``s + t`` | the concatenation of *s* and | (6)(7) |
857| | *t* | |
858+--------------------------+--------------------------------+----------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000859| ``s * n`` or | *n* shallow copies of *s* | (2)(7) |
860| ``n * s`` | concatenated | |
Nick Coghlan273069c2012-08-20 17:14:07 +1000861+--------------------------+--------------------------------+----------+
862| ``s[i]`` | *i*\ th item of *s*, origin 0 | \(3) |
863+--------------------------+--------------------------------+----------+
864| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
865+--------------------------+--------------------------------+----------+
866| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |
867| | with step *k* | |
868+--------------------------+--------------------------------+----------+
869| ``len(s)`` | length of *s* | |
870+--------------------------+--------------------------------+----------+
871| ``min(s)`` | smallest item of *s* | |
872+--------------------------+--------------------------------+----------+
873| ``max(s)`` | largest item of *s* | |
874+--------------------------+--------------------------------+----------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000875| ``s.index(x[, i[, j]])`` | index of the first occurence | \(8) |
Nick Coghlan273069c2012-08-20 17:14:07 +1000876| | of *x* in *s* (at or after | |
877| | index *i* and before index *j*)| |
878+--------------------------+--------------------------------+----------+
879| ``s.count(x)`` | total number of occurences of | |
880| | *x* in *s* | |
881+--------------------------+--------------------------------+----------+
882
883Sequences of the same type also support comparisons. In particular, tuples
884and lists are compared lexicographically by comparing corresponding elements.
885This means that to compare equal, every element must compare equal and the
886two sequences must be of the same type and have the same length. (For full
887details see :ref:`comparisons` in the language reference.)
Georg Brandl116aa622007-08-15 14:28:22 +0000888
Georg Brandl116aa622007-08-15 14:28:22 +0000889Notes:
890
891(1)
Nick Coghlan273069c2012-08-20 17:14:07 +1000892 While the ``in`` and ``not in`` operations are used only for simple
893 containment testing in the general case, some specialised sequences
894 (such as :class:`str`, :class:`bytes` and :class:`bytearray`) also use
895 them for subsequence testing::
896
897 >>> "gg" in "eggs"
898 True
Georg Brandl116aa622007-08-15 14:28:22 +0000899
900(2)
901 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
902 sequence of the same type as *s*). Note also that the copies are shallow;
903 nested structures are not copied. This often haunts new Python programmers;
Nick Coghlan273069c2012-08-20 17:14:07 +1000904 consider::
Georg Brandl116aa622007-08-15 14:28:22 +0000905
906 >>> lists = [[]] * 3
907 >>> lists
908 [[], [], []]
909 >>> lists[0].append(3)
910 >>> lists
911 [[3], [3], [3]]
912
913 What has happened is that ``[[]]`` is a one-element list containing an empty
Christian Heimesfe337bf2008-03-23 21:54:12 +0000914 list, so all three elements of ``[[]] * 3`` are (pointers to) this single empty
915 list. Modifying any of the elements of ``lists`` modifies this single list.
Nick Coghlan273069c2012-08-20 17:14:07 +1000916 You can create a list of different lists this way::
Georg Brandl116aa622007-08-15 14:28:22 +0000917
918 >>> lists = [[] for i in range(3)]
919 >>> lists[0].append(3)
920 >>> lists[1].append(5)
921 >>> lists[2].append(7)
922 >>> lists
923 [[3], [5], [7]]
924
925(3)
926 If *i* or *j* is negative, the index is relative to the end of the string:
Georg Brandl7c676132007-10-23 18:17:00 +0000927 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
928 still ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000929
930(4)
931 The slice of *s* from *i* to *j* is defined as the sequence of items with index
932 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
933 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
934 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
935 empty.
936
937(5)
938 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
Christian Heimes2c181612007-12-17 20:04:13 +0000939 items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words,
Georg Brandl116aa622007-08-15 14:28:22 +0000940 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
941 *j* is reached (but never including *j*). If *i* or *j* is greater than
942 ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become
943 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
944 If *k* is ``None``, it is treated like ``1``.
945
946(6)
Nick Coghlan273069c2012-08-20 17:14:07 +1000947 Concatenating immutable sequences always results in a new object. This
948 means that building up a sequence by repeated concatenation will have a
949 quadratic runtime cost in the total sequence length. To get a linear
950 runtime cost, you must switch to one of the alternatives below:
Georg Brandl495f7b52009-10-27 15:28:25 +0000951
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100952 * if concatenating :class:`str` objects, you can build a list and use
Nick Coghlan273069c2012-08-20 17:14:07 +1000953 :meth:`str.join` at the end or else write to a :class:`io.StringIO`
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000954 instance and retrieve its value when complete
Antoine Pitroufd9ebd42011-11-25 16:33:53 +0100955
956 * if concatenating :class:`bytes` objects, you can similarly use
Nick Coghlan273069c2012-08-20 17:14:07 +1000957 :meth:`bytes.join` or :class:`io.BytesIO`, or you can do in-place
958 concatenation with a :class:`bytearray` object. :class:`bytearray`
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000959 objects are mutable and have an efficient overallocation mechanism
Georg Brandl116aa622007-08-15 14:28:22 +0000960
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000961 * if concatenating :class:`tuple` objects, extend a :class:`list` instead
Nick Coghlan273069c2012-08-20 17:14:07 +1000962
963 * for other types, investigate the relevant class documentation
964
965
966(7)
967 Some sequence types (such as :class:`range`) only support item sequences
968 that follow specific patterns, and hence don't support sequence
969 concatenation or repetition.
970
971(8)
972 ``index`` raises :exc:`ValueError` when *x* is not found in *s*.
973 When supported, the additional arguments to the index method allow
974 efficient searching of subsections of the sequence. Passing the extra
975 arguments is roughly equivalent to using ``s[i:j].index(x)``, only
976 without copying any data and with the returned index being relative to
977 the start of the sequence rather than the start of the slice.
978
979
980.. _typesseq-immutable:
981
982Immutable Sequence Types
983------------------------
984
985.. index::
986 triple: immutable; sequence; types
987 object: tuple
Nick Coghlan83c0ae52012-08-21 17:42:52 +1000988 builtin: hash
Nick Coghlan273069c2012-08-20 17:14:07 +1000989
990The only operation that immutable sequence types generally implement that is
991not also implemented by mutable sequence types is support for the :func:`hash`
992built-in.
993
994This support allows immutable sequences, such as :class:`tuple` instances, to
995be used as :class:`dict` keys and stored in :class:`set` and :class:`frozenset`
996instances.
997
998Attempting to hash an immutable sequence that contains unhashable values will
999result in :exc:`TypeError`.
1000
1001
1002.. _typesseq-mutable:
1003
1004Mutable Sequence Types
1005----------------------
1006
1007.. index::
1008 triple: mutable; sequence; types
1009 object: list
1010 object: bytearray
1011
1012The operations in the following table are defined on mutable sequence types.
1013The :class:`collections.abc.MutableSequence` ABC is provided to make it
1014easier to correctly implement these operations on custom sequence types.
1015
1016In the table *s* is an instance of a mutable sequence type, *t* is any
1017iterable object and *x* is an arbitrary object that meets any type
1018and value restrictions imposed by *s* (for example, :class:`bytearray` only
1019accepts integers that meet the value restriction ``0 <= x <= 255``).
1020
1021
1022.. index::
1023 triple: operations on; sequence; types
1024 triple: operations on; list; type
1025 pair: subscript; assignment
1026 pair: slice; assignment
1027 statement: del
1028 single: append() (sequence method)
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001029 single: clear() (sequence method)
1030 single: copy() (sequence method)
Nick Coghlan273069c2012-08-20 17:14:07 +10001031 single: extend() (sequence method)
Nick Coghlan273069c2012-08-20 17:14:07 +10001032 single: insert() (sequence method)
1033 single: pop() (sequence method)
1034 single: remove() (sequence method)
1035 single: reverse() (sequence method)
1036
1037+------------------------------+--------------------------------+---------------------+
1038| Operation | Result | Notes |
1039+==============================+================================+=====================+
1040| ``s[i] = x`` | item *i* of *s* is replaced by | |
1041| | *x* | |
1042+------------------------------+--------------------------------+---------------------+
1043| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1044| | is replaced by the contents of | |
1045| | the iterable *t* | |
1046+------------------------------+--------------------------------+---------------------+
1047| ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1048+------------------------------+--------------------------------+---------------------+
1049| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1050| | are replaced by those of *t* | |
1051+------------------------------+--------------------------------+---------------------+
1052| ``del s[i:j:k]`` | removes the elements of | |
1053| | ``s[i:j:k]`` from the list | |
1054+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001055| ``s.append(x)`` | appends *x* to the end of the | |
1056| | sequence (same as | |
1057| | ``s[len(s):len(s)] = [x]``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001058+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001059| ``s.clear()`` | removes all items from ``s`` | \(5) |
Nick Coghlan273069c2012-08-20 17:14:07 +10001060| | (same as ``del s[:]``) | |
1061+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001062| ``s.copy()`` | creates a shallow copy of ``s``| \(5) |
Nick Coghlan273069c2012-08-20 17:14:07 +10001063| | (same as ``s[:]``) | |
1064+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001065| ``s.extend(t)`` | extends *s* with the | |
1066| | contents of *t* (same as | |
1067| | ``s[len(s):len(s)] = t``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001068+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001069| ``s.insert(i, x)`` | inserts *x* into *s* at the | |
1070| | index given by *i* | |
1071| | (same as ``s[i:i] = [x]``) | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001072+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001073| ``s.pop([i])`` | retrieves the item at *i* and | \(2) |
1074| | also removes it from *s* | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001075+------------------------------+--------------------------------+---------------------+
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001076| ``s.remove(x)`` | remove the first item from *s* | \(3) |
1077| | where ``s[i] == x`` | |
Nick Coghlan273069c2012-08-20 17:14:07 +10001078+------------------------------+--------------------------------+---------------------+
1079| ``s.reverse()`` | reverses the items of *s* in | \(4) |
1080| | place | |
1081+------------------------------+--------------------------------+---------------------+
1082
1083
1084Notes:
1085
1086(1)
1087 *t* must have the same length as the slice it is replacing.
1088
1089(2)
1090 The optional argument *i* defaults to ``-1``, so that by default the last
1091 item is removed and returned.
1092
1093(3)
1094 ``remove`` raises :exc:`ValueError` when *x* is not found in *s*.
1095
1096(4)
1097 The :meth:`reverse` method modifies the sequence in place for economy of
1098 space when reversing a large sequence. To remind users that it operates by
1099 side effect, it does not return the reversed sequence.
1100
1101(5)
1102 :meth:`clear` and :meth:`!copy` are included for consistency with the
1103 interfaces of mutable containers that don't support slicing operations
1104 (such as :class:`dict` and :class:`set`)
1105
1106 .. versionadded:: 3.3
1107 :meth:`clear` and :meth:`!copy` methods.
1108
1109
1110.. _typesseq-list:
1111
1112Lists
1113-----
1114
1115.. index:: object: list
1116
1117Lists are mutable sequences, typically used to store collections of
1118homogeneous items (where the precise degree of similarity will vary by
1119application).
1120
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001121.. class:: list([iterable])
Nick Coghlan273069c2012-08-20 17:14:07 +10001122
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001123 Lists may be constructed in several ways:
Nick Coghlan273069c2012-08-20 17:14:07 +10001124
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001125 * Using a pair of square brackets to denote the empty list: ``[]``
1126 * Using square brackets, separating items with commas: ``[a]``, ``[a, b, c]``
1127 * Using a list comprehension: ``[x for x in iterable]``
1128 * Using the type constructor: ``list()`` or ``list(iterable)``
Nick Coghlan273069c2012-08-20 17:14:07 +10001129
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001130 The constructor builds a list whose items are the same and in the same
1131 order as *iterable*'s items. *iterable* may be either a sequence, a
1132 container that supports iteration, or an iterator object. If *iterable*
1133 is already a list, a copy is made and returned, similar to ``iterable[:]``.
1134 For example, ``list('abc')`` returns ``['a', 'b', 'c']`` and
1135 ``list( (1, 2, 3) )`` returns ``[1, 2, 3]``.
1136 If no argument is given, the constructor creates a new empty list, ``[]``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001137
Nick Coghlan273069c2012-08-20 17:14:07 +10001138
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001139 Many other operations also produce lists, including the :func:`sorted`
1140 built-in.
Nick Coghlan273069c2012-08-20 17:14:07 +10001141
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001142 Lists implement all of the :ref:`common <typesseq-common>` and
1143 :ref:`mutable <typesseq-mutable>` sequence operations. Lists also provide the
1144 following additional method:
Nick Coghlan273069c2012-08-20 17:14:07 +10001145
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001146 .. method:: list.sort(*, key=None, reverse=None)
Nick Coghlan273069c2012-08-20 17:14:07 +10001147
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001148 This method sorts the list in place, using only ``<`` comparisons
1149 between items. Exceptions are not suppressed - if any comparison operations
1150 fail, the entire sort operation will fail (and the list will likely be left
1151 in a partially modified state).
Nick Coghlan273069c2012-08-20 17:14:07 +10001152
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001153 *key* specifies a function of one argument that is used to extract a
1154 comparison key from each list element (for example, ``key=str.lower``).
1155 The key corresponding to each item in the list is calculated once and
1156 then used for the entire sorting process. The default value of ``None``
1157 means that list items are sorted directly without calculating a separate
1158 key value.
Nick Coghlan273069c2012-08-20 17:14:07 +10001159
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001160 The :func:`functools.cmp_to_key` utility is available to convert a 2.x
1161 style *cmp* function to a *key* function.
Nick Coghlan273069c2012-08-20 17:14:07 +10001162
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001163 *reverse* is a boolean value. If set to ``True``, then the list elements
1164 are sorted as if each comparison were reversed.
Nick Coghlan273069c2012-08-20 17:14:07 +10001165
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001166 This method modifies the sequence in place for economy of space when
1167 sorting a large sequence. To remind users that it operates by side
1168 effect, it does not return the sorted sequence (use :func:`sorted` to
1169 explicitly request a new sorted list instance).
1170
1171 The :meth:`sort` method is guaranteed to be stable. A sort is stable if it
1172 guarantees not to change the relative order of elements that compare equal
1173 --- this is helpful for sorting in multiple passes (for example, sort by
1174 department, then by salary grade).
1175
1176 .. impl-detail::
1177
1178 While a list is being sorted, the effect of attempting to mutate, or even
1179 inspect, the list is undefined. The C implementation of Python makes the
1180 list appear empty for the duration, and raises :exc:`ValueError` if it can
1181 detect that the list has been mutated during a sort.
Nick Coghlan273069c2012-08-20 17:14:07 +10001182
1183
1184.. _typesseq-tuple:
1185
1186Tuples
1187------
1188
1189.. index:: object: tuple
1190
1191Tuples are immutable sequences, typically used to store collections of
1192heterogeneous data (such as the 2-tuples produced by the :func:`enumerate`
1193built-in). Tuples are also used for cases where an immutable sequence of
1194homogeneous data is needed (such as allowing storage in a :class:`set` or
1195:class:`dict` instance).
1196
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001197.. class:: tuple([iterable])
Nick Coghlan273069c2012-08-20 17:14:07 +10001198
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001199 Tuples may be constructed in a number of ways:
Nick Coghlan273069c2012-08-20 17:14:07 +10001200
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001201 * Using a pair of parentheses to denote the empty tuple: ``()``
1202 * Using a trailing comma for a singleton tuple: ``a,`` or ``(a,)``
1203 * Separating items with commas: ``a, b, c`` or ``(a, b, c)``
1204 * Using the :func:`tuple` built-in: ``tuple()`` or ``tuple(iterable)``
Nick Coghlan273069c2012-08-20 17:14:07 +10001205
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001206 The constructor builds a tuple whose items are the same and in the same
1207 order as *iterable*'s items. *iterable* may be either a sequence, a
1208 container that supports iteration, or an iterator object. If *iterable*
1209 is already a tuple, it is returned unchanged. For example,
1210 ``tuple('abc')`` returns ``('a', 'b', 'c')`` and
1211 ``tuple( [1, 2, 3] )`` returns ``(1, 2, 3)``.
1212 If no argument is given, the constructor creates a new empty tuple, ``()``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001213
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001214 Note that it is actually the comma which makes a tuple, not the parentheses.
1215 The parentheses are optional, except in the empty tuple case, or
1216 when they are needed to avoid syntactic ambiguity. For example,
1217 ``f(a, b, c)`` is a function call with three arguments, while
1218 ``f((a, b, c))`` is a function call with a 3-tuple as the sole argument.
1219
1220 Tuples implement all of the :ref:`common <typesseq-common>` sequence
1221 operations.
1222
1223For heterogeneous collections of data where access by name is clearer than
1224access by index, :func:`collections.namedtuple` may be a more appropriate
1225choice than a simple tuple object.
Nick Coghlan273069c2012-08-20 17:14:07 +10001226
1227
1228.. _typesseq-range:
1229
1230Ranges
1231------
1232
1233.. index:: object: range
1234
1235The :class:`range` type represents an immutable sequence of numbers and is
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001236commonly used for looping a specific number of times in :keyword:`for`
1237loops.
Nick Coghlan273069c2012-08-20 17:14:07 +10001238
Ezio Melotti8429b672012-09-14 06:35:09 +03001239.. class:: range(stop)
1240 range(start, stop[, step])
Nick Coghlan273069c2012-08-20 17:14:07 +10001241
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001242 The arguments to the range constructor must be integers (either built-in
1243 :class:`int` or any object that implements the ``__index__`` special
1244 method). If the *step* argument is omitted, it defaults to ``1``.
1245 If the *start* argument is omitted, it defaults to ``0``.
1246 If *step* is zero, :exc:`ValueError` is raised.
1247
1248 For a positive *step*, the contents of a range ``r`` are determined by the
1249 formula ``r[i] = start + step*i`` where ``i >= 0`` and
1250 ``r[i] < stop``.
1251
1252 For a negative *step*, the contents of the range are still determined by
1253 the formula ``r[i] = start + step*i``, but the constraints are ``i >= 0``
1254 and ``r[i] > stop``.
1255
Sandro Tosi4c1b9f42013-01-27 00:33:04 +01001256 A range object will be empty if ``r[0]`` does not meet the value
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001257 constraint. Ranges do support negative indices, but these are interpreted
1258 as indexing from the end of the sequence determined by the positive
1259 indices.
1260
1261 Ranges containing absolute values larger than :data:`sys.maxsize` are
1262 permitted but some features (such as :func:`len`) may raise
1263 :exc:`OverflowError`.
1264
1265 Range examples::
1266
1267 >>> list(range(10))
1268 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1269 >>> list(range(1, 11))
1270 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1271 >>> list(range(0, 30, 5))
1272 [0, 5, 10, 15, 20, 25]
1273 >>> list(range(0, 10, 3))
1274 [0, 3, 6, 9]
1275 >>> list(range(0, -10, -1))
1276 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
1277 >>> list(range(0))
1278 []
1279 >>> list(range(1, 0))
1280 []
1281
1282 Ranges implement all of the :ref:`common <typesseq-common>` sequence operations
1283 except concatenation and repetition (due to the fact that range objects can
1284 only represent sequences that follow a strict pattern and repetition and
1285 concatenation will usually violate that pattern).
1286
1287 .. data: start
1288
1289 The value of the *start* parameter (or ``0`` if the parameter was
1290 not supplied)
1291
1292 .. data: stop
1293
1294 The value of the *stop* parameter
1295
1296 .. data: step
1297
1298 The value of the *step* parameter (or ``1`` if the parameter was
1299 not supplied)
Nick Coghlan273069c2012-08-20 17:14:07 +10001300
1301The advantage of the :class:`range` type over a regular :class:`list` or
1302:class:`tuple` is that a :class:`range` object will always take the same
1303(small) amount of memory, no matter the size of the range it represents (as it
1304only stores the ``start``, ``stop`` and ``step`` values, calculating individual
1305items and subranges as needed).
1306
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001307Range objects implement the :class:`collections.Sequence` ABC, and provide
1308features such as containment tests, element index lookup, slicing and
1309support for negative indices (see :ref:`typesseq`):
1310
1311 >>> r = range(0, 20, 2)
1312 >>> r
1313 range(0, 20, 2)
1314 >>> 11 in r
1315 False
1316 >>> 10 in r
1317 True
1318 >>> r.index(10)
1319 5
1320 >>> r[5]
1321 10
1322 >>> r[:5]
1323 range(0, 10, 2)
1324 >>> r[-1]
1325 18
1326
1327Testing range objects for equality with ``==`` and ``!=`` compares
1328them as sequences. That is, two range objects are considered equal if
1329they represent the same sequence of values. (Note that two range
1330objects that compare equal might have different :attr:`start`,
1331:attr:`stop` and :attr:`step` attributes, for example ``range(0) ==
1332range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.)
1333
1334.. versionchanged:: 3.2
1335 Implement the Sequence ABC.
1336 Support slicing and negative indices.
1337 Test :class:`int` objects for membership in constant time instead of
1338 iterating through all items.
1339
1340.. versionchanged:: 3.3
1341 Define '==' and '!=' to compare range objects based on the
1342 sequence of values they define (instead of comparing based on
1343 object identity).
1344
1345.. versionadded:: 3.3
1346 The :attr:`start`, :attr:`stop` and :attr:`step` attributes.
Nick Coghlan273069c2012-08-20 17:14:07 +10001347
1348
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001349.. index::
1350 single: string; text sequence type
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001351 single: str (built-in class); (see also string)
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001352 object: string
1353
Nick Coghlan273069c2012-08-20 17:14:07 +10001354.. _textseq:
1355
1356Text Sequence Type --- :class:`str`
1357===================================
1358
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001359Textual data in Python is handled with :class:`str` objects, or :dfn:`strings`.
1360Strings are immutable
Chris Jerdonekc33899b2012-10-11 18:57:48 -07001361:ref:`sequences <typesseq>` of Unicode code points. String literals are
Nick Coghlan273069c2012-08-20 17:14:07 +10001362written in a variety of ways:
1363
1364* Single quotes: ``'allows embedded "double" quotes'``
1365* Double quotes: ``"allows embedded 'single' quotes"``.
1366* Triple quoted: ``'''Three single quotes'''``, ``"""Three double quotes"""``
1367
1368Triple quoted strings may span multiple lines - all associated whitespace will
1369be included in the string literal.
1370
1371String literals that are part of a single expression and have only whitespace
Nick Coghlan83c0ae52012-08-21 17:42:52 +10001372between them will be implicitly converted to a single string literal. That
1373is, ``("spam " "eggs") == "spam eggs"``.
Nick Coghlan273069c2012-08-20 17:14:07 +10001374
1375See :ref:`strings` for more about the various forms of string literal,
1376including supported escape sequences, and the ``r`` ("raw") prefix that
1377disables most escape sequence processing.
1378
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001379Strings may also be created from other objects using the :class:`str`
1380constructor.
Nick Coghlan273069c2012-08-20 17:14:07 +10001381
1382Since there is no separate "character" type, indexing a string produces
1383strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``.
1384
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08001385.. index::
1386 object: io.StringIO
1387
Nick Coghlan273069c2012-08-20 17:14:07 +10001388There is also no mutable string type, but :meth:`str.join` or
1389:class:`io.StringIO` can be used to efficiently construct strings from
1390multiple fragments.
1391
1392.. versionchanged:: 3.3
1393 For backwards compatibility with the Python 2 series, the ``u`` prefix is
1394 once again permitted on string literals. It has no effect on the meaning
1395 of string literals and cannot be combined with the ``r`` prefix.
Georg Brandl116aa622007-08-15 14:28:22 +00001396
Chris Jerdonekbb4e9412012-11-28 01:38:40 -08001397
1398.. index::
1399 single: string; str (built-in class)
1400
1401.. class:: str(object='')
1402 str(object=b'', encoding='utf-8', errors='strict')
1403
1404 Return a :ref:`string <textseq>` version of *object*. If *object* is not
1405 provided, returns the empty string. Otherwise, the behavior of ``str()``
1406 depends on whether *encoding* or *errors* is given, as follows.
1407
1408 If neither *encoding* nor *errors* is given, ``str(object)`` returns
1409 :meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1410 printable string representation of *object*. For string objects, this is
1411 the string itself. If *object* does not have a :meth:`~object.__str__`
1412 method, then :func:`str` falls back to returning
1413 :meth:`repr(object) <repr>`.
1414
1415 .. index::
1416 single: buffer protocol; str (built-in class)
1417 single: bytes; str (built-in class)
1418
1419 If at least one of *encoding* or *errors* is given, *object* should be a
1420 :class:`bytes` or :class:`bytearray` object, or more generally any object
1421 that supports the :ref:`buffer protocol <bufferobjects>`. In this case, if
1422 *object* is a :class:`bytes` (or :class:`bytearray`) object, then
1423 ``str(bytes, encoding, errors)`` is equivalent to
1424 :meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1425 object underlying the buffer object is obtained before calling
1426 :meth:`bytes.decode`. See :ref:`binaryseq` and
1427 :ref:`bufferobjects` for information on buffer objects.
1428
1429 Passing a :class:`bytes` object to :func:`str` without the *encoding*
1430 or *errors* arguments falls under the first case of returning the informal
1431 string representation (see also the :option:`-b` command-line option to
1432 Python). For example::
1433
1434 >>> str(b'Zoot!')
1435 "b'Zoot!'"
1436
1437 For more information on the ``str`` class and its methods, see
1438 :ref:`textseq` and the :ref:`string-methods` section below. To output
1439 formatted strings, see the :ref:`string-formatting` section. In addition,
1440 see the :ref:`stringservices` section.
1441
1442
1443.. index::
1444 pair: string; methods
1445
Georg Brandl116aa622007-08-15 14:28:22 +00001446.. _string-methods:
1447
1448String Methods
1449--------------
1450
Nick Coghlan273069c2012-08-20 17:14:07 +10001451.. index::
Nick Coghlan273069c2012-08-20 17:14:07 +10001452 module: re
Georg Brandl116aa622007-08-15 14:28:22 +00001453
Nick Coghlan273069c2012-08-20 17:14:07 +10001454Strings implement all of the :ref:`common <typesseq-common>` sequence
1455operations, along with the additional methods described below.
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001456
Nick Coghlan273069c2012-08-20 17:14:07 +10001457Strings also support two styles of string formatting, one providing a large
1458degree of flexibility and customization (see :meth:`str.format`,
1459:ref:`formatstrings` and :ref:`string-formatting`) and the other based on C
1460``printf`` style formatting that handles a narrower range of types and is
1461slightly harder to use correctly, but is often faster for the cases it can
1462handle (:ref:`old-string-formatting`).
1463
1464The :ref:`textservices` section of the standard library covers a number of
1465other modules that provide various text related utilities (including regular
1466expression support in the :mod:`re` module).
Georg Brandl116aa622007-08-15 14:28:22 +00001467
1468.. method:: str.capitalize()
1469
Senthil Kumaranfa897982010-07-05 11:41:42 +00001470 Return a copy of the string with its first character capitalized and the
Senthil Kumaran37c63a32010-07-06 02:08:36 +00001471 rest lowercased.
Georg Brandl116aa622007-08-15 14:28:22 +00001472
Georg Brandl116aa622007-08-15 14:28:22 +00001473
Benjamin Petersond5890c82012-01-14 13:23:30 -05001474.. method:: str.casefold()
1475
1476 Return a casefolded copy of the string. Casefolded strings may be used for
Benjamin Peterson94303542012-01-18 23:09:32 -05001477 caseless matching.
1478
1479 Casefolding is similar to lowercasing but more aggressive because it is
1480 intended to remove all case distinctions in a string. For example, the German
1481 lowercase letter ``'ß'`` is equivalent to ``"ss"``. Since it is already
1482 lowercase, :meth:`lower` would do nothing to ``'ß'``; :meth:`casefold`
1483 converts it to ``"ss"``.
1484
1485 The casefolding algorithm is described in section 3.13 of the Unicode
1486 Standard.
Benjamin Petersond5890c82012-01-14 13:23:30 -05001487
1488 .. versionadded:: 3.3
1489
1490
Georg Brandl116aa622007-08-15 14:28:22 +00001491.. method:: str.center(width[, fillchar])
1492
1493 Return centered in a string of length *width*. Padding is done using the
1494 specified *fillchar* (default is a space).
1495
Georg Brandl116aa622007-08-15 14:28:22 +00001496
1497.. method:: str.count(sub[, start[, end]])
1498
Benjamin Petersonad3d5c22009-02-26 03:38:59 +00001499 Return the number of non-overlapping occurrences of substring *sub* in the
1500 range [*start*, *end*]. Optional arguments *start* and *end* are
1501 interpreted as in slice notation.
Georg Brandl116aa622007-08-15 14:28:22 +00001502
1503
Victor Stinnere14e2122010-11-07 18:41:46 +00001504.. method:: str.encode(encoding="utf-8", errors="strict")
Georg Brandl116aa622007-08-15 14:28:22 +00001505
Victor Stinnere14e2122010-11-07 18:41:46 +00001506 Return an encoded version of the string as a bytes object. Default encoding
1507 is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
1508 The default for *errors* is ``'strict'``, meaning that encoding errors raise
1509 a :exc:`UnicodeError`. Other possible
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001510 values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1511 ``'backslashreplace'`` and any other name registered via
1512 :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a
1513 list of possible encodings, see section :ref:`standard-encodings`.
Georg Brandl116aa622007-08-15 14:28:22 +00001514
Benjamin Peterson308d6372009-09-18 21:42:35 +00001515 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +00001516 Support for keyword arguments added.
1517
Georg Brandl116aa622007-08-15 14:28:22 +00001518
1519.. method:: str.endswith(suffix[, start[, end]])
1520
1521 Return ``True`` if the string ends with the specified *suffix*, otherwise return
1522 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
1523 *start*, test beginning at that position. With optional *end*, stop comparing
1524 at that position.
1525
Georg Brandl116aa622007-08-15 14:28:22 +00001526
1527.. method:: str.expandtabs([tabsize])
1528
Eli Benderskyc2c89602011-11-11 10:40:14 +02001529 Return a copy of the string where all tab characters are replaced by zero or
Georg Brandl9afde1c2007-11-01 20:32:30 +00001530 more spaces, depending on the current column and the given tab size. The
1531 column number is reset to zero after each newline occurring in the string.
1532 If *tabsize* is not given, a tab size of ``8`` characters is assumed. This
1533 doesn't understand other non-printing characters or escape sequences.
Georg Brandl116aa622007-08-15 14:28:22 +00001534
1535
1536.. method:: str.find(sub[, start[, end]])
1537
Benjamin Petersond99cd812010-04-27 22:58:50 +00001538 Return the lowest index in the string where substring *sub* is found, such
1539 that *sub* is contained in the slice ``s[start:end]``. Optional arguments
1540 *start* and *end* are interpreted as in slice notation. Return ``-1`` if
1541 *sub* is not found.
Georg Brandl116aa622007-08-15 14:28:22 +00001542
Ezio Melotti0ed8c682011-05-09 03:54:30 +03001543 .. note::
1544
1545 The :meth:`~str.find` method should be used only if you need to know the
1546 position of *sub*. To check if *sub* is a substring or not, use the
1547 :keyword:`in` operator::
1548
1549 >>> 'Py' in 'Python'
1550 True
1551
Georg Brandl116aa622007-08-15 14:28:22 +00001552
Benjamin Petersonad3d5c22009-02-26 03:38:59 +00001553.. method:: str.format(*args, **kwargs)
Georg Brandl4b491312007-08-31 09:22:56 +00001554
Georg Brandl1f70cdf2010-03-21 09:04:24 +00001555 Perform a string formatting operation. The string on which this method is
1556 called can contain literal text or replacement fields delimited by braces
1557 ``{}``. Each replacement field contains either the numeric index of a
1558 positional argument, or the name of a keyword argument. Returns a copy of
1559 the string where each replacement field is replaced with the string value of
1560 the corresponding argument.
Georg Brandl4b491312007-08-31 09:22:56 +00001561
1562 >>> "The sum of 1 + 2 is {0}".format(1+2)
1563 'The sum of 1 + 2 is 3'
1564
1565 See :ref:`formatstrings` for a description of the various formatting options
1566 that can be specified in format strings.
1567
Georg Brandl4b491312007-08-31 09:22:56 +00001568
Eric Smith27bbca62010-11-04 17:06:58 +00001569.. method:: str.format_map(mapping)
1570
Éric Araujo2642ad02010-11-06 04:59:27 +00001571 Similar to ``str.format(**mapping)``, except that ``mapping`` is
Eric Smith27bbca62010-11-04 17:06:58 +00001572 used directly and not copied to a :class:`dict` . This is useful
Eric Smith5ad85f82010-11-06 13:22:13 +00001573 if for example ``mapping`` is a dict subclass:
Eric Smith27bbca62010-11-04 17:06:58 +00001574
Eric Smith5ad85f82010-11-06 13:22:13 +00001575 >>> class Default(dict):
1576 ... def __missing__(self, key):
1577 ... return key
1578 ...
1579 >>> '{name} was born in {country}'.format_map(Default(name='Guido'))
1580 'Guido was born in country'
1581
1582 .. versionadded:: 3.2
1583
Eric Smith27bbca62010-11-04 17:06:58 +00001584
Georg Brandl116aa622007-08-15 14:28:22 +00001585.. method:: str.index(sub[, start[, end]])
1586
1587 Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found.
1588
1589
1590.. method:: str.isalnum()
1591
1592 Return true if all characters in the string are alphanumeric and there is at
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001593 least one character, false otherwise. A character ``c`` is alphanumeric if one
1594 of the following returns ``True``: ``c.isalpha()``, ``c.isdecimal()``,
1595 ``c.isdigit()``, or ``c.isnumeric()``.
Georg Brandl116aa622007-08-15 14:28:22 +00001596
Georg Brandl116aa622007-08-15 14:28:22 +00001597
1598.. method:: str.isalpha()
1599
1600 Return true if all characters in the string are alphabetic and there is at least
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001601 one character, false otherwise. Alphabetic characters are those characters defined
1602 in the Unicode character database as "Letter", i.e., those with general category
1603 property being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is different
1604 from the "Alphabetic" property defined in the Unicode Standard.
Georg Brandl116aa622007-08-15 14:28:22 +00001605
Georg Brandl116aa622007-08-15 14:28:22 +00001606
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001607.. method:: str.isdecimal()
1608
1609 Return true if all characters in the string are decimal
1610 characters and there is at least one character, false
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001611 otherwise. Decimal characters are those from general category "Nd". This category
1612 includes digit characters, and all characters
Ezio Melottie130a522011-10-19 10:58:56 +03001613 that can be used to form decimal-radix numbers, e.g. U+0660,
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001614 ARABIC-INDIC DIGIT ZERO.
Georg Brandl48310cd2009-01-03 21:18:54 +00001615
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001616
Georg Brandl116aa622007-08-15 14:28:22 +00001617.. method:: str.isdigit()
1618
1619 Return true if all characters in the string are digits and there is at least one
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001620 character, false otherwise. Digits include decimal characters and digits that need
1621 special handling, such as the compatibility superscript digits. Formally, a digit
1622 is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
Georg Brandl116aa622007-08-15 14:28:22 +00001623
Georg Brandl116aa622007-08-15 14:28:22 +00001624
1625.. method:: str.isidentifier()
1626
1627 Return true if the string is a valid identifier according to the language
Georg Brandl4b491312007-08-31 09:22:56 +00001628 definition, section :ref:`identifiers`.
Georg Brandl116aa622007-08-15 14:28:22 +00001629
Raymond Hettinger378170d2013-03-23 08:21:12 -07001630 Use :func:`keyword.iskeyword` to test for reserved identifiers such as
1631 :keyword:`def` and :keyword:`class`.
Georg Brandl116aa622007-08-15 14:28:22 +00001632
1633.. method:: str.islower()
1634
Ezio Melotti0656a562011-08-15 14:27:19 +03001635 Return true if all cased characters [4]_ in the string are lowercase and
1636 there is at least one cased character, false otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +00001637
Georg Brandl116aa622007-08-15 14:28:22 +00001638
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001639.. method:: str.isnumeric()
1640
1641 Return true if all characters in the string are numeric
1642 characters, and there is at least one character, false
1643 otherwise. Numeric characters include digit characters, and all characters
1644 that have the Unicode numeric value property, e.g. U+2155,
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001645 VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property
1646 value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001647
Georg Brandl48310cd2009-01-03 21:18:54 +00001648
Georg Brandl559e5d72008-06-11 18:37:52 +00001649.. method:: str.isprintable()
1650
1651 Return true if all characters in the string are printable or the string is
1652 empty, false otherwise. Nonprintable characters are those characters defined
1653 in the Unicode character database as "Other" or "Separator", excepting the
1654 ASCII space (0x20) which is considered printable. (Note that printable
1655 characters in this context are those which should not be escaped when
1656 :func:`repr` is invoked on a string. It has no bearing on the handling of
1657 strings written to :data:`sys.stdout` or :data:`sys.stderr`.)
1658
1659
Georg Brandl116aa622007-08-15 14:28:22 +00001660.. method:: str.isspace()
1661
1662 Return true if there are only whitespace characters in the string and there is
Alexander Belopolsky0d267982010-12-23 02:58:25 +00001663 at least one character, false otherwise. Whitespace characters are those
1664 characters defined in the Unicode character database as "Other" or "Separator"
1665 and those with bidirectional property being one of "WS", "B", or "S".
Georg Brandl116aa622007-08-15 14:28:22 +00001666
1667.. method:: str.istitle()
1668
1669 Return true if the string is a titlecased string and there is at least one
1670 character, for example uppercase characters may only follow uncased characters
1671 and lowercase characters only cased ones. Return false otherwise.
1672
Georg Brandl116aa622007-08-15 14:28:22 +00001673
1674.. method:: str.isupper()
1675
Ezio Melotti0656a562011-08-15 14:27:19 +03001676 Return true if all cased characters [4]_ in the string are uppercase and
1677 there is at least one cased character, false otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +00001678
Georg Brandl116aa622007-08-15 14:28:22 +00001679
Georg Brandl495f7b52009-10-27 15:28:25 +00001680.. method:: str.join(iterable)
Georg Brandl116aa622007-08-15 14:28:22 +00001681
Georg Brandl495f7b52009-10-27 15:28:25 +00001682 Return a string which is the concatenation of the strings in the
1683 :term:`iterable` *iterable*. A :exc:`TypeError` will be raised if there are
Terry Jan Reedyf4ec3c52012-01-11 03:29:42 -05001684 any non-string values in *iterable*, including :class:`bytes` objects. The
Georg Brandl495f7b52009-10-27 15:28:25 +00001685 separator between elements is the string providing this method.
Georg Brandl116aa622007-08-15 14:28:22 +00001686
1687
1688.. method:: str.ljust(width[, fillchar])
1689
1690 Return the string left justified in a string of length *width*. Padding is done
1691 using the specified *fillchar* (default is a space). The original string is
Terry Jan Reedyf4ec3c52012-01-11 03:29:42 -05001692 returned if *width* is less than or equal to ``len(s)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001693
Georg Brandl116aa622007-08-15 14:28:22 +00001694
1695.. method:: str.lower()
1696
Ezio Melotti0656a562011-08-15 14:27:19 +03001697 Return a copy of the string with all the cased characters [4]_ converted to
1698 lowercase.
Georg Brandl116aa622007-08-15 14:28:22 +00001699
Benjamin Peterson94303542012-01-18 23:09:32 -05001700 The lowercasing algorithm used is described in section 3.13 of the Unicode
1701 Standard.
1702
Georg Brandl116aa622007-08-15 14:28:22 +00001703
1704.. method:: str.lstrip([chars])
1705
1706 Return a copy of the string with leading characters removed. The *chars*
1707 argument is a string specifying the set of characters to be removed. If omitted
1708 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +00001709 argument is not a prefix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001710
1711 >>> ' spacious '.lstrip()
1712 'spacious '
1713 >>> 'www.example.com'.lstrip('cmowz.')
1714 'example.com'
1715
Georg Brandl116aa622007-08-15 14:28:22 +00001716
Georg Brandlabc38772009-04-12 15:51:51 +00001717.. staticmethod:: str.maketrans(x[, y[, z]])
Georg Brandlceee0772007-11-27 23:48:05 +00001718
1719 This static method returns a translation table usable for :meth:`str.translate`.
1720
1721 If there is only one argument, it must be a dictionary mapping Unicode
1722 ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
1723 strings (of arbitrary lengths) or None. Character keys will then be
1724 converted to ordinals.
1725
1726 If there are two arguments, they must be strings of equal length, and in the
1727 resulting dictionary, each character in x will be mapped to the character at
1728 the same position in y. If there is a third argument, it must be a string,
1729 whose characters will be mapped to None in the result.
1730
1731
Georg Brandl116aa622007-08-15 14:28:22 +00001732.. method:: str.partition(sep)
1733
1734 Split the string at the first occurrence of *sep*, and return a 3-tuple
1735 containing the part before the separator, the separator itself, and the part
1736 after the separator. If the separator is not found, return a 3-tuple containing
1737 the string itself, followed by two empty strings.
1738
Georg Brandl116aa622007-08-15 14:28:22 +00001739
1740.. method:: str.replace(old, new[, count])
1741
1742 Return a copy of the string with all occurrences of substring *old* replaced by
1743 *new*. If the optional argument *count* is given, only the first *count*
1744 occurrences are replaced.
1745
1746
Georg Brandl226878c2007-08-31 10:15:37 +00001747.. method:: str.rfind(sub[, start[, end]])
Georg Brandl116aa622007-08-15 14:28:22 +00001748
Benjamin Petersond99cd812010-04-27 22:58:50 +00001749 Return the highest index in the string where substring *sub* is found, such
1750 that *sub* is contained within ``s[start:end]``. Optional arguments *start*
1751 and *end* are interpreted as in slice notation. Return ``-1`` on failure.
Georg Brandl116aa622007-08-15 14:28:22 +00001752
1753
1754.. method:: str.rindex(sub[, start[, end]])
1755
1756 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1757 found.
1758
1759
1760.. method:: str.rjust(width[, fillchar])
1761
1762 Return the string right justified in a string of length *width*. Padding is done
1763 using the specified *fillchar* (default is a space). The original string is
Terry Jan Reedyf4ec3c52012-01-11 03:29:42 -05001764 returned if *width* is less than or equal to ``len(s)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001765
Georg Brandl116aa622007-08-15 14:28:22 +00001766
1767.. method:: str.rpartition(sep)
1768
1769 Split the string at the last occurrence of *sep*, and return a 3-tuple
1770 containing the part before the separator, the separator itself, and the part
1771 after the separator. If the separator is not found, return a 3-tuple containing
1772 two empty strings, followed by the string itself.
1773
Georg Brandl116aa622007-08-15 14:28:22 +00001774
Ezio Melotticda6b6d2012-02-26 09:39:55 +02001775.. method:: str.rsplit(sep=None, maxsplit=-1)
Georg Brandl116aa622007-08-15 14:28:22 +00001776
1777 Return a list of the words in the string, using *sep* as the delimiter string.
1778 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
1779 ones. If *sep* is not specified or ``None``, any whitespace string is a
1780 separator. Except for splitting from the right, :meth:`rsplit` behaves like
1781 :meth:`split` which is described in detail below.
1782
Georg Brandl116aa622007-08-15 14:28:22 +00001783
1784.. method:: str.rstrip([chars])
1785
1786 Return a copy of the string with trailing characters removed. The *chars*
1787 argument is a string specifying the set of characters to be removed. If omitted
1788 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +00001789 argument is not a suffix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001790
1791 >>> ' spacious '.rstrip()
1792 ' spacious'
1793 >>> 'mississippi'.rstrip('ipz')
1794 'mississ'
1795
Georg Brandl116aa622007-08-15 14:28:22 +00001796
Ezio Melotticda6b6d2012-02-26 09:39:55 +02001797.. method:: str.split(sep=None, maxsplit=-1)
Georg Brandl116aa622007-08-15 14:28:22 +00001798
Georg Brandl226878c2007-08-31 10:15:37 +00001799 Return a list of the words in the string, using *sep* as the delimiter
1800 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
1801 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
Ezio Melottibf3165b2012-05-10 15:30:42 +03001802 specified or ``-1``, then there is no limit on the number of splits
1803 (all possible splits are made).
Georg Brandl9afde1c2007-11-01 20:32:30 +00001804
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001805 If *sep* is given, consecutive delimiters are not grouped together and are
Georg Brandl226878c2007-08-31 10:15:37 +00001806 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
1807 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
Georg Brandl9afde1c2007-11-01 20:32:30 +00001808 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Georg Brandl226878c2007-08-31 10:15:37 +00001809 Splitting an empty string with a specified separator returns ``['']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001810
1811 If *sep* is not specified or is ``None``, a different splitting algorithm is
Georg Brandl9afde1c2007-11-01 20:32:30 +00001812 applied: runs of consecutive whitespace are regarded as a single separator,
1813 and the result will contain no empty strings at the start or end if the
1814 string has leading or trailing whitespace. Consequently, splitting an empty
1815 string or a string consisting of just whitespace with a ``None`` separator
1816 returns ``[]``.
1817
1818 For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and
1819 ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001820
1821
R David Murray1b00f252012-08-15 10:43:58 -04001822.. index::
1823 single: universal newlines; str.splitlines method
1824
Georg Brandl116aa622007-08-15 14:28:22 +00001825.. method:: str.splitlines([keepends])
1826
R David Murray05c35a62012-08-06 16:08:09 -04001827 Return a list of the lines in the string, breaking at line boundaries.
R David Murray1b00f252012-08-15 10:43:58 -04001828 This method uses the :term:`universal newlines` approach to splitting lines.
R David Murray05c35a62012-08-06 16:08:09 -04001829 Line breaks are not included in the resulting list unless *keepends* is
1830 given and true.
R David Murrayae1b94b2012-06-01 16:19:36 -04001831
1832 For example, ``'ab c\n\nde fg\rkl\r\n'.splitlines()`` returns
R David Murray554b3482012-06-02 11:20:29 -04001833 ``['ab c', '', 'de fg', 'kl']``, while the same call with ``splitlines(True)``
Sandro Tosi82a509c2012-08-12 12:35:14 +02001834 returns ``['ab c\n', '\n', 'de fg\r', 'kl\r\n']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001835
R David Murray05c35a62012-08-06 16:08:09 -04001836 Unlike :meth:`~str.split` when a delimiter string *sep* is given, this
1837 method returns an empty list for the empty string, and a terminal line
1838 break does not result in an extra line.
1839
Georg Brandl116aa622007-08-15 14:28:22 +00001840
1841.. method:: str.startswith(prefix[, start[, end]])
1842
1843 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
1844 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
1845 test string beginning at that position. With optional *end*, stop comparing
1846 string at that position.
1847
Georg Brandl116aa622007-08-15 14:28:22 +00001848
1849.. method:: str.strip([chars])
1850
1851 Return a copy of the string with the leading and trailing characters removed.
1852 The *chars* argument is a string specifying the set of characters to be removed.
1853 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
1854 The *chars* argument is not a prefix or suffix; rather, all combinations of its
Christian Heimesfe337bf2008-03-23 21:54:12 +00001855 values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001856
1857 >>> ' spacious '.strip()
1858 'spacious'
1859 >>> 'www.example.com'.strip('cmowz.')
1860 'example'
1861
Georg Brandl116aa622007-08-15 14:28:22 +00001862
1863.. method:: str.swapcase()
1864
1865 Return a copy of the string with uppercase characters converted to lowercase and
Benjamin Petersonb2bf01d2012-01-11 18:17:06 -05001866 vice versa. Note that it is not necessarily true that
1867 ``s.swapcase().swapcase() == s``.
Georg Brandl116aa622007-08-15 14:28:22 +00001868
Georg Brandl116aa622007-08-15 14:28:22 +00001869
1870.. method:: str.title()
1871
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00001872 Return a titlecased version of the string where words start with an uppercase
1873 character and the remaining characters are lowercase.
1874
1875 The algorithm uses a simple language-independent definition of a word as
1876 groups of consecutive letters. The definition works in many contexts but
1877 it means that apostrophes in contractions and possessives form word
1878 boundaries, which may not be the desired result::
1879
1880 >>> "they're bill's friends from the UK".title()
1881 "They'Re Bill'S Friends From The Uk"
1882
1883 A workaround for apostrophes can be constructed using regular expressions::
1884
1885 >>> import re
1886 >>> def titlecase(s):
Andrew Svetlov5c904362012-11-08 17:26:53 +02001887 ... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
1888 ... lambda mo: mo.group(0)[0].upper() +
1889 ... mo.group(0)[1:].lower(),
1890 ... s)
1891 ...
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00001892 >>> titlecase("they're bill's friends.")
1893 "They're Bill's Friends."
Georg Brandl116aa622007-08-15 14:28:22 +00001894
Georg Brandl116aa622007-08-15 14:28:22 +00001895
Georg Brandl4b491312007-08-31 09:22:56 +00001896.. method:: str.translate(map)
Georg Brandl116aa622007-08-15 14:28:22 +00001897
Georg Brandl226878c2007-08-31 10:15:37 +00001898 Return a copy of the *s* where all characters have been mapped through the
Georg Brandl454636f2008-12-27 23:33:20 +00001899 *map* which must be a dictionary of Unicode ordinals (integers) to Unicode
Georg Brandlceee0772007-11-27 23:48:05 +00001900 ordinals, strings or ``None``. Unmapped characters are left untouched.
1901 Characters mapped to ``None`` are deleted.
1902
Georg Brandl454636f2008-12-27 23:33:20 +00001903 You can use :meth:`str.maketrans` to create a translation map from
1904 character-to-character mappings in different formats.
Christian Heimesfe337bf2008-03-23 21:54:12 +00001905
Georg Brandl4b491312007-08-31 09:22:56 +00001906 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +00001907
Georg Brandlceee0772007-11-27 23:48:05 +00001908 An even more flexible approach is to create a custom character mapping
1909 codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
Georg Brandl4b491312007-08-31 09:22:56 +00001910 example).
Georg Brandl116aa622007-08-15 14:28:22 +00001911
1912
1913.. method:: str.upper()
1914
Ezio Melotti0656a562011-08-15 14:27:19 +03001915 Return a copy of the string with all the cased characters [4]_ converted to
1916 uppercase. Note that ``str.upper().isupper()`` might be ``False`` if ``s``
1917 contains uncased characters or if the Unicode category of the resulting
Benjamin Peterson94303542012-01-18 23:09:32 -05001918 character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter,
1919 titlecase).
1920
1921 The uppercasing algorithm used is described in section 3.13 of the Unicode
1922 Standard.
Georg Brandl116aa622007-08-15 14:28:22 +00001923
Georg Brandl116aa622007-08-15 14:28:22 +00001924
1925.. method:: str.zfill(width)
1926
Georg Brandl9afde1c2007-11-01 20:32:30 +00001927 Return the numeric string left filled with zeros in a string of length
1928 *width*. A sign prefix is handled correctly. The original string is
Terry Jan Reedyf4ec3c52012-01-11 03:29:42 -05001929 returned if *width* is less than or equal to ``len(s)``.
Christian Heimesb186d002008-03-18 15:15:01 +00001930
1931
Georg Brandl116aa622007-08-15 14:28:22 +00001932
Georg Brandl4b491312007-08-31 09:22:56 +00001933.. _old-string-formatting:
Georg Brandl116aa622007-08-15 14:28:22 +00001934
Nick Coghlan273069c2012-08-20 17:14:07 +10001935``printf``-style String Formatting
1936----------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00001937
1938.. index::
1939 single: formatting, string (%)
1940 single: interpolation, string (%)
1941 single: string; formatting
1942 single: string; interpolation
1943 single: printf-style formatting
1944 single: sprintf-style formatting
1945 single: % formatting
1946 single: % interpolation
1947
Georg Brandl4b491312007-08-31 09:22:56 +00001948.. note::
1949
Nick Coghlan273069c2012-08-20 17:14:07 +10001950 The formatting operations described here exhibit a variety of quirks that
1951 lead to a number of common errors (such as failing to display tuples and
1952 dictionaries correctly). Using the newer :meth:`str.format` interface
1953 helps avoid these errors, and also provides a generally more powerful,
1954 flexible and extensible approach to formatting text.
Georg Brandl4b491312007-08-31 09:22:56 +00001955
1956String objects have one unique built-in operation: the ``%`` operator (modulo).
1957This is also known as the string *formatting* or *interpolation* operator.
1958Given ``format % values`` (where *format* is a string), ``%`` conversion
1959specifications in *format* are replaced with zero or more elements of *values*.
Nick Coghlan273069c2012-08-20 17:14:07 +10001960The effect is similar to using the :c:func:`sprintf` in the C language.
Georg Brandl116aa622007-08-15 14:28:22 +00001961
1962If *format* requires a single argument, *values* may be a single non-tuple
Ezio Melotti0656a562011-08-15 14:27:19 +03001963object. [5]_ Otherwise, *values* must be a tuple with exactly the number of
Georg Brandl116aa622007-08-15 14:28:22 +00001964items specified by the format string, or a single mapping object (for example, a
1965dictionary).
1966
1967A conversion specifier contains two or more characters and has the following
1968components, which must occur in this order:
1969
1970#. The ``'%'`` character, which marks the start of the specifier.
1971
1972#. Mapping key (optional), consisting of a parenthesised sequence of characters
1973 (for example, ``(somename)``).
1974
1975#. Conversion flags (optional), which affect the result of some conversion
1976 types.
1977
1978#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
1979 actual width is read from the next element of the tuple in *values*, and the
1980 object to convert comes after the minimum field width and optional precision.
1981
1982#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
Eli Benderskyef4902a2011-07-29 09:30:42 +03001983 specified as ``'*'`` (an asterisk), the actual precision is read from the next
Georg Brandl116aa622007-08-15 14:28:22 +00001984 element of the tuple in *values*, and the value to convert comes after the
1985 precision.
1986
1987#. Length modifier (optional).
1988
1989#. Conversion type.
1990
1991When the right argument is a dictionary (or other mapping type), then the
1992formats in the string *must* include a parenthesised mapping key into that
1993dictionary inserted immediately after the ``'%'`` character. The mapping key
Christian Heimesfe337bf2008-03-23 21:54:12 +00001994selects the value to be formatted from the mapping. For example:
Georg Brandl116aa622007-08-15 14:28:22 +00001995
Georg Brandledc9e7f2010-10-17 09:19:03 +00001996 >>> print('%(language)s has %(number)03d quote types.' %
1997 ... {'language': "Python", "number": 2})
Georg Brandl116aa622007-08-15 14:28:22 +00001998 Python has 002 quote types.
1999
2000In this case no ``*`` specifiers may occur in a format (since they require a
2001sequential parameter list).
2002
2003The conversion flag characters are:
2004
2005+---------+---------------------------------------------------------------------+
2006| Flag | Meaning |
2007+=========+=====================================================================+
2008| ``'#'`` | The value conversion will use the "alternate form" (where defined |
2009| | below). |
2010+---------+---------------------------------------------------------------------+
2011| ``'0'`` | The conversion will be zero padded for numeric values. |
2012+---------+---------------------------------------------------------------------+
2013| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
2014| | conversion if both are given). |
2015+---------+---------------------------------------------------------------------+
2016| ``' '`` | (a space) A blank should be left before a positive number (or empty |
2017| | string) produced by a signed conversion. |
2018+---------+---------------------------------------------------------------------+
2019| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
2020| | (overrides a "space" flag). |
2021+---------+---------------------------------------------------------------------+
2022
2023A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002024is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
Georg Brandl116aa622007-08-15 14:28:22 +00002025
2026The conversion types are:
2027
2028+------------+-----------------------------------------------------+-------+
2029| Conversion | Meaning | Notes |
2030+============+=====================================================+=======+
2031| ``'d'`` | Signed integer decimal. | |
2032+------------+-----------------------------------------------------+-------+
2033| ``'i'`` | Signed integer decimal. | |
2034+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002035| ``'o'`` | Signed octal value. | \(1) |
Georg Brandl116aa622007-08-15 14:28:22 +00002036+------------+-----------------------------------------------------+-------+
Benjamin Petersone0124bd2009-03-09 21:04:33 +00002037| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(7) |
Georg Brandl116aa622007-08-15 14:28:22 +00002038+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002039| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00002040+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002041| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00002042+------------+-----------------------------------------------------+-------+
2043| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
2044+------------+-----------------------------------------------------+-------+
2045| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
2046+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00002047| ``'f'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00002048+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00002049| ``'F'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00002050+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00002051| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
2052| | format if exponent is less than -4 or not less than | |
2053| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00002054+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00002055| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
2056| | format if exponent is less than -4 or not less than | |
2057| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00002058+------------+-----------------------------------------------------+-------+
2059| ``'c'`` | Single character (accepts integer or single | |
2060| | character string). | |
2061+------------+-----------------------------------------------------+-------+
Ezio Melotti0639d5a2009-12-19 23:26:38 +00002062| ``'r'`` | String (converts any Python object using | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00002063| | :func:`repr`). | |
2064+------------+-----------------------------------------------------+-------+
Eli Benderskyef4902a2011-07-29 09:30:42 +03002065| ``'s'`` | String (converts any Python object using | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00002066| | :func:`str`). | |
2067+------------+-----------------------------------------------------+-------+
Eli Benderskyef4902a2011-07-29 09:30:42 +03002068| ``'a'`` | String (converts any Python object using | \(5) |
2069| | :func:`ascii`). | |
2070+------------+-----------------------------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +00002071| ``'%'`` | No argument is converted, results in a ``'%'`` | |
2072| | character in the result. | |
2073+------------+-----------------------------------------------------+-------+
2074
2075Notes:
2076
2077(1)
2078 The alternate form causes a leading zero (``'0'``) to be inserted between
2079 left-hand padding and the formatting of the number if the leading character
2080 of the result is not already a zero.
2081
2082(2)
2083 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
2084 the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
2085 and the formatting of the number if the leading character of the result is not
2086 already a zero.
2087
2088(3)
2089 The alternate form causes the result to always contain a decimal point, even if
2090 no digits follow it.
2091
2092 The precision determines the number of digits after the decimal point and
2093 defaults to 6.
2094
2095(4)
2096 The alternate form causes the result to always contain a decimal point, and
2097 trailing zeroes are not removed as they would otherwise be.
2098
2099 The precision determines the number of significant digits before and after the
2100 decimal point and defaults to 6.
2101
2102(5)
Eli Benderskyef4902a2011-07-29 09:30:42 +03002103 If precision is ``N``, the output is truncated to ``N`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +00002104
Georg Brandl116aa622007-08-15 14:28:22 +00002105
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00002106(7)
2107 See :pep:`237`.
2108
Georg Brandl116aa622007-08-15 14:28:22 +00002109Since Python strings have an explicit length, ``%s`` conversions do not assume
2110that ``'\0'`` is the end of the string.
2111
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002112.. XXX Examples?
2113
Mark Dickinson33841c32009-05-01 15:37:04 +00002114.. versionchanged:: 3.1
2115 ``%f`` conversions for numbers whose absolute value is over 1e50 are no
2116 longer replaced by ``%g`` conversions.
Georg Brandl116aa622007-08-15 14:28:22 +00002117
Georg Brandl116aa622007-08-15 14:28:22 +00002118
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08002119.. index::
2120 single: buffer protocol; binary sequence types
2121
Nick Coghlan273069c2012-08-20 17:14:07 +10002122.. _binaryseq:
Georg Brandl116aa622007-08-15 14:28:22 +00002123
Nick Coghlan273069c2012-08-20 17:14:07 +10002124Binary Sequence Types --- :class:`bytes`, :class:`bytearray`, :class:`memoryview`
2125=================================================================================
Georg Brandl116aa622007-08-15 14:28:22 +00002126
2127.. index::
Nick Coghlan273069c2012-08-20 17:14:07 +10002128 object: bytes
Georg Brandl95414632007-11-22 11:00:28 +00002129 object: bytearray
Nick Coghlan273069c2012-08-20 17:14:07 +10002130 object: memoryview
2131 module: array
Georg Brandl116aa622007-08-15 14:28:22 +00002132
Nick Coghlan273069c2012-08-20 17:14:07 +10002133The core built-in types for manipulating binary data are :class:`bytes` and
2134:class:`bytearray`. They are supported by :class:`memoryview` which uses
Chris Jerdonek5fae0e52012-11-20 17:45:51 -08002135the :ref:`buffer protocol <bufferobjects>` to access the memory of other
2136binary objects without needing to make a copy.
Georg Brandl226878c2007-08-31 10:15:37 +00002137
Nick Coghlan273069c2012-08-20 17:14:07 +10002138The :mod:`array` module supports efficient storage of basic data types like
213932-bit integers and IEEE754 double-precision floating values.
Georg Brandl116aa622007-08-15 14:28:22 +00002140
Nick Coghlan273069c2012-08-20 17:14:07 +10002141.. _typebytes:
Senthil Kumaran7cafd262010-10-02 03:16:04 +00002142
Nick Coghlan273069c2012-08-20 17:14:07 +10002143Bytes
2144-----
2145
2146.. index:: object: bytes
2147
2148Bytes objects are immutable sequences of single bytes. Since many major
2149binary protocols are based on the ASCII text encoding, bytes objects offer
2150several methods that are only valid when working with ASCII compatible
2151data and are closely related to string objects in a variety of other ways.
2152
2153Firstly, the syntax for bytes literals is largely the same as that for string
2154literals, except that a ``b`` prefix is added:
2155
2156* Single quotes: ``b'still allows embedded "double" quotes'``
2157* Double quotes: ``b"still allows embedded 'single' quotes"``.
2158* Triple quoted: ``b'''3 single quotes'''``, ``b"""3 double quotes"""``
2159
2160Only ASCII characters are permitted in bytes literals (regardless of the
2161declared source code encoding). Any binary values over 127 must be entered
2162into bytes literals using the appropriate escape sequence.
2163
2164As with string literals, bytes literals may also use a ``r`` prefix to disable
2165processing of escape sequences. See :ref:`strings` for more about the various
2166forms of bytes literal, including supported escape sequences.
2167
2168While bytes literals and representations are based on ASCII text, bytes
2169objects actually behave like immutable sequences of integers, with each
2170value in the sequence restricted such that ``0 <= x < 256`` (attempts to
2171violate this restriction will trigger :exc:`ValueError`. This is done
2172deliberately to emphasise that while many binary formats include ASCII based
2173elements and can be usefully manipulated with some text-oriented algorithms,
2174this is not generally the case for arbitrary binary data (blindly applying
2175text processing algorithms to binary data formats that are not ASCII
2176compatible will usually lead to data corruption).
2177
2178In addition to the literal forms, bytes objects can be created in a number of
2179other ways:
2180
2181* A zero-filled bytes object of a specified length: ``bytes(10)``
2182* From an iterable of integers: ``bytes(range(20))``
2183* Copying existing binary data via the buffer protocol: ``bytes(obj)``
2184
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002185Also see the :ref:`bytes <func-bytes>` built-in.
2186
Nick Coghlan273069c2012-08-20 17:14:07 +10002187Since bytes objects are sequences of integers, for a bytes object *b*,
2188``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes object of
2189length 1. (This contrasts with text strings, where both indexing and
2190slicing will produce a string of length 1)
2191
2192The representation of bytes objects uses the literal format (``b'...'``)
2193since it is often more useful than e.g. ``bytes([46, 46, 46])``. You can
2194always convert a bytes object into a list of integers using ``list(b)``.
Georg Brandl116aa622007-08-15 14:28:22 +00002195
Georg Brandl116aa622007-08-15 14:28:22 +00002196
Nick Coghlan273069c2012-08-20 17:14:07 +10002197.. note::
2198 For Python 2.x users: In the Python 2.x series, a variety of implicit
2199 conversions between 8-bit strings (the closest thing 2.x offers to a
2200 built-in binary data type) and Unicode strings were permitted. This was a
2201 backwards compatibility workaround to account for the fact that Python
2202 originally only supported 8-bit text, and Unicode text was a later
2203 addition. In Python 3.x, those implicit conversions are gone - conversions
2204 between 8-bit binary data and Unicode text must be explicit, and bytes and
2205 string objects will always compare unequal.
Raymond Hettingerc50846a2010-04-05 18:56:31 +00002206
Georg Brandl116aa622007-08-15 14:28:22 +00002207
Nick Coghlan273069c2012-08-20 17:14:07 +10002208.. _typebytearray:
Georg Brandl116aa622007-08-15 14:28:22 +00002209
Nick Coghlan273069c2012-08-20 17:14:07 +10002210Bytearray Objects
2211-----------------
Georg Brandl116aa622007-08-15 14:28:22 +00002212
Nick Coghlan273069c2012-08-20 17:14:07 +10002213.. index:: object: bytearray
Georg Brandl495f7b52009-10-27 15:28:25 +00002214
Nick Coghlan273069c2012-08-20 17:14:07 +10002215:class:`bytearray` objects are a mutable counterpart to :class:`bytes`
2216objects. There is no dedicated literal syntax for bytearray objects, instead
2217they are always created by calling the constructor:
Georg Brandl116aa622007-08-15 14:28:22 +00002218
Nick Coghlan273069c2012-08-20 17:14:07 +10002219* Creating an empty instance: ``bytearray()``
2220* Creating a zero-filled instance with a given length: ``bytearray(10)``
2221* From an iterable of integers: ``bytearray(range(20))``
Ezio Melotti971ba4c2012-10-27 23:25:18 +03002222* Copying existing binary data via the buffer protocol: ``bytearray(b'Hi!')``
Eli Benderskycbbaa962011-02-25 05:47:53 +00002223
Nick Coghlan273069c2012-08-20 17:14:07 +10002224As bytearray objects are mutable, they support the
2225:ref:`mutable <typesseq-mutable>` sequence operations in addition to the
2226common bytes and bytearray operations described in :ref:`bytes-methods`.
Georg Brandl116aa622007-08-15 14:28:22 +00002227
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002228Also see the :ref:`bytearray <func-bytearray>` built-in.
2229
Georg Brandl495f7b52009-10-27 15:28:25 +00002230
Georg Brandl226878c2007-08-31 10:15:37 +00002231.. _bytes-methods:
2232
Nick Coghlan273069c2012-08-20 17:14:07 +10002233Bytes and Bytearray Operations
2234------------------------------
Georg Brandl226878c2007-08-31 10:15:37 +00002235
2236.. index:: pair: bytes; methods
Georg Brandl95414632007-11-22 11:00:28 +00002237 pair: bytearray; methods
Georg Brandl226878c2007-08-31 10:15:37 +00002238
Nick Coghlan273069c2012-08-20 17:14:07 +10002239Both bytes and bytearray objects support the :ref:`common <typesseq-common>`
2240sequence operations. They interoperate not just with operands of the same
2241type, but with any object that supports the
2242:ref:`buffer protocol <bufferobjects>`. Due to this flexibility, they can be
2243freely mixed in operations without causing errors. However, the return type
2244of the result may depend on the order of operands.
Guido van Rossum98297ee2007-11-06 21:34:58 +00002245
Nick Coghlan273069c2012-08-20 17:14:07 +10002246Due to the common use of ASCII text as the basis for binary protocols, bytes
2247and bytearray objects provide almost all methods found on text strings, with
2248the exceptions of:
Georg Brandl226878c2007-08-31 10:15:37 +00002249
Nick Coghlan273069c2012-08-20 17:14:07 +10002250* :meth:`str.encode` (which converts text strings to bytes objects)
2251* :meth:`str.format` and :meth:`str.format_map` (which are used to format
2252 text for display to users)
2253* :meth:`str.isidentifier`, :meth:`str.isnumeric`, :meth:`str.isdecimal`,
2254 :meth:`str.isprintable` (which are used to check various properties of
2255 text strings which are not typically applicable to binary protocols).
2256
2257All other string methods are supported, although sometimes with slight
2258differences in functionality and semantics (as described below).
Antoine Pitrouac65d962011-10-20 23:54:17 +02002259
Georg Brandl7c676132007-10-23 18:17:00 +00002260.. note::
Georg Brandl226878c2007-08-31 10:15:37 +00002261
Georg Brandl95414632007-11-22 11:00:28 +00002262 The methods on bytes and bytearray objects don't accept strings as their
Georg Brandl7c676132007-10-23 18:17:00 +00002263 arguments, just as the methods on strings don't accept bytes as their
Nick Coghlan273069c2012-08-20 17:14:07 +10002264 arguments. For example, you have to write::
Georg Brandl226878c2007-08-31 10:15:37 +00002265
Georg Brandl7c676132007-10-23 18:17:00 +00002266 a = "abc"
2267 b = a.replace("a", "f")
2268
Nick Coghlan273069c2012-08-20 17:14:07 +10002269 and::
Georg Brandl7c676132007-10-23 18:17:00 +00002270
2271 a = b"abc"
2272 b = a.replace(b"a", b"f")
Georg Brandl226878c2007-08-31 10:15:37 +00002273
Nick Coghlan273069c2012-08-20 17:14:07 +10002274Whenever a bytes or bytearray method needs to interpret the bytes as
2275characters (e.g. the :meth:`is...` methods, :meth:`split`, :meth:`strip`),
2276the ASCII character set is assumed (text strings use Unicode semantics).
2277
2278.. note::
2279 Using these ASCII based methods to manipulate binary data that is not
2280 stored in an ASCII based format may lead to data corruption.
2281
2282The search operations (:keyword:`in`, :meth:`count`, :meth:`find`,
2283:meth:`index`, :meth:`rfind` and :meth:`rindex`) all accept both integers
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002284in the range 0 to 255 (inclusive) as well as bytes and byte array sequences.
Nick Coghlan273069c2012-08-20 17:14:07 +10002285
2286.. versionchanged:: 3.3
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002287 All of the search methods also accept an integer in the range 0 to 255
2288 (inclusive) as their first argument.
Nick Coghlan273069c2012-08-20 17:14:07 +10002289
2290
2291Each bytes and bytearray instance provides a :meth:`decode` convenience
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002292method that is the inverse of :meth:`str.encode`:
Georg Brandl226878c2007-08-31 10:15:37 +00002293
Victor Stinnere14e2122010-11-07 18:41:46 +00002294.. method:: bytes.decode(encoding="utf-8", errors="strict")
2295 bytearray.decode(encoding="utf-8", errors="strict")
Georg Brandl4f5f98d2009-05-04 21:01:20 +00002296
Victor Stinnere14e2122010-11-07 18:41:46 +00002297 Return a string decoded from the given bytes. Default encoding is
2298 ``'utf-8'``. *errors* may be given to set a different
Georg Brandl4f5f98d2009-05-04 21:01:20 +00002299 error handling scheme. The default for *errors* is ``'strict'``, meaning
2300 that encoding errors raise a :exc:`UnicodeError`. Other possible values are
2301 ``'ignore'``, ``'replace'`` and any other name registered via
2302 :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a
2303 list of possible encodings, see section :ref:`standard-encodings`.
2304
Benjamin Peterson308d6372009-09-18 21:42:35 +00002305 .. versionchanged:: 3.1
2306 Added support for keyword arguments.
2307
Nick Coghlan273069c2012-08-20 17:14:07 +10002308Since 2 hexadecimal digits correspond precisely to a single byte, hexadecimal
2309numbers are a commonly used format for describing binary data. Accordingly,
2310the bytes and bytearray types have an additional class method to read data in
2311that format:
Georg Brandl226878c2007-08-31 10:15:37 +00002312
Georg Brandlabc38772009-04-12 15:51:51 +00002313.. classmethod:: bytes.fromhex(string)
2314 bytearray.fromhex(string)
Georg Brandl226878c2007-08-31 10:15:37 +00002315
Georg Brandl18da8f02008-07-01 20:08:02 +00002316 This :class:`bytes` class method returns a bytes or bytearray object,
2317 decoding the given string object. The string must contain two hexadecimal
2318 digits per byte, spaces are ignored.
Georg Brandl226878c2007-08-31 10:15:37 +00002319
Nick Coghlan273069c2012-08-20 17:14:07 +10002320 >>> bytes.fromhex('2Ef0 F1f2 ')
2321 b'.\xf0\xf1\xf2'
Georg Brandl226878c2007-08-31 10:15:37 +00002322
Georg Brandlabc38772009-04-12 15:51:51 +00002323
2324The maketrans and translate methods differ in semantics from the versions
2325available on strings:
Georg Brandl48310cd2009-01-03 21:18:54 +00002326
Georg Brandl454636f2008-12-27 23:33:20 +00002327.. method:: bytes.translate(table[, delete])
Georg Brandl751771b2009-05-31 21:38:37 +00002328 bytearray.translate(table[, delete])
Georg Brandl226878c2007-08-31 10:15:37 +00002329
Georg Brandl454636f2008-12-27 23:33:20 +00002330 Return a copy of the bytes or bytearray object where all bytes occurring in
2331 the optional argument *delete* are removed, and the remaining bytes have been
2332 mapped through the given translation table, which must be a bytes object of
2333 length 256.
Georg Brandl226878c2007-08-31 10:15:37 +00002334
Georg Brandlabc38772009-04-12 15:51:51 +00002335 You can use the :func:`bytes.maketrans` method to create a translation table.
Georg Brandl226878c2007-08-31 10:15:37 +00002336
Georg Brandl454636f2008-12-27 23:33:20 +00002337 Set the *table* argument to ``None`` for translations that only delete
2338 characters::
Georg Brandl226878c2007-08-31 10:15:37 +00002339
Georg Brandl454636f2008-12-27 23:33:20 +00002340 >>> b'read this short text'.translate(None, b'aeiou')
2341 b'rd ths shrt txt'
Georg Brandl226878c2007-08-31 10:15:37 +00002342
2343
Georg Brandlabc38772009-04-12 15:51:51 +00002344.. staticmethod:: bytes.maketrans(from, to)
Georg Brandl751771b2009-05-31 21:38:37 +00002345 bytearray.maketrans(from, to)
Georg Brandlabc38772009-04-12 15:51:51 +00002346
2347 This static method returns a translation table usable for
2348 :meth:`bytes.translate` that will map each character in *from* into the
2349 character at the same position in *to*; *from* and *to* must be bytes objects
2350 and have the same length.
2351
2352 .. versionadded:: 3.1
2353
2354
Nick Coghlan273069c2012-08-20 17:14:07 +10002355.. _typememoryview:
2356
2357Memory Views
2358------------
2359
2360:class:`memoryview` objects allow Python code to access the internal data
2361of an object that supports the :ref:`buffer protocol <bufferobjects>` without
2362copying.
2363
2364.. class:: memoryview(obj)
2365
2366 Create a :class:`memoryview` that references *obj*. *obj* must support the
2367 buffer protocol. Built-in objects that support the buffer protocol include
2368 :class:`bytes` and :class:`bytearray`.
2369
2370 A :class:`memoryview` has the notion of an *element*, which is the
2371 atomic memory unit handled by the originating object *obj*. For many
2372 simple types such as :class:`bytes` and :class:`bytearray`, an element
2373 is a single byte, but other types such as :class:`array.array` may have
2374 bigger elements.
2375
2376 ``len(view)`` is equal to the length of :class:`~memoryview.tolist`.
2377 If ``view.ndim = 0``, the length is 1. If ``view.ndim = 1``, the length
2378 is equal to the number of elements in the view. For higher dimensions,
2379 the length is equal to the length of the nested list representation of
2380 the view. The :class:`~memoryview.itemsize` attribute will give you the
2381 number of bytes in a single element.
2382
2383 A :class:`memoryview` supports slicing to expose its data. If
2384 :class:`~memoryview.format` is one of the native format specifiers
2385 from the :mod:`struct` module, indexing will return a single element
2386 with the correct type. Full slicing will result in a subview::
2387
2388 >>> v = memoryview(b'abcefg')
2389 >>> v[1]
2390 98
2391 >>> v[-1]
2392 103
2393 >>> v[1:4]
2394 <memory at 0x7f3ddc9f4350>
2395 >>> bytes(v[1:4])
2396 b'bce'
2397
2398 Other native formats::
2399
2400 >>> import array
2401 >>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
2402 >>> a[0]
2403 -11111111
2404 >>> a[-1]
2405 44444444
2406 >>> a[2:3].tolist()
2407 [-33333333]
2408 >>> a[::2].tolist()
2409 [-11111111, -33333333]
2410 >>> a[::-1].tolist()
2411 [44444444, -33333333, 22222222, -11111111]
2412
2413 .. versionadded:: 3.3
2414
2415 If the underlying object is writable, the memoryview supports slice
2416 assignment. Resizing is not allowed::
2417
2418 >>> data = bytearray(b'abcefg')
2419 >>> v = memoryview(data)
2420 >>> v.readonly
2421 False
2422 >>> v[0] = ord(b'z')
2423 >>> data
2424 bytearray(b'zbcefg')
2425 >>> v[1:4] = b'123'
2426 >>> data
2427 bytearray(b'z123fg')
2428 >>> v[2:3] = b'spam'
2429 Traceback (most recent call last):
2430 File "<stdin>", line 1, in <module>
2431 ValueError: memoryview assignment: lvalue and rvalue have different structures
2432 >>> v[2:6] = b'spam'
2433 >>> data
2434 bytearray(b'z1spam')
2435
Stefan Kraha3b84fb2012-09-02 14:50:56 +02002436 One-dimensional memoryviews of hashable (read-only) types with formats
2437 'B', 'b' or 'c' are also hashable. The hash is defined as
2438 ``hash(m) == hash(m.tobytes())``::
Nick Coghlan273069c2012-08-20 17:14:07 +10002439
2440 >>> v = memoryview(b'abcefg')
2441 >>> hash(v) == hash(b'abcefg')
2442 True
2443 >>> hash(v[2:4]) == hash(b'ce')
2444 True
2445 >>> hash(v[::-2]) == hash(b'abcefg'[::-2])
2446 True
2447
Nick Coghlan273069c2012-08-20 17:14:07 +10002448 .. versionchanged:: 3.3
Stefan Kraha3b84fb2012-09-02 14:50:56 +02002449 One-dimensional memoryviews with formats 'B', 'b' or 'c' are now hashable.
Nick Coghlan273069c2012-08-20 17:14:07 +10002450
Nick Coghlan273069c2012-08-20 17:14:07 +10002451 :class:`memoryview` has several methods:
2452
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002453 .. method:: __eq__(exporter)
2454
2455 A memoryview and a :pep:`3118` exporter are equal if their shapes are
2456 equivalent and if all corresponding values are equal when the operands'
2457 respective format codes are interpreted using :mod:`struct` syntax.
2458
2459 For the subset of :mod:`struct` format strings currently supported by
2460 :meth:`tolist`, ``v`` and ``w`` are equal if ``v.tolist() == w.tolist()``::
2461
2462 >>> import array
2463 >>> a = array.array('I', [1, 2, 3, 4, 5])
2464 >>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
2465 >>> c = array.array('b', [5, 3, 1])
2466 >>> x = memoryview(a)
2467 >>> y = memoryview(b)
2468 >>> x == a == y == b
2469 True
2470 >>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
2471 True
2472 >>> z = y[::-2]
2473 >>> z == c
2474 True
2475 >>> z.tolist() == c.tolist()
2476 True
2477
2478 If either format string is not supported by the :mod:`struct` module,
2479 then the objects will always compare as unequal (even if the format
2480 strings and buffer contents are identical)::
2481
2482 >>> from ctypes import BigEndianStructure, c_long
2483 >>> class BEPoint(BigEndianStructure):
2484 ... _fields_ = [("x", c_long), ("y", c_long)]
2485 ...
2486 >>> point = BEPoint(100, 200)
2487 >>> a = memoryview(point)
2488 >>> b = memoryview(point)
2489 >>> a == point
2490 False
2491 >>> a == b
2492 False
2493
2494 Note that, as with floating point numbers, ``v is w`` does *not* imply
2495 ``v == w`` for memoryview objects.
2496
2497 .. versionchanged:: 3.3
Stefan Krahab0c3c72012-08-30 12:09:09 +02002498 Previous versions compared the raw memory disregarding the item format
2499 and the logical array structure.
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002500
Nick Coghlan273069c2012-08-20 17:14:07 +10002501 .. method:: tobytes()
2502
2503 Return the data in the buffer as a bytestring. This is equivalent to
2504 calling the :class:`bytes` constructor on the memoryview. ::
2505
2506 >>> m = memoryview(b"abc")
2507 >>> m.tobytes()
2508 b'abc'
2509 >>> bytes(m)
2510 b'abc'
2511
2512 For non-contiguous arrays the result is equal to the flattened list
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002513 representation with all elements converted to bytes. :meth:`tobytes`
2514 supports all format strings, including those that are not in
2515 :mod:`struct` module syntax.
Nick Coghlan273069c2012-08-20 17:14:07 +10002516
2517 .. method:: tolist()
2518
2519 Return the data in the buffer as a list of elements. ::
2520
2521 >>> memoryview(b'abc').tolist()
2522 [97, 98, 99]
2523 >>> import array
2524 >>> a = array.array('d', [1.1, 2.2, 3.3])
2525 >>> m = memoryview(a)
2526 >>> m.tolist()
2527 [1.1, 2.2, 3.3]
2528
Stefan Krahab0c3c72012-08-30 12:09:09 +02002529 .. versionchanged:: 3.3
2530 :meth:`tolist` now supports all single character native formats in
2531 :mod:`struct` module syntax as well as multi-dimensional
2532 representations.
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002533
Nick Coghlan273069c2012-08-20 17:14:07 +10002534 .. method:: release()
2535
2536 Release the underlying buffer exposed by the memoryview object. Many
2537 objects take special actions when a view is held on them (for example,
2538 a :class:`bytearray` would temporarily forbid resizing); therefore,
2539 calling release() is handy to remove these restrictions (and free any
2540 dangling resources) as soon as possible.
2541
2542 After this method has been called, any further operation on the view
2543 raises a :class:`ValueError` (except :meth:`release()` itself which can
2544 be called multiple times)::
2545
2546 >>> m = memoryview(b'abc')
2547 >>> m.release()
2548 >>> m[0]
2549 Traceback (most recent call last):
2550 File "<stdin>", line 1, in <module>
2551 ValueError: operation forbidden on released memoryview object
2552
2553 The context management protocol can be used for a similar effect,
2554 using the ``with`` statement::
2555
2556 >>> with memoryview(b'abc') as m:
2557 ... m[0]
2558 ...
2559 97
2560 >>> m[0]
2561 Traceback (most recent call last):
2562 File "<stdin>", line 1, in <module>
2563 ValueError: operation forbidden on released memoryview object
2564
2565 .. versionadded:: 3.2
2566
2567 .. method:: cast(format[, shape])
2568
2569 Cast a memoryview to a new format or shape. *shape* defaults to
2570 ``[byte_length//new_itemsize]``, which means that the result view
2571 will be one-dimensional. The return value is a new memoryview, but
2572 the buffer itself is not copied. Supported casts are 1D -> C-contiguous
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002573 and C-contiguous -> 1D.
2574
2575 Both formats are restricted to single element native formats in
2576 :mod:`struct` syntax. One of the formats must be a byte format
Nick Coghlan273069c2012-08-20 17:14:07 +10002577 ('B', 'b' or 'c'). The byte length of the result must be the same
2578 as the original length.
2579
2580 Cast 1D/long to 1D/unsigned bytes::
2581
2582 >>> import array
2583 >>> a = array.array('l', [1,2,3])
2584 >>> x = memoryview(a)
2585 >>> x.format
2586 'l'
2587 >>> x.itemsize
2588 8
2589 >>> len(x)
2590 3
2591 >>> x.nbytes
2592 24
2593 >>> y = x.cast('B')
2594 >>> y.format
2595 'B'
2596 >>> y.itemsize
2597 1
2598 >>> len(y)
2599 24
2600 >>> y.nbytes
2601 24
2602
2603 Cast 1D/unsigned bytes to 1D/char::
2604
2605 >>> b = bytearray(b'zyz')
2606 >>> x = memoryview(b)
2607 >>> x[0] = b'a'
2608 Traceback (most recent call last):
2609 File "<stdin>", line 1, in <module>
2610 ValueError: memoryview: invalid value for format "B"
2611 >>> y = x.cast('c')
2612 >>> y[0] = b'a'
2613 >>> b
2614 bytearray(b'ayz')
2615
2616 Cast 1D/bytes to 3D/ints to 1D/signed char::
2617
2618 >>> import struct
2619 >>> buf = struct.pack("i"*12, *list(range(12)))
2620 >>> x = memoryview(buf)
2621 >>> y = x.cast('i', shape=[2,2,3])
2622 >>> y.tolist()
2623 [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
2624 >>> y.format
2625 'i'
2626 >>> y.itemsize
2627 4
2628 >>> len(y)
2629 2
2630 >>> y.nbytes
2631 48
2632 >>> z = y.cast('b')
2633 >>> z.format
2634 'b'
2635 >>> z.itemsize
2636 1
2637 >>> len(z)
2638 48
2639 >>> z.nbytes
2640 48
2641
Terry Jan Reedy0f847642013-03-11 18:34:00 -04002642 Cast 1D/unsigned char to 2D/unsigned long::
Nick Coghlan273069c2012-08-20 17:14:07 +10002643
2644 >>> buf = struct.pack("L"*6, *list(range(6)))
2645 >>> x = memoryview(buf)
2646 >>> y = x.cast('L', shape=[2,3])
2647 >>> len(y)
2648 2
2649 >>> y.nbytes
2650 48
2651 >>> y.tolist()
2652 [[0, 1, 2], [3, 4, 5]]
2653
2654 .. versionadded:: 3.3
2655
2656 There are also several readonly attributes available:
2657
2658 .. attribute:: obj
2659
2660 The underlying object of the memoryview::
2661
2662 >>> b = bytearray(b'xyz')
2663 >>> m = memoryview(b)
2664 >>> m.obj is b
2665 True
2666
2667 .. versionadded:: 3.3
2668
2669 .. attribute:: nbytes
2670
2671 ``nbytes == product(shape) * itemsize == len(m.tobytes())``. This is
2672 the amount of space in bytes that the array would use in a contiguous
2673 representation. It is not necessarily equal to len(m)::
2674
2675 >>> import array
2676 >>> a = array.array('i', [1,2,3,4,5])
2677 >>> m = memoryview(a)
2678 >>> len(m)
2679 5
2680 >>> m.nbytes
2681 20
2682 >>> y = m[::2]
2683 >>> len(y)
2684 3
2685 >>> y.nbytes
2686 12
2687 >>> len(y.tobytes())
2688 12
2689
2690 Multi-dimensional arrays::
2691
2692 >>> import struct
2693 >>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
2694 >>> x = memoryview(buf)
2695 >>> y = x.cast('d', shape=[3,4])
2696 >>> y.tolist()
2697 [[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]]
2698 >>> len(y)
2699 3
2700 >>> y.nbytes
2701 96
2702
2703 .. versionadded:: 3.3
2704
2705 .. attribute:: readonly
2706
2707 A bool indicating whether the memory is read only.
2708
2709 .. attribute:: format
2710
2711 A string containing the format (in :mod:`struct` module style) for each
2712 element in the view. A memoryview can be created from exporters with
2713 arbitrary format strings, but some methods (e.g. :meth:`tolist`) are
Nick Coghlan06e1ab02012-08-25 17:59:50 +10002714 restricted to native single element formats.
Nick Coghlan273069c2012-08-20 17:14:07 +10002715
Stefan Krahab0c3c72012-08-30 12:09:09 +02002716 .. versionchanged:: 3.3
2717 format ``'B'`` is now handled according to the struct module syntax.
2718 This means that ``memoryview(b'abc')[0] == b'abc'[0] == 97``.
2719
Nick Coghlan273069c2012-08-20 17:14:07 +10002720 .. attribute:: itemsize
2721
2722 The size in bytes of each element of the memoryview::
2723
2724 >>> import array, struct
2725 >>> m = memoryview(array.array('H', [32000, 32001, 32002]))
2726 >>> m.itemsize
2727 2
2728 >>> m[0]
2729 32000
2730 >>> struct.calcsize('H') == m.itemsize
2731 True
2732
2733 .. attribute:: ndim
2734
2735 An integer indicating how many dimensions of a multi-dimensional array the
2736 memory represents.
2737
2738 .. attribute:: shape
2739
2740 A tuple of integers the length of :attr:`ndim` giving the shape of the
Alexander Belopolskye8677c02012-09-03 17:29:22 -04002741 memory as an N-dimensional array.
2742
2743 .. versionchanged:: 3.3
2744 An empty tuple instead of None when ndim = 0.
Nick Coghlan273069c2012-08-20 17:14:07 +10002745
2746 .. attribute:: strides
2747
2748 A tuple of integers the length of :attr:`ndim` giving the size in bytes to
2749 access each element for each dimension of the array.
2750
Alexander Belopolskye8677c02012-09-03 17:29:22 -04002751 .. versionchanged:: 3.3
2752 An empty tuple instead of None when ndim = 0.
2753
Nick Coghlan273069c2012-08-20 17:14:07 +10002754 .. attribute:: suboffsets
2755
2756 Used internally for PIL-style arrays. The value is informational only.
2757
2758 .. attribute:: c_contiguous
2759
2760 A bool indicating whether the memory is C-contiguous.
2761
2762 .. versionadded:: 3.3
2763
2764 .. attribute:: f_contiguous
2765
2766 A bool indicating whether the memory is Fortran contiguous.
2767
2768 .. versionadded:: 3.3
2769
2770 .. attribute:: contiguous
2771
2772 A bool indicating whether the memory is contiguous.
2773
2774 .. versionadded:: 3.3
2775
2776
Georg Brandl116aa622007-08-15 14:28:22 +00002777.. _types-set:
2778
2779Set Types --- :class:`set`, :class:`frozenset`
2780==============================================
2781
2782.. index:: object: set
2783
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002784A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00002785Common uses include membership testing, removing duplicates from a sequence, and
2786computing mathematical operations such as intersection, union, difference, and
2787symmetric difference.
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002788(For other containers see the built-in :class:`dict`, :class:`list`,
Georg Brandl116aa622007-08-15 14:28:22 +00002789and :class:`tuple` classes, and the :mod:`collections` module.)
2790
Georg Brandl116aa622007-08-15 14:28:22 +00002791Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
2792set``. Being an unordered collection, sets do not record element position or
2793order of insertion. Accordingly, sets do not support indexing, slicing, or
2794other sequence-like behavior.
2795
Georg Brandl22b34312009-07-26 14:54:51 +00002796There are currently two built-in set types, :class:`set` and :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00002797The :class:`set` type is mutable --- the contents can be changed using methods
2798like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value
2799and cannot be used as either a dictionary key or as an element of another set.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002800The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be
Georg Brandl116aa622007-08-15 14:28:22 +00002801altered after it is created; it can therefore be used as a dictionary key or as
2802an element of another set.
2803
Georg Brandl99cd9572010-03-21 09:10:32 +00002804Non-empty sets (not frozensets) can be created by placing a comma-separated list
Georg Brandl53b95e72010-03-21 11:53:50 +00002805of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
2806:class:`set` constructor.
Georg Brandl99cd9572010-03-21 09:10:32 +00002807
Georg Brandl116aa622007-08-15 14:28:22 +00002808The constructors for both classes work the same:
2809
2810.. class:: set([iterable])
2811 frozenset([iterable])
2812
2813 Return a new set or frozenset object whose elements are taken from
2814 *iterable*. The elements of a set must be hashable. To represent sets of
2815 sets, the inner sets must be :class:`frozenset` objects. If *iterable* is
2816 not specified, a new empty set is returned.
2817
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002818 Instances of :class:`set` and :class:`frozenset` provide the following
2819 operations:
Georg Brandl116aa622007-08-15 14:28:22 +00002820
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002821 .. describe:: len(s)
Georg Brandl116aa622007-08-15 14:28:22 +00002822
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002823 Return the cardinality of set *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00002824
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002825 .. describe:: x in s
Georg Brandl116aa622007-08-15 14:28:22 +00002826
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002827 Test *x* for membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00002828
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002829 .. describe:: x not in s
Georg Brandl116aa622007-08-15 14:28:22 +00002830
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002831 Test *x* for non-membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00002832
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002833 .. method:: isdisjoint(other)
Guido van Rossum58da9312007-11-10 23:39:45 +00002834
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002835 Return True if the set has no elements in common with *other*. Sets are
Georg Brandl2ee470f2008-07-16 12:55:28 +00002836 disjoint if and only if their intersection is the empty set.
Guido van Rossum58da9312007-11-10 23:39:45 +00002837
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002838 .. method:: issubset(other)
2839 set <= other
Georg Brandl116aa622007-08-15 14:28:22 +00002840
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002841 Test whether every element in the set is in *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00002842
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002843 .. method:: set < other
Georg Brandla6f52782007-09-01 15:49:30 +00002844
Andrew Svetlov5bb42072012-11-01 21:47:54 +02002845 Test whether the set is a proper subset of *other*, that is,
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002846 ``set <= other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00002847
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002848 .. method:: issuperset(other)
2849 set >= other
Georg Brandl116aa622007-08-15 14:28:22 +00002850
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002851 Test whether every element in *other* is in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00002852
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002853 .. method:: set > other
Georg Brandla6f52782007-09-01 15:49:30 +00002854
Andrew Svetlov5bb42072012-11-01 21:47:54 +02002855 Test whether the set is a proper superset of *other*, that is, ``set >=
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002856 other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00002857
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002858 .. method:: union(other, ...)
2859 set | other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00002860
Benjamin Petersonb58dda72009-01-18 22:27:04 +00002861 Return a new set with elements from the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00002862
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002863 .. method:: intersection(other, ...)
2864 set & other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00002865
Benjamin Petersonb58dda72009-01-18 22:27:04 +00002866 Return a new set with elements common to the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00002867
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002868 .. method:: difference(other, ...)
2869 set - other - ...
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002870
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002871 Return a new set with elements in the set that are not in the others.
Georg Brandl116aa622007-08-15 14:28:22 +00002872
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002873 .. method:: symmetric_difference(other)
2874 set ^ other
Georg Brandl116aa622007-08-15 14:28:22 +00002875
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002876 Return a new set with elements in either the set or *other* but not both.
Georg Brandl116aa622007-08-15 14:28:22 +00002877
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002878 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00002879
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002880 Return a new set with a shallow copy of *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00002881
2882
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002883 Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
2884 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
2885 :meth:`issuperset` methods will accept any iterable as an argument. In
2886 contrast, their operator based counterparts require their arguments to be
2887 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
2888 in favor of the more readable ``set('abc').intersection('cbs')``.
Georg Brandl116aa622007-08-15 14:28:22 +00002889
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002890 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
2891 sets are equal if and only if every element of each set is contained in the
2892 other (each is a subset of the other). A set is less than another set if and
2893 only if the first set is a proper subset of the second set (is a subset, but
2894 is not equal). A set is greater than another set if and only if the first set
2895 is a proper superset of the second set (is a superset, but is not equal).
Georg Brandl116aa622007-08-15 14:28:22 +00002896
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002897 Instances of :class:`set` are compared to instances of :class:`frozenset`
2898 based on their members. For example, ``set('abc') == frozenset('abc')``
2899 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
Georg Brandl116aa622007-08-15 14:28:22 +00002900
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002901 The subset and equality comparisons do not generalize to a complete ordering
2902 function. For example, any two disjoint sets are not equal and are not
2903 subsets of each other, so *all* of the following return ``False``: ``a<b``,
Georg Brandl05f5ab72008-09-24 09:11:47 +00002904 ``a==b``, or ``a>b``.
Georg Brandl116aa622007-08-15 14:28:22 +00002905
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002906 Since sets only define partial ordering (subset relationships), the output of
2907 the :meth:`list.sort` method is undefined for lists of sets.
Georg Brandl116aa622007-08-15 14:28:22 +00002908
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002909 Set elements, like dictionary keys, must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +00002910
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002911 Binary operations that mix :class:`set` instances with :class:`frozenset`
2912 return the type of the first operand. For example: ``frozenset('ab') |
2913 set('bc')`` returns an instance of :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00002914
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002915 The following table lists operations available for :class:`set` that do not
2916 apply to immutable instances of :class:`frozenset`:
Georg Brandl116aa622007-08-15 14:28:22 +00002917
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002918 .. method:: update(other, ...)
2919 set |= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00002920
Georg Brandla6053b42009-09-01 08:11:14 +00002921 Update the set, adding elements from all others.
Georg Brandl116aa622007-08-15 14:28:22 +00002922
Georg Brandlc28e1fa2008-06-10 19:20:26 +00002923 .. method:: intersection_update(other, ...)
2924 set &= other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00002925
Georg Brandla6053b42009-09-01 08:11:14 +00002926 Update the set, keeping only elements found in it and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00002927
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002928 .. method:: difference_update(other, ...)
2929 set -= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00002930
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00002931 Update the set, removing elements found in others.
2932
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002933 .. method:: symmetric_difference_update(other)
2934 set ^= other
Georg Brandl116aa622007-08-15 14:28:22 +00002935
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002936 Update the set, keeping only elements found in either set, but not in both.
Georg Brandl116aa622007-08-15 14:28:22 +00002937
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002938 .. method:: add(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00002939
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002940 Add element *elem* to the set.
Georg Brandl116aa622007-08-15 14:28:22 +00002941
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002942 .. method:: remove(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00002943
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002944 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
2945 not contained in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00002946
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002947 .. method:: discard(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00002948
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002949 Remove element *elem* from the set if it is present.
Georg Brandl116aa622007-08-15 14:28:22 +00002950
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002951 .. method:: pop()
Georg Brandl116aa622007-08-15 14:28:22 +00002952
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002953 Remove and return an arbitrary element from the set. Raises
2954 :exc:`KeyError` if the set is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00002955
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002956 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00002957
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002958 Remove all elements from the set.
Georg Brandl116aa622007-08-15 14:28:22 +00002959
2960
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002961 Note, the non-operator versions of the :meth:`update`,
2962 :meth:`intersection_update`, :meth:`difference_update`, and
2963 :meth:`symmetric_difference_update` methods will accept any iterable as an
2964 argument.
Georg Brandl116aa622007-08-15 14:28:22 +00002965
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002966 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
2967 :meth:`discard` methods may be a set. To support searching for an equivalent
2968 frozenset, the *elem* set is temporarily mutated during the search and then
2969 restored. During the search, the *elem* set should not be read or mutated
2970 since it does not have a meaningful value.
Benjamin Peterson699adb92008-05-08 22:27:58 +00002971
Georg Brandl116aa622007-08-15 14:28:22 +00002972
2973.. _typesmapping:
2974
2975Mapping Types --- :class:`dict`
2976===============================
2977
2978.. index::
2979 object: mapping
2980 object: dictionary
2981 triple: operations on; mapping; types
2982 triple: operations on; dictionary; type
2983 statement: del
2984 builtin: len
2985
Chris Jerdonek11f3f172012-11-03 12:05:55 -07002986A :term:`mapping` object maps :term:`hashable` values to arbitrary objects.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002987Mappings are mutable objects. There is currently only one standard mapping
Nick Coghlan83c0ae52012-08-21 17:42:52 +10002988type, the :dfn:`dictionary`. (For other containers see the built-in
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002989:class:`list`, :class:`set`, and :class:`tuple` classes, and the
2990:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +00002991
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002992A dictionary's keys are *almost* arbitrary values. Values that are not
2993:term:`hashable`, that is, values containing lists, dictionaries or other
2994mutable types (that are compared by value rather than by object identity) may
2995not be used as keys. Numeric types used for keys obey the normal rules for
2996numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
2997then they can be used interchangeably to index the same dictionary entry. (Note
2998however, that since computers store floating-point numbers as approximations it
2999is usually unwise to use them as dictionary keys.)
Georg Brandl116aa622007-08-15 14:28:22 +00003000
3001Dictionaries can be created by placing a comma-separated list of ``key: value``
3002pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
3003'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
3004
Chris Jerdonekf3413172012-10-13 03:22:33 -07003005.. class:: dict(**kwarg)
3006 dict(mapping, **kwarg)
3007 dict(iterable, **kwarg)
Georg Brandl116aa622007-08-15 14:28:22 +00003008
Chris Jerdonekf3413172012-10-13 03:22:33 -07003009 Return a new dictionary initialized from an optional positional argument
3010 and a possibly empty set of keyword arguments.
3011
3012 If no positional argument is given, an empty dictionary is created.
3013 If a positional argument is given and it is a mapping object, a dictionary
3014 is created with the same key-value pairs as the mapping object. Otherwise,
3015 the positional argument must be an :term:`iterator` object. Each item in
3016 the iterable must itself be an iterator with exactly two objects. The
3017 first object of each item becomes a key in the new dictionary, and the
3018 second object the corresponding value. If a key occurs more than once, the
3019 last value for that key becomes the corresponding value in the new
Georg Brandld22a8152007-09-04 17:43:37 +00003020 dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00003021
Chris Jerdonekf3413172012-10-13 03:22:33 -07003022 If keyword arguments are given, the keyword arguments and their values are
3023 added to the dictionary created from the positional argument. If a key
3024 being added is already present, the value from the keyword argument
3025 replaces the value from the positional argument.
Georg Brandl116aa622007-08-15 14:28:22 +00003026
Chris Jerdonekf3413172012-10-13 03:22:33 -07003027 To illustrate, the following examples all return a dictionary equal to
Ezio Melottia20879f2012-10-26 19:14:16 +03003028 ``{"one": 1, "two": 2, "three": 3}``::
Georg Brandl116aa622007-08-15 14:28:22 +00003029
Ezio Melottia20879f2012-10-26 19:14:16 +03003030 >>> a = dict(one=1, two=2, three=3)
3031 >>> b = {'one': 1, 'two': 2, 'three': 3}
3032 >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
3033 >>> d = dict([('two', 2), ('one', 1), ('three', 3)])
3034 >>> e = dict({'three': 3, 'one': 1, 'two': 2})
Chris Jerdonekf3413172012-10-13 03:22:33 -07003035 >>> a == b == c == d == e
3036 True
3037
3038 Providing keyword arguments as in the first example only works for keys that
3039 are valid Python identifiers. Otherwise, any valid keys can be used.
Georg Brandl116aa622007-08-15 14:28:22 +00003040
Georg Brandl116aa622007-08-15 14:28:22 +00003041
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003042 These are the operations that dictionaries support (and therefore, custom
3043 mapping types should support too):
Georg Brandl116aa622007-08-15 14:28:22 +00003044
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003045 .. describe:: len(d)
Georg Brandl116aa622007-08-15 14:28:22 +00003046
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003047 Return the number of items in the dictionary *d*.
Georg Brandl116aa622007-08-15 14:28:22 +00003048
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003049 .. describe:: d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00003050
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003051 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
3052 not in the map.
Georg Brandl48310cd2009-01-03 21:18:54 +00003053
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003054 If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
3055 is not present, the ``d[key]`` operation calls that method with the key *key*
3056 as argument. The ``d[key]`` operation then returns or raises whatever is
3057 returned or raised by the ``__missing__(key)`` call if the key is not
3058 present. No other operations or methods invoke :meth:`__missing__`. If
3059 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
Raymond Hettinger5254e972011-01-08 09:35:38 +00003060 :meth:`__missing__` must be a method; it cannot be an instance variable::
3061
3062 >>> class Counter(dict):
3063 ... def __missing__(self, key):
3064 ... return 0
3065 >>> c = Counter()
3066 >>> c['red']
3067 0
3068 >>> c['red'] += 1
3069 >>> c['red']
3070 1
3071
3072 See :class:`collections.Counter` for a complete implementation including
3073 other methods helpful for accumulating and managing tallies.
Georg Brandl116aa622007-08-15 14:28:22 +00003074
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003075 .. describe:: d[key] = value
Georg Brandl116aa622007-08-15 14:28:22 +00003076
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003077 Set ``d[key]`` to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00003078
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003079 .. describe:: del d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00003080
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003081 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
3082 map.
Georg Brandl116aa622007-08-15 14:28:22 +00003083
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003084 .. describe:: key in d
Georg Brandl116aa622007-08-15 14:28:22 +00003085
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003086 Return ``True`` if *d* has a key *key*, else ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00003087
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003088 .. describe:: key not in d
Georg Brandl116aa622007-08-15 14:28:22 +00003089
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003090 Equivalent to ``not key in d``.
Georg Brandl116aa622007-08-15 14:28:22 +00003091
Benjamin Petersond23f8222009-04-05 19:13:16 +00003092 .. describe:: iter(d)
3093
3094 Return an iterator over the keys of the dictionary. This is a shortcut
Georg Brandlede6c2a2010-01-05 10:22:04 +00003095 for ``iter(d.keys())``.
Benjamin Petersond23f8222009-04-05 19:13:16 +00003096
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003097 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00003098
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003099 Remove all items from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00003100
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003101 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00003102
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003103 Return a shallow copy of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00003104
Georg Brandlabc38772009-04-12 15:51:51 +00003105 .. classmethod:: fromkeys(seq[, value])
Georg Brandl116aa622007-08-15 14:28:22 +00003106
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003107 Create a new dictionary with keys from *seq* and values set to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00003108
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003109 :meth:`fromkeys` is a class method that returns a new dictionary. *value*
3110 defaults to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00003111
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003112 .. method:: get(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00003113
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003114 Return the value for *key* if *key* is in the dictionary, else *default*.
3115 If *default* is not given, it defaults to ``None``, so that this method
3116 never raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00003117
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003118 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +00003119
Victor Stinner0db176f2012-04-16 00:16:30 +02003120 Return a new view of the dictionary's items (``(key, value)`` pairs).
3121 See the :ref:`documentation of view objects <dict-views>`.
Georg Brandl116aa622007-08-15 14:28:22 +00003122
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003123 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +00003124
Victor Stinner0db176f2012-04-16 00:16:30 +02003125 Return a new view of the dictionary's keys. See the :ref:`documentation
3126 of view objects <dict-views>`.
Georg Brandl116aa622007-08-15 14:28:22 +00003127
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003128 .. method:: pop(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00003129
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003130 If *key* is in the dictionary, remove it and return its value, else return
3131 *default*. If *default* is not given and *key* is not in the dictionary,
3132 a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00003133
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003134 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +00003135
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003136 Remove and return an arbitrary ``(key, value)`` pair from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00003137
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003138 :meth:`popitem` is useful to destructively iterate over a dictionary, as
3139 often used in set algorithms. If the dictionary is empty, calling
3140 :meth:`popitem` raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00003141
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003142 .. method:: setdefault(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00003143
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003144 If *key* is in the dictionary, return its value. If not, insert *key*
3145 with a value of *default* and return *default*. *default* defaults to
3146 ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00003147
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003148 .. method:: update([other])
Georg Brandl116aa622007-08-15 14:28:22 +00003149
Éric Araujo0fc86b82010-08-18 22:29:54 +00003150 Update the dictionary with the key/value pairs from *other*, overwriting
3151 existing keys. Return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00003152
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003153 :meth:`update` accepts either another dictionary object or an iterable of
Georg Brandlfda21062010-09-25 16:56:36 +00003154 key/value pairs (as tuples or other iterables of length two). If keyword
Benjamin Peterson8719ad52009-09-11 22:24:02 +00003155 arguments are specified, the dictionary is then updated with those
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003156 key/value pairs: ``d.update(red=1, blue=2)``.
Georg Brandl116aa622007-08-15 14:28:22 +00003157
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00003158 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +00003159
Victor Stinner0db176f2012-04-16 00:16:30 +02003160 Return a new view of the dictionary's values. See the
3161 :ref:`documentation of view objects <dict-views>`.
3162
3163.. seealso::
3164 :class:`types.MappingProxyType` can be used to create a read-only view
3165 of a :class:`dict`.
Georg Brandld22a8152007-09-04 17:43:37 +00003166
3167
Benjamin Peterson44309e62008-11-22 00:41:45 +00003168.. _dict-views:
3169
Georg Brandld22a8152007-09-04 17:43:37 +00003170Dictionary view objects
3171-----------------------
3172
3173The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
3174:meth:`dict.items` are *view objects*. They provide a dynamic view on the
3175dictionary's entries, which means that when the dictionary changes, the view
Benjamin Petersonce0506c2008-11-17 21:47:41 +00003176reflects these changes.
Georg Brandld22a8152007-09-04 17:43:37 +00003177
3178Dictionary views can be iterated over to yield their respective data, and
3179support membership tests:
3180
3181.. describe:: len(dictview)
3182
3183 Return the number of entries in the dictionary.
3184
3185.. describe:: iter(dictview)
3186
3187 Return an iterator over the keys, values or items (represented as tuples of
3188 ``(key, value)``) in the dictionary.
3189
3190 Keys and values are iterated over in an arbitrary order which is non-random,
3191 varies across Python implementations, and depends on the dictionary's history
3192 of insertions and deletions. If keys, values and items views are iterated
3193 over with no intervening modifications to the dictionary, the order of items
3194 will directly correspond. This allows the creation of ``(value, key)`` pairs
3195 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
3196 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
3197
Georg Brandl81269142009-05-17 08:31:29 +00003198 Iterating views while adding or deleting entries in the dictionary may raise
3199 a :exc:`RuntimeError` or fail to iterate over all entries.
Benjamin Petersond23f8222009-04-05 19:13:16 +00003200
Georg Brandld22a8152007-09-04 17:43:37 +00003201.. describe:: x in dictview
3202
3203 Return ``True`` if *x* is in the underlying dictionary's keys, values or
3204 items (in the latter case, *x* should be a ``(key, value)`` tuple).
3205
3206
Benjamin Petersonce0506c2008-11-17 21:47:41 +00003207Keys views are set-like since their entries are unique and hashable. If all
Georg Brandlf74cf772010-10-15 16:03:02 +00003208values are hashable, so that ``(key, value)`` pairs are unique and hashable,
3209then the items view is also set-like. (Values views are not treated as set-like
3210since the entries are generally not unique.) For set-like views, all of the
Nick Coghlan273069c2012-08-20 17:14:07 +10003211operations defined for the abstract base class :class:`collections.abc.Set` are
Georg Brandlf74cf772010-10-15 16:03:02 +00003212available (for example, ``==``, ``<``, or ``^``).
Georg Brandl116aa622007-08-15 14:28:22 +00003213
Georg Brandlc53c9662007-09-04 17:58:02 +00003214An example of dictionary view usage::
3215
3216 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
3217 >>> keys = dishes.keys()
3218 >>> values = dishes.values()
3219
3220 >>> # iteration
3221 >>> n = 0
3222 >>> for val in values:
3223 ... n += val
3224 >>> print(n)
3225 504
3226
3227 >>> # keys and values are iterated over in the same order
3228 >>> list(keys)
3229 ['eggs', 'bacon', 'sausage', 'spam']
3230 >>> list(values)
3231 [2, 1, 1, 500]
3232
3233 >>> # view objects are dynamic and reflect dict changes
3234 >>> del dishes['eggs']
3235 >>> del dishes['sausage']
3236 >>> list(keys)
3237 ['spam', 'bacon']
3238
3239 >>> # set operations
3240 >>> keys & {'eggs', 'bacon', 'salad'}
Gregory P. Smithe8388122008-09-04 04:18:09 +00003241 {'bacon'}
Georg Brandlf74cf772010-10-15 16:03:02 +00003242 >>> keys ^ {'sausage', 'juice'}
Sandro Tosi2a8d1952011-08-02 18:42:04 +02003243 {'juice', 'sausage', 'bacon', 'spam'}
Georg Brandlc53c9662007-09-04 17:58:02 +00003244
3245
Georg Brandl116aa622007-08-15 14:28:22 +00003246.. _typecontextmanager:
3247
3248Context Manager Types
3249=====================
3250
Georg Brandl116aa622007-08-15 14:28:22 +00003251.. index::
3252 single: context manager
3253 single: context management protocol
3254 single: protocol; context management
3255
3256Python's :keyword:`with` statement supports the concept of a runtime context
Antoine Pitroua6540902010-12-12 20:09:18 +00003257defined by a context manager. This is implemented using a pair of methods
Georg Brandl116aa622007-08-15 14:28:22 +00003258that allow user-defined classes to define a runtime context that is entered
Antoine Pitroua6540902010-12-12 20:09:18 +00003259before the statement body is executed and exited when the statement ends:
Georg Brandl116aa622007-08-15 14:28:22 +00003260
3261
3262.. method:: contextmanager.__enter__()
3263
3264 Enter the runtime context and return either this object or another object
3265 related to the runtime context. The value returned by this method is bound to
3266 the identifier in the :keyword:`as` clause of :keyword:`with` statements using
3267 this context manager.
3268
Antoine Pitrou11cb9612010-09-15 11:11:28 +00003269 An example of a context manager that returns itself is a :term:`file object`.
3270 File objects return themselves from __enter__() to allow :func:`open` to be
3271 used as the context expression in a :keyword:`with` statement.
Georg Brandl116aa622007-08-15 14:28:22 +00003272
3273 An example of a context manager that returns a related object is the one
Christian Heimesfaf2f632008-01-06 16:59:19 +00003274 returned by :func:`decimal.localcontext`. These managers set the active
Georg Brandl116aa622007-08-15 14:28:22 +00003275 decimal context to a copy of the original decimal context and then return the
3276 copy. This allows changes to be made to the current decimal context in the body
3277 of the :keyword:`with` statement without affecting code outside the
3278 :keyword:`with` statement.
3279
3280
3281.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
3282
Georg Brandl9afde1c2007-11-01 20:32:30 +00003283 Exit the runtime context and return a Boolean flag indicating if any exception
Georg Brandl116aa622007-08-15 14:28:22 +00003284 that occurred should be suppressed. If an exception occurred while executing the
3285 body of the :keyword:`with` statement, the arguments contain the exception type,
3286 value and traceback information. Otherwise, all three arguments are ``None``.
3287
3288 Returning a true value from this method will cause the :keyword:`with` statement
3289 to suppress the exception and continue execution with the statement immediately
3290 following the :keyword:`with` statement. Otherwise the exception continues
3291 propagating after this method has finished executing. Exceptions that occur
3292 during execution of this method will replace any exception that occurred in the
3293 body of the :keyword:`with` statement.
3294
3295 The exception passed in should never be reraised explicitly - instead, this
3296 method should return a false value to indicate that the method completed
3297 successfully and does not want to suppress the raised exception. This allows
3298 context management code (such as ``contextlib.nested``) to easily detect whether
3299 or not an :meth:`__exit__` method has actually failed.
3300
3301Python defines several context managers to support easy thread synchronisation,
3302prompt closure of files or other objects, and simpler manipulation of the active
3303decimal arithmetic context. The specific types are not treated specially beyond
3304their implementation of the context management protocol. See the
3305:mod:`contextlib` module for some examples.
3306
Antoine Pitroua6540902010-12-12 20:09:18 +00003307Python's :term:`generator`\s and the :class:`contextlib.contextmanager` decorator
Christian Heimesd8654cf2007-12-02 15:22:16 +00003308provide a convenient way to implement these protocols. If a generator function is
Antoine Pitroua6540902010-12-12 20:09:18 +00003309decorated with the :class:`contextlib.contextmanager` decorator, it will return a
Georg Brandl116aa622007-08-15 14:28:22 +00003310context manager implementing the necessary :meth:`__enter__` and
3311:meth:`__exit__` methods, rather than the iterator produced by an undecorated
3312generator function.
3313
3314Note that there is no specific slot for any of these methods in the type
3315structure for Python objects in the Python/C API. Extension types wanting to
3316define these methods must provide them as a normal Python accessible method.
3317Compared to the overhead of setting up the runtime context, the overhead of a
3318single class dictionary lookup is negligible.
3319
3320
3321.. _typesother:
3322
3323Other Built-in Types
3324====================
3325
3326The interpreter supports several other kinds of objects. Most of these support
3327only one or two operations.
3328
3329
3330.. _typesmodules:
3331
3332Modules
3333-------
3334
3335The only special operation on a module is attribute access: ``m.name``, where
3336*m* is a module and *name* accesses a name defined in *m*'s symbol table.
3337Module attributes can be assigned to. (Note that the :keyword:`import`
3338statement is not, strictly speaking, an operation on a module object; ``import
3339foo`` does not require a module object named *foo* to exist, rather it requires
3340an (external) *definition* for a module named *foo* somewhere.)
3341
Senthil Kumarana6bac952011-07-04 11:28:30 -07003342A special attribute of every module is :attr:`__dict__`. This is the dictionary
Georg Brandl116aa622007-08-15 14:28:22 +00003343containing the module's symbol table. Modifying this dictionary will actually
3344change the module's symbol table, but direct assignment to the :attr:`__dict__`
3345attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines
3346``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying
3347:attr:`__dict__` directly is not recommended.
3348
3349Modules built into the interpreter are written like this: ``<module 'sys'
3350(built-in)>``. If loaded from a file, they are written as ``<module 'os' from
3351'/usr/local/lib/pythonX.Y/os.pyc'>``.
3352
3353
3354.. _typesobjects:
3355
3356Classes and Class Instances
3357---------------------------
3358
3359See :ref:`objects` and :ref:`class` for these.
3360
3361
3362.. _typesfunctions:
3363
3364Functions
3365---------
3366
3367Function objects are created by function definitions. The only operation on a
3368function object is to call it: ``func(argument-list)``.
3369
3370There are really two flavors of function objects: built-in functions and
3371user-defined functions. Both support the same operation (to call the function),
3372but the implementation is different, hence the different object types.
3373
3374See :ref:`function` for more information.
3375
3376
3377.. _typesmethods:
3378
3379Methods
3380-------
3381
3382.. index:: object: method
3383
3384Methods are functions that are called using the attribute notation. There are
3385two flavors: built-in methods (such as :meth:`append` on lists) and class
3386instance methods. Built-in methods are described with the types that support
3387them.
3388
Georg Brandl2e0b7552007-11-27 12:43:08 +00003389If you access a method (a function defined in a class namespace) through an
3390instance, you get a special object: a :dfn:`bound method` (also called
3391:dfn:`instance method`) object. When called, it will add the ``self`` argument
3392to the argument list. Bound methods have two special read-only attributes:
3393``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
3394the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
3395is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
3396arg-n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00003397
Georg Brandl2e0b7552007-11-27 12:43:08 +00003398Like function objects, bound method objects support getting arbitrary
3399attributes. However, since method attributes are actually stored on the
3400underlying function object (``meth.__func__``), setting method attributes on
Ezio Melotti8b6b1762012-11-09 01:08:25 +02003401bound methods is disallowed. Attempting to set an attribute on a method
3402results in an :exc:`AttributeError` being raised. In order to set a method
3403attribute, you need to explicitly set it on the underlying function object::
Georg Brandl116aa622007-08-15 14:28:22 +00003404
Ezio Melotti8b6b1762012-11-09 01:08:25 +02003405 >>> class C:
3406 ... def method(self):
3407 ... pass
3408 ...
3409 >>> c = C()
3410 >>> c.method.whoami = 'my name is method' # can't set on the method
3411 Traceback (most recent call last):
3412 File "<stdin>", line 1, in <module>
3413 AttributeError: 'method' object has no attribute 'whoami'
3414 >>> c.method.__func__.whoami = 'my name is method'
3415 >>> c.method.whoami
3416 'my name is method'
Georg Brandl116aa622007-08-15 14:28:22 +00003417
3418See :ref:`types` for more information.
3419
3420
3421.. _bltin-code-objects:
3422
3423Code Objects
3424------------
3425
3426.. index:: object: code
3427
3428.. index::
3429 builtin: compile
3430 single: __code__ (function object attribute)
3431
3432Code objects are used by the implementation to represent "pseudo-compiled"
3433executable Python code such as a function body. They differ from function
3434objects because they don't contain a reference to their global execution
3435environment. Code objects are returned by the built-in :func:`compile` function
3436and can be extracted from function objects through their :attr:`__code__`
3437attribute. See also the :mod:`code` module.
3438
3439.. index::
3440 builtin: exec
3441 builtin: eval
3442
3443A code object can be executed or evaluated by passing it (instead of a source
3444string) to the :func:`exec` or :func:`eval` built-in functions.
3445
3446See :ref:`types` for more information.
3447
3448
3449.. _bltin-type-objects:
3450
3451Type Objects
3452------------
3453
3454.. index::
3455 builtin: type
3456 module: types
3457
3458Type objects represent the various object types. An object's type is accessed
3459by the built-in function :func:`type`. There are no special operations on
3460types. The standard module :mod:`types` defines names for all standard built-in
3461types.
3462
Martin v. Löwis250ad612008-04-07 05:43:42 +00003463Types are written like this: ``<class 'int'>``.
Georg Brandl116aa622007-08-15 14:28:22 +00003464
3465
3466.. _bltin-null-object:
3467
3468The Null Object
3469---------------
3470
3471This object is returned by functions that don't explicitly return a value. It
3472supports no special operations. There is exactly one null object, named
Benjamin Peterson98f2b9b2011-07-30 12:26:27 -05003473``None`` (a built-in name). ``type(None)()`` produces the same singleton.
Georg Brandl116aa622007-08-15 14:28:22 +00003474
3475It is written as ``None``.
3476
3477
3478.. _bltin-ellipsis-object:
3479
3480The Ellipsis Object
3481-------------------
3482
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003483This object is commonly used by slicing (see :ref:`slicings`). It supports no
3484special operations. There is exactly one ellipsis object, named
3485:const:`Ellipsis` (a built-in name). ``type(Ellipsis)()`` produces the
3486:const:`Ellipsis` singleton.
Georg Brandl116aa622007-08-15 14:28:22 +00003487
3488It is written as ``Ellipsis`` or ``...``.
3489
3490
Éric Araujo18ddf822011-09-01 23:10:36 +02003491.. _bltin-notimplemented-object:
3492
Benjamin Peterson50211fa2011-07-30 09:57:24 -05003493The NotImplemented Object
3494-------------------------
3495
3496This object is returned from comparisons and binary operations when they are
3497asked to operate on types they don't support. See :ref:`comparisons` for more
Benjamin Peterson98f2b9b2011-07-30 12:26:27 -05003498information. There is exactly one ``NotImplemented`` object.
3499``type(NotImplemented)()`` produces the singleton instance.
Benjamin Peterson50211fa2011-07-30 09:57:24 -05003500
3501It is written as ``NotImplemented``.
3502
Georg Brandl116aa622007-08-15 14:28:22 +00003503
Éric Araujo18ddf822011-09-01 23:10:36 +02003504.. _bltin-boolean-values:
3505
Georg Brandl116aa622007-08-15 14:28:22 +00003506Boolean Values
3507--------------
3508
3509Boolean values are the two constant objects ``False`` and ``True``. They are
3510used to represent truth values (although other values can also be considered
3511false or true). In numeric contexts (for example when used as the argument to
3512an arithmetic operator), they behave like the integers 0 and 1, respectively.
Ezio Melottic1f26f62011-12-02 19:47:24 +02003513The built-in function :func:`bool` can be used to convert any value to a
3514Boolean, if the value can be interpreted as a truth value (see section
3515:ref:`truth` above).
Georg Brandl116aa622007-08-15 14:28:22 +00003516
3517.. index::
3518 single: False
3519 single: True
3520 pair: Boolean; values
3521
3522They are written as ``False`` and ``True``, respectively.
3523
3524
3525.. _typesinternal:
3526
3527Internal Objects
3528----------------
3529
3530See :ref:`types` for this information. It describes stack frame objects,
3531traceback objects, and slice objects.
3532
3533
3534.. _specialattrs:
3535
3536Special Attributes
3537==================
3538
3539The implementation adds a few special read-only attributes to several object
3540types, where they are relevant. Some of these are not reported by the
3541:func:`dir` built-in function.
3542
3543
3544.. attribute:: object.__dict__
3545
3546 A dictionary or other mapping object used to store an object's (writable)
3547 attributes.
3548
3549
3550.. attribute:: instance.__class__
3551
3552 The class to which a class instance belongs.
3553
3554
3555.. attribute:: class.__bases__
3556
Benjamin Peterson1baf4652009-12-31 03:11:23 +00003557 The tuple of base classes of a class object.
Georg Brandl116aa622007-08-15 14:28:22 +00003558
3559
3560.. attribute:: class.__name__
3561
3562 The name of the class or type.
3563
Georg Brandl7a51e582009-03-28 19:13:21 +00003564
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003565.. attribute:: class.__qualname__
3566
3567 The :term:`qualified name` of the class or type.
3568
3569 .. versionadded:: 3.3
3570
3571
Benjamin Petersond23f8222009-04-05 19:13:16 +00003572.. attribute:: class.__mro__
3573
3574 This attribute is a tuple of classes that are considered when looking for
3575 base classes during method resolution.
3576
3577
3578.. method:: class.mro()
3579
3580 This method can be overridden by a metaclass to customize the method
3581 resolution order for its instances. It is called at class instantiation, and
3582 its result is stored in :attr:`__mro__`.
3583
3584
Georg Brandl7a51e582009-03-28 19:13:21 +00003585.. method:: class.__subclasses__
3586
Florent Xicluna74e64952011-10-28 11:21:19 +02003587 Each class keeps a list of weak references to its immediate subclasses. This
3588 method returns a list of all those references still alive.
Benjamin Petersond23f8222009-04-05 19:13:16 +00003589 Example::
Georg Brandl7a51e582009-03-28 19:13:21 +00003590
3591 >>> int.__subclasses__()
Florent Xicluna74e64952011-10-28 11:21:19 +02003592 [<class 'bool'>]
Georg Brandl7a51e582009-03-28 19:13:21 +00003593
3594
Georg Brandl116aa622007-08-15 14:28:22 +00003595.. rubric:: Footnotes
3596
Ezio Melotti0656a562011-08-15 14:27:19 +03003597.. [1] Additional information on these special methods may be found in the Python
Georg Brandl116aa622007-08-15 14:28:22 +00003598 Reference Manual (:ref:`customization`).
3599
Ezio Melotti0656a562011-08-15 14:27:19 +03003600.. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
Georg Brandl116aa622007-08-15 14:28:22 +00003601 similarly for tuples.
3602
Ezio Melotti0656a562011-08-15 14:27:19 +03003603.. [3] They must have since the parser can't tell the type of the operands.
Georg Brandl116aa622007-08-15 14:28:22 +00003604
Ezio Melotti0656a562011-08-15 14:27:19 +03003605.. [4] Cased characters are those with general category property being one of
3606 "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase).
3607
3608.. [5] To format only a tuple you should therefore provide a singleton tuple whose only
Georg Brandl116aa622007-08-15 14:28:22 +00003609 element is the tuple to be formatted.