blob: 638bafd034f58009c59ed489b049930f05806641 [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
15The principal built-in types are numerics, sequences, mappings, files, classes,
16instances 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
123.. index:: pair: chaining; comparisons
124
Georg Brandl905ec322007-09-28 13:39:25 +0000125There are eight comparison operations in Python. They all have the same
126priority (which is higher than that of the Boolean operations). Comparisons can
Georg Brandl116aa622007-08-15 14:28:22 +0000127be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
128y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
129evaluated at all when ``x < y`` is found to be false).
130
Georg Brandl81ac1ce2007-08-31 17:17:17 +0000131.. index::
132 pair: operator; comparison
133 operator: ==
134 operator: <
135 operator: >
136 operator: <=
137 operator: >=
138 operator: !=
139 operator: is
140 operator: is not
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142This table summarizes the comparison operations:
143
Georg Brandlfd855162008-01-07 09:13:03 +0000144+------------+-------------------------+
145| Operation | Meaning |
146+============+=========================+
147| ``<`` | strictly less than |
148+------------+-------------------------+
149| ``<=`` | less than or equal |
150+------------+-------------------------+
151| ``>`` | strictly greater than |
152+------------+-------------------------+
153| ``>=`` | greater than or equal |
154+------------+-------------------------+
155| ``==`` | equal |
156+------------+-------------------------+
157| ``!=`` | not equal |
158+------------+-------------------------+
159| ``is`` | object identity |
160+------------+-------------------------+
161| ``is not`` | negated object identity |
162+------------+-------------------------+
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000163
164.. index::
Georg Brandl116aa622007-08-15 14:28:22 +0000165 pair: object; numeric
166 pair: objects; comparing
167
Georg Brandl905ec322007-09-28 13:39:25 +0000168Objects of different types, except different numeric types, never compare equal.
Georg Brandl116aa622007-08-15 14:28:22 +0000169Furthermore, some types (for example, file objects) support only a degenerate
Georg Brandl905ec322007-09-28 13:39:25 +0000170notion of comparison where any two objects of that type are unequal. The ``<``,
171``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
172any operand is a complex number, the objects are of different types that cannot
173be compared, or other cases where there is no defined ordering.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl905ec322007-09-28 13:39:25 +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
220numbers are implemented using :ctype:`double` in C---all bets on their
221precision are off unless you happen to know the machine you are working
222with. Complex numbers have a real and imaginary part, which are each
223implemented using :ctype:`double` in C. To extract these parts from a
224complex number *z*, use ``z.real`` and ``z.imag``. (The standard library
225includes additional numeric types, :mod:`fractions` that hold rationals,
226and :mod:`decimal` that hold floating-point numbers with user-definable
227precision.)
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229.. index::
230 pair: numeric; literals
231 pair: integer; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000232 pair: floating point; literals
233 pair: complex number; literals
234 pair: hexadecimal; literals
235 pair: octal; literals
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000236 pair: binary; literals
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238Numbers are created by numeric literals or as the result of built-in functions
Georg Brandl905ec322007-09-28 13:39:25 +0000239and operators. Unadorned integer literals (including hex, octal and binary
240numbers) yield integers. Numeric literals containing a decimal point or an
241exponent sign yield floating point numbers. Appending ``'j'`` or ``'J'`` to a
242numeric literal yields an imaginary number (a complex number with a zero real
243part) which you can add to an integer or float to get a complex number with real
244and imaginary parts.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246.. index::
247 single: arithmetic
248 builtin: int
Georg Brandl116aa622007-08-15 14:28:22 +0000249 builtin: float
250 builtin: complex
251
252Python fully supports mixed arithmetic: when a binary arithmetic operator has
253operands of different numeric types, the operand with the "narrower" type is
Georg Brandl905ec322007-09-28 13:39:25 +0000254widened to that of the other, where integer is narrower than floating point,
255which is narrower than complex. Comparisons between numbers of mixed type use
256the same rule. [#]_ The constructors :func:`int`, :func:`float`, and
257:func:`complex` can be used to produce numbers of a specific type.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259All numeric types (except complex) support the following operations, sorted by
260ascending priority (operations in the same box have the same priority; all
261numeric operations have a higher priority than comparison operations):
262
Georg Brandl905ec322007-09-28 13:39:25 +0000263+---------------------+---------------------------------+-------+--------------------+
264| Operation | Result | Notes | Full documentation |
Neal Norwitz1d2aef52007-10-02 07:26:14 +0000265+=====================+=================================+=======+====================+
Georg Brandl905ec322007-09-28 13:39:25 +0000266| ``x + y`` | sum of *x* and *y* | | |
267+---------------------+---------------------------------+-------+--------------------+
268| ``x - y`` | difference of *x* and *y* | | |
269+---------------------+---------------------------------+-------+--------------------+
270| ``x * y`` | product of *x* and *y* | | |
271+---------------------+---------------------------------+-------+--------------------+
272| ``x / y`` | quotient of *x* and *y* | | |
273+---------------------+---------------------------------+-------+--------------------+
274| ``x // y`` | floored quotient of *x* and | \(1) | |
275| | *y* | | |
276+---------------------+---------------------------------+-------+--------------------+
277| ``x % y`` | remainder of ``x / y`` | \(2) | |
278+---------------------+---------------------------------+-------+--------------------+
279| ``-x`` | *x* negated | | |
280+---------------------+---------------------------------+-------+--------------------+
281| ``+x`` | *x* unchanged | | |
282+---------------------+---------------------------------+-------+--------------------+
283| ``abs(x)`` | absolute value or magnitude of | | :func:`abs` |
284| | *x* | | |
285+---------------------+---------------------------------+-------+--------------------+
286| ``int(x)`` | *x* converted to integer | \(3) | :func:`int` |
287+---------------------+---------------------------------+-------+--------------------+
Georg Brandl74f36692008-01-06 17:39:49 +0000288| ``float(x)`` | *x* converted to floating point | \(4) | :func:`float` |
Georg Brandl905ec322007-09-28 13:39:25 +0000289+---------------------+---------------------------------+-------+--------------------+
290| ``complex(re, im)`` | a complex number with real part | | :func:`complex` |
291| | *re*, imaginary part *im*. | | |
292| | *im* defaults to zero. | | |
293+---------------------+---------------------------------+-------+--------------------+
294| ``c.conjugate()`` | conjugate of the complex number | | |
295| | *c* | | |
296+---------------------+---------------------------------+-------+--------------------+
297| ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | \(2) | :func:`divmod` |
298+---------------------+---------------------------------+-------+--------------------+
Georg Brandl60fe2f12008-01-07 09:16:46 +0000299| ``pow(x, y)`` | *x* to the power *y* | \(5) | :func:`pow` |
Georg Brandl905ec322007-09-28 13:39:25 +0000300+---------------------+---------------------------------+-------+--------------------+
Georg Brandl60fe2f12008-01-07 09:16:46 +0000301| ``x ** y`` | *x* to the power *y* | \(5) | |
Georg Brandl905ec322007-09-28 13:39:25 +0000302+---------------------+---------------------------------+-------+--------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304.. index::
305 triple: operations on; numeric; types
306 single: conjugate() (complex number method)
307
308Notes:
309
310(1)
Georg Brandl905ec322007-09-28 13:39:25 +0000311 Also referred to as integer division. The resultant value is a whole
312 integer, though the result's type is not necessarily int. The result is
313 always rounded towards minus infinity: ``1//2`` is ``0``, ``(-1)//2`` is
314 ``-1``, ``1//(-2)`` is ``-1``, and ``(-1)//(-2)`` is ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
316(2)
Georg Brandl905ec322007-09-28 13:39:25 +0000317 Not for complex numbers. Instead convert to floats using :func:`abs` if
318 appropriate.
319
320(3)
Georg Brandl116aa622007-08-15 14:28:22 +0000321 .. index::
322 module: math
323 single: floor() (in module math)
324 single: ceil() (in module math)
325 pair: numeric; conversions
326 pair: C; language
327
Georg Brandlba956ae2007-11-29 17:24:34 +0000328 Conversion from floating point to integer may round or truncate
Georg Brandl116aa622007-08-15 14:28:22 +0000329 as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module
330 for well-defined conversions.
331
Georg Brandl74f36692008-01-06 17:39:49 +0000332(4)
Christian Heimes99170a52007-12-19 02:07:34 +0000333 float also accepts the strings "nan" and "inf" with an optional prefix "+"
334 or "-" for Not a Number (NaN) and positive or negative infinity.
Christian Heimes7f044312008-01-06 17:05:40 +0000335
Georg Brandl74f36692008-01-06 17:39:49 +0000336(5)
Christian Heimes7f044312008-01-06 17:05:40 +0000337 Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
338 programming languages.
339
Christian Heimes99170a52007-12-19 02:07:34 +0000340
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000341
Christian Heimesfaf2f632008-01-06 16:59:19 +0000342All :class:`numbers.Real` types (:class:`int` and
343:class:`float`) also include the following operations:
344
345+--------------------+--------------------------------+--------+
346| Operation | Result | Notes |
347+====================+================================+========+
348| ``trunc(x)`` | *x* truncated to Integral | |
349+--------------------+--------------------------------+--------+
350| ``round(x[, n])`` | *x* rounded to n digits, | |
351| | rounding half to even. If n is | |
352| | omitted, it defaults to 0. | |
353+--------------------+--------------------------------+--------+
354| ``math.floor(x)`` | the greatest Integral <= *x* | |
355+--------------------+--------------------------------+--------+
356| ``math.ceil(x)`` | the least Integral >= *x* | |
357+--------------------+--------------------------------+--------+
358
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000359For additional numeric operations see the :mod:`math` and :mod:`cmath`
360modules.
361
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000362.. XXXJH exceptions: overflow (when? what operations?) zerodivision
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364
365.. _bitstring-ops:
366
367Bit-string Operations on Integer Types
368--------------------------------------
369
370.. _bit-string-operations:
371
Georg Brandl905ec322007-09-28 13:39:25 +0000372Integers support additional operations that make sense only for bit-strings.
373Negative numbers are treated as their 2's complement value (this assumes a
374sufficiently large number of bits that no overflow occurs during the operation).
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Christian Heimesfaf2f632008-01-06 16:59:19 +0000376The priorities of the binary bitwise operations are all lower than the numeric
Georg Brandl116aa622007-08-15 14:28:22 +0000377operations and higher than the comparisons; the unary operation ``~`` has the
378same priority as the other unary numeric operations (``+`` and ``-``).
379
380This table lists the bit-string operations sorted in ascending priority
381(operations in the same box have the same priority):
382
383+------------+--------------------------------+----------+
384| Operation | Result | Notes |
385+============+================================+==========+
386| ``x | y`` | bitwise :dfn:`or` of *x* and | |
387| | *y* | |
388+------------+--------------------------------+----------+
389| ``x ^ y`` | bitwise :dfn:`exclusive or` of | |
390| | *x* and *y* | |
391+------------+--------------------------------+----------+
392| ``x & y`` | bitwise :dfn:`and` of *x* and | |
393| | *y* | |
394+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000395| ``x << n`` | *x* shifted left by *n* bits | (1)(2) |
Georg Brandl116aa622007-08-15 14:28:22 +0000396+------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000397| ``x >> n`` | *x* shifted right by *n* bits | (1)(3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000398+------------+--------------------------------+----------+
399| ``~x`` | the bits of *x* inverted | |
400+------------+--------------------------------+----------+
401
402.. index::
403 triple: operations on; integer; types
404 pair: bit-string; operations
405 pair: shifting; operations
406 pair: masking; operations
407
408Notes:
409
410(1)
411 Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
412
413(2)
414 A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``
415 without overflow check.
416
417(3)
418 A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without
419 overflow check.
420
421
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000422Additional Methods on Integer Types
423-----------------------------------
424
425.. method:: int.bit_length()
426
427 For any integer ``x``, ``x.bit_length()`` returns the number of
428 bits necessary to represent ``x`` in binary, excluding the sign
429 and any leading zeros::
430
431 >>> n = 37
432 >>> bin(n)
433 '0b100101'
434 >>> n.bit_length()
435 6
436 >>> n = -0b00011010
437 >>> n.bit_length()
438 5
439
440 More precisely, if ``x`` is nonzero then ``x.bit_length()`` is the
441 unique positive integer ``k`` such that ``2**(k-1) <= abs(x) <
442 2**k``. Equivalently, ``x.bit_length()`` is equal to ``1 +
443 floor(log(x, 2))`` [#]_ . If ``x`` is zero then ``x.bit_length()``
444 gives ``0``.
445
446 Equivalent to::
447
448 def bit_length(self):
449 'Number of bits necessary to represent self in binary.'
450 return len(bin(self).lstrip('-0b'))
451
452
453 .. versionadded:: 3.1
454
455
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000456Additional Methods on Float
457---------------------------
458
Benjamin Petersond7b03282008-09-13 15:58:53 +0000459The float type has some additional methods.
460
461.. method:: float.as_integer_ratio()
462
463 Return a pair of integers whose ratio is exactly equal to the
464 original float and with a positive denominator. Raises
465 :exc:`OverflowError` on infinities and a :exc:`ValueError` on
466 NaNs.
467
468 .. versionadded:: 2.6
469
470Two methods support conversion to
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000471and from hexadecimal strings. Since Python's floats are stored
472internally as binary numbers, converting a float to or from a
473*decimal* string usually involves a small rounding error. In
474contrast, hexadecimal strings allow exact representation and
475specification of floating-point numbers. This can be useful when
476debugging, and in numerical work.
477
478
479.. method:: float.hex()
480
481 Return a representation of a floating-point number as a hexadecimal
482 string. For finite floating-point numbers, this representation
483 will always include a leading ``0x`` and a trailing ``p`` and
484 exponent.
485
486
487.. method:: float.fromhex(s)
488
489 Class method to return the float represented by a hexadecimal
490 string *s*. The string *s* may have leading and trailing
491 whitespace.
492
493
494Note that :meth:`float.hex` is an instance method, while
495:meth:`float.fromhex` is a class method.
496
497A hexadecimal string takes the form::
498
499 [sign] ['0x'] integer ['.' fraction] ['p' exponent]
500
501where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
502and ``fraction`` are strings of hexadecimal digits, and ``exponent``
503is a decimal integer with an optional leading sign. Case is not
504significant, and there must be at least one hexadecimal digit in
505either the integer or the fraction. This syntax is similar to the
506syntax specified in section 6.4.4.2 of the C99 standard, and also to
507the syntax used in Java 1.5 onwards. In particular, the output of
508:meth:`float.hex` is usable as a hexadecimal floating-point literal in
509C or Java code, and hexadecimal strings produced by C's ``%a`` format
510character or Java's ``Double.toHexString`` are accepted by
511:meth:`float.fromhex`.
512
513
514Note that the exponent is written in decimal rather than hexadecimal,
515and that it gives the power of 2 by which to multiply the coefficient.
516For example, the hexadecimal string ``0x3.a7p10`` represents the
517floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
518``3740.0``::
519
520 >>> float.fromhex('0x3.a7p10')
521 3740.0
522
523
524Applying the reverse conversion to ``3740.0`` gives a different
525hexadecimal string representing the same number::
526
527 >>> float.hex(3740.0)
528 '0x1.d380000000000p+11'
529
530
Georg Brandl6ea420b2008-07-16 12:58:29 +0000531.. _typeiter:
532
Georg Brandl116aa622007-08-15 14:28:22 +0000533Iterator Types
534==============
535
Georg Brandl116aa622007-08-15 14:28:22 +0000536.. index::
537 single: iterator protocol
538 single: protocol; iterator
539 single: sequence; iteration
540 single: container; iteration over
541
542Python supports a concept of iteration over containers. This is implemented
543using two distinct methods; these are used to allow user-defined classes to
544support iteration. Sequences, described below in more detail, always support
545the iteration methods.
546
547One method needs to be defined for container objects to provide iteration
548support:
549
Christian Heimes790c8232008-01-07 21:14:23 +0000550.. XXX duplicated in reference/datamodel!
Georg Brandl116aa622007-08-15 14:28:22 +0000551
Christian Heimes790c8232008-01-07 21:14:23 +0000552.. method:: container.__iter__()
Georg Brandl116aa622007-08-15 14:28:22 +0000553
554 Return an iterator object. The object is required to support the iterator
555 protocol described below. If a container supports different types of
556 iteration, additional methods can be provided to specifically request
557 iterators for those iteration types. (An example of an object supporting
558 multiple forms of iteration would be a tree structure which supports both
559 breadth-first and depth-first traversal.) This method corresponds to the
560 :attr:`tp_iter` slot of the type structure for Python objects in the Python/C
561 API.
562
563The iterator objects themselves are required to support the following two
564methods, which together form the :dfn:`iterator protocol`:
565
566
567.. method:: iterator.__iter__()
568
569 Return the iterator object itself. This is required to allow both containers
570 and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
571 This method corresponds to the :attr:`tp_iter` slot of the type structure for
572 Python objects in the Python/C API.
573
574
Georg Brandl905ec322007-09-28 13:39:25 +0000575.. method:: iterator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000576
577 Return the next item from the container. If there are no further items, raise
578 the :exc:`StopIteration` exception. This method corresponds to the
579 :attr:`tp_iternext` slot of the type structure for Python objects in the
580 Python/C API.
581
582Python defines several iterator objects to support iteration over general and
583specific sequence types, dictionaries, and other more specialized forms. The
584specific types are not important beyond their implementation of the iterator
585protocol.
586
Georg Brandl905ec322007-09-28 13:39:25 +0000587Once an iterator's :meth:`__next__` method raises :exc:`StopIteration`, it must
588continue to do so on subsequent calls. Implementations that do not obey this
589property are deemed broken.
Georg Brandl116aa622007-08-15 14:28:22 +0000590
Georg Brandl9afde1c2007-11-01 20:32:30 +0000591Python's :term:`generator`\s provide a convenient way to implement the iterator
592protocol. If a container object's :meth:`__iter__` method is implemented as a
593generator, it will automatically return an iterator object (technically, a
594generator object) supplying the :meth:`__iter__` and :meth:`__next__` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000595
596
597.. _typesseq:
598
Georg Brandl95414632007-11-22 11:00:28 +0000599Sequence Types --- :class:`str`, :class:`bytes`, :class:`bytearray`, :class:`list`, :class:`tuple`, :class:`range`
600==================================================================================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000601
Georg Brandl95414632007-11-22 11:00:28 +0000602There are five sequence types: strings, byte sequences, byte arrays, lists,
603tuples, and range objects. (For other containers see the built-in
604:class:`dict`, :class:`list`, :class:`set`, and :class:`tuple` classes, and the
Georg Brandl4b491312007-08-31 09:22:56 +0000605:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607.. index::
608 object: sequence
609 object: string
Georg Brandl4b491312007-08-31 09:22:56 +0000610 object: bytes
Georg Brandl7c676132007-10-23 18:17:00 +0000611 object: buffer
Georg Brandl116aa622007-08-15 14:28:22 +0000612 object: tuple
613 object: list
Georg Brandl116aa622007-08-15 14:28:22 +0000614 object: range
615
Georg Brandl7c676132007-10-23 18:17:00 +0000616Strings contain Unicode characters. Their literals are written in single or
617double quotes: ``'xyzzy'``, ``"frobozz"``. See :ref:`strings` for more about
618string literals. In addition to the functionality described here, there are
619also string-specific methods described in the :ref:`string-methods` section.
620
Georg Brandl95414632007-11-22 11:00:28 +0000621Bytes and bytearray objects contain single bytes -- the former is immutable
Georg Brandl18da8f02008-07-01 20:08:02 +0000622while the latter is a mutable sequence. Bytes objects can be constructed the
623constructor, :func:`bytes`, and from literals; use a ``b`` prefix with normal
624string syntax: ``b'xyzzy'``. To construct byte arrays, use the
625:func:`bytearray` function.
Georg Brandl4b491312007-08-31 09:22:56 +0000626
Georg Brandl226878c2007-08-31 10:15:37 +0000627.. warning::
Georg Brandl4b491312007-08-31 09:22:56 +0000628
629 While string objects are sequences of characters (represented by strings of
Georg Brandl95414632007-11-22 11:00:28 +0000630 length 1), bytes and bytearray objects are sequences of *integers* (between 0
Georg Brandl7c676132007-10-23 18:17:00 +0000631 and 255), representing the ASCII value of single bytes. That means that for
Georg Brandl18da8f02008-07-01 20:08:02 +0000632 a bytes or bytearray object *b*, ``b[0]`` will be an integer, while
633 ``b[0:1]`` will be a bytes or bytearray object of length 1. The
634 representation of bytes objects uses the literal format (``b'...'``) since it
635 is generally more useful than e.g. ``bytes([50, 19, 100])``. You can always
636 convert a bytes object into a list of integers using ``list(b)``.
Georg Brandl4b491312007-08-31 09:22:56 +0000637
Georg Brandl2326a792007-09-01 12:08:51 +0000638 Also, while in previous Python versions, byte strings and Unicode strings
639 could be exchanged for each other rather freely (barring encoding issues),
Georg Brandl7c676132007-10-23 18:17:00 +0000640 strings and bytes are now completely separate concepts. There's no implicit
641 en-/decoding if you pass and object of the wrong type. A string always
Georg Brandl95414632007-11-22 11:00:28 +0000642 compares unequal to a bytes or bytearray object.
Georg Brandl2326a792007-09-01 12:08:51 +0000643
Georg Brandl4b491312007-08-31 09:22:56 +0000644Lists are constructed with square brackets, separating items with commas: ``[a,
645b, c]``. Tuples are constructed by the comma operator (not within square
646brackets), with or without enclosing parentheses, but an empty tuple must have
647the enclosing parentheses, such as ``a, b, c`` or ``()``. A single item tuple
648must have a trailing comma, such as ``(d,)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000649
Georg Brandl95414632007-11-22 11:00:28 +0000650Objects of type range are created using the :func:`range` function. They don't
651support slicing, concatenation or repetition, and using ``in``, ``not in``,
652:func:`min` or :func:`max` on them is inefficient.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654Most sequence types support the following operations. The ``in`` and ``not in``
655operations have the same priorities as the comparison operations. The ``+`` and
656``*`` operations have the same priority as the corresponding numeric operations.
Christian Heimes043d6f62008-01-07 17:19:16 +0000657[#]_ Additional methods are provided for :ref:`typesseq-mutable`.
Georg Brandl116aa622007-08-15 14:28:22 +0000658
659This table lists the sequence operations sorted in ascending priority
660(operations in the same box have the same priority). In the table, *s* and *t*
661are sequences of the same type; *n*, *i* and *j* are integers:
662
663+------------------+--------------------------------+----------+
664| Operation | Result | Notes |
665+==================+================================+==========+
666| ``x in s`` | ``True`` if an item of *s* is | \(1) |
667| | equal to *x*, else ``False`` | |
668+------------------+--------------------------------+----------+
669| ``x not in s`` | ``False`` if an item of *s* is | \(1) |
670| | equal to *x*, else ``True`` | |
671+------------------+--------------------------------+----------+
672| ``s + t`` | the concatenation of *s* and | \(6) |
673| | *t* | |
674+------------------+--------------------------------+----------+
675| ``s * n, n * s`` | *n* shallow copies of *s* | \(2) |
676| | concatenated | |
677+------------------+--------------------------------+----------+
678| ``s[i]`` | *i*'th item of *s*, origin 0 | \(3) |
679+------------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000680| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000681+------------------+--------------------------------+----------+
Christian Heimes043d6f62008-01-07 17:19:16 +0000682| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |
Georg Brandl116aa622007-08-15 14:28:22 +0000683| | with step *k* | |
684+------------------+--------------------------------+----------+
685| ``len(s)`` | length of *s* | |
686+------------------+--------------------------------+----------+
687| ``min(s)`` | smallest item of *s* | |
688+------------------+--------------------------------+----------+
689| ``max(s)`` | largest item of *s* | |
690+------------------+--------------------------------+----------+
691
Georg Brandl7c676132007-10-23 18:17:00 +0000692Sequence types also support comparisons. In particular, tuples and lists are
693compared lexicographically by comparing corresponding elements. This means that
Georg Brandl4b491312007-08-31 09:22:56 +0000694to compare equal, every element must compare equal and the two sequences must be
Georg Brandl7c676132007-10-23 18:17:00 +0000695of the same type and have the same length. (For full details see
Georg Brandl4b491312007-08-31 09:22:56 +0000696:ref:`comparisons` in the language reference.)
Georg Brandl116aa622007-08-15 14:28:22 +0000697
698.. index::
699 triple: operations on; sequence; types
700 builtin: len
701 builtin: min
702 builtin: max
703 pair: concatenation; operation
704 pair: repetition; operation
705 pair: subscript; operation
706 pair: slice; operation
Georg Brandl116aa622007-08-15 14:28:22 +0000707 operator: in
708 operator: not in
709
710Notes:
711
712(1)
Georg Brandl4b491312007-08-31 09:22:56 +0000713 When *s* is a string object, the ``in`` and ``not in`` operations act like a
714 substring test.
Georg Brandl116aa622007-08-15 14:28:22 +0000715
716(2)
717 Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
718 sequence of the same type as *s*). Note also that the copies are shallow;
719 nested structures are not copied. This often haunts new Python programmers;
Christian Heimesfe337bf2008-03-23 21:54:12 +0000720 consider:
Georg Brandl116aa622007-08-15 14:28:22 +0000721
722 >>> lists = [[]] * 3
723 >>> lists
724 [[], [], []]
725 >>> lists[0].append(3)
726 >>> lists
727 [[3], [3], [3]]
728
729 What has happened is that ``[[]]`` is a one-element list containing an empty
Christian Heimesfe337bf2008-03-23 21:54:12 +0000730 list, so all three elements of ``[[]] * 3`` are (pointers to) this single empty
731 list. Modifying any of the elements of ``lists`` modifies this single list.
732 You can create a list of different lists this way:
Georg Brandl116aa622007-08-15 14:28:22 +0000733
734 >>> lists = [[] for i in range(3)]
735 >>> lists[0].append(3)
736 >>> lists[1].append(5)
737 >>> lists[2].append(7)
738 >>> lists
739 [[3], [5], [7]]
740
741(3)
742 If *i* or *j* is negative, the index is relative to the end of the string:
Georg Brandl7c676132007-10-23 18:17:00 +0000743 ``len(s) + i`` or ``len(s) + j`` is substituted. But note that ``-0`` is
744 still ``0``.
Georg Brandl116aa622007-08-15 14:28:22 +0000745
746(4)
747 The slice of *s* from *i* to *j* is defined as the sequence of items with index
748 *k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
749 ``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
750 ``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
751 empty.
752
753(5)
754 The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
Christian Heimes2c181612007-12-17 20:04:13 +0000755 items with index ``x = i + n*k`` such that ``0 <= n < (j-i)/k``. In other words,
Georg Brandl116aa622007-08-15 14:28:22 +0000756 the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
757 *j* is reached (but never including *j*). If *i* or *j* is greater than
758 ``len(s)``, use ``len(s)``. If *i* or *j* are omitted or ``None``, they become
759 "end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
760 If *k* is ``None``, it is treated like ``1``.
761
762(6)
763 If *s* and *t* are both strings, some Python implementations such as CPython can
764 usually perform an in-place optimization for assignments of the form ``s=s+t``
765 or ``s+=t``. When applicable, this optimization makes quadratic run-time much
766 less likely. This optimization is both version and implementation dependent.
767 For performance sensitive code, it is preferable to use the :meth:`str.join`
768 method which assures consistent linear concatenation performance across versions
769 and implementations.
770
Georg Brandl116aa622007-08-15 14:28:22 +0000771
772.. _string-methods:
773
774String Methods
775--------------
776
777.. index:: pair: string; methods
778
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000779String objects support the methods listed below. Note that none of these
780methods take keyword arguments.
781
782In addition, Python's strings support the sequence type methods described in
783the :ref:`typesseq` section. To output formatted strings, see the
784:ref:`string-formatting` section. Also, see the :mod:`re` module for string
785functions based on regular expressions.
Georg Brandl116aa622007-08-15 14:28:22 +0000786
787.. method:: str.capitalize()
788
789 Return a copy of the string with only its first character capitalized.
790
Georg Brandl116aa622007-08-15 14:28:22 +0000791
792.. method:: str.center(width[, fillchar])
793
794 Return centered in a string of length *width*. Padding is done using the
795 specified *fillchar* (default is a space).
796
Georg Brandl116aa622007-08-15 14:28:22 +0000797
798.. method:: str.count(sub[, start[, end]])
799
Georg Brandl9afde1c2007-11-01 20:32:30 +0000800 Return the number of occurrences of substring *sub* in the range [*start*,
801 *end*]. Optional arguments *start* and *end* are interpreted as in slice
802 notation.
Georg Brandl116aa622007-08-15 14:28:22 +0000803
804
Georg Brandl226878c2007-08-31 10:15:37 +0000805.. method:: str.encode([encoding[, errors]])
Georg Brandl116aa622007-08-15 14:28:22 +0000806
807 Return an encoded version of the string. Default encoding is the current
808 default string encoding. *errors* may be given to set a different error
809 handling scheme. The default for *errors* is ``'strict'``, meaning that
810 encoding errors raise a :exc:`UnicodeError`. Other possible values are
811 ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and
812 any other name registered via :func:`codecs.register_error`, see section
813 :ref:`codec-base-classes`. For a list of possible encodings, see section
814 :ref:`standard-encodings`.
815
Georg Brandl116aa622007-08-15 14:28:22 +0000816
817.. method:: str.endswith(suffix[, start[, end]])
818
819 Return ``True`` if the string ends with the specified *suffix*, otherwise return
820 ``False``. *suffix* can also be a tuple of suffixes to look for. With optional
821 *start*, test beginning at that position. With optional *end*, stop comparing
822 at that position.
823
Georg Brandl116aa622007-08-15 14:28:22 +0000824
825.. method:: str.expandtabs([tabsize])
826
Georg Brandl9afde1c2007-11-01 20:32:30 +0000827 Return a copy of the string where all tab characters are replaced by one or
828 more spaces, depending on the current column and the given tab size. The
829 column number is reset to zero after each newline occurring in the string.
830 If *tabsize* is not given, a tab size of ``8`` characters is assumed. This
831 doesn't understand other non-printing characters or escape sequences.
Georg Brandl116aa622007-08-15 14:28:22 +0000832
833
834.. method:: str.find(sub[, start[, end]])
835
836 Return the lowest index in the string where substring *sub* is found, such that
837 *sub* is contained in the range [*start*, *end*]. Optional arguments *start*
838 and *end* are interpreted as in slice notation. Return ``-1`` if *sub* is not
839 found.
840
841
Georg Brandl1c502b52008-05-12 16:50:12 +0000842.. method:: str.format(format_string, *args, **kwargs)
Georg Brandl4b491312007-08-31 09:22:56 +0000843
844 Perform a string formatting operation. The *format_string* argument can
845 contain literal text or replacement fields delimited by braces ``{}``. Each
846 replacement field contains either the numeric index of a positional argument,
847 or the name of a keyword argument. Returns a copy of *format_string* where
848 each replacement field is replaced with the string value of the corresponding
849 argument.
850
851 >>> "The sum of 1 + 2 is {0}".format(1+2)
852 'The sum of 1 + 2 is 3'
853
854 See :ref:`formatstrings` for a description of the various formatting options
855 that can be specified in format strings.
856
Georg Brandl4b491312007-08-31 09:22:56 +0000857
Georg Brandl116aa622007-08-15 14:28:22 +0000858.. method:: str.index(sub[, start[, end]])
859
860 Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found.
861
862
863.. method:: str.isalnum()
864
865 Return true if all characters in the string are alphanumeric and there is at
866 least one character, false otherwise.
867
Georg Brandl116aa622007-08-15 14:28:22 +0000868
869.. method:: str.isalpha()
870
871 Return true if all characters in the string are alphabetic and there is at least
872 one character, false otherwise.
873
Georg Brandl116aa622007-08-15 14:28:22 +0000874
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000875.. method:: str.isdecimal()
876
877 Return true if all characters in the string are decimal
878 characters and there is at least one character, false
879 otherwise. Decimal characters include digit characters, and all characters
880 that that can be used to form decimal-radix numbers, e.g. U+0660,
881 ARABIC-INDIC DIGIT ZERO.
882
883
Georg Brandl116aa622007-08-15 14:28:22 +0000884.. method:: str.isdigit()
885
886 Return true if all characters in the string are digits and there is at least one
887 character, false otherwise.
888
Georg Brandl116aa622007-08-15 14:28:22 +0000889
890.. method:: str.isidentifier()
891
892 Return true if the string is a valid identifier according to the language
Georg Brandl4b491312007-08-31 09:22:56 +0000893 definition, section :ref:`identifiers`.
Georg Brandl116aa622007-08-15 14:28:22 +0000894
895
896.. method:: str.islower()
897
898 Return true if all cased characters in the string are lowercase and there is at
899 least one cased character, false otherwise.
900
Georg Brandl116aa622007-08-15 14:28:22 +0000901
Mark Summerfieldbbfd71d2008-07-01 15:50:04 +0000902.. method:: str.isnumeric()
903
904 Return true if all characters in the string are numeric
905 characters, and there is at least one character, false
906 otherwise. Numeric characters include digit characters, and all characters
907 that have the Unicode numeric value property, e.g. U+2155,
908 VULGAR FRACTION ONE FIFTH.
909
910
Georg Brandl559e5d72008-06-11 18:37:52 +0000911.. method:: str.isprintable()
912
913 Return true if all characters in the string are printable or the string is
914 empty, false otherwise. Nonprintable characters are those characters defined
915 in the Unicode character database as "Other" or "Separator", excepting the
916 ASCII space (0x20) which is considered printable. (Note that printable
917 characters in this context are those which should not be escaped when
918 :func:`repr` is invoked on a string. It has no bearing on the handling of
919 strings written to :data:`sys.stdout` or :data:`sys.stderr`.)
920
921
Georg Brandl116aa622007-08-15 14:28:22 +0000922.. method:: str.isspace()
923
924 Return true if there are only whitespace characters in the string and there is
925 at least one character, false otherwise.
926
Georg Brandl116aa622007-08-15 14:28:22 +0000927
928.. method:: str.istitle()
929
930 Return true if the string is a titlecased string and there is at least one
931 character, for example uppercase characters may only follow uncased characters
932 and lowercase characters only cased ones. Return false otherwise.
933
Georg Brandl116aa622007-08-15 14:28:22 +0000934
935.. method:: str.isupper()
936
937 Return true if all cased characters in the string are uppercase and there is at
938 least one cased character, false otherwise.
939
Georg Brandl116aa622007-08-15 14:28:22 +0000940
941.. method:: str.join(seq)
942
Georg Brandl07431a32008-08-02 16:34:27 +0000943 Return a string which is the concatenation of the strings in the sequence
944 *seq*. A :exc:`TypeError` will be raised if there are any non-string values
945 in *seq*, including :class:`bytes` objects. The separator between elements
946 is the string providing this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000947
948
949.. method:: str.ljust(width[, fillchar])
950
951 Return the string left justified in a string of length *width*. Padding is done
952 using the specified *fillchar* (default is a space). The original string is
953 returned if *width* is less than ``len(s)``.
954
Georg Brandl116aa622007-08-15 14:28:22 +0000955
956.. method:: str.lower()
957
958 Return a copy of the string converted to lowercase.
959
Georg Brandl116aa622007-08-15 14:28:22 +0000960
961.. method:: str.lstrip([chars])
962
963 Return a copy of the string with leading characters removed. The *chars*
964 argument is a string specifying the set of characters to be removed. If omitted
965 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +0000966 argument is not a prefix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +0000967
968 >>> ' spacious '.lstrip()
969 'spacious '
970 >>> 'www.example.com'.lstrip('cmowz.')
971 'example.com'
972
Georg Brandl116aa622007-08-15 14:28:22 +0000973
Georg Brandlceee0772007-11-27 23:48:05 +0000974.. method:: str.maketrans(x[, y[, z]])
975
976 This static method returns a translation table usable for :meth:`str.translate`.
977
978 If there is only one argument, it must be a dictionary mapping Unicode
979 ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
980 strings (of arbitrary lengths) or None. Character keys will then be
981 converted to ordinals.
982
983 If there are two arguments, they must be strings of equal length, and in the
984 resulting dictionary, each character in x will be mapped to the character at
985 the same position in y. If there is a third argument, it must be a string,
986 whose characters will be mapped to None in the result.
987
988
Georg Brandl116aa622007-08-15 14:28:22 +0000989.. method:: str.partition(sep)
990
991 Split the string at the first occurrence of *sep*, and return a 3-tuple
992 containing the part before the separator, the separator itself, and the part
993 after the separator. If the separator is not found, return a 3-tuple containing
994 the string itself, followed by two empty strings.
995
Georg Brandl116aa622007-08-15 14:28:22 +0000996
997.. method:: str.replace(old, new[, count])
998
999 Return a copy of the string with all occurrences of substring *old* replaced by
1000 *new*. If the optional argument *count* is given, only the first *count*
1001 occurrences are replaced.
1002
1003
Georg Brandl226878c2007-08-31 10:15:37 +00001004.. method:: str.rfind(sub[, start[, end]])
Georg Brandl116aa622007-08-15 14:28:22 +00001005
1006 Return the highest index in the string where substring *sub* is found, such that
1007 *sub* is contained within s[start,end]. Optional arguments *start* and *end*
1008 are interpreted as in slice notation. Return ``-1`` on failure.
1009
1010
1011.. method:: str.rindex(sub[, start[, end]])
1012
1013 Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
1014 found.
1015
1016
1017.. method:: str.rjust(width[, fillchar])
1018
1019 Return the string right justified in a string of length *width*. Padding is done
1020 using the specified *fillchar* (default is a space). The original string is
1021 returned if *width* is less than ``len(s)``.
1022
Georg Brandl116aa622007-08-15 14:28:22 +00001023
1024.. method:: str.rpartition(sep)
1025
1026 Split the string at the last occurrence of *sep*, and return a 3-tuple
1027 containing the part before the separator, the separator itself, and the part
1028 after the separator. If the separator is not found, return a 3-tuple containing
1029 two empty strings, followed by the string itself.
1030
Georg Brandl116aa622007-08-15 14:28:22 +00001031
Georg Brandl226878c2007-08-31 10:15:37 +00001032.. method:: str.rsplit([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +00001033
1034 Return a list of the words in the string, using *sep* as the delimiter string.
1035 If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
1036 ones. If *sep* is not specified or ``None``, any whitespace string is a
1037 separator. Except for splitting from the right, :meth:`rsplit` behaves like
1038 :meth:`split` which is described in detail below.
1039
Georg Brandl116aa622007-08-15 14:28:22 +00001040
1041.. method:: str.rstrip([chars])
1042
1043 Return a copy of the string with trailing characters removed. The *chars*
1044 argument is a string specifying the set of characters to be removed. If omitted
1045 or ``None``, the *chars* argument defaults to removing whitespace. The *chars*
Christian Heimesfe337bf2008-03-23 21:54:12 +00001046 argument is not a suffix; rather, all combinations of its values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001047
1048 >>> ' spacious '.rstrip()
1049 ' spacious'
1050 >>> 'mississippi'.rstrip('ipz')
1051 'mississ'
1052
Georg Brandl116aa622007-08-15 14:28:22 +00001053
Georg Brandl226878c2007-08-31 10:15:37 +00001054.. method:: str.split([sep[, maxsplit]])
Georg Brandl116aa622007-08-15 14:28:22 +00001055
Georg Brandl226878c2007-08-31 10:15:37 +00001056 Return a list of the words in the string, using *sep* as the delimiter
1057 string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
1058 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
1059 specified, then there is no limit on the number of splits (all possible
Georg Brandl9afde1c2007-11-01 20:32:30 +00001060 splits are made).
1061
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001062 If *sep* is given, consecutive delimiters are not grouped together and are
Georg Brandl226878c2007-08-31 10:15:37 +00001063 deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
1064 ``['1', '', '2']``). The *sep* argument may consist of multiple characters
Georg Brandl9afde1c2007-11-01 20:32:30 +00001065 (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Georg Brandl226878c2007-08-31 10:15:37 +00001066 Splitting an empty string with a specified separator returns ``['']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001067
1068 If *sep* is not specified or is ``None``, a different splitting algorithm is
Georg Brandl9afde1c2007-11-01 20:32:30 +00001069 applied: runs of consecutive whitespace are regarded as a single separator,
1070 and the result will contain no empty strings at the start or end if the
1071 string has leading or trailing whitespace. Consequently, splitting an empty
1072 string or a string consisting of just whitespace with a ``None`` separator
1073 returns ``[]``.
1074
1075 For example, ``' 1 2 3 '.split()`` returns ``['1', '2', '3']``, and
1076 ``' 1 2 3 '.split(None, 1)`` returns ``['1', '2 3 ']``.
Georg Brandl116aa622007-08-15 14:28:22 +00001077
1078
1079.. method:: str.splitlines([keepends])
1080
1081 Return a list of the lines in the string, breaking at line boundaries. Line
1082 breaks are not included in the resulting list unless *keepends* is given and
1083 true.
1084
1085
1086.. method:: str.startswith(prefix[, start[, end]])
1087
1088 Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
1089 *prefix* can also be a tuple of prefixes to look for. With optional *start*,
1090 test string beginning at that position. With optional *end*, stop comparing
1091 string at that position.
1092
Georg Brandl116aa622007-08-15 14:28:22 +00001093
1094.. method:: str.strip([chars])
1095
1096 Return a copy of the string with the leading and trailing characters removed.
1097 The *chars* argument is a string specifying the set of characters to be removed.
1098 If omitted or ``None``, the *chars* argument defaults to removing whitespace.
1099 The *chars* argument is not a prefix or suffix; rather, all combinations of its
Christian Heimesfe337bf2008-03-23 21:54:12 +00001100 values are stripped:
Georg Brandl116aa622007-08-15 14:28:22 +00001101
1102 >>> ' spacious '.strip()
1103 'spacious'
1104 >>> 'www.example.com'.strip('cmowz.')
1105 'example'
1106
Georg Brandl116aa622007-08-15 14:28:22 +00001107
1108.. method:: str.swapcase()
1109
1110 Return a copy of the string with uppercase characters converted to lowercase and
1111 vice versa.
1112
Georg Brandl116aa622007-08-15 14:28:22 +00001113
1114.. method:: str.title()
1115
1116 Return a titlecased version of the string: words start with uppercase
1117 characters, all remaining cased characters are lowercase.
1118
Georg Brandl116aa622007-08-15 14:28:22 +00001119
Georg Brandl4b491312007-08-31 09:22:56 +00001120.. method:: str.translate(map)
Georg Brandl116aa622007-08-15 14:28:22 +00001121
Georg Brandl226878c2007-08-31 10:15:37 +00001122 Return a copy of the *s* where all characters have been mapped through the
Georg Brandlceee0772007-11-27 23:48:05 +00001123 *map* which must be a dictionary of Unicode ordinals(integers) to Unicode
1124 ordinals, strings or ``None``. Unmapped characters are left untouched.
1125 Characters mapped to ``None`` are deleted.
1126
Benjamin Petersond9c03e02008-12-13 03:03:41 +00001127 You can use :meth:`str.maketrans` to create a translation table. For string
1128 objects, set the *table* argument to ``None`` for translations that only
1129 delete characters:
Christian Heimesfe337bf2008-03-23 21:54:12 +00001130
Georg Brandl4b491312007-08-31 09:22:56 +00001131 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +00001132
Georg Brandlceee0772007-11-27 23:48:05 +00001133 An even more flexible approach is to create a custom character mapping
1134 codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
Georg Brandl4b491312007-08-31 09:22:56 +00001135 example).
Georg Brandl116aa622007-08-15 14:28:22 +00001136
1137
1138.. method:: str.upper()
1139
1140 Return a copy of the string converted to uppercase.
1141
Georg Brandl116aa622007-08-15 14:28:22 +00001142
1143.. method:: str.zfill(width)
1144
Georg Brandl9afde1c2007-11-01 20:32:30 +00001145 Return the numeric string left filled with zeros in a string of length
1146 *width*. A sign prefix is handled correctly. The original string is
1147 returned if *width* is less than ``len(s)``.
Christian Heimesb186d002008-03-18 15:15:01 +00001148
1149
Georg Brandl116aa622007-08-15 14:28:22 +00001150
Georg Brandl4b491312007-08-31 09:22:56 +00001151.. _old-string-formatting:
Georg Brandl116aa622007-08-15 14:28:22 +00001152
Georg Brandl4b491312007-08-31 09:22:56 +00001153Old String Formatting Operations
1154--------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00001155
1156.. index::
1157 single: formatting, string (%)
1158 single: interpolation, string (%)
1159 single: string; formatting
1160 single: string; interpolation
1161 single: printf-style formatting
1162 single: sprintf-style formatting
1163 single: % formatting
1164 single: % interpolation
1165
Georg Brandl81ac1ce2007-08-31 17:17:17 +00001166.. XXX is the note enough?
Georg Brandl4b491312007-08-31 09:22:56 +00001167
1168.. note::
1169
Georg Brandl226878c2007-08-31 10:15:37 +00001170 The formatting operations described here are obsolete and may go away in future
Georg Brandl4b491312007-08-31 09:22:56 +00001171 versions of Python. Use the new :ref:`string-formatting` in new code.
1172
1173String objects have one unique built-in operation: the ``%`` operator (modulo).
1174This is also known as the string *formatting* or *interpolation* operator.
1175Given ``format % values`` (where *format* is a string), ``%`` conversion
1176specifications in *format* are replaced with zero or more elements of *values*.
1177The effect is similar to the using :cfunc:`sprintf` in the C language.
Georg Brandl116aa622007-08-15 14:28:22 +00001178
1179If *format* requires a single argument, *values* may be a single non-tuple
1180object. [#]_ Otherwise, *values* must be a tuple with exactly the number of
1181items specified by the format string, or a single mapping object (for example, a
1182dictionary).
1183
1184A conversion specifier contains two or more characters and has the following
1185components, which must occur in this order:
1186
1187#. The ``'%'`` character, which marks the start of the specifier.
1188
1189#. Mapping key (optional), consisting of a parenthesised sequence of characters
1190 (for example, ``(somename)``).
1191
1192#. Conversion flags (optional), which affect the result of some conversion
1193 types.
1194
1195#. Minimum field width (optional). If specified as an ``'*'`` (asterisk), the
1196 actual width is read from the next element of the tuple in *values*, and the
1197 object to convert comes after the minimum field width and optional precision.
1198
1199#. Precision (optional), given as a ``'.'`` (dot) followed by the precision. If
1200 specified as ``'*'`` (an asterisk), the actual width is read from the next
1201 element of the tuple in *values*, and the value to convert comes after the
1202 precision.
1203
1204#. Length modifier (optional).
1205
1206#. Conversion type.
1207
1208When the right argument is a dictionary (or other mapping type), then the
1209formats in the string *must* include a parenthesised mapping key into that
1210dictionary inserted immediately after the ``'%'`` character. The mapping key
Christian Heimesfe337bf2008-03-23 21:54:12 +00001211selects the value to be formatted from the mapping. For example:
Georg Brandl116aa622007-08-15 14:28:22 +00001212
Christian Heimesfe337bf2008-03-23 21:54:12 +00001213
1214 >>> print('%(language)s has %(#)03d quote types.' % \
1215 ... {'language': "Python", "#": 2})
Georg Brandl116aa622007-08-15 14:28:22 +00001216 Python has 002 quote types.
1217
1218In this case no ``*`` specifiers may occur in a format (since they require a
1219sequential parameter list).
1220
1221The conversion flag characters are:
1222
1223+---------+---------------------------------------------------------------------+
1224| Flag | Meaning |
1225+=========+=====================================================================+
1226| ``'#'`` | The value conversion will use the "alternate form" (where defined |
1227| | below). |
1228+---------+---------------------------------------------------------------------+
1229| ``'0'`` | The conversion will be zero padded for numeric values. |
1230+---------+---------------------------------------------------------------------+
1231| ``'-'`` | The converted value is left adjusted (overrides the ``'0'`` |
1232| | conversion if both are given). |
1233+---------+---------------------------------------------------------------------+
1234| ``' '`` | (a space) A blank should be left before a positive number (or empty |
1235| | string) produced by a signed conversion. |
1236+---------+---------------------------------------------------------------------+
1237| ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion |
1238| | (overrides a "space" flag). |
1239+---------+---------------------------------------------------------------------+
1240
1241A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001242is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
Georg Brandl116aa622007-08-15 14:28:22 +00001243
1244The conversion types are:
1245
1246+------------+-----------------------------------------------------+-------+
1247| Conversion | Meaning | Notes |
1248+============+=====================================================+=======+
1249| ``'d'`` | Signed integer decimal. | |
1250+------------+-----------------------------------------------------+-------+
1251| ``'i'`` | Signed integer decimal. | |
1252+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001253| ``'o'`` | Signed octal value. | \(1) |
Georg Brandl116aa622007-08-15 14:28:22 +00001254+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001255| ``'u'`` | Obselete type -- it is identical to ``'d'``. | \(7) |
Georg Brandl116aa622007-08-15 14:28:22 +00001256+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001257| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001258+------------+-----------------------------------------------------+-------+
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001259| ``'X'`` | Signed hexadecimal (uppercase). | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001260+------------+-----------------------------------------------------+-------+
1261| ``'e'`` | Floating point exponential format (lowercase). | \(3) |
1262+------------+-----------------------------------------------------+-------+
1263| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
1264+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00001265| ``'f'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001266+------------+-----------------------------------------------------+-------+
Eric Smith22b85b32008-07-17 19:18:29 +00001267| ``'F'`` | Floating point decimal format. | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001268+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00001269| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
1270| | format if exponent is less than -4 or not less than | |
1271| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00001272+------------+-----------------------------------------------------+-------+
Christian Heimes8dc226f2008-05-06 23:45:46 +00001273| ``'G'`` | Floating point format. Uses uppercase exponential | \(4) |
1274| | format if exponent is less than -4 or not less than | |
1275| | precision, decimal format otherwise. | |
Georg Brandl116aa622007-08-15 14:28:22 +00001276+------------+-----------------------------------------------------+-------+
1277| ``'c'`` | Single character (accepts integer or single | |
1278| | character string). | |
1279+------------+-----------------------------------------------------+-------+
1280| ``'r'`` | String (converts any python object using | \(5) |
1281| | :func:`repr`). | |
1282+------------+-----------------------------------------------------+-------+
Georg Brandl4b491312007-08-31 09:22:56 +00001283| ``'s'`` | String (converts any python object using | |
Georg Brandl116aa622007-08-15 14:28:22 +00001284| | :func:`str`). | |
1285+------------+-----------------------------------------------------+-------+
1286| ``'%'`` | No argument is converted, results in a ``'%'`` | |
1287| | character in the result. | |
1288+------------+-----------------------------------------------------+-------+
1289
1290Notes:
1291
1292(1)
1293 The alternate form causes a leading zero (``'0'``) to be inserted between
1294 left-hand padding and the formatting of the number if the leading character
1295 of the result is not already a zero.
1296
1297(2)
1298 The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
1299 the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
1300 and the formatting of the number if the leading character of the result is not
1301 already a zero.
1302
1303(3)
1304 The alternate form causes the result to always contain a decimal point, even if
1305 no digits follow it.
1306
1307 The precision determines the number of digits after the decimal point and
1308 defaults to 6.
1309
1310(4)
1311 The alternate form causes the result to always contain a decimal point, and
1312 trailing zeroes are not removed as they would otherwise be.
1313
1314 The precision determines the number of significant digits before and after the
1315 decimal point and defaults to 6.
1316
1317(5)
Georg Brandl116aa622007-08-15 14:28:22 +00001318 The precision determines the maximal number of characters used.
1319
Georg Brandl116aa622007-08-15 14:28:22 +00001320
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001321(7)
1322 See :pep:`237`.
1323
Georg Brandl116aa622007-08-15 14:28:22 +00001324Since Python strings have an explicit length, ``%s`` conversions do not assume
1325that ``'\0'`` is the end of the string.
1326
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001327.. XXX Examples?
1328
Georg Brandl116aa622007-08-15 14:28:22 +00001329For safety reasons, floating point precisions are clipped to 50; ``%f``
1330conversions for numbers whose absolute value is over 1e25 are replaced by ``%g``
1331conversions. [#]_ All other errors raise exceptions.
1332
1333.. index::
1334 module: string
1335 module: re
1336
1337Additional string operations are defined in standard modules :mod:`string` and
1338:mod:`re`.
1339
1340
1341.. _typesseq-range:
1342
Georg Brandl905ec322007-09-28 13:39:25 +00001343Range Type
1344----------
Georg Brandl116aa622007-08-15 14:28:22 +00001345
1346.. index:: object: range
1347
1348The :class:`range` type is an immutable sequence which is commonly used for
1349looping. The advantage of the :class:`range` type is that an :class:`range`
1350object will always take the same amount of memory, no matter the size of the
1351range it represents. There are no consistent performance advantages.
1352
Georg Brandl905ec322007-09-28 13:39:25 +00001353Range objects have very little behavior: they only support indexing, iteration,
Georg Brandl116aa622007-08-15 14:28:22 +00001354and the :func:`len` function.
1355
1356
1357.. _typesseq-mutable:
1358
1359Mutable Sequence Types
1360----------------------
1361
1362.. index::
1363 triple: mutable; sequence; types
1364 object: list
Georg Brandl95414632007-11-22 11:00:28 +00001365 object: bytearray
Georg Brandl116aa622007-08-15 14:28:22 +00001366
Georg Brandl95414632007-11-22 11:00:28 +00001367List and bytearray objects support additional operations that allow in-place
Georg Brandl226878c2007-08-31 10:15:37 +00001368modification of the object. Other mutable sequence types (when added to the
1369language) should also support these operations. Strings and tuples are
1370immutable sequence types: such objects cannot be modified once created. The
1371following operations are defined on mutable sequence types (where *x* is an
1372arbitrary object).
1373
Georg Brandl95414632007-11-22 11:00:28 +00001374Note that while lists allow their items to be of any type, bytearray object
Georg Brandl226878c2007-08-31 10:15:37 +00001375"items" are all integers in the range 0 <= x < 256.
Georg Brandl116aa622007-08-15 14:28:22 +00001376
1377+------------------------------+--------------------------------+---------------------+
1378| Operation | Result | Notes |
1379+==============================+================================+=====================+
1380| ``s[i] = x`` | item *i* of *s* is replaced by | |
1381| | *x* | |
1382+------------------------------+--------------------------------+---------------------+
1383| ``s[i:j] = t`` | slice of *s* from *i* to *j* | |
1384| | is replaced by the contents of | |
1385| | the iterable *t* | |
1386+------------------------------+--------------------------------+---------------------+
1387| ``del s[i:j]`` | same as ``s[i:j] = []`` | |
1388+------------------------------+--------------------------------+---------------------+
1389| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` | \(1) |
1390| | are replaced by those of *t* | |
1391+------------------------------+--------------------------------+---------------------+
1392| ``del s[i:j:k]`` | removes the elements of | |
1393| | ``s[i:j:k]`` from the list | |
1394+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001395| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |
Georg Brandl116aa622007-08-15 14:28:22 +00001396| | [x]`` | |
1397+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001398| ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +00001399| | x`` | |
1400+------------------------------+--------------------------------+---------------------+
1401| ``s.count(x)`` | return number of *i*'s for | |
1402| | which ``s[i] == x`` | |
1403+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001404| ``s.index(x[, i[, j]])`` | return smallest *k* such that | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001405| | ``s[k] == x`` and ``i <= k < | |
1406| | j`` | |
1407+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001408| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +00001409+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001410| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | \(5) |
Georg Brandl116aa622007-08-15 14:28:22 +00001411| | return x`` | |
1412+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001413| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | \(3) |
Georg Brandl116aa622007-08-15 14:28:22 +00001414+------------------------------+--------------------------------+---------------------+
Georg Brandl226878c2007-08-31 10:15:37 +00001415| ``s.reverse()`` | reverses the items of *s* in | \(6) |
Georg Brandl116aa622007-08-15 14:28:22 +00001416| | place | |
1417+------------------------------+--------------------------------+---------------------+
Raymond Hettinger7f732952008-02-14 13:34:38 +00001418| ``s.sort([key[, reverse]])`` | sort the items of *s* in place | (6), (7), (8) |
Georg Brandl116aa622007-08-15 14:28:22 +00001419+------------------------------+--------------------------------+---------------------+
1420
1421.. index::
1422 triple: operations on; sequence; types
1423 triple: operations on; list; type
1424 pair: subscript; assignment
1425 pair: slice; assignment
Georg Brandl116aa622007-08-15 14:28:22 +00001426 statement: del
Georg Brandl226878c2007-08-31 10:15:37 +00001427 single: append() (sequence method)
1428 single: extend() (sequence method)
1429 single: count() (sequence method)
1430 single: index() (sequence method)
1431 single: insert() (sequence method)
1432 single: pop() (sequence method)
1433 single: remove() (sequence method)
1434 single: reverse() (sequence method)
1435 single: sort() (sequence method)
Georg Brandl116aa622007-08-15 14:28:22 +00001436
1437Notes:
1438
1439(1)
Georg Brandl226878c2007-08-31 10:15:37 +00001440 *t* must have the same length as the slice it is replacing.
Georg Brandl116aa622007-08-15 14:28:22 +00001441
1442(2)
Georg Brandl116aa622007-08-15 14:28:22 +00001443 *x* can be any iterable object.
1444
Georg Brandl226878c2007-08-31 10:15:37 +00001445(3)
Georg Brandl116aa622007-08-15 14:28:22 +00001446 Raises :exc:`ValueError` when *x* is not found in *s*. When a negative index is
Georg Brandl226878c2007-08-31 10:15:37 +00001447 passed as the second or third parameter to the :meth:`index` method, the sequence
Georg Brandl116aa622007-08-15 14:28:22 +00001448 length is added, as for slice indices. If it is still negative, it is truncated
1449 to zero, as for slice indices.
1450
Georg Brandl226878c2007-08-31 10:15:37 +00001451(4)
Georg Brandl116aa622007-08-15 14:28:22 +00001452 When a negative index is passed as the first parameter to the :meth:`insert`
Georg Brandl226878c2007-08-31 10:15:37 +00001453 method, the sequence length is added, as for slice indices. If it is still
Georg Brandl116aa622007-08-15 14:28:22 +00001454 negative, it is truncated to zero, as for slice indices.
1455
Georg Brandl226878c2007-08-31 10:15:37 +00001456(5)
1457 The optional argument *i* defaults to ``-1``, so that by default the last
1458 item is removed and returned.
1459
Georg Brandl116aa622007-08-15 14:28:22 +00001460(6)
Georg Brandl226878c2007-08-31 10:15:37 +00001461 The :meth:`sort` and :meth:`reverse` methods modify the sequence in place for
1462 economy of space when sorting or reversing a large sequence. To remind you
1463 that they operate by side effect, they don't return the sorted or reversed
1464 sequence.
Georg Brandl116aa622007-08-15 14:28:22 +00001465
1466(7)
Georg Brandl116aa622007-08-15 14:28:22 +00001467 The :meth:`sort` method takes optional arguments for controlling the
Raymond Hettinger7f732952008-02-14 13:34:38 +00001468 comparisons. Each must be specified as a keyword argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001469
Georg Brandl116aa622007-08-15 14:28:22 +00001470 *key* specifies a function of one argument that is used to extract a comparison
Christian Heimesfaf2f632008-01-06 16:59:19 +00001471 key from each list element: ``key=str.lower``. The default value is ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001472
1473 *reverse* is a boolean value. If set to ``True``, then the list elements are
1474 sorted as if each comparison were reversed.
1475
Raymond Hettinger71161862008-02-14 13:32:18 +00001476 The :meth:`sort` method is guaranteed to be stable. A
Georg Brandl116aa622007-08-15 14:28:22 +00001477 sort is stable if it guarantees not to change the relative order of elements
1478 that compare equal --- this is helpful for sorting in multiple passes (for
1479 example, sort by department, then by salary grade).
1480
Georg Brandl116aa622007-08-15 14:28:22 +00001481 While a list is being sorted, the effect of attempting to mutate, or even
Raymond Hettinger71161862008-02-14 13:32:18 +00001482 inspect, the list is undefined. The C implementation
Georg Brandl116aa622007-08-15 14:28:22 +00001483 makes the list appear empty for the duration, and raises :exc:`ValueError` if it
1484 can detect that the list has been mutated during a sort.
1485
Raymond Hettinger7f732952008-02-14 13:34:38 +00001486(8)
1487 :meth:`sort` is not supported by :class:`bytearray` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001488
Georg Brandl226878c2007-08-31 10:15:37 +00001489.. _bytes-methods:
1490
Georg Brandl95414632007-11-22 11:00:28 +00001491Bytes and Byte Array Methods
1492----------------------------
Georg Brandl226878c2007-08-31 10:15:37 +00001493
1494.. index:: pair: bytes; methods
Georg Brandl95414632007-11-22 11:00:28 +00001495 pair: bytearray; methods
Georg Brandl226878c2007-08-31 10:15:37 +00001496
Georg Brandl95414632007-11-22 11:00:28 +00001497Bytes and bytearray objects, being "strings of bytes", have all methods found on
Georg Brandl7c676132007-10-23 18:17:00 +00001498strings, with the exception of :func:`encode`, :func:`format` and
Guido van Rossum98297ee2007-11-06 21:34:58 +00001499:func:`isidentifier`, which do not make sense with these types. For converting
1500the objects to strings, they have a :func:`decode` method.
1501
1502Wherever one of these methods needs to interpret the bytes as characters
1503(e.g. the :func:`is...` methods), the ASCII character set is assumed.
Georg Brandl226878c2007-08-31 10:15:37 +00001504
Georg Brandl7c676132007-10-23 18:17:00 +00001505.. note::
Georg Brandl226878c2007-08-31 10:15:37 +00001506
Georg Brandl95414632007-11-22 11:00:28 +00001507 The methods on bytes and bytearray objects don't accept strings as their
Georg Brandl7c676132007-10-23 18:17:00 +00001508 arguments, just as the methods on strings don't accept bytes as their
1509 arguments. For example, you have to write ::
Georg Brandl226878c2007-08-31 10:15:37 +00001510
Georg Brandl7c676132007-10-23 18:17:00 +00001511 a = "abc"
1512 b = a.replace("a", "f")
1513
1514 and ::
1515
1516 a = b"abc"
1517 b = a.replace(b"a", b"f")
Georg Brandl226878c2007-08-31 10:15:37 +00001518
1519
Georg Brandl95414632007-11-22 11:00:28 +00001520The bytes and bytearray types have an additional class method:
Georg Brandl226878c2007-08-31 10:15:37 +00001521
1522.. method:: bytes.fromhex(string)
Georg Brandl18da8f02008-07-01 20:08:02 +00001523 bytearray.fromhex(string)
Georg Brandl226878c2007-08-31 10:15:37 +00001524
Georg Brandl18da8f02008-07-01 20:08:02 +00001525 This :class:`bytes` class method returns a bytes or bytearray object,
1526 decoding the given string object. The string must contain two hexadecimal
1527 digits per byte, spaces are ignored.
Georg Brandl226878c2007-08-31 10:15:37 +00001528
Georg Brandl18da8f02008-07-01 20:08:02 +00001529 >>> bytes.fromhex('f0 f1f2 ')
1530 b'\xf0\xf1\xf2'
Georg Brandl226878c2007-08-31 10:15:37 +00001531
Georg Brandl7c676132007-10-23 18:17:00 +00001532.. XXX verify/document translate() semantics!
Georg Brandl226878c2007-08-31 10:15:37 +00001533
Georg Brandl7c676132007-10-23 18:17:00 +00001534 .. method:: bytes.translate(table[, delete])
Georg Brandl226878c2007-08-31 10:15:37 +00001535
1536 Return a copy of the bytes object where all bytes occurring in the optional
Georg Brandl7f13e6b2007-08-31 10:37:15 +00001537 argument *delete* are removed, and the remaining bytes have been mapped
Georg Brandl226878c2007-08-31 10:15:37 +00001538 through the given translation table, which must be a bytes object of length
1539 256.
1540
1541 You can use the :func:`maketrans` helper function in the :mod:`string` module to
1542 create a translation table.
1543
1544 .. XXX a None table doesn't seem to be supported
Georg Brandl7f13e6b2007-08-31 10:37:15 +00001545 Set the *table* argument to ``None`` for translations that only delete characters::
Georg Brandl226878c2007-08-31 10:15:37 +00001546
1547 >>> 'read this short text'.translate(None, 'aeiou')
1548 'rd ths shrt txt'
1549
1550
Georg Brandl116aa622007-08-15 14:28:22 +00001551.. _types-set:
1552
1553Set Types --- :class:`set`, :class:`frozenset`
1554==============================================
1555
1556.. index:: object: set
1557
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001558A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001559Common uses include membership testing, removing duplicates from a sequence, and
1560computing mathematical operations such as intersection, union, difference, and
1561symmetric difference.
1562(For other containers see the built in :class:`dict`, :class:`list`,
1563and :class:`tuple` classes, and the :mod:`collections` module.)
1564
Georg Brandl116aa622007-08-15 14:28:22 +00001565Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
1566set``. Being an unordered collection, sets do not record element position or
1567order of insertion. Accordingly, sets do not support indexing, slicing, or
1568other sequence-like behavior.
1569
1570There are currently two builtin set types, :class:`set` and :class:`frozenset`.
1571The :class:`set` type is mutable --- the contents can be changed using methods
1572like :meth:`add` and :meth:`remove`. Since it is mutable, it has no hash value
1573and cannot be used as either a dictionary key or as an element of another set.
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001574The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be
Georg Brandl116aa622007-08-15 14:28:22 +00001575altered after it is created; it can therefore be used as a dictionary key or as
1576an element of another set.
1577
1578The constructors for both classes work the same:
1579
1580.. class:: set([iterable])
1581 frozenset([iterable])
1582
1583 Return a new set or frozenset object whose elements are taken from
1584 *iterable*. The elements of a set must be hashable. To represent sets of
1585 sets, the inner sets must be :class:`frozenset` objects. If *iterable* is
1586 not specified, a new empty set is returned.
1587
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001588 Instances of :class:`set` and :class:`frozenset` provide the following
1589 operations:
Georg Brandl116aa622007-08-15 14:28:22 +00001590
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001591 .. describe:: len(s)
Georg Brandl116aa622007-08-15 14:28:22 +00001592
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001593 Return the cardinality of set *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001594
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001595 .. describe:: x in s
Georg Brandl116aa622007-08-15 14:28:22 +00001596
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001597 Test *x* for membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001598
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001599 .. describe:: x not in s
Georg Brandl116aa622007-08-15 14:28:22 +00001600
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001601 Test *x* for non-membership in *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001602
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001603 .. method:: isdisjoint(other)
Guido van Rossum58da9312007-11-10 23:39:45 +00001604
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001605 Return True if the set has no elements in common with *other*. Sets are
Georg Brandl2ee470f2008-07-16 12:55:28 +00001606 disjoint if and only if their intersection is the empty set.
Guido van Rossum58da9312007-11-10 23:39:45 +00001607
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001608 .. method:: issubset(other)
1609 set <= other
Georg Brandl116aa622007-08-15 14:28:22 +00001610
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001611 Test whether every element in the set is in *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00001612
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001613 .. method:: set < other
Georg Brandla6f52782007-09-01 15:49:30 +00001614
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001615 Test whether the set is a true subset of *other*, that is,
1616 ``set <= other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00001617
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001618 .. method:: issuperset(other)
1619 set >= other
Georg Brandl116aa622007-08-15 14:28:22 +00001620
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001621 Test whether every element in *other* is in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001622
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001623 .. method:: set > other
Georg Brandla6f52782007-09-01 15:49:30 +00001624
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001625 Test whether the set is a true superset of *other*, that is, ``set >=
1626 other and set != other``.
Georg Brandla6f52782007-09-01 15:49:30 +00001627
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001628 .. method:: union(other, ...)
1629 set | other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001630
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001631 Return a new set with elements from both sets.
Georg Brandl116aa622007-08-15 14:28:22 +00001632
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001633 .. method:: intersection(other, ...)
1634 set & other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00001635
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001636 Return a new set with elements common to both sets.
Georg Brandl116aa622007-08-15 14:28:22 +00001637
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001638 .. method:: difference(other, ...)
1639 set - other - ...
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001640
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001641 Return a new set with elements in the set that are not in the others.
Georg Brandl116aa622007-08-15 14:28:22 +00001642
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001643 .. method:: symmetric_difference(other)
1644 set ^ other
Georg Brandl116aa622007-08-15 14:28:22 +00001645
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001646 Return a new set with elements in either the set or *other* but not both.
Georg Brandl116aa622007-08-15 14:28:22 +00001647
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001648 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00001649
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001650 Return a new set with a shallow copy of *s*.
Georg Brandl116aa622007-08-15 14:28:22 +00001651
1652
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001653 Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
1654 :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
1655 :meth:`issuperset` methods will accept any iterable as an argument. In
1656 contrast, their operator based counterparts require their arguments to be
1657 sets. This precludes error-prone constructions like ``set('abc') & 'cbs'``
1658 in favor of the more readable ``set('abc').intersection('cbs')``.
Georg Brandl116aa622007-08-15 14:28:22 +00001659
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001660 Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
1661 sets are equal if and only if every element of each set is contained in the
1662 other (each is a subset of the other). A set is less than another set if and
1663 only if the first set is a proper subset of the second set (is a subset, but
1664 is not equal). A set is greater than another set if and only if the first set
1665 is a proper superset of the second set (is a superset, but is not equal).
Georg Brandl116aa622007-08-15 14:28:22 +00001666
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001667 Instances of :class:`set` are compared to instances of :class:`frozenset`
1668 based on their members. For example, ``set('abc') == frozenset('abc')``
1669 returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
Georg Brandl116aa622007-08-15 14:28:22 +00001670
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001671 The subset and equality comparisons do not generalize to a complete ordering
1672 function. For example, any two disjoint sets are not equal and are not
1673 subsets of each other, so *all* of the following return ``False``: ``a<b``,
Georg Brandl05f5ab72008-09-24 09:11:47 +00001674 ``a==b``, or ``a>b``.
Georg Brandl116aa622007-08-15 14:28:22 +00001675
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001676 Since sets only define partial ordering (subset relationships), the output of
1677 the :meth:`list.sort` method is undefined for lists of sets.
Georg Brandl116aa622007-08-15 14:28:22 +00001678
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001679 Set elements, like dictionary keys, must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +00001680
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001681 Binary operations that mix :class:`set` instances with :class:`frozenset`
1682 return the type of the first operand. For example: ``frozenset('ab') |
1683 set('bc')`` returns an instance of :class:`frozenset`.
Georg Brandl116aa622007-08-15 14:28:22 +00001684
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001685 The following table lists operations available for :class:`set` that do not
1686 apply to immutable instances of :class:`frozenset`:
Georg Brandl116aa622007-08-15 14:28:22 +00001687
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001688 .. method:: update(other, ...)
1689 set |= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001690
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001691 Update the set, adding elements from *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00001692
Georg Brandlc28e1fa2008-06-10 19:20:26 +00001693 .. method:: intersection_update(other, ...)
1694 set &= other & ...
Georg Brandl116aa622007-08-15 14:28:22 +00001695
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001696 Update the set, keeping only elements found in it and *other*.
Georg Brandl116aa622007-08-15 14:28:22 +00001697
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001698 .. method:: difference_update(other, ...)
1699 set -= other | ...
Georg Brandl116aa622007-08-15 14:28:22 +00001700
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001701 Update the set, removing elements found in others.
1702
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001703 .. method:: symmetric_difference_update(other)
1704 set ^= other
Georg Brandl116aa622007-08-15 14:28:22 +00001705
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001706 Update the set, keeping only elements found in either set, but not in both.
Georg Brandl116aa622007-08-15 14:28:22 +00001707
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001708 .. method:: add(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001709
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001710 Add element *elem* to the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001711
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001712 .. method:: remove(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001713
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001714 Remove element *elem* from the set. Raises :exc:`KeyError` if *elem* is
1715 not contained in the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001716
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001717 .. method:: discard(elem)
Georg Brandl116aa622007-08-15 14:28:22 +00001718
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001719 Remove element *elem* from the set if it is present.
Georg Brandl116aa622007-08-15 14:28:22 +00001720
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001721 .. method:: pop()
Georg Brandl116aa622007-08-15 14:28:22 +00001722
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001723 Remove and return an arbitrary element from the set. Raises
1724 :exc:`KeyError` if the set is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001725
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001726 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00001727
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001728 Remove all elements from the set.
Georg Brandl116aa622007-08-15 14:28:22 +00001729
1730
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001731 Note, the non-operator versions of the :meth:`update`,
1732 :meth:`intersection_update`, :meth:`difference_update`, and
1733 :meth:`symmetric_difference_update` methods will accept any iterable as an
1734 argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001735
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001736 Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
1737 :meth:`discard` methods may be a set. To support searching for an equivalent
1738 frozenset, the *elem* set is temporarily mutated during the search and then
1739 restored. During the search, the *elem* set should not be read or mutated
1740 since it does not have a meaningful value.
Benjamin Peterson699adb92008-05-08 22:27:58 +00001741
Georg Brandl116aa622007-08-15 14:28:22 +00001742
1743.. _typesmapping:
1744
1745Mapping Types --- :class:`dict`
1746===============================
1747
1748.. index::
1749 object: mapping
1750 object: dictionary
1751 triple: operations on; mapping; types
1752 triple: operations on; dictionary; type
1753 statement: del
1754 builtin: len
1755
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001756A :dfn:`mapping` object maps :term:`hashable` values to arbitrary objects.
1757Mappings are mutable objects. There is currently only one standard mapping
1758type, the :dfn:`dictionary`. (For other containers see the built in
1759:class:`list`, :class:`set`, and :class:`tuple` classes, and the
1760:mod:`collections` module.)
Georg Brandl116aa622007-08-15 14:28:22 +00001761
Guido van Rossum2cc30da2007-11-02 23:46:40 +00001762A dictionary's keys are *almost* arbitrary values. Values that are not
1763:term:`hashable`, that is, values containing lists, dictionaries or other
1764mutable types (that are compared by value rather than by object identity) may
1765not be used as keys. Numeric types used for keys obey the normal rules for
1766numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
1767then they can be used interchangeably to index the same dictionary entry. (Note
1768however, that since computers store floating-point numbers as approximations it
1769is usually unwise to use them as dictionary keys.)
Georg Brandl116aa622007-08-15 14:28:22 +00001770
1771Dictionaries can be created by placing a comma-separated list of ``key: value``
1772pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
1773'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
1774
1775.. class:: dict([arg])
1776
Georg Brandld22a8152007-09-04 17:43:37 +00001777 Return a new dictionary initialized from an optional positional argument or
1778 from a set of keyword arguments. If no arguments are given, return a new
1779 empty dictionary. If the positional argument *arg* is a mapping object,
1780 return a dictionary mapping the same keys to the same values as does the
1781 mapping object. Otherwise the positional argument must be a sequence, a
1782 container that supports iteration, or an iterator object. The elements of
1783 the argument must each also be of one of those kinds, and each must in turn
1784 contain exactly two objects. The first is used as a key in the new
1785 dictionary, and the second as the key's value. If a given key is seen more
1786 than once, the last value associated with it is retained in the new
1787 dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00001788
1789 If keyword arguments are given, the keywords themselves with their associated
Georg Brandld22a8152007-09-04 17:43:37 +00001790 values are added as items to the dictionary. If a key is specified both in
1791 the positional argument and as a keyword argument, the value associated with
1792 the keyword is retained in the dictionary. For example, these all return a
Georg Brandl116aa622007-08-15 14:28:22 +00001793 dictionary equal to ``{"one": 2, "two": 3}``:
1794
1795 * ``dict(one=2, two=3)``
Georg Brandl116aa622007-08-15 14:28:22 +00001796 * ``dict({'one': 2, 'two': 3})``
Georg Brandl116aa622007-08-15 14:28:22 +00001797 * ``dict(zip(('one', 'two'), (2, 3)))``
Georg Brandl116aa622007-08-15 14:28:22 +00001798 * ``dict([['two', 3], ['one', 2]])``
1799
Georg Brandld22a8152007-09-04 17:43:37 +00001800 The first example only works for keys that are valid Python identifiers; the
1801 others work with any valid keys.
Georg Brandl116aa622007-08-15 14:28:22 +00001802
Georg Brandl116aa622007-08-15 14:28:22 +00001803
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001804 These are the operations that dictionaries support (and therefore, custom
1805 mapping types should support too):
Georg Brandl116aa622007-08-15 14:28:22 +00001806
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001807 .. describe:: len(d)
Georg Brandl116aa622007-08-15 14:28:22 +00001808
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001809 Return the number of items in the dictionary *d*.
Georg Brandl116aa622007-08-15 14:28:22 +00001810
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001811 .. describe:: d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00001812
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001813 Return the item of *d* with key *key*. Raises a :exc:`KeyError` if *key* is
1814 not in the map.
1815
1816 If a subclass of dict defines a method :meth:`__missing__`, if the key *key*
1817 is not present, the ``d[key]`` operation calls that method with the key *key*
1818 as argument. The ``d[key]`` operation then returns or raises whatever is
1819 returned or raised by the ``__missing__(key)`` call if the key is not
1820 present. No other operations or methods invoke :meth:`__missing__`. If
1821 :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
1822 :meth:`__missing__` must be a method; it cannot be an instance variable. For
1823 an example, see :class:`collections.defaultdict`.
Georg Brandl116aa622007-08-15 14:28:22 +00001824
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001825 .. describe:: d[key] = value
Georg Brandl116aa622007-08-15 14:28:22 +00001826
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001827 Set ``d[key]`` to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00001828
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001829 .. describe:: del d[key]
Georg Brandl116aa622007-08-15 14:28:22 +00001830
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001831 Remove ``d[key]`` from *d*. Raises a :exc:`KeyError` if *key* is not in the
1832 map.
Georg Brandl116aa622007-08-15 14:28:22 +00001833
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001834 .. describe:: key in d
Georg Brandl116aa622007-08-15 14:28:22 +00001835
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001836 Return ``True`` if *d* has a key *key*, else ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00001837
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001838 .. describe:: key not in d
Georg Brandl116aa622007-08-15 14:28:22 +00001839
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001840 Equivalent to ``not key in d``.
Georg Brandl116aa622007-08-15 14:28:22 +00001841
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001842 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +00001843
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001844 Remove all items from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00001845
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001846 .. method:: copy()
Georg Brandl116aa622007-08-15 14:28:22 +00001847
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001848 Return a shallow copy of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00001849
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001850 .. method:: fromkeys(seq[, value])
Georg Brandl116aa622007-08-15 14:28:22 +00001851
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001852 Create a new dictionary with keys from *seq* and values set to *value*.
Georg Brandl116aa622007-08-15 14:28:22 +00001853
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001854 :meth:`fromkeys` is a class method that returns a new dictionary. *value*
1855 defaults to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001856
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001857 .. method:: get(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00001858
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001859 Return the value for *key* if *key* is in the dictionary, else *default*.
1860 If *default* is not given, it defaults to ``None``, so that this method
1861 never raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00001862
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001863 .. method:: items()
Georg Brandl116aa622007-08-15 14:28:22 +00001864
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001865 Return a new view of the dictionary's items (``(key, value)`` pairs). See
1866 below for documentation of view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001867
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001868 .. method:: keys()
Georg Brandl116aa622007-08-15 14:28:22 +00001869
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001870 Return a new view of the dictionary's keys. See below for documentation of
1871 view objects.
Georg Brandl116aa622007-08-15 14:28:22 +00001872
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001873 .. method:: pop(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00001874
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001875 If *key* is in the dictionary, remove it and return its value, else return
1876 *default*. If *default* is not given and *key* is not in the dictionary,
1877 a :exc:`KeyError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001878
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001879 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +00001880
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001881 Remove and return an arbitrary ``(key, value)`` pair from the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +00001882
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001883 :meth:`popitem` is useful to destructively iterate over a dictionary, as
1884 often used in set algorithms. If the dictionary is empty, calling
1885 :meth:`popitem` raises a :exc:`KeyError`.
Georg Brandl116aa622007-08-15 14:28:22 +00001886
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001887 .. method:: setdefault(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +00001888
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001889 If *key* is in the dictionary, return its value. If not, insert *key*
1890 with a value of *default* and return *default*. *default* defaults to
1891 ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001892
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001893 .. method:: update([other])
Georg Brandl116aa622007-08-15 14:28:22 +00001894
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001895 Update the dictionary with the key/value pairs from *other*, overwriting
1896 existing keys. Return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00001897
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001898 :meth:`update` accepts either another dictionary object or an iterable of
1899 key/value pairs (as a tuple or other iterable of length two). If keyword
1900 arguments are specified, the dictionary is then is updated with those
1901 key/value pairs: ``d.update(red=1, blue=2)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001902
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001903 .. method:: values()
Georg Brandl116aa622007-08-15 14:28:22 +00001904
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +00001905 Return a new view of the dictionary's values. See below for documentation of
1906 view objects.
Georg Brandld22a8152007-09-04 17:43:37 +00001907
1908
Benjamin Peterson44309e62008-11-22 00:41:45 +00001909.. _dict-views:
1910
Georg Brandld22a8152007-09-04 17:43:37 +00001911Dictionary view objects
1912-----------------------
1913
1914The objects returned by :meth:`dict.keys`, :meth:`dict.values` and
1915:meth:`dict.items` are *view objects*. They provide a dynamic view on the
1916dictionary's entries, which means that when the dictionary changes, the view
Benjamin Petersonce0506c2008-11-17 21:47:41 +00001917reflects these changes.
Georg Brandld22a8152007-09-04 17:43:37 +00001918
1919Dictionary views can be iterated over to yield their respective data, and
1920support membership tests:
1921
1922.. describe:: len(dictview)
1923
1924 Return the number of entries in the dictionary.
1925
1926.. describe:: iter(dictview)
1927
1928 Return an iterator over the keys, values or items (represented as tuples of
1929 ``(key, value)``) in the dictionary.
1930
1931 Keys and values are iterated over in an arbitrary order which is non-random,
1932 varies across Python implementations, and depends on the dictionary's history
1933 of insertions and deletions. If keys, values and items views are iterated
1934 over with no intervening modifications to the dictionary, the order of items
1935 will directly correspond. This allows the creation of ``(value, key)`` pairs
1936 using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
1937 create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
1938
1939.. describe:: x in dictview
1940
1941 Return ``True`` if *x* is in the underlying dictionary's keys, values or
1942 items (in the latter case, *x* should be a ``(key, value)`` tuple).
1943
1944
Benjamin Petersonce0506c2008-11-17 21:47:41 +00001945Keys views are set-like since their entries are unique and hashable. If all
1946values are hashable, so that (key, value) pairs are unique and hashable, then
1947the items view is also set-like. (Values views are not treated as set-like
1948since the entries are generally not unique.) Then these set operations are
1949available ("other" refers either to another view or a set):
Georg Brandld22a8152007-09-04 17:43:37 +00001950
1951.. describe:: dictview & other
1952
1953 Return the intersection of the dictview and the other object as a new set.
1954
1955.. describe:: dictview | other
1956
1957 Return the union of the dictview and the other object as a new set.
1958
1959.. describe:: dictview - other
1960
1961 Return the difference between the dictview and the other object (all elements
1962 in *dictview* that aren't in *other*) as a new set.
1963
1964.. describe:: dictview ^ other
1965
1966 Return the symmetric difference (all elements either in *dictview* or
1967 *other*, but not in both) of the dictview and the other object as a new set.
1968
Georg Brandl116aa622007-08-15 14:28:22 +00001969
Georg Brandlc53c9662007-09-04 17:58:02 +00001970An example of dictionary view usage::
1971
1972 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
1973 >>> keys = dishes.keys()
1974 >>> values = dishes.values()
1975
1976 >>> # iteration
1977 >>> n = 0
1978 >>> for val in values:
1979 ... n += val
1980 >>> print(n)
1981 504
1982
1983 >>> # keys and values are iterated over in the same order
1984 >>> list(keys)
1985 ['eggs', 'bacon', 'sausage', 'spam']
1986 >>> list(values)
1987 [2, 1, 1, 500]
1988
1989 >>> # view objects are dynamic and reflect dict changes
1990 >>> del dishes['eggs']
1991 >>> del dishes['sausage']
1992 >>> list(keys)
1993 ['spam', 'bacon']
1994
1995 >>> # set operations
1996 >>> keys & {'eggs', 'bacon', 'salad'}
Gregory P. Smithe8388122008-09-04 04:18:09 +00001997 {'bacon'}
Georg Brandlc53c9662007-09-04 17:58:02 +00001998
1999
Georg Brandl116aa622007-08-15 14:28:22 +00002000.. _bltin-file-objects:
2001
2002File Objects
2003============
2004
2005.. index::
2006 object: file
2007 builtin: file
2008 module: os
2009 module: socket
2010
Georg Brandl81ac1ce2007-08-31 17:17:17 +00002011.. XXX this is quite out of date, must be updated with "io" module
2012
Georg Brandl116aa622007-08-15 14:28:22 +00002013File objects are implemented using C's ``stdio`` package and can be
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002014created with the built-in :func:`open` function. File
Georg Brandl116aa622007-08-15 14:28:22 +00002015objects are also returned by some other built-in functions and methods,
2016such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile`
Guido van Rossum2cc30da2007-11-02 23:46:40 +00002017method of socket objects. Temporary files can be created using the
2018:mod:`tempfile` module, and high-level file operations such as copying,
2019moving, and deleting files and directories can be achieved with the
2020:mod:`shutil` module.
Georg Brandl116aa622007-08-15 14:28:22 +00002021
2022When a file operation fails for an I/O-related reason, the exception
2023:exc:`IOError` is raised. This includes situations where the operation is not
2024defined for some reason, like :meth:`seek` on a tty device or writing a file
2025opened for reading.
2026
2027Files have the following methods:
2028
2029
2030.. method:: file.close()
2031
2032 Close the file. A closed file cannot be read or written any more. Any operation
2033 which requires that the file be open will raise a :exc:`ValueError` after the
2034 file has been closed. Calling :meth:`close` more than once is allowed.
2035
Georg Brandle6bcc912008-05-12 18:05:20 +00002036 You can avoid having to call this method explicitly if you use
Georg Brandl116aa622007-08-15 14:28:22 +00002037 the :keyword:`with` statement. For example, the following code will
Christian Heimes5b5e81c2007-12-31 16:14:33 +00002038 automatically close *f* when the :keyword:`with` block is exited::
Georg Brandl116aa622007-08-15 14:28:22 +00002039
Benjamin Petersona986dfa2008-07-31 21:10:28 +00002040 from __future__ import with_statement # This isn't required in Python 2.6
Georg Brandl116aa622007-08-15 14:28:22 +00002041
2042 with open("hello.txt") as f:
2043 for line in f:
Collin Winterc79461b2007-09-01 23:34:30 +00002044 print(line)
Georg Brandl116aa622007-08-15 14:28:22 +00002045
2046 In older versions of Python, you would have needed to do this to get the same
2047 effect::
2048
2049 f = open("hello.txt")
2050 try:
2051 for line in f:
Collin Winterc79461b2007-09-01 23:34:30 +00002052 print(line)
Georg Brandl116aa622007-08-15 14:28:22 +00002053 finally:
2054 f.close()
2055
2056 .. note::
2057
2058 Not all "file-like" types in Python support use as a context manager for the
2059 :keyword:`with` statement. If your code is intended to work with any file-like
2060 object, you can use the function :func:`contextlib.closing` instead of using
2061 the object directly.
2062
2063
2064.. method:: file.flush()
2065
2066 Flush the internal buffer, like ``stdio``'s :cfunc:`fflush`. This may be a
2067 no-op on some file-like objects.
2068
2069
2070.. method:: file.fileno()
2071
2072 .. index::
Georg Brandl9afde1c2007-11-01 20:32:30 +00002073 pair: file; descriptor
Georg Brandl116aa622007-08-15 14:28:22 +00002074 module: fcntl
2075
2076 Return the integer "file descriptor" that is used by the underlying
2077 implementation to request I/O operations from the operating system. This can be
2078 useful for other, lower level interfaces that use file descriptors, such as the
2079 :mod:`fcntl` module or :func:`os.read` and friends.
2080
2081 .. note::
2082
2083 File-like objects which do not have a real file descriptor should *not* provide
2084 this method!
2085
2086
2087.. method:: file.isatty()
2088
2089 Return ``True`` if the file is connected to a tty(-like) device, else ``False``.
2090
2091 .. note::
2092
2093 If a file-like object is not associated with a real file, this method should
2094 *not* be implemented.
2095
2096
2097.. method:: file.__next__()
2098
2099 A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
2100 *f* is closed). When a file is used as an iterator, typically in a
Georg Brandl6911e3c2007-09-04 07:15:32 +00002101 :keyword:`for` loop (for example, ``for line in f: print(line)``), the
Georg Brandl116aa622007-08-15 14:28:22 +00002102 :meth:`__next__` method is called repeatedly. This method returns the next
2103 input line, or raises :exc:`StopIteration` when EOF is hit when the file is open
2104 for reading (behavior is undefined when the file is open for writing). In order
2105 to make a :keyword:`for` loop the most efficient way of looping over the lines
2106 of a file (a very common operation), the :meth:`__next__` method uses a hidden
2107 read-ahead buffer. As a consequence of using a read-ahead buffer, combining
2108 :meth:`__next__` with other file methods (like :meth:`readline`) does not work
2109 right. However, using :meth:`seek` to reposition the file to an absolute
2110 position will flush the read-ahead buffer.
2111
Georg Brandl116aa622007-08-15 14:28:22 +00002112
2113.. method:: file.read([size])
2114
2115 Read at most *size* bytes from the file (less if the read hits EOF before
2116 obtaining *size* bytes). If the *size* argument is negative or omitted, read
2117 all data until EOF is reached. The bytes are returned as a string object. An
2118 empty string is returned when EOF is encountered immediately. (For certain
2119 files, like ttys, it makes sense to continue reading after an EOF is hit.) Note
2120 that this method may call the underlying C function :cfunc:`fread` more than
2121 once in an effort to acquire as close to *size* bytes as possible. Also note
Georg Brandl86b2fb92008-07-16 03:43:04 +00002122 that when in non-blocking mode, less data than was requested may be
Georg Brandl116aa622007-08-15 14:28:22 +00002123 returned, even if no *size* parameter was given.
2124
2125
2126.. method:: file.readline([size])
2127
2128 Read one entire line from the file. A trailing newline character is kept in the
2129 string (but may be absent when a file ends with an incomplete line). [#]_ If
2130 the *size* argument is present and non-negative, it is a maximum byte count
2131 (including the trailing newline) and an incomplete line may be returned. An
2132 empty string is returned *only* when EOF is encountered immediately.
2133
2134 .. note::
2135
2136 Unlike ``stdio``'s :cfunc:`fgets`, the returned string contains null characters
2137 (``'\0'``) if they occurred in the input.
2138
2139
2140.. method:: file.readlines([sizehint])
2141
2142 Read until EOF using :meth:`readline` and return a list containing the lines
2143 thus read. If the optional *sizehint* argument is present, instead of
2144 reading up to EOF, whole lines totalling approximately *sizehint* bytes
2145 (possibly after rounding up to an internal buffer size) are read. Objects
2146 implementing a file-like interface may choose to ignore *sizehint* if it
2147 cannot be implemented, or cannot be implemented efficiently.
2148
2149
2150.. method:: file.seek(offset[, whence])
2151
2152 Set the file's current position, like ``stdio``'s :cfunc:`fseek`. The *whence*
2153 argument is optional and defaults to ``os.SEEK_SET`` or ``0`` (absolute file
2154 positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the
2155 current position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's
Christian Heimesfaf2f632008-01-06 16:59:19 +00002156 end). There is no return value.
2157
2158 For example, ``f.seek(2, os.SEEK_CUR)`` advances the position by two and
2159 ``f.seek(-3, os.SEEK_END)`` sets the position to the third to last.
2160
2161 Note that if the file is opened for appending
Georg Brandl116aa622007-08-15 14:28:22 +00002162 (mode ``'a'`` or ``'a+'``), any :meth:`seek` operations will be undone at the
2163 next write. If the file is only opened for writing in append mode (mode
2164 ``'a'``), this method is essentially a no-op, but it remains useful for files
2165 opened in append mode with reading enabled (mode ``'a+'``). If the file is
2166 opened in text mode (without ``'b'``), only offsets returned by :meth:`tell` are
2167 legal. Use of other offsets causes undefined behavior.
2168
2169 Note that not all file objects are seekable.
2170
Georg Brandl116aa622007-08-15 14:28:22 +00002171
2172.. method:: file.tell()
2173
2174 Return the file's current position, like ``stdio``'s :cfunc:`ftell`.
2175
2176 .. note::
2177
2178 On Windows, :meth:`tell` can return illegal values (after an :cfunc:`fgets`)
2179 when reading files with Unix-style line-endings. Use binary mode (``'rb'``) to
2180 circumvent this problem.
2181
2182
2183.. method:: file.truncate([size])
2184
2185 Truncate the file's size. If the optional *size* argument is present, the file
2186 is truncated to (at most) that size. The size defaults to the current position.
2187 The current file position is not changed. Note that if a specified size exceeds
2188 the file's current size, the result is platform-dependent: possibilities
2189 include that the file may remain unchanged, increase to the specified size as if
2190 zero-filled, or increase to the specified size with undefined new content.
2191 Availability: Windows, many Unix variants.
2192
2193
2194.. method:: file.write(str)
2195
Georg Brandl38889802008-03-21 19:42:31 +00002196 Write a string to the file. Due to buffering, the string may not actually
2197 show up in the file until the :meth:`flush` or :meth:`close` method is
2198 called.
2199
2200 The meaning of the return value is not defined for every file-like object.
2201 Some (mostly low-level) file-like objects may return the number of bytes
2202 actually written, others return ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +00002203
2204
2205.. method:: file.writelines(sequence)
2206
2207 Write a sequence of strings to the file. The sequence can be any iterable
2208 object producing strings, typically a list of strings. There is no return value.
2209 (The name is intended to match :meth:`readlines`; :meth:`writelines` does not
2210 add line separators.)
2211
2212Files support the iterator protocol. Each iteration returns the same result as
2213``file.readline()``, and iteration ends when the :meth:`readline` method returns
2214an empty string.
2215
2216File objects also offer a number of other interesting attributes. These are not
2217required for file-like objects, but should be implemented if they make sense for
2218the particular object.
2219
2220
2221.. attribute:: file.closed
2222
2223 bool indicating the current state of the file object. This is a read-only
2224 attribute; the :meth:`close` method changes the value. It may not be available
2225 on all file-like objects.
2226
2227
Georg Brandl4b491312007-08-31 09:22:56 +00002228.. XXX does this still apply?
Georg Brandl116aa622007-08-15 14:28:22 +00002229.. attribute:: file.encoding
2230
Georg Brandlf6945182008-02-01 11:56:49 +00002231 The encoding that this file uses. When strings are written to a file,
Georg Brandl116aa622007-08-15 14:28:22 +00002232 they will be converted to byte strings using this encoding. In addition, when
2233 the file is connected to a terminal, the attribute gives the encoding that the
2234 terminal is likely to use (that information might be incorrect if the user has
2235 misconfigured the terminal). The attribute is read-only and may not be present
2236 on all file-like objects. It may also be ``None``, in which case the file uses
Georg Brandlf6945182008-02-01 11:56:49 +00002237 the system default encoding for converting strings.
Georg Brandl116aa622007-08-15 14:28:22 +00002238
Georg Brandl116aa622007-08-15 14:28:22 +00002239
Benjamin Petersondcf97b92008-07-02 17:30:14 +00002240.. attribute:: file.errors
2241
2242 The Unicode error handler used along with the encoding.
2243
2244
Georg Brandl116aa622007-08-15 14:28:22 +00002245.. attribute:: file.mode
2246
2247 The I/O mode for the file. If the file was created using the :func:`open`
2248 built-in function, this will be the value of the *mode* parameter. This is a
2249 read-only attribute and may not be present on all file-like objects.
2250
2251
2252.. attribute:: file.name
2253
2254 If the file object was created using :func:`open`, the name of the file.
2255 Otherwise, some string that indicates the source of the file object, of the
2256 form ``<...>``. This is a read-only attribute and may not be present on all
2257 file-like objects.
2258
2259
2260.. attribute:: file.newlines
2261
2262 If Python was built with the :option:`--with-universal-newlines` option to
2263 :program:`configure` (the default) this read-only attribute exists, and for
2264 files opened in universal newline read mode it keeps track of the types of
2265 newlines encountered while reading the file. The values it can take are
2266 ``'\r'``, ``'\n'``, ``'\r\n'``, ``None`` (unknown, no newlines read yet) or a
2267 tuple containing all the newline types seen, to indicate that multiple newline
2268 conventions were encountered. For files not opened in universal newline read
2269 mode the value of this attribute will be ``None``.
2270
2271
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002272.. _typememoryview:
2273
2274memoryview Types
2275================
2276
2277:class:`memoryview`\s allow Python code to access the internal data of an object
Georg Brandl1009d392008-09-10 07:14:18 +00002278that supports the buffer protocol without copying. Memory can be interpreted as
2279simple bytes or complex data structures.
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
Benjamin Peterson5e19e442008-09-10 21:47:03 +00002287 ``len(view)`` returns the total number of bytes in the memoryview, *view*.
2288
Georg Brandl1009d392008-09-10 07:14:18 +00002289 A :class:`memoryview` supports slicing to expose its data. Taking a single
2290 index will return a single byte. Full slicing will result in a subview::
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002291
2292 >>> v = memoryview(b'abcefg')
2293 >>> v[1]
2294 b'b'
2295 >>> v[-1]
2296 b'g'
2297 >>> v[1:4]
2298 <memory at 0x77ab28>
2299 >>> bytes(v[1:4])
2300 b'bce'
2301 >>> v[3:-1]
2302 <memory at 0x744f18>
2303 >>> bytes(v[4:-1])
2304
Georg Brandl1009d392008-09-10 07:14:18 +00002305 If the object the memory view is over supports changing its data, the
2306 memoryview supports slice assignment::
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002307
2308 >>> data = bytearray(b'abcefg')
2309 >>> v = memoryview(data)
2310 >>> v.readonly
2311 False
2312 >>> v[0] = 'z'
2313 >>> data
2314 bytearray(b'zbcefg')
2315 >>> v[1:4] = b'123'
2316 >>> data
2317 bytearray(b'a123fg')
2318 >>> v[2] = b'spam'
2319 Traceback (most recent call last):
2320 File "<stdin>", line 1, in <module>
2321 ValueError: cannot modify size of memoryview object
2322
2323 Notice how the size of the memoryview object can not be changed.
2324
2325
Benjamin Peterson0c804652008-09-10 21:31:58 +00002326 :class:`memoryview` has two methods:
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002327
2328 .. method:: tobytes()
2329
Benjamin Peterson0c804652008-09-10 21:31:58 +00002330 Return the data in the buffer as a bytestring.
2331
2332 .. method:: tolist()
2333
2334 Return the data in the buffer as a list of integers. ::
2335
2336 >>> memoryview(b'abc').tolist()
2337 [97, 98, 99]
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002338
2339 There are also several readonly attributes available:
2340
2341 .. attribute:: format
2342
2343 A string containing the format (in :mod:`struct` module style) for each
2344 element in the view. This defaults to ``'B'``, a simple bytestring.
2345
2346 .. attribute:: itemsize
2347
2348 The size in bytes of each element of the memoryview.
2349
2350 .. attribute:: shape
2351
Georg Brandl1009d392008-09-10 07:14:18 +00002352 A tuple of integers the length of :attr:`ndim` giving the shape of the
2353 memory as a N-dimensional array.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002354
2355 .. attribute:: ndim
2356
2357 An integer indicating how many dimensions of a multi-dimensional array the
2358 memory represents.
2359
2360 .. attribute:: strides
2361
Benjamin Peterson2409dc72008-09-10 21:38:31 +00002362 A tuple of integers the length of :attr:`ndim` giving the size in bytes to
2363 access each element for each dimension of the array.
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002364
Benjamin Peterson1b25b922008-09-09 22:15:27 +00002365 .. memoryview.suboffsets isn't documented because it only seems useful for C
2366
2367
Georg Brandl116aa622007-08-15 14:28:22 +00002368.. _typecontextmanager:
2369
2370Context Manager Types
2371=====================
2372
Georg Brandl116aa622007-08-15 14:28:22 +00002373.. index::
2374 single: context manager
2375 single: context management protocol
2376 single: protocol; context management
2377
2378Python's :keyword:`with` statement supports the concept of a runtime context
2379defined by a context manager. This is implemented using two separate methods
2380that allow user-defined classes to define a runtime context that is entered
2381before the statement body is executed and exited when the statement ends.
2382
2383The :dfn:`context management protocol` consists of a pair of methods that need
2384to be provided for a context manager object to define a runtime context:
2385
2386
2387.. method:: contextmanager.__enter__()
2388
2389 Enter the runtime context and return either this object or another object
2390 related to the runtime context. The value returned by this method is bound to
2391 the identifier in the :keyword:`as` clause of :keyword:`with` statements using
2392 this context manager.
2393
2394 An example of a context manager that returns itself is a file object. File
2395 objects return themselves from __enter__() to allow :func:`open` to be used as
2396 the context expression in a :keyword:`with` statement.
2397
2398 An example of a context manager that returns a related object is the one
Christian Heimesfaf2f632008-01-06 16:59:19 +00002399 returned by :func:`decimal.localcontext`. These managers set the active
Georg Brandl116aa622007-08-15 14:28:22 +00002400 decimal context to a copy of the original decimal context and then return the
2401 copy. This allows changes to be made to the current decimal context in the body
2402 of the :keyword:`with` statement without affecting code outside the
2403 :keyword:`with` statement.
2404
2405
2406.. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
2407
Georg Brandl9afde1c2007-11-01 20:32:30 +00002408 Exit the runtime context and return a Boolean flag indicating if any exception
Georg Brandl116aa622007-08-15 14:28:22 +00002409 that occurred should be suppressed. If an exception occurred while executing the
2410 body of the :keyword:`with` statement, the arguments contain the exception type,
2411 value and traceback information. Otherwise, all three arguments are ``None``.
2412
2413 Returning a true value from this method will cause the :keyword:`with` statement
2414 to suppress the exception and continue execution with the statement immediately
2415 following the :keyword:`with` statement. Otherwise the exception continues
2416 propagating after this method has finished executing. Exceptions that occur
2417 during execution of this method will replace any exception that occurred in the
2418 body of the :keyword:`with` statement.
2419
2420 The exception passed in should never be reraised explicitly - instead, this
2421 method should return a false value to indicate that the method completed
2422 successfully and does not want to suppress the raised exception. This allows
2423 context management code (such as ``contextlib.nested``) to easily detect whether
2424 or not an :meth:`__exit__` method has actually failed.
2425
2426Python defines several context managers to support easy thread synchronisation,
2427prompt closure of files or other objects, and simpler manipulation of the active
2428decimal arithmetic context. The specific types are not treated specially beyond
2429their implementation of the context management protocol. See the
2430:mod:`contextlib` module for some examples.
2431
Christian Heimesd8654cf2007-12-02 15:22:16 +00002432Python's :term:`generator`\s and the ``contextlib.contextfactory`` :term:`decorator`
2433provide a convenient way to implement these protocols. If a generator function is
Georg Brandl116aa622007-08-15 14:28:22 +00002434decorated with the ``contextlib.contextfactory`` decorator, it will return a
2435context manager implementing the necessary :meth:`__enter__` and
2436:meth:`__exit__` methods, rather than the iterator produced by an undecorated
2437generator function.
2438
2439Note that there is no specific slot for any of these methods in the type
2440structure for Python objects in the Python/C API. Extension types wanting to
2441define these methods must provide them as a normal Python accessible method.
2442Compared to the overhead of setting up the runtime context, the overhead of a
2443single class dictionary lookup is negligible.
2444
2445
2446.. _typesother:
2447
2448Other Built-in Types
2449====================
2450
2451The interpreter supports several other kinds of objects. Most of these support
2452only one or two operations.
2453
2454
2455.. _typesmodules:
2456
2457Modules
2458-------
2459
2460The only special operation on a module is attribute access: ``m.name``, where
2461*m* is a module and *name* accesses a name defined in *m*'s symbol table.
2462Module attributes can be assigned to. (Note that the :keyword:`import`
2463statement is not, strictly speaking, an operation on a module object; ``import
2464foo`` does not require a module object named *foo* to exist, rather it requires
2465an (external) *definition* for a module named *foo* somewhere.)
2466
2467A special member of every module is :attr:`__dict__`. This is the dictionary
2468containing the module's symbol table. Modifying this dictionary will actually
2469change the module's symbol table, but direct assignment to the :attr:`__dict__`
2470attribute is not possible (you can write ``m.__dict__['a'] = 1``, which defines
2471``m.a`` to be ``1``, but you can't write ``m.__dict__ = {}``). Modifying
2472:attr:`__dict__` directly is not recommended.
2473
2474Modules built into the interpreter are written like this: ``<module 'sys'
2475(built-in)>``. If loaded from a file, they are written as ``<module 'os' from
2476'/usr/local/lib/pythonX.Y/os.pyc'>``.
2477
2478
2479.. _typesobjects:
2480
2481Classes and Class Instances
2482---------------------------
2483
2484See :ref:`objects` and :ref:`class` for these.
2485
2486
2487.. _typesfunctions:
2488
2489Functions
2490---------
2491
2492Function objects are created by function definitions. The only operation on a
2493function object is to call it: ``func(argument-list)``.
2494
2495There are really two flavors of function objects: built-in functions and
2496user-defined functions. Both support the same operation (to call the function),
2497but the implementation is different, hence the different object types.
2498
2499See :ref:`function` for more information.
2500
2501
2502.. _typesmethods:
2503
2504Methods
2505-------
2506
2507.. index:: object: method
2508
2509Methods are functions that are called using the attribute notation. There are
2510two flavors: built-in methods (such as :meth:`append` on lists) and class
2511instance methods. Built-in methods are described with the types that support
2512them.
2513
Georg Brandl2e0b7552007-11-27 12:43:08 +00002514If you access a method (a function defined in a class namespace) through an
2515instance, you get a special object: a :dfn:`bound method` (also called
2516:dfn:`instance method`) object. When called, it will add the ``self`` argument
2517to the argument list. Bound methods have two special read-only attributes:
2518``m.__self__`` is the object on which the method operates, and ``m.__func__`` is
2519the function implementing the method. Calling ``m(arg-1, arg-2, ..., arg-n)``
2520is completely equivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,
2521arg-n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00002522
Georg Brandl2e0b7552007-11-27 12:43:08 +00002523Like function objects, bound method objects support getting arbitrary
2524attributes. However, since method attributes are actually stored on the
2525underlying function object (``meth.__func__``), setting method attributes on
2526bound methods is disallowed. Attempting to set a method attribute results in a
Georg Brandl116aa622007-08-15 14:28:22 +00002527:exc:`TypeError` being raised. In order to set a method attribute, you need to
2528explicitly set it on the underlying function object::
2529
2530 class C:
2531 def method(self):
2532 pass
2533
2534 c = C()
Christian Heimesff737952007-11-27 10:40:20 +00002535 c.method.__func__.whoami = 'my name is c'
Georg Brandl116aa622007-08-15 14:28:22 +00002536
2537See :ref:`types` for more information.
2538
2539
2540.. _bltin-code-objects:
2541
2542Code Objects
2543------------
2544
2545.. index:: object: code
2546
2547.. index::
2548 builtin: compile
2549 single: __code__ (function object attribute)
2550
2551Code objects are used by the implementation to represent "pseudo-compiled"
2552executable Python code such as a function body. They differ from function
2553objects because they don't contain a reference to their global execution
2554environment. Code objects are returned by the built-in :func:`compile` function
2555and can be extracted from function objects through their :attr:`__code__`
2556attribute. See also the :mod:`code` module.
2557
2558.. index::
2559 builtin: exec
2560 builtin: eval
2561
2562A code object can be executed or evaluated by passing it (instead of a source
2563string) to the :func:`exec` or :func:`eval` built-in functions.
2564
2565See :ref:`types` for more information.
2566
2567
2568.. _bltin-type-objects:
2569
2570Type Objects
2571------------
2572
2573.. index::
2574 builtin: type
2575 module: types
2576
2577Type objects represent the various object types. An object's type is accessed
2578by the built-in function :func:`type`. There are no special operations on
2579types. The standard module :mod:`types` defines names for all standard built-in
2580types.
2581
Martin v. Löwis250ad612008-04-07 05:43:42 +00002582Types are written like this: ``<class 'int'>``.
Georg Brandl116aa622007-08-15 14:28:22 +00002583
2584
2585.. _bltin-null-object:
2586
2587The Null Object
2588---------------
2589
2590This object is returned by functions that don't explicitly return a value. It
2591supports no special operations. There is exactly one null object, named
2592``None`` (a built-in name).
2593
2594It is written as ``None``.
2595
2596
2597.. _bltin-ellipsis-object:
2598
2599The Ellipsis Object
2600-------------------
2601
Georg Brandlcb8ecb12007-09-04 06:35:14 +00002602This object is commonly used by slicing (see :ref:`slicings`). It supports no
2603special operations. There is exactly one ellipsis object, named
Georg Brandl116aa622007-08-15 14:28:22 +00002604:const:`Ellipsis` (a built-in name).
2605
2606It is written as ``Ellipsis`` or ``...``.
2607
2608
2609Boolean Values
2610--------------
2611
2612Boolean values are the two constant objects ``False`` and ``True``. They are
2613used to represent truth values (although other values can also be considered
2614false or true). In numeric contexts (for example when used as the argument to
2615an arithmetic operator), they behave like the integers 0 and 1, respectively.
2616The built-in function :func:`bool` can be used to cast any value to a Boolean,
2617if the value can be interpreted as a truth value (see section Truth Value
2618Testing above).
2619
2620.. index::
2621 single: False
2622 single: True
2623 pair: Boolean; values
2624
2625They are written as ``False`` and ``True``, respectively.
2626
2627
2628.. _typesinternal:
2629
2630Internal Objects
2631----------------
2632
2633See :ref:`types` for this information. It describes stack frame objects,
2634traceback objects, and slice objects.
2635
2636
2637.. _specialattrs:
2638
2639Special Attributes
2640==================
2641
2642The implementation adds a few special read-only attributes to several object
2643types, where they are relevant. Some of these are not reported by the
2644:func:`dir` built-in function.
2645
2646
2647.. attribute:: object.__dict__
2648
2649 A dictionary or other mapping object used to store an object's (writable)
2650 attributes.
2651
2652
2653.. attribute:: instance.__class__
2654
2655 The class to which a class instance belongs.
2656
2657
2658.. attribute:: class.__bases__
2659
2660 The tuple of base classes of a class object. If there are no base classes, this
2661 will be an empty tuple.
2662
2663
2664.. attribute:: class.__name__
2665
2666 The name of the class or type.
2667
2668.. rubric:: Footnotes
2669
2670.. [#] Additional information on these special methods may be found in the Python
2671 Reference Manual (:ref:`customization`).
2672
2673.. [#] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
2674 similarly for tuples.
2675
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00002676.. [#] Beware of this formula! It's mathematically valid, but as a
2677 Python expression it will not give correct results for all ``x``,
2678 as a consequence of the limited precision of floating-point
2679 arithmetic.
2680
Georg Brandl116aa622007-08-15 14:28:22 +00002681.. [#] They must have since the parser can't tell the type of the operands.
2682
2683.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
2684 element is the tuple to be formatted.
2685
2686.. [#] These numbers are fairly arbitrary. They are intended to avoid printing endless
2687 strings of meaningless digits without hampering correct use and without having
2688 to know the exact precision of floating point values on a particular machine.
2689
Georg Brandl116aa622007-08-15 14:28:22 +00002690.. [#] The advantage of leaving the newline on is that returning an empty string is
2691 then an unambiguous EOF indication. It is also possible (in cases where it
2692 might matter, for example, if you want to make an exact copy of a file while
2693 scanning its lines) to tell whether the last line of a file ended in a newline
2694 or not (yes this happens!).