blob: f2bb99d8e88d7629aee213c30378deeb52e4bad8 [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 Brandl116aa622007-08-15 14:28:22 +000018Some operations are supported by several object types; in particular,
19practically all objects can be compared, tested for truth value, and converted
20to a string (with the :func:`repr` function or the slightly different
21:func:`str` function). The latter function is implicitly used when an object is
22written by the :func:`print` function.
23
24
25.. _truth:
26
27Truth Value Testing
28===================
29
30.. index::
31 statement: if
32 statement: while
33 pair: truth; value
34 pair: Boolean; operations
35 single: false
36
37Any object can be tested for truth value, for use in an :keyword:`if` or
38:keyword:`while` condition or as operand of the Boolean operations below. The
39following values are considered false:
40
41 .. index:: single: None (Built-in object)
42
43* ``None``
44
45 .. index:: single: False (Built-in object)
46
47* ``False``
48
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +000049* zero of any numeric type, for example, ``0``, ``0.0``, ``0j``.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51* any empty sequence, for example, ``''``, ``()``, ``[]``.
52
53* any empty mapping, for example, ``{}``.
54
55* instances of user-defined classes, if the class defines a :meth:`__bool__` or
56 :meth:`__len__` method, when that method returns the integer zero or
57 :class:`bool` value ``False``. [#]_
58
59.. index:: single: true
60
61All other values are considered true --- so objects of many types are always
62true.
63
64.. index::
65 operator: or
66 operator: and
67 single: False
68 single: True
69
70Operations and built-in functions that have a Boolean result always return ``0``
71or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
72(Important exception: the Boolean operations ``or`` and ``and`` always return
73one of their operands.)
74
75
76.. _boolean:
77
78Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not`
79====================================================================
80
81.. index:: pair: Boolean; operations
82
83These are the Boolean operations, ordered by ascending priority:
84
85+-------------+---------------------------------+-------+
86| Operation | Result | Notes |
87+=============+=================================+=======+
88| ``x or y`` | if *x* is false, then *y*, else | \(1) |
89| | *x* | |
90+-------------+---------------------------------+-------+
91| ``x and y`` | if *x* is false, then *x*, else | \(2) |
92| | *y* | |
93+-------------+---------------------------------+-------+
94| ``not x`` | if *x* is false, then ``True``, | \(3) |
95| | else ``False`` | |
96+-------------+---------------------------------+-------+
97
98.. index::
99 operator: and
100 operator: or
101 operator: not
102
103Notes:
104
105(1)
106 This is a short-circuit operator, so it only evaluates the second
107 argument if the first one is :const:`False`.
108
109(2)
110 This is a short-circuit operator, so it only evaluates the second
111 argument if the first one is :const:`True`.
112
113(3)
114 ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
115 interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
116
117
118.. _stdcomparisons:
119
120Comparisons
121===========
122
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000123.. index::
124 pair: chaining; comparisons
125 pair: operator; comparison
126 operator: ==
127 operator: <
128 operator: <=
129 operator: >
130 operator: >=
131 operator: !=
132 operator: is
133 operator: is not
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Georg Brandl905ec322007-09-28 13:39:25 +0000135There are eight comparison operations in Python. They all have the same
136priority (which is higher than that of the Boolean operations). Comparisons can
Georg Brandl116aa622007-08-15 14:28:22 +0000137be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
138y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
139evaluated at all when ``x < y`` is found to be false).
140
141This table summarizes the comparison operations:
142
Georg Brandlfd855162008-01-07 09:13:03 +0000143+------------+-------------------------+
144| Operation | Meaning |
145+============+=========================+
146| ``<`` | strictly less than |
147+------------+-------------------------+
148| ``<=`` | less than or equal |
149+------------+-------------------------+
150| ``>`` | strictly greater than |
151+------------+-------------------------+
152| ``>=`` | greater than or equal |
153+------------+-------------------------+
154| ``==`` | equal |
155+------------+-------------------------+
156| ``!=`` | not equal |
157+------------+-------------------------+
158| ``is`` | object identity |
159+------------+-------------------------+
160| ``is not`` | negated object identity |
161+------------+-------------------------+
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000162
163.. index::
Georg Brandl116aa622007-08-15 14:28:22 +0000164 pair: object; numeric
165 pair: objects; comparing
166
Georg Brandl905ec322007-09-28 13:39:25 +0000167Objects of different types, except different numeric types, never compare equal.
Antoine Pitroue231e392009-12-19 18:22:15 +0000168Furthermore, some types (for example, function objects) support only a degenerate
Georg Brandl905ec322007-09-28 13:39:25 +0000169notion of comparison where any two objects of that type are unequal. The ``<``,
170``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
Mark Dickinsonf673f0c2010-03-13 09:48:39 +0000171comparing a complex number with another built-in numeric type, when the objects
172are of different types that cannot be compared, or in other cases where there is
173no defined ordering.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl48310cd2009-01-03 21:18:54 +0000175.. index::
Georg Brandl905ec322007-09-28 13:39:25 +0000176 single: __eq__() (instance method)
177 single: __ne__() (instance method)
178 single: __lt__() (instance method)
179 single: __le__() (instance method)
180 single: __gt__() (instance method)
181 single: __ge__() (instance method)
Georg Brandl116aa622007-08-15 14:28:22 +0000182
Georg Brandl05f5ab72008-09-24 09:11:47 +0000183Non-identical instances of a class normally compare as non-equal unless the
184class defines the :meth:`__eq__` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Georg Brandl905ec322007-09-28 13:39:25 +0000186Instances of a class cannot be ordered with respect to other instances of the
187same class, or other types of object, unless the class defines enough of the
Georg Brandl05f5ab72008-09-24 09:11:47 +0000188methods :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` (in
189general, :meth:`__lt__` and :meth:`__eq__` are sufficient, if you want the
190conventional meanings of the comparison operators).
Georg Brandl905ec322007-09-28 13:39:25 +0000191
192The behavior of the :keyword:`is` and :keyword:`is not` operators cannot be
193customized; also they can be applied to any two objects and never raise an
194exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. index::
197 operator: in
198 operator: not in
199
200Two more operations with the same syntactic priority, ``in`` and ``not in``, are
201supported only by sequence types (below).
202
203
204.. _typesnumeric:
205
Georg Brandl905ec322007-09-28 13:39:25 +0000206Numeric Types --- :class:`int`, :class:`float`, :class:`complex`
207================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209.. index::
210 object: numeric
211 object: Boolean
212 object: integer
Georg Brandl116aa622007-08-15 14:28:22 +0000213 object: floating point
214 object: complex number
215 pair: C; language
216
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000217There are three distinct numeric types: :dfn:`integers`, :dfn:`floating
218point numbers`, and :dfn:`complex numbers`. In addition, Booleans are a
219subtype of integers. Integers have unlimited precision. Floating point
Georg Brandl60203b42010-10-06 10:11:56 +0000220numbers are usually implemented using :c:type:`double` in C; information
Mark Dickinson74f59022010-08-04 18:42:43 +0000221about the precision and internal representation of floating point
222numbers for the machine on which your program is running is available
223in :data:`sys.float_info`. Complex numbers have a real and imaginary
224part, which are each a floating point number. To extract these parts
225from a complex number *z*, use ``z.real`` and ``z.imag``. (The standard
226library includes additional numeric types, :mod:`fractions` that hold
227rationals, and :mod:`decimal` that hold floating-point numbers with
228user-definable precision.)
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230.. index::
231 pair: numeric; literals
232 pair: integer; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000233 pair: floating point; literals
234 pair: complex number; literals
235 pair: hexadecimal; literals
236 pair: octal; literals
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000237 pair: binary; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239Numbers are created by numeric literals or as the result of built-in functions
Georg Brandl905ec322007-09-28 13:39:25 +0000240and operators. Unadorned integer literals (including hex, octal and binary
241numbers) yield integers. Numeric literals containing a decimal point or an
242exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
243numeric literal yields an imaginary number (a complex number with a zero real
244part) which you can add to an integer or float to get a complex number with real
245and imaginary parts.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247.. index::
248 single: arithmetic
249 builtin: int
Georg Brandl116aa622007-08-15 14:28:22 +0000250 builtin: float
251 builtin: complex
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000252 operator: +
253 operator: -
254 operator: *
255 operator: /
256 operator: //
257 operator: %
258 operator: **
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260Python fully supports mixed arithmetic: when a binary arithmetic operator has
261operands of different numeric types, the operand with the "narrower" type is
Georg Brandl905ec322007-09-28 13:39:25 +0000262widened to that of the other, where integer is narrower than floating point,
263which is narrower than complex. Comparisons between numbers of mixed type use
264the same rule. [#]_ The constructors :func:`int`, :func:`float`, and
265:func:`complex` can be used to produce numbers of a specific type.
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267All numeric types (except complex) support the following operations, sorted by
268ascending priority (operations in the same box have the same priority; all
269numeric operations have a higher priority than comparison operations):
270
Georg Brandl905ec322007-09-28 13:39:25 +0000271+---------------------+---------------------------------+-------+--------------------+
272| Operation | Result | Notes | Full documentation |
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000273+=====================+=================================+=======+====================+
Georg Brandl905ec322007-09-28 13:39:25 +0000274| ``x + y`` | sum of *x* and *y* | | |
275+---------------------+---------------------------------+-------+--------------------+
276| ``x - y`` | difference of *x* and *y* | | |
277+---------------------+---------------------------------+-------+--------------------+
278| ``x * y`` | product of *x* and *y* | | |
279+---------------------+---------------------------------+-------+--------------------+
280| ``x / y`` | quotient of *x* and *y* | | |
281+---------------------+---------------------------------+-------+--------------------+
282| ``x // y`` | floored quotient of *x* and | \(1) | |
283| | *y* | | |
284+---------------------+---------------------------------+-------+--------------------+
285| ``x % y`` | remainder of ``x / y`` | \(2) | |
286+---------------------+---------------------------------+-------+--------------------+
287| ``-x`` | *x* negated | | |
288+---------------------+---------------------------------+-------+--------------------+
289| ``+x`` | *x* unchanged | | |
290+---------------------+---------------------------------+-------+--------------------+
291| ``abs(x)`` | absolute value or magnitude of | | :func:`abs` |
292| | *x* | | |
293+---------------------+---------------------------------+-------+--------------------+
294| ``int(x)`` | *x* converted to integer | \(3) | :func:`int` |
295+---------------------+---------------------------------+-------+--------------------+
Georg Brandl74f36692008-01-06 17:39:49 +0000296| ``float(x)`` | *x* converted to floating point | \(4) | :func:`float` |
Georg Brandl905ec322007-09-28 13:39:25 +0000297+---------------------+---------------------------------+-------+--------------------+
298| ``complex(re, im)`` | a complex number with real part | | :func:`complex` |
299| | *re*, imaginary part *im*. | | |
300| | *im* defaults to zero. | | |
301+---------------------+---------------------------------+-------+--------------------+
302| ``c.conjugate()`` | conjugate of the complex number | | |
303| | *c* | | |
304+---------------------+---------------------------------+-------+--------------------+
305| ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` |
306+---------------------+---------------------------------+-------+--------------------+
Georg Brandl60fe2f12008-01-07 09:16:46 +0000307| ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` |
Georg Brandl905ec322007-09-28 13:39:25 +0000308+---------------------+---------------------------------+-------+--------------------+
Georg Brandl60fe2f12008-01-07 09:16:46 +0000309| ``x ** y`` | *x* to the power *y* | \(5) | |
Georg Brandl905ec322007-09-28 13:39:25 +0000310+---------------------+---------------------------------+-------+--------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312.. index::
313 triple: operations on; numeric; types
314 single: conjugate() (complex number method)
315
316Notes:
317
318(1)
Georg Brandl905ec322007-09-28 13:39:25 +0000319 Also referred to as integer division. The resultant value is a whole
320 integer, though the result's type is not necessarily int. The result is
321 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is
322 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324(2)
Georg Brandl905ec322007-09-28 13:39:25 +0000325 Not for complex numbers. Instead convert to floats using :func:`abs` if
326 appropriate.
327
328(3)
Georg Brandl116aa622007-08-15 14:28:22 +0000329 .. index::
330 module: math
331 single: floor() (in module math)
332 single: ceil() (in module math)
Benjamin Peterson28d88b42009-01-09 03:03:23 +0000333 single: trunc() (in module math)
Georg Brandl116aa622007-08-15 14:28:22 +0000334 pair: numeric; conversions
335 pair: C; language
336
Georg Brandlba956ae2007-11-29 17:24:34 +0000337 Conversion from floating point to integer may round or truncate
Georg Brandl116aa622007-08-15 14:28:22 +0000338 as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module
339 for well-defined conversions.
340
Georg Brandl74f36692008-01-06 17:39:49 +0000341(4)
Georg Brandl48310cd2009-01-03 21:18:54 +0000342 float also accepts the strings "nan" and "inf" with an optional prefix "+"
Christian Heimes99170a52007-12-19 02:07:34 +0000343 or "-" for Not a Number (NaN) and positive or negative infinity.
Christian Heimes7f044312008-01-06 17:05:40 +0000344
Georg Brandl74f36692008-01-06 17:39:49 +0000345(5)
Christian Heimes7f044312008-01-06 17:05:40 +0000346 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
347 programming languages.
348
Georg Brandl48310cd2009-01-03 21:18:54 +0000349
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000350
Christian Heimesfaf2f632008-01-06 16:59:19 +0000351All :class:`numbers.Real` types (:class:`int` and
352:class:`float`) also include the following operations:
353
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000354+--------------------+------------------------------------+--------+
355| Operation | Result | Notes |
356+====================+====================================+========+
357| ``math.trunc(x)`` | *x* truncated to Integral | |
358+--------------------+------------------------------------+--------+
359| ``round(x[, n])`` | *x* rounded to n digits, | |
360| | rounding half to even. If n is | |
361| | omitted, it defaults to 0. | |
362+--------------------+------------------------------------+--------+
363| ``math.floor(x)`` | the greatest integral float <= *x* | |
364+--------------------+------------------------------------+--------+
365| ``math.ceil(x)`` | the least integral float >= *x* | |
366+--------------------+------------------------------------+--------+
Christian Heimesfaf2f632008-01-06 16:59:19 +0000367
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000368For additional numeric operations see the :mod:`math` and :mod:`cmath`
369modules.
370
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000371.. XXXJH exceptions: overflow (when? what operations?) zerodivision
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373
374.. _bitstring-ops:
375
376Bit-string Operations on Integer Types
377--------------------------------------
378
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000379.. index::
380 triple: operations on; integer; types
381 pair: bit-string; operations
382 pair: shifting; operations
383 pair: masking; operations
384 operator: ^
385 operator: &
386 operator: <<
387 operator: >>
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl905ec322007-09-28 13:39:25 +0000389Integers support additional operations that make sense only for bit-strings.
390Negative numbers are treated as their 2's complement value (this assumes a
391sufficiently large number of bits that no overflow occurs during the operation).
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Christian Heimesfaf2f632008-01-06 16:59:19 +0000393The priorities of the binary bitwise operations are all lower than the numeric
Georg Brandl116aa622007-08-15 14:28:22 +0000394operations and higher than the comparisons; the unary operation ``~`` has the
395same priority as the other unary numeric operations (``+`` and ``-``).
396
397This table lists the bit-string operations sorted in ascending priority
398(operations in the same box have the same priority):
399
400+------------+--------------------------------+----------+
401| Operation | Result | Notes |
402+============+================================+==========+
403| ``x | y`` | bitwise :dfn:`or` of *x* and | |
404| | *y* | |
405+------------+--------------------------------+----------+
406| ``x ^ y`` | bitwise :dfn:`exclusive or` of | |
407| | *x* and *y* | |
408+------------+--------------------------------+----------+
409| ``x & y`` | bitwise :dfn:`and` of *x* and | |
410| | *y* | |
411+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000412| ``x << n`` | *x* shifted left by *n* bits | (1)(2) |
Georg Brandl116aa622007-08-15 14:28:22 +0000413+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000414| ``x >> n`` | *x* shifted right by *n* bits | (1)(3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000415+------------+--------------------------------+----------+
416| ``~x`` | the bits of *x* inverted | |
417+------------+--------------------------------+----------+
418
Georg Brandl116aa622007-08-15 14:28:22 +0000419Notes:
420
421(1)
422 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
423
424(2)
425 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
426 without overflow check.
427
428(3)
429 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
430 overflow check.
431
432
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000433Additional Methods on Integer Types
434-----------------------------------
435
436.. method:: int.bit_length()
437
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000438 Return the number of bits necessary to represent an integer in binary,
439 excluding the sign and leading zeros::
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000440
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000441 >>> n = -37
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000442 >>> bin(n)
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000443 '-0b100101'
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000444 >>> n.bit_length()
445 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000446
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000447 More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
448 unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
449 Equivalently, when ``abs(x)`` is small enough to have a correctly
450 rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
451 If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000452
453 Equivalent to::
454
455 def bit_length(self):
Senthil Kumaran0aae6dc2010-06-22 02:57:23 +0000456 s = bin(self) # binary representation: bin(-37) --> '-0b100101'
Raymond Hettingerd3e18b72008-12-19 09:11:49 +0000457 s = s.lstrip('-0b') # remove leading zeros and minus sign
458 return len(s) # len('100101') --> 6
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000459
460 .. versionadded:: 3.1
461
Georg Brandl67b21b72010-08-17 15:07:14 +0000462.. method:: int.to_bytes(length, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000463
464 Return an array of bytes representing an integer.
465
466 >>> (1024).to_bytes(2, byteorder='big')
467 b'\x04\x00'
468 >>> (1024).to_bytes(10, byteorder='big')
469 b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
470 >>> (-1024).to_bytes(10, byteorder='big', signed=True)
471 b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
472 >>> x = 1000
473 >>> x.to_bytes((x.bit_length() // 8) + 1, byteorder='little')
474 b'\xe8\x03'
475
476 The integer is represented using *length* bytes. An :exc:`OverflowError`
477 is raised if the integer is not representable with the given number of
478 bytes.
479
480 The *byteorder* argument determines the byte order used to represent the
481 integer. If *byteorder* is ``"big"``, the most significant byte is at the
482 beginning of the byte array. If *byteorder* is ``"little"``, the most
483 significant byte is at the end of the byte array. To request the native
484 byte order of the host system, use :data:`sys.byteorder` as the byte order
485 value.
486
487 The *signed* argument determines whether two's complement is used to
488 represent the integer. If *signed* is ``False`` and a negative integer is
489 given, an :exc:`OverflowError` is raised. The default value for *signed*
490 is ``False``.
491
492 .. versionadded:: 3.2
493
Georg Brandl67b21b72010-08-17 15:07:14 +0000494.. classmethod:: int.from_bytes(bytes, byteorder, \*, signed=False)
Alexandre Vassalottic36c3782010-01-09 20:35:09 +0000495
496 Return the integer represented by the given array of bytes.
497
498 >>> int.from_bytes(b'\x00\x10', byteorder='big')
499 16
500 >>> int.from_bytes(b'\x00\x10', byteorder='little')
501 4096
502 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
503 -1024
504 >>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
505 64512
506 >>> int.from_bytes([255, 0, 0], byteorder='big')
507 16711680
508
509 The argument *bytes* must either support the buffer protocol or be an
510 iterable producing bytes. :class:`bytes` and :class:`bytearray` are
511 examples of built-in objects that support the buffer protocol.
512
513 The *byteorder* argument determines the byte order used to represent the
514 integer. If *byteorder* is ``"big"``, the most significant byte is at the
515 beginning of the byte array. If *byteorder* is ``"little"``, the most
516 significant byte is at the end of the byte array. To request the native
517 byte order of the host system, use :data:`sys.byteorder` as the byte order
518 value.
519
520 The *signed* argument indicates whether two's complement is used to
521 represent the integer.
522
523 .. versionadded:: 3.2
524
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000525
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000526Additional Methods on Float
527---------------------------
528
Benjamin Petersond7b03282008-09-13 15:58:53 +0000529The float type has some additional methods.
530
531.. method:: float.as_integer_ratio()
532
Mark Dickinson4a3c7c42010-11-07 12:48:18 +0000533 Return a pair of integers whose ratio is exactly equal to the
534 original float and with a positive denominator. Raises
535 :exc:`OverflowError` on infinities and a :exc:`ValueError` on
536 NaNs.
537
538.. method:: float.is_integer()
539
540 Return ``True`` if the float instance is finite with integral
541 value, and ``False`` otherwise::
542
543 >>> (-2.0).is_integer()
544 True
545 >>> (3.2).is_integer()
546 False
Georg Brandl48310cd2009-01-03 21:18:54 +0000547
Benjamin Petersond7b03282008-09-13 15:58:53 +0000548Two methods support conversion to
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000549and from hexadecimal strings. Since Python's floats are stored
550internally as binary numbers, converting a float to or from a
551*decimal* string usually involves a small rounding error. In
552contrast, hexadecimal strings allow exact representation and
553specification of floating-point numbers. This can be useful when
554debugging, and in numerical work.
555
556
557.. method:: float.hex()
558
559 Return a representation of a floating-point number as a hexadecimal
560 string. For finite floating-point numbers, this representation
561 will always include a leading ``0x`` and a trailing ``p`` and
562 exponent.
563
564
Georg Brandlabc38772009-04-12 15:51:51 +0000565.. classmethod:: float.fromhex(s)
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000566
567 Class method to return the float represented by a hexadecimal
568 string *s*. The string *s* may have leading and trailing
569 whitespace.
570
571
572Note that :meth:`float.hex` is an instance method, while
573:meth:`float.fromhex` is a class method.
574
575A hexadecimal string takes the form::
576
577 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
578
579where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
580and ``fraction`` are strings of hexadecimal digits, and ``exponent``
581is a decimal integer with an optional leading sign. Case is not
582significant, and there must be at least one hexadecimal digit in
583either the integer or the fraction. This syntax is similar to the
584syntax specified in section 6.4.4.2 of the C99 standard, and also to
585the syntax used in Java 1.5 onwards. In particular, the output of
586:meth:`float.hex` is usable as a hexadecimal floating-point literal in
587C or Java code, and hexadecimal strings produced by C's ``%a`` format
588character or Java's ``Double.toHexString`` are accepted by
589:meth:`float.fromhex`.
590
591
592Note that the exponent is written in decimal rather than hexadecimal,
593and that it gives the power of 2 by which to multiply the coefficient.
594For example, the hexadecimal string ``0x3.a7p10`` represents the
595floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
596``3740.0``::
597
598 >>> float.fromhex('0x3.a7p10')
599 3740.0
600
601
602Applying the reverse conversion to ``3740.0`` gives a different
603hexadecimal string representing the same number::
604
605 >>> float.hex(3740.0)
606 '0x1.d380000000000p+11'
607
608
Mark Dickinsondc787d22010-05-23 13:33:13 +0000609.. _numeric-hash:
610
611Hashing of numeric types
612------------------------
613
614For numbers ``x`` and ``y``, possibly of different types, it's a requirement
615that ``hash(x) == hash(y)`` whenever ``x == y`` (see the :meth:`__hash__`
616method documentation for more details). For ease of implementation and
617efficiency across a variety of numeric types (including :class:`int`,
618:class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`)
619Python's hash for numeric types is based on a single mathematical function
620that's defined for any rational number, and hence applies to all instances of
621:class:`int` and :class:`fraction.Fraction`, and all finite instances of
622:class:`float` and :class:`decimal.Decimal`. Essentially, this function is
623given by reduction modulo ``P`` for a fixed prime ``P``. The value of ``P`` is
624made available to Python as the :attr:`modulus` attribute of
625:data:`sys.hash_info`.
626
627.. impl-detail::
628
629 Currently, the prime used is ``P = 2**31 - 1`` on machines with 32-bit C
630 longs and ``P = 2**61 - 1`` on machines with 64-bit C longs.
631
632Here are the rules in detail:
633
634 - If ``x = m / n`` is a nonnegative rational number and ``n`` is not divisible
635 by ``P``, define ``hash(x)`` as ``m * invmod(n, P) % P``, where ``invmod(n,
636 P)`` gives the inverse of ``n`` modulo ``P``.
637
638 - If ``x = m / n`` is a nonnegative rational number and ``n`` is
639 divisible by ``P`` (but ``m`` is not) then ``n`` has no inverse
640 modulo ``P`` and the rule above doesn't apply; in this case define
641 ``hash(x)`` to be the constant value ``sys.hash_info.inf``.
642
643 - If ``x = m / n`` is a negative rational number define ``hash(x)``
644 as ``-hash(-x)``. If the resulting hash is ``-1``, replace it with
645 ``-2``.
646
647 - The particular values ``sys.hash_info.inf``, ``-sys.hash_info.inf``
648 and ``sys.hash_info.nan`` are used as hash values for positive
649 infinity, negative infinity, or nans (respectively). (All hashable
650 nans have the same hash value.)
651
652 - For a :class:`complex` number ``z``, the hash values of the real
653 and imaginary parts are combined by computing ``hash(z.real) +
654 sys.hash_info.imag * hash(z.imag)``, reduced modulo
655 ``2**sys.hash_info.width`` so that it lies in
656 ``range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width -
657 1))``. Again, if the result is ``-1``, it's replaced with ``-2``.
658
659
660To clarify the above rules, here's some example Python code,
661equivalent to the builtin hash, for computing the hash of a rational
662number, :class:`float`, or :class:`complex`::
663
664
665 import sys, math
666
667 def hash_fraction(m, n):
668 """Compute the hash of a rational number m / n.
669
670 Assumes m and n are integers, with n positive.
671 Equivalent to hash(fractions.Fraction(m, n)).
672
673 """
674 P = sys.hash_info.modulus
675 # Remove common factors of P. (Unnecessary if m and n already coprime.)
676 while m % P == n % P == 0:
677 m, n = m // P, n // P
678
679 if n % P == 0:
680 hash_ = sys.hash_info.inf
681 else:
682 # Fermat's Little Theorem: pow(n, P-1, P) is 1, so
683 # pow(n, P-2, P) gives the inverse of n modulo P.
684 hash_ = (abs(m) % P) * pow(n, P - 2, P) % P
685 if m < 0:
686 hash_ = -hash_
687 if hash_ == -1:
688 hash_ = -2
689 return hash_
690
691 def hash_float(x):
692 """Compute the hash of a float x."""
693
694 if math.isnan(x):
695 return sys.hash_info.nan
696 elif math.isinf(x):
697 return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
698 else:
699 return hash_fraction(*x.as_integer_ratio())
700
701 def hash_complex(z):
702 """Compute the hash of a complex number z."""
703
704 hash_ = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
705 # do a signed reduction modulo 2**sys.hash_info.width
706 M = 2**(sys.hash_info.width - 1)
707 hash_ = (hash_ & (M - 1)) - (hash & M)
708 if hash_ == -1:
709 hash_ == -2
710 return hash_
711
Georg Brandl6ea420b2008-07-16 12:58:29 +0000712.. _typeiter:
713
Georg Brandl116aa622007-08-15 14:28:22 +0000714Iterator Types
715==============
716
Georg Brandl116aa622007-08-15 14:28:22 +0000717.. index::
718 single: iterator protocol
719 single: protocol; iterator
720 single: sequence; iteration
721 single: container; iteration over
722
723Python supports a concept of iteration over containers. This is implemented
724using two distinct methods; these are used to allow user-defined classes to
725support iteration. Sequences, described below in more detail, always support
726the iteration methods.
727
728One method needs to be defined for container objects to provide iteration
729support:
730
Christian Heimes790c8232008-01-07 21:14:23 +0000731.. XXX duplicated in reference/datamodel!
Georg Brandl116aa622007-08-15 14:28:22 +0000732
Christian Heimes790c8232008-01-07 21:14:23 +0000733.. method:: container.__iter__()
Georg Brandl116aa622007-08-15 14:28:22 +0000734
735 Return an iterator object. The object is required to support the iterator
736 protocol described below. If a container supports different types of
737 iteration, additional methods can be provided to specifically request
738 iterators for those iteration types. (An example of an object supporting
739 multiple forms of iteration would be a tree structure which supports both
740 breadth-first and depth-first traversal.) This method corresponds to the
741 :attr:`tp_iter` slot of the type structure for Python objects in the Python/C
742 API.
743
744The iterator objects themselves are required to support the following two
745methods, which together form the :dfn:`iterator protocol`:
746
747
748.. method:: iterator.__iter__()
749
750 Return the iterator object itself. This is required to allow both containers
751 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
752 This method corresponds to the :attr:`tp_iter` slot of the type structure for
753 Python objects in the Python/C API.
754
755
Georg Brandl905ec322007-09-28 13:39:25 +0000756.. method:: iterator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000757
758 Return the next item from the container. If there are no further items, raise
759 the :exc:`StopIteration` exception. This method corresponds to the
760 :attr:`tp_iternext` slot of the type structure for Python objects in the
761 Python/C API.
762
763Python defines several iterator objects to support iteration over general and
764specific sequence types, dictionaries, and other more specialized forms. The
765specific types are not important beyond their implementation of the iterator
766protocol.
767
Georg Brandl905ec322007-09-28 13:39:25 +0000768Once an iterator's :meth:`__next__` method raises :exc:`StopIteration`, it must
769continue to do so on subsequent calls. Implementations that do not obey this
770property are deemed broken.
Georg Brandl116aa622007-08-15 14:28:22 +0000771
Benjamin Peterson0289b152009-06-28 17:22:03 +0000772
773.. _generator-types:
774
775Generator Types
776---------------
777
Georg Brandl9afde1c2007-11-01 20:32:30 +0000778Python's :term:`generator`\s provide a convenient way to implement the iterator
779protocol. If a container object's :meth:`__iter__` method is implemented as a
780generator, it will automatically return an iterator object (technically, a
781generator object) supplying the :meth:`__iter__` and :meth:`__next__` methods.
Benjamin Peterson0289b152009-06-28 17:22:03 +0000782More information about generators can be found in :ref:`the documentation for
783the yield expression <yieldexpr>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000784
785
786.. _typesseq:
787
Georg Brandl95414632007-11-22 11:00:28 +0000788Sequence Types --- :class:`str`, :class:`bytes`, :class:`bytearray`, :class:`list`, :class:`tuple`, :class:`range`
789==================================================================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000790
Georg Brandle17d5862009-01-18 10:40:25 +0000791There are six sequence types: strings, byte sequences (:class:`bytes` objects),
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000792byte arrays (:class:`bytearray` objects), lists, tuples, and range objects. For
793other containers see the built in :class:`dict` and :class:`set` classes, and
794the :mod:`collections` module.
Georg Brandle17d5862009-01-18 10:40:25 +0000795
Georg Brandl116aa622007-08-15 14:28:22 +0000796
797.. index::
798 object: sequence
799 object: string
Georg Brandl4b491312007-08-31 09:22:56 +0000800 object: bytes
Georg Brandle17d5862009-01-18 10:40:25 +0000801 object: bytearray
Georg Brandl116aa622007-08-15 14:28:22 +0000802 object: tuple
803 object: list
Georg Brandl116aa622007-08-15 14:28:22 +0000804 object: range
805
Georg Brandl7c676132007-10-23 18:17:00 +0000806Strings contain Unicode characters. Their literals are written in single or
807double quotes: ``'xyzzy'``, ``"frobozz"``. See :ref:`strings` for more about
808string literals. In addition to the functionality described here, there are
809also string-specific methods described in the :ref:`string-methods` section.
810
Georg Brandl95414632007-11-22 11:00:28 +0000811Bytes and bytearray objects contain single bytes -- the former is immutable
Georg Brandl18da8f02008-07-01 20:08:02 +0000812while the latter is a mutable sequence. Bytes objects can be constructed the
813constructor, :func:`bytes`, and from literals; use a ``b`` prefix with normal
814string syntax: ``b'xyzzy'``. To construct byte arrays, use the
815:func:`bytearray` function.
Georg Brandl4b491312007-08-31 09:22:56 +0000816
Raymond Hettingerf4477702010-11-04 02:39:07 +0000817While string objects are sequences of characters (represented by strings of
818length 1), bytes and bytearray objects are sequences of *integers* (between 0
819and 255), representing the ASCII value of single bytes. That means that for
820a bytes or bytearray object *b*, ``b[0]`` will be an integer, while
821``b[0:1]`` will be a bytes or bytearray object of length 1. The
822representation of bytes objects uses the literal format (``b'...'``) since it
823is generally more useful than e.g. ``bytes([50, 19, 100])``. You can always
824convert a bytes object into a list of integers using ``list(b)``.
Georg Brandl4b491312007-08-31 09:22:56 +0000825
Raymond Hettingerf4477702010-11-04 02:39:07 +0000826Also, while in previous Python versions, byte strings and Unicode strings
827could be exchanged for each other rather freely (barring encoding issues),
828strings and bytes are now completely separate concepts. There's no implicit
829en-/decoding if you pass an object of the wrong type. A string always
830compares unequal to a bytes or bytearray object.
Georg Brandl2326a792007-09-01 12:08:51 +0000831
Georg Brandl4b491312007-08-31 09:22:56 +0000832Lists are constructed with square brackets, separating items with commas: ``[a,
833b, c]``. Tuples are constructed by the comma operator (not within square
834brackets), with or without enclosing parentheses, but an empty tuple must have
835the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple
836must have a trailing comma, such as ``(d,)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Georg Brandl95414632007-11-22 11:00:28 +0000838Objects of type range are created using the :func:`range` function. They don't
839support slicing, concatenation or repetition, and using ``in``, ``not in``,
840:func:`min` or :func:`max` on them is inefficient.
Georg Brandl116aa622007-08-15 14:28:22 +0000841
842Most sequence types support the following operations. The ``in`` and ``not in``
843operations have the same priorities as the comparison operations. The ``+`` and
844``*`` operations have the same priority as the corresponding numeric operations.
Christian Heimes043d6f62008-01-07 17:19:16 +0000845[#]_ Additional methods are provided for :ref:`typesseq-mutable`.
Georg Brandl116aa622007-08-15 14:28:22 +0000846
847This table lists the sequence operations sorted in ascending priority
848(operations in the same box have the same priority). In the table, *s* and *t*
849are sequences of the same type; *n*, *i* and *j* are integers:
850
851+------------------+--------------------------------+----------+
852| Operation | Result | Notes |
853+==================+================================+==========+
854| ``x in s`` | ``True`` if an item of *s* is | \(1) |
855| | equal to *x*, else ``False`` | |
856+------------------+--------------------------------+----------+
857| ``x not in s`` | ``False`` if an item of *s* is | \(1) |
858| | equal to *x*, else ``True`` | |
859+------------------+--------------------------------+----------+
860| ``s + t`` | the concatenation of *s* and | \(6) |
861| | *t* | |
862+------------------+--------------------------------+----------+
863| ``s * n, n * s`` | *n* shallow copies of *s* | \(2) |
864| | concatenated | |
865+------------------+--------------------------------+----------+
866| ``s[i]`` | *i*'th item of *s*, origin 0 | \(3) |
867+------------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000868| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000869+------------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000870| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |
Georg Brandl116aa622007-08-15 14:28:22 +0000871| | with step *k* | |
872+------------------+--------------------------------+----------+
873| ``len(s)`` | length of *s* | |
874+------------------+--------------------------------+----------+
875| ``min(s)`` | smallest item of *s* | |
876+------------------+--------------------------------+----------+
877| ``max(s)`` | largest item of *s* | |
878+------------------+--------------------------------+----------+
879
Georg Brandl7c676132007-10-23 18:17:00 +0000880Sequence types also support comparisons. In particular, tuples and lists are
881compared lexicographically by comparing corresponding elements. This means that
Georg Brandl4b491312007-08-31 09:22:56 +0000882to compare equal, every element must compare equal and the two sequences must be
Georg Brandl7c676132007-10-23 18:17:00 +0000883of the same type and have the same length. (For full details see
Georg Brandl4b491312007-08-31 09:22:56 +0000884:ref:`comparisons` in the language reference.)
Georg Brandl116aa622007-08-15 14:28:22 +0000885
886.. index::
887 triple: operations on; sequence; types
888 builtin: len
889 builtin: min
890 builtin: max
891 pair: concatenation; operation
892 pair: repetition; operation
893 pair: subscript; operation
894 pair: slice; operation
Georg Brandl116aa622007-08-15 14:28:22 +0000895 operator: in
896 operator: not in
897
898Notes:
899
900(1)
Georg Brandl4b491312007-08-31 09:22:56 +0000901 When *s* is a string object, the ``in`` and ``not in`` operations act like a
902 substring test.
Georg Brandl116aa622007-08-15 14:28:22 +0000903
904(2)
905 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
906 sequence of the same type as *s*). Note also that the copies are shallow;
907 nested structures are not copied. This often haunts new Python programmers;
Christian Heimesfe337bf2008-03-23 21:54:12 +0000908 consider:
Georg Brandl116aa622007-08-15 14:28:22 +0000909
910 >>> lists = [[]] * 3
911 >>> lists
912 [[], [], []]
913 >>> lists[0].append(3)
914 >>> lists
915 [[3], [3], [3]]
916
917 What has happened is that ``[[]]`` is a one-element list containing an empty
Christian Heimesfe337bf2008-03-23 21:54:12 +0000918 list, so all three elements of ``[[]] * 3`` are (pointers to) this single empty
919 list. Modifying any of the elements of ``lists`` modifies this single list.
920 You can create a list of different lists this way:
Georg Brandl116aa622007-08-15 14:28:22 +0000921
922 >>> lists = [[] for i in range(3)]
923 >>> lists[0].append(3)
924 >>> lists[1].append(5)
925 >>> lists[2].append(7)
926 >>> lists
927 [[3], [5], [7]]
928
929(3)
930 If *i* or *j* is negative, the index is relative to the end of the string:
Georg Brandl7c676132007-10-23 18:17:00 +0000931 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
932 still ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000933
934(4)
935 The slice of *s* from *i* to *j* is defined as the sequence of items with index
936 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
937 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
938 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
939 empty.
940
941(5)
942 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
Christian Heimes2c181612007-12-17 20:04:13 +0000943 items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words,
Georg Brandl116aa622007-08-15 14:28:22 +0000944 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
945 *j* is reached (but never including *j*). If *i* or *j* is greater than
946 ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become
947 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
948 If *k* is ``None``, it is treated like ``1``.
949
950(6)
Georg Brandl495f7b52009-10-27 15:28:25 +0000951 .. impl-detail::
952
953 If *s* and *t* are both strings, some Python implementations such as
954 CPython can usually perform an in-place optimization for assignments of
955 the form ``s = s + t`` or ``s += t``. When applicable, this optimization
956 makes quadratic run-time much less likely. This optimization is both
957 version and implementation dependent. For performance sensitive code, it
958 is preferable to use the :meth:`str.join` method which assures consistent
959 linear concatenation performance across versions and implementations.
Georg Brandl116aa622007-08-15 14:28:22 +0000960
Georg Brandl116aa622007-08-15 14:28:22 +0000961
962.. _string-methods:
963
964String Methods
965--------------
966
967.. index:: pair: string; methods
968
Benjamin Peterson308d6372009-09-18 21:42:35 +0000969String objects support the methods listed below.
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000970
Benjamin Peterson308d6372009-09-18 21:42:35 +0000971In addition, Python's strings support the sequence type methods described in the
972:ref:`typesseq` section. To output formatted strings, see the
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000973:ref:`string-formatting` section. Also, see the :mod:`re` module for string
974functions based on regular expressions.
Georg Brandl116aa622007-08-15 14:28:22 +0000975
976.. method:: str.capitalize()
977
Senthil Kumaranfa897982010-07-05 11:41:42 +0000978 Return a copy of the string with its first character capitalized and the
Senthil Kumaran37c63a32010-07-06 02:08:36 +0000979 rest lowercased.
Georg Brandl116aa622007-08-15 14:28:22 +0000980
Georg Brandl116aa622007-08-15 14:28:22 +0000981
982.. method:: str.center(width[, fillchar])
983
984 Return centered in a string of length *width*. Padding is done using the
985 specified *fillchar* (default is a space).
986
Georg Brandl116aa622007-08-15 14:28:22 +0000987
988.. method:: str.count(sub[, start[, end]])
989
Benjamin Petersonad3d5c22009-02-26 03:38:59 +0000990 Return the number of non-overlapping occurrences of substring *sub* in the
991 range [*start*, *end*]. Optional arguments *start* and *end* are
992 interpreted as in slice notation.
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994
Victor Stinnere14e2122010-11-07 18:41:46 +0000995.. method:: str.encode(encoding="utf-8", errors="strict")
Georg Brandl116aa622007-08-15 14:28:22 +0000996
Victor Stinnere14e2122010-11-07 18:41:46 +0000997 Return an encoded version of the string as a bytes object. Default encoding
998 is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
999 The default for *errors* is ``'strict'``, meaning that encoding errors raise
1000 a :exc:`UnicodeError`. Other possible
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001001 values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1002 ``'backslashreplace'`` and any other name registered via
1003 :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a
1004 list of possible encodings, see section :ref:`standard-encodings`.
Georg Brandl116aa622007-08-15 14:28:22 +00001005
Benjamin Peterson308d6372009-09-18 21:42:35 +00001006 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +00001007 Support for keyword arguments added.
1008
Georg Brandl116aa622007-08-15 14:28:22 +00001009
1010.. method:: str.endswith(suffix[, start[, end]])
1011
1012 Return ``True`` if the string ends with the specified *suffix*, otherwise return
1013 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
1014 *start*, test beginning at that position. With optional *end*, stop comparing
1015 at that position.
1016
Georg Brandl116aa622007-08-15 14:28:22 +00001017
1018.. method:: str.expandtabs([tabsize])
1019
Georg Brandl9afde1c2007-11-01 20:32:30 +00001020 Return a copy of the string where all tab characters are replaced by one or
1021 more spaces, depending on the current column and the given tab size. The
1022 column number is reset to zero after each newline occurring in the string.
1023 If *tabsize* is not given, a tab size of ``8`` characters is assumed. This
1024 doesn't understand other non-printing characters or escape sequences.
Georg Brandl116aa622007-08-15 14:28:22 +00001025
1026
1027.. method:: str.find(sub[, start[, end]])
1028
Benjamin Petersond99cd812010-04-27 22:58:50 +00001029 Return the lowest index in the string where substring *sub* is found, such
1030 that *sub* is contained in the slice ``s[start:end]``. Optional arguments
1031 *start* and *end* are interpreted as in slice notation. Return ``-1`` if
1032 *sub* is not found.
Georg Brandl116aa622007-08-15 14:28:22 +00001033
1034
Benjamin Petersonad3d5c22009-02-26 03:38:59 +00001035.. method:: str.format(*args, **kwargs)
Georg Brandl4b491312007-08-31 09:22:56 +00001036
Georg Brandl1f70cdf2010-03-21 09:04:24 +00001037 Perform a string formatting operation. The string on which this method is
1038 called can contain literal text or replacement fields delimited by braces
1039 ``{}``. Each replacement field contains either the numeric index of a
1040 positional argument, or the name of a keyword argument. Returns a copy of
1041 the string where each replacement field is replaced with the string value of
1042 the corresponding argument.
Georg Brandl4b491312007-08-31 09:22:56 +00001043
1044 >>> "The sum of 1 + 2 is {0}".format(1+2)
1045 'The sum of 1 + 2 is 3'
1046
1047 See :ref:`formatstrings` for a description of the various formatting options
1048 that can be specified in format strings.
1049
Georg Brandl4b491312007-08-31 09:22:56 +00001050
Eric Smith27bbca62010-11-04 17:06:58 +00001051.. method:: str.format_map(mapping)
1052
Éric Araujo2642ad02010-11-06 04:59:27 +00001053 Similar to ``str.format(**mapping)``, except that ``mapping`` is
Eric Smith27bbca62010-11-04 17:06:58 +00001054 used directly and not copied to a :class:`dict` . This is useful
Eric Smith5ad85f82010-11-06 13:22:13 +00001055 if for example ``mapping`` is a dict subclass:
Eric Smith27bbca62010-11-04 17:06:58 +00001056
Eric Smith5ad85f82010-11-06 13:22:13 +00001057 >>> class Default(dict):
1058 ... def __missing__(self, key):
1059 ... return key
1060 ...
1061 >>> '{name} was born in {country}'.format_map(Default(name='Guido'))
1062 'Guido was born in country'
1063
1064 .. versionadded:: 3.2
1065
Eric Smith27bbca62010-11-04 17:06:58 +00001066
Georg Brandl116aa622007-08-15 14:28:22 +00001067.. method:: str.index(sub[, start[, end]])
1068
1069 Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found.
1070
1071
1072.. method:: str.isalnum()
1073
1074 Return true if all characters in the string are alphanumeric and there is at
1075 least one character, false otherwise.
1076
Georg Brandl116aa622007-08-15 14:28:22 +00001077
1078.. method:: str.isalpha()
1079
1080 Return true if all characters in the string are alphabetic and there is at least
1081 one character, false otherwise.
1082
Georg Brandl116aa622007-08-15 14:28:22 +00001083
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001084.. method:: str.isdecimal()
1085
1086 Return true if all characters in the string are decimal
1087 characters and there is at least one character, false
1088 otherwise. Decimal characters include digit characters, and all characters
1089 that that can be used to form decimal-radix numbers, e.g. U+0660,
1090 ARABIC-INDIC DIGIT ZERO.
Georg Brandl48310cd2009-01-03 21:18:54 +00001091
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001092
Georg Brandl116aa622007-08-15 14:28:22 +00001093.. method:: str.isdigit()
1094
1095 Return true if all characters in the string are digits and there is at least one
1096 character, false otherwise.
1097
Georg Brandl116aa622007-08-15 14:28:22 +00001098
1099.. method:: str.isidentifier()
1100
1101 Return true if the string is a valid identifier according to the language
Georg Brandl4b491312007-08-31 09:22:56 +00001102 definition, section :ref:`identifiers`.
Georg Brandl116aa622007-08-15 14:28:22 +00001103
1104
1105.. method:: str.islower()
1106
1107 Return true if all cased characters in the string are lowercase and there is at
1108 least one cased character, false otherwise.
1109
Georg Brandl116aa622007-08-15 14:28:22 +00001110
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +00001111.. method:: str.isnumeric()
1112
1113 Return true if all characters in the string are numeric
1114 characters, and there is at least one character, false
1115 otherwise. Numeric characters include digit characters, and all characters
1116 that have the Unicode numeric value property, e.g. U+2155,
1117 VULGAR FRACTION ONE FIFTH.
1118
Georg Brandl48310cd2009-01-03 21:18:54 +00001119
Georg Brandl559e5d72008-06-11 18:37:52 +00001120.. method:: str.isprintable()
1121
1122 Return true if all characters in the string are printable or the string is
1123 empty, false otherwise. Nonprintable characters are those characters defined
1124 in the Unicode character database as "Other" or "Separator", excepting the
1125 ASCII space (0x20) which is considered printable. (Note that printable
1126 characters in this context are those which should not be escaped when
1127 :func:`repr` is invoked on a string. It has no bearing on the handling of
1128 strings written to :data:`sys.stdout` or :data:`sys.stderr`.)
1129
1130
Georg Brandl116aa622007-08-15 14:28:22 +00001131.. method:: str.isspace()
1132
1133 Return true if there are only whitespace characters in the string and there is
1134 at least one character, false otherwise.
1135
Georg Brandl116aa622007-08-15 14:28:22 +00001136
1137.. method:: str.istitle()
1138
1139 Return true if the string is a titlecased string and there is at least one
1140 character, for example uppercase characters may only follow uncased characters
1141 and lowercase characters only cased ones. Return false otherwise.
1142
Georg Brandl116aa622007-08-15 14:28:22 +00001143
1144.. method:: str.isupper()
1145
1146 Return true if all cased characters in the string are uppercase and there is at
1147 least one cased character, false otherwise.
1148
Georg Brandl116aa622007-08-15 14:28:22 +00001149
Georg Brandl495f7b52009-10-27 15:28:25 +00001150.. method:: str.join(iterable)
Georg Brandl116aa622007-08-15 14:28:22 +00001151
Georg Brandl495f7b52009-10-27 15:28:25 +00001152 Return a string which is the concatenation of the strings in the
1153 :term:`iterable` *iterable*. A :exc:`TypeError` will be raised if there are
1154 any non-string values in *seq*, including :class:`bytes` objects. The
1155 separator between elements is the string providing this method.
Georg Brandl116aa622007-08-15 14:28:22 +00001156
1157
1158.. method:: str.ljust(width[, fillchar])
1159
1160 Return the string left justified in a string of length *width*. Padding is done
1161 using the specified *fillchar* (default is a space). The original string is
1162 returned if *width* is less than ``len(s)``.
1163
Georg Brandl116aa622007-08-15 14:28:22 +00001164
1165.. method:: str.lower()
1166
1167 Return a copy of the string converted to lowercase.
1168
Georg Brandl116aa622007-08-15 14:28:22 +00001169
1170.. method:: str.lstrip([chars])
1171
1172 Return a copy of the string with leading characters removed. The *chars*
1173 argument is a string specifying the set of characters to be removed. If omitted
1174 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +00001175 argument is not a prefix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001176
1177 >>> ' spacious '.lstrip()
1178 'spacious '
1179 >>> 'www.example.com'.lstrip('cmowz.')
1180 'example.com'
1181
Georg Brandl116aa622007-08-15 14:28:22 +00001182
Georg Brandlabc38772009-04-12 15:51:51 +00001183.. staticmethod:: str.maketrans(x[, y[, z]])
Georg Brandlceee0772007-11-27 23:48:05 +00001184
1185 This static method returns a translation table usable for :meth:`str.translate`.
1186
1187 If there is only one argument, it must be a dictionary mapping Unicode
1188 ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
1189 strings (of arbitrary lengths) or None. Character keys will then be
1190 converted to ordinals.
1191
1192 If there are two arguments, they must be strings of equal length, and in the
1193 resulting dictionary, each character in x will be mapped to the character at
1194 the same position in y. If there is a third argument, it must be a string,
1195 whose characters will be mapped to None in the result.
1196
1197
Georg Brandl116aa622007-08-15 14:28:22 +00001198.. method:: str.partition(sep)
1199
1200 Split the string at the first occurrence of *sep*, and return a 3-tuple
1201 containing the part before the separator, the separator itself, and the part
1202 after the separator. If the separator is not found, return a 3-tuple containing
1203 the string itself, followed by two empty strings.
1204
Georg Brandl116aa622007-08-15 14:28:22 +00001205
1206.. method:: str.replace(old, new[, count])
1207
1208 Return a copy of the string with all occurrences of substring *old* replaced by
1209 *new*. If the optional argument *count* is given, only the first *count*
1210 occurrences are replaced.
1211
1212
Georg Brandl226878c2007-08-31 10:15:37 +00001213.. method:: str.rfind(sub[, start[, end]])
Georg Brandl116aa622007-08-15 14:28:22 +00001214
Benjamin Petersond99cd812010-04-27 22:58:50 +00001215 Return the highest index in the string where substring *sub* is found, such
1216 that *sub* is contained within ``s[start:end]``. Optional arguments *start*
1217 and *end* are interpreted as in slice notation. Return ``-1`` on failure.
Georg Brandl116aa622007-08-15 14:28:22 +00001218
1219
1220.. method:: str.rindex(sub[, start[, end]])
1221
1222 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1223 found.
1224
1225
1226.. method:: str.rjust(width[, fillchar])
1227
1228 Return the string right justified in a string of length *width*. Padding is done
1229 using the specified *fillchar* (default is a space). The original string is
1230 returned if *width* is less than ``len(s)``.
1231
Georg Brandl116aa622007-08-15 14:28:22 +00001232
1233.. method:: str.rpartition(sep)
1234
1235 Split the string at the last occurrence of *sep*, and return a 3-tuple
1236 containing the part before the separator, the separator itself, and the part
1237 after the separator. If the separator is not found, return a 3-tuple containing
1238 two empty strings, followed by the string itself.
1239
Georg Brandl116aa622007-08-15 14:28:22 +00001240
Georg Brandl226878c2007-08-31 10:15:37 +00001241.. method:: str.rsplit([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +00001242
1243 Return a list of the words in the string, using *sep* as the delimiter string.
1244 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
1245 ones. If *sep* is not specified or ``None``, any whitespace string is a
1246 separator. Except for splitting from the right, :meth:`rsplit` behaves like
1247 :meth:`split` which is described in detail below.
1248
Georg Brandl116aa622007-08-15 14:28:22 +00001249
1250.. method:: str.rstrip([chars])
1251
1252 Return a copy of the string with trailing characters removed. The *chars*
1253 argument is a string specifying the set of characters to be removed. If omitted
1254 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +00001255 argument is not a suffix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001256
1257 >>> ' spacious '.rstrip()
1258 ' spacious'
1259 >>> 'mississippi'.rstrip('ipz')
1260 'mississ'
1261
Georg Brandl116aa622007-08-15 14:28:22 +00001262
Georg Brandl226878c2007-08-31 10:15:37 +00001263.. method:: str.split([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +00001264
Georg Brandl226878c2007-08-31 10:15:37 +00001265 Return a list of the words in the string, using *sep* as the delimiter
1266 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
1267 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
1268 specified, then there is no limit on the number of splits (all possible
Georg Brandl9afde1c2007-11-01 20:32:30 +00001269 splits are made).
1270
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001271 If *sep* is given, consecutive delimiters are not grouped together and are
Georg Brandl226878c2007-08-31 10:15:37 +00001272 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
1273 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
Georg Brandl9afde1c2007-11-01 20:32:30 +00001274 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Georg Brandl226878c2007-08-31 10:15:37 +00001275 Splitting an empty string with a specified separator returns ``['']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001276
1277 If *sep* is not specified or is ``None``, a different splitting algorithm is
Georg Brandl9afde1c2007-11-01 20:32:30 +00001278 applied: runs of consecutive whitespace are regarded as a single separator,
1279 and the result will contain no empty strings at the start or end if the
1280 string has leading or trailing whitespace. Consequently, splitting an empty
1281 string or a string consisting of just whitespace with a ``None`` separator
1282 returns ``[]``.
1283
1284 For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and
1285 ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001286
1287
1288.. method:: str.splitlines([keepends])
1289
1290 Return a list of the lines in the string, breaking at line boundaries. Line
1291 breaks are not included in the resulting list unless *keepends* is given and
1292 true.
1293
1294
1295.. method:: str.startswith(prefix[, start[, end]])
1296
1297 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
1298 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
1299 test string beginning at that position. With optional *end*, stop comparing
1300 string at that position.
1301
Georg Brandl116aa622007-08-15 14:28:22 +00001302
1303.. method:: str.strip([chars])
1304
1305 Return a copy of the string with the leading and trailing characters removed.
1306 The *chars* argument is a string specifying the set of characters to be removed.
1307 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
1308 The *chars* argument is not a prefix or suffix; rather, all combinations of its
Christian Heimesfe337bf2008-03-23 21:54:12 +00001309 values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001310
1311 >>> ' spacious '.strip()
1312 'spacious'
1313 >>> 'www.example.com'.strip('cmowz.')
1314 'example'
1315
Georg Brandl116aa622007-08-15 14:28:22 +00001316
1317.. method:: str.swapcase()
1318
1319 Return a copy of the string with uppercase characters converted to lowercase and
1320 vice versa.
1321
Georg Brandl116aa622007-08-15 14:28:22 +00001322
1323.. method:: str.title()
1324
Raymond Hettingerb8b0ba12009-09-29 06:22:28 +00001325 Return a titlecased version of the string where words start with an uppercase
1326 character and the remaining characters are lowercase.
1327
1328 The algorithm uses a simple language-independent definition of a word as
1329 groups of consecutive letters. The definition works in many contexts but
1330 it means that apostrophes in contractions and possessives form word
1331 boundaries, which may not be the desired result::
1332
1333 >>> "they're bill's friends from the UK".title()
1334 "They'Re Bill'S Friends From The Uk"
1335
1336 A workaround for apostrophes can be constructed using regular expressions::
1337
1338 >>> import re
1339 >>> def titlecase(s):
1340 return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
1341 lambda mo: mo.group(0)[0].upper() +
1342 mo.group(0)[1:].lower(),
1343 s)
1344
1345 >>> titlecase("they're bill's friends.")
1346 "They're Bill's Friends."
Georg Brandl116aa622007-08-15 14:28:22 +00001347
Georg Brandl116aa622007-08-15 14:28:22 +00001348
Georg Brandl4b491312007-08-31 09:22:56 +00001349.. method:: str.translate(map)
Georg Brandl116aa622007-08-15 14:28:22 +00001350
Georg Brandl226878c2007-08-31 10:15:37 +00001351 Return a copy of the *s* where all characters have been mapped through the
Georg Brandl454636f2008-12-27 23:33:20 +00001352 *map* which must be a dictionary of Unicode ordinals (integers) to Unicode
Georg Brandlceee0772007-11-27 23:48:05 +00001353 ordinals, strings or ``None``. Unmapped characters are left untouched.
1354 Characters mapped to ``None`` are deleted.
1355
Georg Brandl454636f2008-12-27 23:33:20 +00001356 You can use :meth:`str.maketrans` to create a translation map from
1357 character-to-character mappings in different formats.
Christian Heimesfe337bf2008-03-23 21:54:12 +00001358
Georg Brandl4b491312007-08-31 09:22:56 +00001359 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +00001360
Georg Brandlceee0772007-11-27 23:48:05 +00001361 An even more flexible approach is to create a custom character mapping
1362 codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
Georg Brandl4b491312007-08-31 09:22:56 +00001363 example).
Georg Brandl116aa622007-08-15 14:28:22 +00001364
1365
1366.. method:: str.upper()
1367
1368 Return a copy of the string converted to uppercase.
1369
Georg Brandl116aa622007-08-15 14:28:22 +00001370
1371.. method:: str.zfill(width)
1372
Georg Brandl9afde1c2007-11-01 20:32:30 +00001373 Return the numeric string left filled with zeros in a string of length
1374 *width*. A sign prefix is handled correctly. The original string is
1375 returned if *width* is less than ``len(s)``.
Christian Heimesb186d002008-03-18 15:15:01 +00001376
1377
Georg Brandl116aa622007-08-15 14:28:22 +00001378
Georg Brandl4b491312007-08-31 09:22:56 +00001379.. _old-string-formatting:
Georg Brandl116aa622007-08-15 14:28:22 +00001380
Georg Brandl4b491312007-08-31 09:22:56 +00001381Old String Formatting Operations
1382--------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00001383
1384.. index::
1385 single: formatting, string (%)
1386 single: interpolation, string (%)
1387 single: string; formatting
1388 single: string; interpolation
1389 single: printf-style formatting
1390 single: sprintf-style formatting
1391 single: % formatting
1392 single: % interpolation
1393
Georg Brandl81ac1ce2007-08-31 17:17:17 +00001394.. XXX is the note enough?
Georg Brandl4b491312007-08-31 09:22:56 +00001395
1396.. note::
1397
Georg Brandl226878c2007-08-31 10:15:37 +00001398 The formatting operations described here are obsolete and may go away in future
Georg Brandl4b491312007-08-31 09:22:56 +00001399 versions of Python. Use the new :ref:`string-formatting` in new code.
1400
1401String objects have one unique built-in operation: the ``%`` operator (modulo).
1402This is also known as the string *formatting* or *interpolation* operator.
1403Given ``format % values`` (where *format* is a string), ``%`` conversion
1404specifications in *format* are replaced with zero or more elements of *values*.
Georg Brandl60203b42010-10-06 10:11:56 +00001405The effect is similar to the using :c:func:`sprintf` in the C language.
Georg Brandl116aa622007-08-15 14:28:22 +00001406
1407If *format* requires a single argument, *values* may be a single non-tuple
1408object. [#]_ Otherwise, *values* must be a tuple with exactly the number of
1409items specified by the format string, or a single mapping object (for example, a
1410dictionary).
1411
1412A conversion specifier contains two or more characters and has the following
1413components, which must occur in this order:
1414
1415#. The ``'%'`` character, which marks the start of the specifier.
1416
1417#. Mapping key (optional), consisting of a parenthesised sequence of characters
1418 (for example, ``(somename)``).
1419
1420#. Conversion flags (optional), which affect the result of some conversion
1421 types.
1422
1423#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
1424 actual width is read from the next element of the tuple in *values*, and the
1425 object to convert comes after the minimum field width and optional precision.
1426
1427#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
1428 specified as ``'*'`` (an asterisk), the actual width is read from the next
1429 element of the tuple in *values*, and the value to convert comes after the
1430 precision.
1431
1432#. Length modifier (optional).
1433
1434#. Conversion type.
1435
1436When the right argument is a dictionary (or other mapping type), then the
1437formats in the string *must* include a parenthesised mapping key into that
1438dictionary inserted immediately after the ``'%'`` character. The mapping key
Christian Heimesfe337bf2008-03-23 21:54:12 +00001439selects the value to be formatted from the mapping. For example:
Georg Brandl116aa622007-08-15 14:28:22 +00001440
Georg Brandledc9e7f2010-10-17 09:19:03 +00001441 >>> print('%(language)s has %(number)03d quote types.' %
1442 ... {'language': "Python", "number": 2})
Georg Brandl116aa622007-08-15 14:28:22 +00001443 Python has 002 quote types.
1444
1445In this case no ``*`` specifiers may occur in a format (since they require a
1446sequential parameter list).
1447
1448The conversion flag characters are:
1449
1450+---------+---------------------------------------------------------------------+
1451| Flag | Meaning |
1452+=========+=====================================================================+
1453| ``'#'`` | The value conversion will use the "alternate form" (where defined |
1454| | below). |
1455+---------+---------------------------------------------------------------------+
1456| ``'0'`` | The conversion will be zero padded for numeric values. |
1457+---------+---------------------------------------------------------------------+
1458| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
1459| | conversion if both are given). |
1460+---------+---------------------------------------------------------------------+
1461| ``' '`` | (a space) A blank should be left before a positive number (or empty |
1462| | string) produced by a signed conversion. |
1463+---------+---------------------------------------------------------------------+
1464| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
1465| | (overrides a "space" flag). |
1466+---------+---------------------------------------------------------------------+
1467
1468A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001469is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
Georg Brandl116aa622007-08-15 14:28:22 +00001470
1471The conversion types are:
1472
1473+------------+-----------------------------------------------------+-------+
1474| Conversion | Meaning | Notes |
1475+============+=====================================================+=======+
1476| ``'d'`` | Signed integer decimal. | |
1477+------------+-----------------------------------------------------+-------+
1478| ``'i'`` | Signed integer decimal. | |
1479+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001480| ``'o'`` | Signed octal value. | \(1) |
Georg Brandl116aa622007-08-15 14:28:22 +00001481+------------+-----------------------------------------------------+-------+
Benjamin Petersone0124bd2009-03-09 21:04:33 +00001482| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(7) |
Georg Brandl116aa622007-08-15 14:28:22 +00001483+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001484| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001485+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001486| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001487+------------+-----------------------------------------------------+-------+
1488| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
1489+------------+-----------------------------------------------------+-------+
1490| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
1491+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00001492| ``'f'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001493+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00001494| ``'F'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001495+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00001496| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
1497| | format if exponent is less than -4 or not less than | |
1498| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00001499+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00001500| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
1501| | format if exponent is less than -4 or not less than | |
1502| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00001503+------------+-----------------------------------------------------+-------+
1504| ``'c'`` | Single character (accepts integer or single | |
1505| | character string). | |
1506+------------+-----------------------------------------------------+-------+
Ezio Melotti0639d5a2009-12-19 23:26:38 +00001507| ``'r'`` | String (converts any Python object using | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00001508| | :func:`repr`). | |
1509+------------+-----------------------------------------------------+-------+
Ezio Melotti0639d5a2009-12-19 23:26:38 +00001510| ``'s'`` | String (converts any Python object using | |
Georg Brandl116aa622007-08-15 14:28:22 +00001511| | :func:`str`). | |
1512+------------+-----------------------------------------------------+-------+
1513| ``'%'`` | No argument is converted, results in a ``'%'`` | |
1514| | character in the result. | |
1515+------------+-----------------------------------------------------+-------+
1516
1517Notes:
1518
1519(1)
1520 The alternate form causes a leading zero (``'0'``) to be inserted between
1521 left-hand padding and the formatting of the number if the leading character
1522 of the result is not already a zero.
1523
1524(2)
1525 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
1526 the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
1527 and the formatting of the number if the leading character of the result is not
1528 already a zero.
1529
1530(3)
1531 The alternate form causes the result to always contain a decimal point, even if
1532 no digits follow it.
1533
1534 The precision determines the number of digits after the decimal point and
1535 defaults to 6.
1536
1537(4)
1538 The alternate form causes the result to always contain a decimal point, and
1539 trailing zeroes are not removed as they would otherwise be.
1540
1541 The precision determines the number of significant digits before and after the
1542 decimal point and defaults to 6.
1543
1544(5)
Georg Brandl116aa622007-08-15 14:28:22 +00001545 The precision determines the maximal number of characters used.
1546
Georg Brandl116aa622007-08-15 14:28:22 +00001547
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001548(7)
1549 See :pep:`237`.
1550
Georg Brandl116aa622007-08-15 14:28:22 +00001551Since Python strings have an explicit length, ``%s`` conversions do not assume
1552that ``'\0'`` is the end of the string.
1553
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001554.. XXX Examples?
1555
Mark Dickinson33841c32009-05-01 15:37:04 +00001556.. versionchanged:: 3.1
1557 ``%f`` conversions for numbers whose absolute value is over 1e50 are no
1558 longer replaced by ``%g`` conversions.
Georg Brandl116aa622007-08-15 14:28:22 +00001559
1560.. index::
1561 module: string
1562 module: re
1563
1564Additional string operations are defined in standard modules :mod:`string` and
1565:mod:`re`.
1566
1567
1568.. _typesseq-range:
1569
Georg Brandl905ec322007-09-28 13:39:25 +00001570Range Type
1571----------
Georg Brandl116aa622007-08-15 14:28:22 +00001572
1573.. index:: object: range
1574
1575The :class:`range` type is an immutable sequence which is commonly used for
1576looping. The advantage of the :class:`range` type is that an :class:`range`
1577object will always take the same amount of memory, no matter the size of the
1578range it represents. There are no consistent performance advantages.
1579
Daniel Stutzbach9f0cbf12010-09-13 21:16:29 +00001580Range objects have relatively little behavior: they support indexing,
1581iteration, the :func:`len` function, and the following methods.
Georg Brandl116aa622007-08-15 14:28:22 +00001582
Daniel Stutzbach9f0cbf12010-09-13 21:16:29 +00001583.. method:: range.count(x)
1584
1585 Return the number of *i*'s for which ``s[i] == x``. Normally the
1586 result will be 0 or 1, but it could be greater if *x* defines an
1587 unusual equality function.
1588
1589 .. versionadded:: 3.2
1590
1591.. method:: range.index(x)
1592
1593 Return the smallest *i* such that ``s[i] == x``. Raises
1594 :exc:`ValueError` when *x* is not in the range.
1595
1596 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +00001597
1598.. _typesseq-mutable:
1599
1600Mutable Sequence Types
1601----------------------
1602
1603.. index::
1604 triple: mutable; sequence; types
1605 object: list
Georg Brandl95414632007-11-22 11:00:28 +00001606 object: bytearray
Georg Brandl116aa622007-08-15 14:28:22 +00001607
Georg Brandl95414632007-11-22 11:00:28 +00001608List and bytearray objects support additional operations that allow in-place
Georg Brandl226878c2007-08-31 10:15:37 +00001609modification of the object. Other mutable sequence types (when added to the
1610language) should also support these operations. Strings and tuples are
1611immutable sequence types: such objects cannot be modified once created. The
1612following operations are defined on mutable sequence types (where *x* is an
1613arbitrary object).
1614
Georg Brandl95414632007-11-22 11:00:28 +00001615Note that while lists allow their items to be of any type, bytearray object
Georg Brandl226878c2007-08-31 10:15:37 +00001616"items" are all integers in the range 0 <= x < 256.
Georg Brandl116aa622007-08-15 14:28:22 +00001617
Senthil Kumaran7cafd262010-10-02 03:16:04 +00001618.. index::
1619 triple: operations on; sequence; types
1620 triple: operations on; list; type
1621 pair: subscript; assignment
1622 pair: slice; assignment
1623 statement: del
1624 single: append() (sequence method)
1625 single: extend() (sequence method)
1626 single: count() (sequence method)
1627 single: index() (sequence method)
1628 single: insert() (sequence method)
1629 single: pop() (sequence method)
1630 single: remove() (sequence method)
1631 single: reverse() (sequence method)
1632 single: sort() (sequence method)
1633
Georg Brandl116aa622007-08-15 14:28:22 +00001634+------------------------------+--------------------------------+---------------------+
1635| Operation | Result | Notes |
1636+==============================+================================+=====================+
1637| ``s[i] = x`` | item *i* of *s* is replaced by | |
1638| | *x* | |
1639+------------------------------+--------------------------------+---------------------+
1640| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1641| | is replaced by the contents of | |
1642| | the iterable *t* | |
1643+------------------------------+--------------------------------+---------------------+
1644| ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1645+------------------------------+--------------------------------+---------------------+
1646| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1647| | are replaced by those of *t* | |
1648+------------------------------+--------------------------------+---------------------+
1649| ``del s[i:j:k]`` | removes the elements of | |
1650| | ``s[i:j:k]`` from the list | |
1651+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001652| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |
Georg Brandl116aa622007-08-15 14:28:22 +00001653| | [x]`` | |
1654+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001655| ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001656| | x`` | |
1657+------------------------------+--------------------------------+---------------------+
1658| ``s.count(x)`` | return number of *i*'s for | |
1659| | which ``s[i] == x`` | |
1660+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001661| ``s.index(x[, i[, j]])`` | return smallest *k* such that | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001662| | ``s[k] == x`` and ``i <= k < | |
1663| | j`` | |
1664+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001665| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +00001666+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001667| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00001668| | return x`` | |
1669+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001670| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001671+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001672| ``s.reverse()`` | reverses the items of *s* in | \(6) |
Georg Brandl116aa622007-08-15 14:28:22 +00001673| | place | |
1674+------------------------------+--------------------------------+---------------------+
Raymond Hettinger7f732952008-02-14 13:34:38 +00001675| ``s.sort([key[, reverse]])`` | sort the items of *s* in place | (6), (7), (8) |
Georg Brandl116aa622007-08-15 14:28:22 +00001676+------------------------------+--------------------------------+---------------------+
1677
Georg Brandl116aa622007-08-15 14:28:22 +00001678
1679Notes:
1680
1681(1)
Georg Brandl226878c2007-08-31 10:15:37 +00001682 *t* must have the same length as the slice it is replacing.
Georg Brandl116aa622007-08-15 14:28:22 +00001683
1684(2)
Georg Brandl116aa622007-08-15 14:28:22 +00001685 *x* can be any iterable object.
1686
Georg Brandl226878c2007-08-31 10:15:37 +00001687(3)
Georg Brandl116aa622007-08-15 14:28:22 +00001688 Raises :exc:`ValueError` when *x* is not found in *s*. When a negative index is
Georg Brandl226878c2007-08-31 10:15:37 +00001689 passed as the second or third parameter to the :meth:`index` method, the sequence
Georg Brandl116aa622007-08-15 14:28:22 +00001690 length is added, as for slice indices. If it is still negative, it is truncated
1691 to zero, as for slice indices.
1692
Georg Brandl226878c2007-08-31 10:15:37 +00001693(4)
Georg Brandl116aa622007-08-15 14:28:22 +00001694 When a negative index is passed as the first parameter to the :meth:`insert`
Georg Brandl226878c2007-08-31 10:15:37 +00001695 method, the sequence length is added, as for slice indices. If it is still
Georg Brandl116aa622007-08-15 14:28:22 +00001696 negative, it is truncated to zero, as for slice indices.
1697
Georg Brandl226878c2007-08-31 10:15:37 +00001698(5)
1699 The optional argument *i* defaults to ``-1``, so that by default the last
1700 item is removed and returned.
1701
Georg Brandl116aa622007-08-15 14:28:22 +00001702(6)
Georg Brandl226878c2007-08-31 10:15:37 +00001703 The :meth:`sort` and :meth:`reverse` methods modify the sequence in place for
1704 economy of space when sorting or reversing a large sequence. To remind you
1705 that they operate by side effect, they don't return the sorted or reversed
1706 sequence.
Georg Brandl116aa622007-08-15 14:28:22 +00001707
1708(7)
Georg Brandl116aa622007-08-15 14:28:22 +00001709 The :meth:`sort` method takes optional arguments for controlling the
Raymond Hettinger7f732952008-02-14 13:34:38 +00001710 comparisons. Each must be specified as a keyword argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001711
Georg Brandl116aa622007-08-15 14:28:22 +00001712 *key* specifies a function of one argument that is used to extract a comparison
Christian Heimesfaf2f632008-01-06 16:59:19 +00001713 key from each list element: ``key=str.lower``. The default value is ``None``.
Raymond Hettingerc50846a2010-04-05 18:56:31 +00001714 Use :func:`functools.cmp_to_key` to convert an
1715 old-style *cmp* function to a *key* function.
1716
Georg Brandl116aa622007-08-15 14:28:22 +00001717
1718 *reverse* is a boolean value. If set to ``True``, then the list elements are
1719 sorted as if each comparison were reversed.
1720
Raymond Hettinger71161862008-02-14 13:32:18 +00001721 The :meth:`sort` method is guaranteed to be stable. A
Georg Brandl116aa622007-08-15 14:28:22 +00001722 sort is stable if it guarantees not to change the relative order of elements
1723 that compare equal --- this is helpful for sorting in multiple passes (for
1724 example, sort by department, then by salary grade).
1725
Georg Brandl495f7b52009-10-27 15:28:25 +00001726 .. impl-detail::
1727
1728 While a list is being sorted, the effect of attempting to mutate, or even
1729 inspect, the list is undefined. The C implementation of Python makes the
1730 list appear empty for the duration, and raises :exc:`ValueError` if it can
1731 detect that the list has been mutated during a sort.
Georg Brandl116aa622007-08-15 14:28:22 +00001732
Raymond Hettinger7f732952008-02-14 13:34:38 +00001733(8)
1734 :meth:`sort` is not supported by :class:`bytearray` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001735
Georg Brandl495f7b52009-10-27 15:28:25 +00001736
Georg Brandl226878c2007-08-31 10:15:37 +00001737.. _bytes-methods:
1738
Georg Brandl95414632007-11-22 11:00:28 +00001739Bytes and Byte Array Methods
1740----------------------------
Georg Brandl226878c2007-08-31 10:15:37 +00001741
1742.. index:: pair: bytes; methods
Georg Brandl95414632007-11-22 11:00:28 +00001743 pair: bytearray; methods
Georg Brandl226878c2007-08-31 10:15:37 +00001744
Georg Brandl95414632007-11-22 11:00:28 +00001745Bytes and bytearray objects, being "strings of bytes", have all methods found on
Georg Brandl7c676132007-10-23 18:17:00 +00001746strings, with the exception of :func:`encode`, :func:`format` and
Guido van Rossum98297ee2007-11-06 21:34:58 +00001747:func:`isidentifier`, which do not make sense with these types. For converting
1748the objects to strings, they have a :func:`decode` method.
1749
1750Wherever one of these methods needs to interpret the bytes as characters
1751(e.g. the :func:`is...` methods), the ASCII character set is assumed.
Georg Brandl226878c2007-08-31 10:15:37 +00001752
Georg Brandl7c676132007-10-23 18:17:00 +00001753.. note::
Georg Brandl226878c2007-08-31 10:15:37 +00001754
Georg Brandl95414632007-11-22 11:00:28 +00001755 The methods on bytes and bytearray objects don't accept strings as their
Georg Brandl7c676132007-10-23 18:17:00 +00001756 arguments, just as the methods on strings don't accept bytes as their
1757 arguments. For example, you have to write ::
Georg Brandl226878c2007-08-31 10:15:37 +00001758
Georg Brandl7c676132007-10-23 18:17:00 +00001759 a = "abc"
1760 b = a.replace("a", "f")
1761
1762 and ::
1763
1764 a = b"abc"
1765 b = a.replace(b"a", b"f")
Georg Brandl226878c2007-08-31 10:15:37 +00001766
1767
Victor Stinnere14e2122010-11-07 18:41:46 +00001768.. method:: bytes.decode(encoding="utf-8", errors="strict")
1769 bytearray.decode(encoding="utf-8", errors="strict")
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001770
Victor Stinnere14e2122010-11-07 18:41:46 +00001771 Return a string decoded from the given bytes. Default encoding is
1772 ``'utf-8'``. *errors* may be given to set a different
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001773 error handling scheme. The default for *errors* is ``'strict'``, meaning
1774 that encoding errors raise a :exc:`UnicodeError`. Other possible values are
1775 ``'ignore'``, ``'replace'`` and any other name registered via
1776 :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a
1777 list of possible encodings, see section :ref:`standard-encodings`.
1778
Benjamin Peterson308d6372009-09-18 21:42:35 +00001779 .. versionchanged:: 3.1
1780 Added support for keyword arguments.
1781
Georg Brandl4f5f98d2009-05-04 21:01:20 +00001782
Georg Brandl95414632007-11-22 11:00:28 +00001783The bytes and bytearray types have an additional class method:
Georg Brandl226878c2007-08-31 10:15:37 +00001784
Georg Brandlabc38772009-04-12 15:51:51 +00001785.. classmethod:: bytes.fromhex(string)
1786 bytearray.fromhex(string)
Georg Brandl226878c2007-08-31 10:15:37 +00001787
Georg Brandl18da8f02008-07-01 20:08:02 +00001788 This :class:`bytes` class method returns a bytes or bytearray object,
1789 decoding the given string object. The string must contain two hexadecimal
1790 digits per byte, spaces are ignored.
Georg Brandl226878c2007-08-31 10:15:37 +00001791
Georg Brandl18da8f02008-07-01 20:08:02 +00001792 >>> bytes.fromhex('f0 f1f2 ')
1793 b'\xf0\xf1\xf2'
Georg Brandl226878c2007-08-31 10:15:37 +00001794
Georg Brandlabc38772009-04-12 15:51:51 +00001795
1796The maketrans and translate methods differ in semantics from the versions
1797available on strings:
Georg Brandl48310cd2009-01-03 21:18:54 +00001798
Georg Brandl454636f2008-12-27 23:33:20 +00001799.. method:: bytes.translate(table[, delete])
Georg Brandl751771b2009-05-31 21:38:37 +00001800 bytearray.translate(table[, delete])
Georg Brandl226878c2007-08-31 10:15:37 +00001801
Georg Brandl454636f2008-12-27 23:33:20 +00001802 Return a copy of the bytes or bytearray object where all bytes occurring in
1803 the optional argument *delete* are removed, and the remaining bytes have been
1804 mapped through the given translation table, which must be a bytes object of
1805 length 256.
Georg Brandl226878c2007-08-31 10:15:37 +00001806
Georg Brandlabc38772009-04-12 15:51:51 +00001807 You can use the :func:`bytes.maketrans` method to create a translation table.
Georg Brandl226878c2007-08-31 10:15:37 +00001808
Georg Brandl454636f2008-12-27 23:33:20 +00001809 Set the *table* argument to ``None`` for translations that only delete
1810 characters::
Georg Brandl226878c2007-08-31 10:15:37 +00001811
Georg Brandl454636f2008-12-27 23:33:20 +00001812 >>> b'read this short text'.translate(None, b'aeiou')
1813 b'rd ths shrt txt'
Georg Brandl226878c2007-08-31 10:15:37 +00001814
1815
Georg Brandlabc38772009-04-12 15:51:51 +00001816.. staticmethod:: bytes.maketrans(from, to)
Georg Brandl751771b2009-05-31 21:38:37 +00001817 bytearray.maketrans(from, to)
Georg Brandlabc38772009-04-12 15:51:51 +00001818
1819 This static method returns a translation table usable for
1820 :meth:`bytes.translate` that will map each character in *from* into the
1821 character at the same position in *to*; *from* and *to* must be bytes objects
1822 and have the same length.
1823
1824 .. versionadded:: 3.1
1825
1826
Georg Brandl116aa622007-08-15 14:28:22 +00001827.. _types-set:
1828
1829Set Types --- :class:`set`, :class:`frozenset`
1830==============================================
1831
1832.. index:: object: set
1833
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001834A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001835Common uses include membership testing, removing duplicates from a sequence, and
1836computing mathematical operations such as intersection, union, difference, and
1837symmetric difference.
1838(For other containers see the built in :class:`dict`, :class:`list`,
1839and :class:`tuple` classes, and the :mod:`collections` module.)
1840
Georg Brandl116aa622007-08-15 14:28:22 +00001841Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
1842set``. Being an unordered collection, sets do not record element position or
1843order of insertion. Accordingly, sets do not support indexing, slicing, or
1844other sequence-like behavior.
1845
Georg Brandl22b34312009-07-26 14:54:51 +00001846There are currently two built-in set types, :class:`set` and :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00001847The :class:`set` type is mutable --- the contents can be changed using methods
1848like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value
1849and cannot be used as either a dictionary key or as an element of another set.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001850The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be
Georg Brandl116aa622007-08-15 14:28:22 +00001851altered after it is created; it can therefore be used as a dictionary key or as
1852an element of another set.
1853
Georg Brandl99cd9572010-03-21 09:10:32 +00001854Non-empty sets (not frozensets) can be created by placing a comma-separated list
Georg Brandl53b95e72010-03-21 11:53:50 +00001855of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the
1856:class:`set` constructor.
Georg Brandl99cd9572010-03-21 09:10:32 +00001857
Georg Brandl116aa622007-08-15 14:28:22 +00001858The constructors for both classes work the same:
1859
1860.. class:: set([iterable])
1861 frozenset([iterable])
1862
1863 Return a new set or frozenset object whose elements are taken from
1864 *iterable*. The elements of a set must be hashable. To represent sets of
1865 sets, the inner sets must be :class:`frozenset` objects. If *iterable* is
1866 not specified, a new empty set is returned.
1867
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001868 Instances of :class:`set` and :class:`frozenset` provide the following
1869 operations:
Georg Brandl116aa622007-08-15 14:28:22 +00001870
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001871 .. describe:: len(s)
Georg Brandl116aa622007-08-15 14:28:22 +00001872
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001873 Return the cardinality of set *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001874
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001875 .. describe:: x in s
Georg Brandl116aa622007-08-15 14:28:22 +00001876
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001877 Test *x* for membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001878
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001879 .. describe:: x not in s
Georg Brandl116aa622007-08-15 14:28:22 +00001880
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001881 Test *x* for non-membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001882
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001883 .. method:: isdisjoint(other)
Guido van Rossum58da9312007-11-10 23:39:45 +00001884
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001885 Return True if the set has no elements in common with *other*. Sets are
Georg Brandl2ee470f2008-07-16 12:55:28 +00001886 disjoint if and only if their intersection is the empty set.
Guido van Rossum58da9312007-11-10 23:39:45 +00001887
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001888 .. method:: issubset(other)
1889 set <= other
Georg Brandl116aa622007-08-15 14:28:22 +00001890
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001891 Test whether every element in the set is in *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00001892
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001893 .. method:: set < other
Georg Brandla6f52782007-09-01 15:49:30 +00001894
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001895 Test whether the set is a true subset of *other*, that is,
1896 ``set <= other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00001897
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001898 .. method:: issuperset(other)
1899 set >= other
Georg Brandl116aa622007-08-15 14:28:22 +00001900
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001901 Test whether every element in *other* is in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001902
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001903 .. method:: set > other
Georg Brandla6f52782007-09-01 15:49:30 +00001904
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001905 Test whether the set is a true superset of *other*, that is, ``set >=
1906 other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00001907
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001908 .. method:: union(other, ...)
1909 set | other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001910
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001911 Return a new set with elements from the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00001912
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001913 .. method:: intersection(other, ...)
1914 set & other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00001915
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001916 Return a new set with elements common to the set and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00001917
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001918 .. method:: difference(other, ...)
1919 set - other - ...
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001920
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001921 Return a new set with elements in the set that are not in the others.
Georg Brandl116aa622007-08-15 14:28:22 +00001922
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001923 .. method:: symmetric_difference(other)
1924 set ^ other
Georg Brandl116aa622007-08-15 14:28:22 +00001925
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001926 Return a new set with elements in either the set or *other* but not both.
Georg Brandl116aa622007-08-15 14:28:22 +00001927
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001928 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00001929
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001930 Return a new set with a shallow copy of *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001931
1932
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001933 Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
1934 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
1935 :meth:`issuperset` methods will accept any iterable as an argument. In
1936 contrast, their operator based counterparts require their arguments to be
1937 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
1938 in favor of the more readable ``set('abc').intersection('cbs')``.
Georg Brandl116aa622007-08-15 14:28:22 +00001939
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001940 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
1941 sets are equal if and only if every element of each set is contained in the
1942 other (each is a subset of the other). A set is less than another set if and
1943 only if the first set is a proper subset of the second set (is a subset, but
1944 is not equal). A set is greater than another set if and only if the first set
1945 is a proper superset of the second set (is a superset, but is not equal).
Georg Brandl116aa622007-08-15 14:28:22 +00001946
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001947 Instances of :class:`set` are compared to instances of :class:`frozenset`
1948 based on their members. For example, ``set('abc') == frozenset('abc')``
1949 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
Georg Brandl116aa622007-08-15 14:28:22 +00001950
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001951 The subset and equality comparisons do not generalize to a complete ordering
1952 function. For example, any two disjoint sets are not equal and are not
1953 subsets of each other, so *all* of the following return ``False``: ``a<b``,
Georg Brandl05f5ab72008-09-24 09:11:47 +00001954 ``a==b``, or ``a>b``.
Georg Brandl116aa622007-08-15 14:28:22 +00001955
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001956 Since sets only define partial ordering (subset relationships), the output of
1957 the :meth:`list.sort` method is undefined for lists of sets.
Georg Brandl116aa622007-08-15 14:28:22 +00001958
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001959 Set elements, like dictionary keys, must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +00001960
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001961 Binary operations that mix :class:`set` instances with :class:`frozenset`
1962 return the type of the first operand. For example: ``frozenset('ab') |
1963 set('bc')`` returns an instance of :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00001964
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001965 The following table lists operations available for :class:`set` that do not
1966 apply to immutable instances of :class:`frozenset`:
Georg Brandl116aa622007-08-15 14:28:22 +00001967
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001968 .. method:: update(other, ...)
1969 set |= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001970
Georg Brandla6053b42009-09-01 08:11:14 +00001971 Update the set, adding elements from all others.
Georg Brandl116aa622007-08-15 14:28:22 +00001972
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001973 .. method:: intersection_update(other, ...)
1974 set &= other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00001975
Georg Brandla6053b42009-09-01 08:11:14 +00001976 Update the set, keeping only elements found in it and all others.
Georg Brandl116aa622007-08-15 14:28:22 +00001977
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001978 .. method:: difference_update(other, ...)
1979 set -= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001980
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001981 Update the set, removing elements found in others.
1982
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001983 .. method:: symmetric_difference_update(other)
1984 set ^= other
Georg Brandl116aa622007-08-15 14:28:22 +00001985
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001986 Update the set, keeping only elements found in either set, but not in both.
Georg Brandl116aa622007-08-15 14:28:22 +00001987
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001988 .. method:: add(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001989
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001990 Add element *elem* to the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001991
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001992 .. method:: remove(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001993
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001994 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
1995 not contained in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001996
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001997 .. method:: discard(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001998
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001999 Remove element *elem* from the set if it is present.
Georg Brandl116aa622007-08-15 14:28:22 +00002000
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002001 .. method:: pop()
Georg Brandl116aa622007-08-15 14:28:22 +00002002
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002003 Remove and return an arbitrary element from the set. Raises
2004 :exc:`KeyError` if the set is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00002005
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002006 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00002007
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002008 Remove all elements from the set.
Georg Brandl116aa622007-08-15 14:28:22 +00002009
2010
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002011 Note, the non-operator versions of the :meth:`update`,
2012 :meth:`intersection_update`, :meth:`difference_update`, and
2013 :meth:`symmetric_difference_update` methods will accept any iterable as an
2014 argument.
Georg Brandl116aa622007-08-15 14:28:22 +00002015
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002016 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
2017 :meth:`discard` methods may be a set. To support searching for an equivalent
2018 frozenset, the *elem* set is temporarily mutated during the search and then
2019 restored. During the search, the *elem* set should not be read or mutated
2020 since it does not have a meaningful value.
Benjamin Peterson699adb92008-05-08 22:27:58 +00002021
Georg Brandl116aa622007-08-15 14:28:22 +00002022
2023.. _typesmapping:
2024
2025Mapping Types --- :class:`dict`
2026===============================
2027
2028.. index::
2029 object: mapping
2030 object: dictionary
2031 triple: operations on; mapping; types
2032 triple: operations on; dictionary; type
2033 statement: del
2034 builtin: len
2035
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002036A :dfn:`mapping` object maps :term:`hashable` values to arbitrary objects.
2037Mappings are mutable objects. There is currently only one standard mapping
2038type, the :dfn:`dictionary`. (For other containers see the built in
2039:class:`list`, :class:`set`, and :class:`tuple` classes, and the
2040:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +00002041
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002042A dictionary's keys are *almost* arbitrary values. Values that are not
2043:term:`hashable`, that is, values containing lists, dictionaries or other
2044mutable types (that are compared by value rather than by object identity) may
2045not be used as keys. Numeric types used for keys obey the normal rules for
2046numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
2047then they can be used interchangeably to index the same dictionary entry. (Note
2048however, that since computers store floating-point numbers as approximations it
2049is usually unwise to use them as dictionary keys.)
Georg Brandl116aa622007-08-15 14:28:22 +00002050
2051Dictionaries can be created by placing a comma-separated list of ``key: value``
2052pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
2053'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
2054
2055.. class:: dict([arg])
2056
Georg Brandld22a8152007-09-04 17:43:37 +00002057 Return a new dictionary initialized from an optional positional argument or
2058 from a set of keyword arguments. If no arguments are given, return a new
2059 empty dictionary. If the positional argument *arg* is a mapping object,
2060 return a dictionary mapping the same keys to the same values as does the
2061 mapping object. Otherwise the positional argument must be a sequence, a
2062 container that supports iteration, or an iterator object. The elements of
2063 the argument must each also be of one of those kinds, and each must in turn
2064 contain exactly two objects. The first is used as a key in the new
2065 dictionary, and the second as the key's value. If a given key is seen more
2066 than once, the last value associated with it is retained in the new
2067 dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00002068
2069 If keyword arguments are given, the keywords themselves with their associated
Georg Brandld22a8152007-09-04 17:43:37 +00002070 values are added as items to the dictionary. If a key is specified both in
2071 the positional argument and as a keyword argument, the value associated with
2072 the keyword is retained in the dictionary. For example, these all return a
Georg Brandlc16e8f12010-10-17 11:23:56 +00002073 dictionary equal to ``{"one": 1, "two": 2}``:
Georg Brandl116aa622007-08-15 14:28:22 +00002074
Georg Brandlc16e8f12010-10-17 11:23:56 +00002075 * ``dict(one=1, two=2)``
2076 * ``dict({'one': 1, 'two': 2})``
2077 * ``dict(zip(('one', 'two'), (1, 2)))``
2078 * ``dict([['two', 2], ['one', 1]])``
Georg Brandl116aa622007-08-15 14:28:22 +00002079
Georg Brandld22a8152007-09-04 17:43:37 +00002080 The first example only works for keys that are valid Python identifiers; the
2081 others work with any valid keys.
Georg Brandl116aa622007-08-15 14:28:22 +00002082
Georg Brandl116aa622007-08-15 14:28:22 +00002083
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002084 These are the operations that dictionaries support (and therefore, custom
2085 mapping types should support too):
Georg Brandl116aa622007-08-15 14:28:22 +00002086
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002087 .. describe:: len(d)
Georg Brandl116aa622007-08-15 14:28:22 +00002088
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002089 Return the number of items in the dictionary *d*.
Georg Brandl116aa622007-08-15 14:28:22 +00002090
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002091 .. describe:: d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00002092
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002093 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
2094 not in the map.
Georg Brandl48310cd2009-01-03 21:18:54 +00002095
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002096 If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
2097 is not present, the ``d[key]`` operation calls that method with the key *key*
2098 as argument. The ``d[key]`` operation then returns or raises whatever is
2099 returned or raised by the ``__missing__(key)`` call if the key is not
2100 present. No other operations or methods invoke :meth:`__missing__`. If
2101 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
2102 :meth:`__missing__` must be a method; it cannot be an instance variable. For
2103 an example, see :class:`collections.defaultdict`.
Georg Brandl116aa622007-08-15 14:28:22 +00002104
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002105 .. describe:: d[key] = value
Georg Brandl116aa622007-08-15 14:28:22 +00002106
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002107 Set ``d[key]`` to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00002108
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002109 .. describe:: del d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00002110
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002111 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
2112 map.
Georg Brandl116aa622007-08-15 14:28:22 +00002113
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002114 .. describe:: key in d
Georg Brandl116aa622007-08-15 14:28:22 +00002115
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002116 Return ``True`` if *d* has a key *key*, else ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00002117
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002118 .. describe:: key not in d
Georg Brandl116aa622007-08-15 14:28:22 +00002119
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002120 Equivalent to ``not key in d``.
Georg Brandl116aa622007-08-15 14:28:22 +00002121
Benjamin Petersond23f8222009-04-05 19:13:16 +00002122 .. describe:: iter(d)
2123
2124 Return an iterator over the keys of the dictionary. This is a shortcut
Georg Brandlede6c2a2010-01-05 10:22:04 +00002125 for ``iter(d.keys())``.
Benjamin Petersond23f8222009-04-05 19:13:16 +00002126
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002127 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00002128
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002129 Remove all items from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00002130
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002131 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00002132
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002133 Return a shallow copy of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00002134
Georg Brandlabc38772009-04-12 15:51:51 +00002135 .. classmethod:: fromkeys(seq[, value])
Georg Brandl116aa622007-08-15 14:28:22 +00002136
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002137 Create a new dictionary with keys from *seq* and values set to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00002138
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002139 :meth:`fromkeys` is a class method that returns a new dictionary. *value*
2140 defaults to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00002141
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002142 .. method:: get(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00002143
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002144 Return the value for *key* if *key* is in the dictionary, else *default*.
2145 If *default* is not given, it defaults to ``None``, so that this method
2146 never raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00002147
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002148 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +00002149
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002150 Return a new view of the dictionary's items (``(key, value)`` pairs). See
2151 below for documentation of view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00002152
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002153 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +00002154
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002155 Return a new view of the dictionary's keys. See below for documentation of
2156 view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00002157
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002158 .. method:: pop(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00002159
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002160 If *key* is in the dictionary, remove it and return its value, else return
2161 *default*. If *default* is not given and *key* is not in the dictionary,
2162 a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00002163
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002164 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +00002165
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002166 Remove and return an arbitrary ``(key, value)`` pair from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00002167
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002168 :meth:`popitem` is useful to destructively iterate over a dictionary, as
2169 often used in set algorithms. If the dictionary is empty, calling
2170 :meth:`popitem` raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00002171
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002172 .. method:: setdefault(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00002173
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002174 If *key* is in the dictionary, return its value. If not, insert *key*
2175 with a value of *default* and return *default*. *default* defaults to
2176 ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00002177
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002178 .. method:: update([other])
Georg Brandl116aa622007-08-15 14:28:22 +00002179
Éric Araujo0fc86b82010-08-18 22:29:54 +00002180 Update the dictionary with the key/value pairs from *other*, overwriting
2181 existing keys. Return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00002182
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002183 :meth:`update` accepts either another dictionary object or an iterable of
Georg Brandlfda21062010-09-25 16:56:36 +00002184 key/value pairs (as tuples or other iterables of length two). If keyword
Benjamin Peterson8719ad52009-09-11 22:24:02 +00002185 arguments are specified, the dictionary is then updated with those
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002186 key/value pairs: ``d.update(red=1, blue=2)``.
Georg Brandl116aa622007-08-15 14:28:22 +00002187
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002188 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +00002189
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00002190 Return a new view of the dictionary's values. See below for documentation of
2191 view objects.
Georg Brandld22a8152007-09-04 17:43:37 +00002192
2193
Benjamin Peterson44309e62008-11-22 00:41:45 +00002194.. _dict-views:
2195
Georg Brandld22a8152007-09-04 17:43:37 +00002196Dictionary view objects
2197-----------------------
2198
2199The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
2200:meth:`dict.items` are *view objects*. They provide a dynamic view on the
2201dictionary's entries, which means that when the dictionary changes, the view
Benjamin Petersonce0506c2008-11-17 21:47:41 +00002202reflects these changes.
Georg Brandld22a8152007-09-04 17:43:37 +00002203
2204Dictionary views can be iterated over to yield their respective data, and
2205support membership tests:
2206
2207.. describe:: len(dictview)
2208
2209 Return the number of entries in the dictionary.
2210
2211.. describe:: iter(dictview)
2212
2213 Return an iterator over the keys, values or items (represented as tuples of
2214 ``(key, value)``) in the dictionary.
2215
2216 Keys and values are iterated over in an arbitrary order which is non-random,
2217 varies across Python implementations, and depends on the dictionary's history
2218 of insertions and deletions. If keys, values and items views are iterated
2219 over with no intervening modifications to the dictionary, the order of items
2220 will directly correspond. This allows the creation of ``(value, key)`` pairs
2221 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
2222 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
2223
Georg Brandl81269142009-05-17 08:31:29 +00002224 Iterating views while adding or deleting entries in the dictionary may raise
2225 a :exc:`RuntimeError` or fail to iterate over all entries.
Benjamin Petersond23f8222009-04-05 19:13:16 +00002226
Georg Brandld22a8152007-09-04 17:43:37 +00002227.. describe:: x in dictview
2228
2229 Return ``True`` if *x* is in the underlying dictionary's keys, values or
2230 items (in the latter case, *x* should be a ``(key, value)`` tuple).
2231
2232
Benjamin Petersonce0506c2008-11-17 21:47:41 +00002233Keys views are set-like since their entries are unique and hashable. If all
Georg Brandlf74cf772010-10-15 16:03:02 +00002234values are hashable, so that ``(key, value)`` pairs are unique and hashable,
2235then the items view is also set-like. (Values views are not treated as set-like
2236since the entries are generally not unique.) For set-like views, all of the
2237operations defined for the abstract base class :class:`collections.Set` are
2238available (for example, ``==``, ``<``, or ``^``).
Georg Brandl116aa622007-08-15 14:28:22 +00002239
Georg Brandlc53c9662007-09-04 17:58:02 +00002240An example of dictionary view usage::
2241
2242 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
2243 >>> keys = dishes.keys()
2244 >>> values = dishes.values()
2245
2246 >>> # iteration
2247 >>> n = 0
2248 >>> for val in values:
2249 ... n += val
2250 >>> print(n)
2251 504
2252
2253 >>> # keys and values are iterated over in the same order
2254 >>> list(keys)
2255 ['eggs', 'bacon', 'sausage', 'spam']
2256 >>> list(values)
2257 [2, 1, 1, 500]
2258
2259 >>> # view objects are dynamic and reflect dict changes
2260 >>> del dishes['eggs']
2261 >>> del dishes['sausage']
2262 >>> list(keys)
2263 ['spam', 'bacon']
2264
2265 >>> # set operations
2266 >>> keys & {'eggs', 'bacon', 'salad'}
Gregory P. Smithe8388122008-09-04 04:18:09 +00002267 {'bacon'}
Georg Brandlf74cf772010-10-15 16:03:02 +00002268 >>> keys ^ {'sausage', 'juice'}
2269 {'juice', 'eggs', 'bacon', 'spam'}
Georg Brandlc53c9662007-09-04 17:58:02 +00002270
2271
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002272.. _typememoryview:
2273
Benjamin Peterson08bf91c2010-04-11 16:12:57 +00002274memoryview type
2275===============
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002276
Benjamin Peterson08bf91c2010-04-11 16:12:57 +00002277:class:`memoryview` objects allow Python code to access the internal data
2278of an object that supports the buffer protocol without copying. Memory
2279is generally interpreted as simple bytes.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002280
2281.. class:: memoryview(obj)
2282
Georg Brandl1009d392008-09-10 07:14:18 +00002283 Create a :class:`memoryview` that references *obj*. *obj* must support the
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002284 buffer protocol. Builtin objects that support the buffer protocol include
2285 :class:`bytes` and :class:`bytearray`.
2286
Antoine Pitrouc7795152010-07-12 20:01:52 +00002287 A :class:`memoryview` has the notion of an *element*, which is the
2288 atomic memory unit handled by the originating object *obj*. For many
2289 simple types such as :class:`bytes` and :class:`bytearray`, an element
2290 is a single byte, but other types such as :class:`array.array` may have
2291 bigger elements.
2292
2293 ``len(view)`` returns the total number of elements in the memoryview,
2294 *view*. The :class:`~memoryview.itemsize` attribute will give you the
2295 number of bytes in a single element.
Benjamin Peterson5e19e442008-09-10 21:47:03 +00002296
Georg Brandl1009d392008-09-10 07:14:18 +00002297 A :class:`memoryview` supports slicing to expose its data. Taking a single
Antoine Pitrouc7795152010-07-12 20:01:52 +00002298 index will return a single element as a :class:`bytes` object. Full
2299 slicing will result in a subview::
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002300
2301 >>> v = memoryview(b'abcefg')
2302 >>> v[1]
2303 b'b'
2304 >>> v[-1]
2305 b'g'
2306 >>> v[1:4]
2307 <memory at 0x77ab28>
2308 >>> bytes(v[1:4])
2309 b'bce'
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002310
Antoine Pitrouc7795152010-07-12 20:01:52 +00002311 If the object the memoryview is over supports changing its data, the
Georg Brandl1009d392008-09-10 07:14:18 +00002312 memoryview supports slice assignment::
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002313
2314 >>> data = bytearray(b'abcefg')
2315 >>> v = memoryview(data)
2316 >>> v.readonly
2317 False
Benjamin Peterson52504012009-06-23 03:09:33 +00002318 >>> v[0] = b'z'
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002319 >>> data
2320 bytearray(b'zbcefg')
2321 >>> v[1:4] = b'123'
2322 >>> data
2323 bytearray(b'a123fg')
2324 >>> v[2] = b'spam'
2325 Traceback (most recent call last):
2326 File "<stdin>", line 1, in <module>
2327 ValueError: cannot modify size of memoryview object
2328
Benjamin Petersond7c3ed52010-06-27 22:32:30 +00002329 Notice how the size of the memoryview object cannot be changed.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002330
Antoine Pitrou6e6cc832010-09-09 12:59:39 +00002331 :class:`memoryview` has several methods:
2332
Georg Brandl47859162010-09-10 20:43:53 +00002333 .. method:: tobytes()
2334
2335 Return the data in the buffer as a bytestring. This is equivalent to
2336 calling the :class:`bytes` constructor on the memoryview. ::
2337
2338 >>> m = memoryview(b"abc")
2339 >>> m.tobytes()
2340 b'abc'
2341 >>> bytes(m)
2342 b'abc'
2343
2344 .. method:: tolist()
2345
2346 Return the data in the buffer as a list of integers. ::
2347
2348 >>> memoryview(b'abc').tolist()
2349 [97, 98, 99]
2350
Antoine Pitrou6e6cc832010-09-09 12:59:39 +00002351 .. method:: release()
2352
2353 Release the underlying buffer exposed by the memoryview object. Many
2354 objects take special actions when a view is held on them (for example,
2355 a :class:`bytearray` would temporarily forbid resizing); therefore,
2356 calling release() is handy to remove these restrictions (and free any
2357 dangling resources) as soon as possible.
2358
2359 After this method has been called, any further operation on the view
2360 raises a :class:`ValueError` (except :meth:`release()` itself which can
2361 be called multiple times)::
2362
2363 >>> m = memoryview(b'abc')
2364 >>> m.release()
2365 >>> m[0]
2366 Traceback (most recent call last):
2367 File "<stdin>", line 1, in <module>
2368 ValueError: operation forbidden on released memoryview object
2369
2370 The context management protocol can be used for a similar effect,
2371 using the ``with`` statement::
2372
2373 >>> with memoryview(b'abc') as m:
2374 ... m[0]
2375 ...
2376 b'a'
2377 >>> m[0]
2378 Traceback (most recent call last):
2379 File "<stdin>", line 1, in <module>
2380 ValueError: operation forbidden on released memoryview object
2381
2382 .. versionadded:: 3.2
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002383
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002384 There are also several readonly attributes available:
2385
2386 .. attribute:: format
2387
2388 A string containing the format (in :mod:`struct` module style) for each
2389 element in the view. This defaults to ``'B'``, a simple bytestring.
2390
2391 .. attribute:: itemsize
2392
Antoine Pitrouc7795152010-07-12 20:01:52 +00002393 The size in bytes of each element of the memoryview::
2394
2395 >>> m = memoryview(array.array('H', [1,2,3]))
2396 >>> m.itemsize
2397 2
2398 >>> m[0]
2399 b'\x01\x00'
2400 >>> len(m[0]) == m.itemsize
2401 True
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002402
2403 .. attribute:: shape
2404
Georg Brandl1009d392008-09-10 07:14:18 +00002405 A tuple of integers the length of :attr:`ndim` giving the shape of the
2406 memory as a N-dimensional array.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002407
2408 .. attribute:: ndim
2409
2410 An integer indicating how many dimensions of a multi-dimensional array the
2411 memory represents.
2412
2413 .. attribute:: strides
2414
Benjamin Peterson2409dc72008-09-10 21:38:31 +00002415 A tuple of integers the length of :attr:`ndim` giving the size in bytes to
2416 access each element for each dimension of the array.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002417
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002418 .. memoryview.suboffsets isn't documented because it only seems useful for C
2419
2420
Georg Brandl116aa622007-08-15 14:28:22 +00002421.. _typecontextmanager:
2422
2423Context Manager Types
2424=====================
2425
Georg Brandl116aa622007-08-15 14:28:22 +00002426.. index::
2427 single: context manager
2428 single: context management protocol
2429 single: protocol; context management
2430
2431Python's :keyword:`with` statement supports the concept of a runtime context
2432defined by a context manager. This is implemented using two separate methods
2433that allow user-defined classes to define a runtime context that is entered
2434before the statement body is executed and exited when the statement ends.
2435
2436The :dfn:`context management protocol` consists of a pair of methods that need
2437to be provided for a context manager object to define a runtime context:
2438
2439
2440.. method:: contextmanager.__enter__()
2441
2442 Enter the runtime context and return either this object or another object
2443 related to the runtime context. The value returned by this method is bound to
2444 the identifier in the :keyword:`as` clause of :keyword:`with` statements using
2445 this context manager.
2446
Antoine Pitrou11cb9612010-09-15 11:11:28 +00002447 An example of a context manager that returns itself is a :term:`file object`.
2448 File objects return themselves from __enter__() to allow :func:`open` to be
2449 used as the context expression in a :keyword:`with` statement.
Georg Brandl116aa622007-08-15 14:28:22 +00002450
2451 An example of a context manager that returns a related object is the one
Christian Heimesfaf2f632008-01-06 16:59:19 +00002452 returned by :func:`decimal.localcontext`. These managers set the active
Georg Brandl116aa622007-08-15 14:28:22 +00002453 decimal context to a copy of the original decimal context and then return the
2454 copy. This allows changes to be made to the current decimal context in the body
2455 of the :keyword:`with` statement without affecting code outside the
2456 :keyword:`with` statement.
2457
2458
2459.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
2460
Georg Brandl9afde1c2007-11-01 20:32:30 +00002461 Exit the runtime context and return a Boolean flag indicating if any exception
Georg Brandl116aa622007-08-15 14:28:22 +00002462 that occurred should be suppressed. If an exception occurred while executing the
2463 body of the :keyword:`with` statement, the arguments contain the exception type,
2464 value and traceback information. Otherwise, all three arguments are ``None``.
2465
2466 Returning a true value from this method will cause the :keyword:`with` statement
2467 to suppress the exception and continue execution with the statement immediately
2468 following the :keyword:`with` statement. Otherwise the exception continues
2469 propagating after this method has finished executing. Exceptions that occur
2470 during execution of this method will replace any exception that occurred in the
2471 body of the :keyword:`with` statement.
2472
2473 The exception passed in should never be reraised explicitly - instead, this
2474 method should return a false value to indicate that the method completed
2475 successfully and does not want to suppress the raised exception. This allows
2476 context management code (such as ``contextlib.nested``) to easily detect whether
2477 or not an :meth:`__exit__` method has actually failed.
2478
2479Python defines several context managers to support easy thread synchronisation,
2480prompt closure of files or other objects, and simpler manipulation of the active
2481decimal arithmetic context. The specific types are not treated specially beyond
2482their implementation of the context management protocol. See the
2483:mod:`contextlib` module for some examples.
2484
Georg Brandla6053b42009-09-01 08:11:14 +00002485Python's :term:`generator`\s and the ``contextlib.contextmanager`` :term:`decorator`
Christian Heimesd8654cf2007-12-02 15:22:16 +00002486provide a convenient way to implement these protocols. If a generator function is
Georg Brandla6053b42009-09-01 08:11:14 +00002487decorated with the ``contextlib.contextmanager`` decorator, it will return a
Georg Brandl116aa622007-08-15 14:28:22 +00002488context manager implementing the necessary :meth:`__enter__` and
2489:meth:`__exit__` methods, rather than the iterator produced by an undecorated
2490generator function.
2491
2492Note that there is no specific slot for any of these methods in the type
2493structure for Python objects in the Python/C API. Extension types wanting to
2494define these methods must provide them as a normal Python accessible method.
2495Compared to the overhead of setting up the runtime context, the overhead of a
2496single class dictionary lookup is negligible.
2497
2498
2499.. _typesother:
2500
2501Other Built-in Types
2502====================
2503
2504The interpreter supports several other kinds of objects. Most of these support
2505only one or two operations.
2506
2507
2508.. _typesmodules:
2509
2510Modules
2511-------
2512
2513The only special operation on a module is attribute access: ``m.name``, where
2514*m* is a module and *name* accesses a name defined in *m*'s symbol table.
2515Module attributes can be assigned to. (Note that the :keyword:`import`
2516statement is not, strictly speaking, an operation on a module object; ``import
2517foo`` does not require a module object named *foo* to exist, rather it requires
2518an (external) *definition* for a module named *foo* somewhere.)
2519
2520A special member of every module is :attr:`__dict__`. This is the dictionary
2521containing the module's symbol table. Modifying this dictionary will actually
2522change the module's symbol table, but direct assignment to the :attr:`__dict__`
2523attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines
2524``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying
2525:attr:`__dict__` directly is not recommended.
2526
2527Modules built into the interpreter are written like this: ``<module 'sys'
2528(built-in)>``. If loaded from a file, they are written as ``<module 'os' from
2529'/usr/local/lib/pythonX.Y/os.pyc'>``.
2530
2531
2532.. _typesobjects:
2533
2534Classes and Class Instances
2535---------------------------
2536
2537See :ref:`objects` and :ref:`class` for these.
2538
2539
2540.. _typesfunctions:
2541
2542Functions
2543---------
2544
2545Function objects are created by function definitions. The only operation on a
2546function object is to call it: ``func(argument-list)``.
2547
2548There are really two flavors of function objects: built-in functions and
2549user-defined functions. Both support the same operation (to call the function),
2550but the implementation is different, hence the different object types.
2551
2552See :ref:`function` for more information.
2553
2554
2555.. _typesmethods:
2556
2557Methods
2558-------
2559
2560.. index:: object: method
2561
2562Methods are functions that are called using the attribute notation. There are
2563two flavors: built-in methods (such as :meth:`append` on lists) and class
2564instance methods. Built-in methods are described with the types that support
2565them.
2566
Georg Brandl2e0b7552007-11-27 12:43:08 +00002567If you access a method (a function defined in a class namespace) through an
2568instance, you get a special object: a :dfn:`bound method` (also called
2569:dfn:`instance method`) object. When called, it will add the ``self`` argument
2570to the argument list. Bound methods have two special read-only attributes:
2571``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
2572the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
2573is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
2574arg-n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00002575
Georg Brandl2e0b7552007-11-27 12:43:08 +00002576Like function objects, bound method objects support getting arbitrary
2577attributes. However, since method attributes are actually stored on the
2578underlying function object (``meth.__func__``), setting method attributes on
2579bound methods is disallowed. Attempting to set a method attribute results in a
Georg Brandl116aa622007-08-15 14:28:22 +00002580:exc:`TypeError` being raised. In order to set a method attribute, you need to
2581explicitly set it on the underlying function object::
2582
2583 class C:
2584 def method(self):
2585 pass
2586
2587 c = C()
Christian Heimesff737952007-11-27 10:40:20 +00002588 c.method.__func__.whoami = 'my name is c'
Georg Brandl116aa622007-08-15 14:28:22 +00002589
2590See :ref:`types` for more information.
2591
2592
2593.. _bltin-code-objects:
2594
2595Code Objects
2596------------
2597
2598.. index:: object: code
2599
2600.. index::
2601 builtin: compile
2602 single: __code__ (function object attribute)
2603
2604Code objects are used by the implementation to represent "pseudo-compiled"
2605executable Python code such as a function body. They differ from function
2606objects because they don't contain a reference to their global execution
2607environment. Code objects are returned by the built-in :func:`compile` function
2608and can be extracted from function objects through their :attr:`__code__`
2609attribute. See also the :mod:`code` module.
2610
2611.. index::
2612 builtin: exec
2613 builtin: eval
2614
2615A code object can be executed or evaluated by passing it (instead of a source
2616string) to the :func:`exec` or :func:`eval` built-in functions.
2617
2618See :ref:`types` for more information.
2619
2620
2621.. _bltin-type-objects:
2622
2623Type Objects
2624------------
2625
2626.. index::
2627 builtin: type
2628 module: types
2629
2630Type objects represent the various object types. An object's type is accessed
2631by the built-in function :func:`type`. There are no special operations on
2632types. The standard module :mod:`types` defines names for all standard built-in
2633types.
2634
Martin v. Löwis250ad612008-04-07 05:43:42 +00002635Types are written like this: ``<class 'int'>``.
Georg Brandl116aa622007-08-15 14:28:22 +00002636
2637
2638.. _bltin-null-object:
2639
2640The Null Object
2641---------------
2642
2643This object is returned by functions that don't explicitly return a value. It
2644supports no special operations. There is exactly one null object, named
2645``None`` (a built-in name).
2646
2647It is written as ``None``.
2648
2649
2650.. _bltin-ellipsis-object:
2651
2652The Ellipsis Object
2653-------------------
2654
Georg Brandlcb8ecb12007-09-04 06:35:14 +00002655This object is commonly used by slicing (see :ref:`slicings`). It supports no
2656special operations. There is exactly one ellipsis object, named
Georg Brandl116aa622007-08-15 14:28:22 +00002657:const:`Ellipsis` (a built-in name).
2658
2659It is written as ``Ellipsis`` or ``...``.
2660
2661
2662Boolean Values
2663--------------
2664
2665Boolean values are the two constant objects ``False`` and ``True``. They are
2666used to represent truth values (although other values can also be considered
2667false or true). In numeric contexts (for example when used as the argument to
2668an arithmetic operator), they behave like the integers 0 and 1, respectively.
2669The built-in function :func:`bool` can be used to cast any value to a Boolean,
2670if the value can be interpreted as a truth value (see section Truth Value
2671Testing above).
2672
2673.. index::
2674 single: False
2675 single: True
2676 pair: Boolean; values
2677
2678They are written as ``False`` and ``True``, respectively.
2679
2680
2681.. _typesinternal:
2682
2683Internal Objects
2684----------------
2685
2686See :ref:`types` for this information. It describes stack frame objects,
2687traceback objects, and slice objects.
2688
2689
2690.. _specialattrs:
2691
2692Special Attributes
2693==================
2694
2695The implementation adds a few special read-only attributes to several object
2696types, where they are relevant. Some of these are not reported by the
2697:func:`dir` built-in function.
2698
2699
2700.. attribute:: object.__dict__
2701
2702 A dictionary or other mapping object used to store an object's (writable)
2703 attributes.
2704
2705
2706.. attribute:: instance.__class__
2707
2708 The class to which a class instance belongs.
2709
2710
2711.. attribute:: class.__bases__
2712
Benjamin Peterson1baf4652009-12-31 03:11:23 +00002713 The tuple of base classes of a class object.
Georg Brandl116aa622007-08-15 14:28:22 +00002714
2715
2716.. attribute:: class.__name__
2717
2718 The name of the class or type.
2719
Georg Brandl7a51e582009-03-28 19:13:21 +00002720
Benjamin Petersond23f8222009-04-05 19:13:16 +00002721The following attributes are only supported by :term:`new-style class`\ es.
2722
2723.. attribute:: class.__mro__
2724
2725 This attribute is a tuple of classes that are considered when looking for
2726 base classes during method resolution.
2727
2728
2729.. method:: class.mro()
2730
2731 This method can be overridden by a metaclass to customize the method
2732 resolution order for its instances. It is called at class instantiation, and
2733 its result is stored in :attr:`__mro__`.
2734
2735
Georg Brandl7a51e582009-03-28 19:13:21 +00002736.. method:: class.__subclasses__
2737
Benjamin Petersond23f8222009-04-05 19:13:16 +00002738 Each new-style class keeps a list of weak references to its immediate
2739 subclasses. This method returns a list of all those references still alive.
2740 Example::
Georg Brandl7a51e582009-03-28 19:13:21 +00002741
2742 >>> int.__subclasses__()
2743 [<type 'bool'>]
2744
2745
Georg Brandl116aa622007-08-15 14:28:22 +00002746.. rubric:: Footnotes
2747
2748.. [#] Additional information on these special methods may be found in the Python
2749 Reference Manual (:ref:`customization`).
2750
2751.. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
2752 similarly for tuples.
2753
2754.. [#] They must have since the parser can't tell the type of the operands.
2755
2756.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
2757 element is the tuple to be formatted.