Home | History | Annotate | Download | only in library
      1 .. XXX: reference/datamodel and this have quite a few overlaps!
      2 
      3 
      4 .. _bltin-types:
      5 
      6 **************
      7 Built-in Types
      8 **************
      9 
     10 The following sections describe the standard types that are built into the
     11 interpreter.
     12 
     13 .. note::
     14 
     15    Historically (until release 2.2), Python's built-in types have differed from
     16    user-defined types because it was not possible to use the built-in types as the
     17    basis for object-oriented inheritance. This limitation no longer
     18    exists.
     19 
     20 .. index:: pair: built-in; types
     21 
     22 The principal built-in types are numerics, sequences, mappings, files, classes,
     23 instances and exceptions.
     24 
     25 .. index:: statement: print
     26 
     27 Some operations are supported by several object types; in particular,
     28 practically all objects can be compared, tested for truth value, and converted
     29 to a string (with the :ref:`repr() <func-repr>` function or the slightly different
     30 :func:`str` function).  The latter function is implicitly used when an object is
     31 written by the :func:`print` function.
     32 
     33 
     34 .. _truth:
     35 
     36 Truth Value Testing
     37 ===================
     38 
     39 .. index::
     40    statement: if
     41    statement: while
     42    pair: truth; value
     43    pair: Boolean; operations
     44    single: false
     45 
     46 Any object can be tested for truth value, for use in an :keyword:`if` or
     47 :keyword:`while` condition or as operand of the Boolean operations below. The
     48 following values are considered false:
     49 
     50   .. index:: single: None (Built-in object)
     51 
     52 * ``None``
     53 
     54   .. index:: single: False (Built-in object)
     55 
     56 * ``False``
     57 
     58 * zero of any numeric type, for example, ``0``, ``0L``, ``0.0``, ``0j``.
     59 
     60 * any empty sequence, for example, ``''``, ``()``, ``[]``.
     61 
     62 * any empty mapping, for example, ``{}``.
     63 
     64 * instances of user-defined classes, if the class defines a :meth:`__nonzero__`
     65   or :meth:`__len__` method, when that method returns the integer zero or
     66   :class:`bool` value ``False``. [1]_
     67 
     68 .. index:: single: true
     69 
     70 All other values are considered true --- so objects of many types are always
     71 true.
     72 
     73 .. index::
     74    operator: or
     75    operator: and
     76    single: False
     77    single: True
     78 
     79 Operations and built-in functions that have a Boolean result always return ``0``
     80 or ``False`` for false and ``1`` or ``True`` for true, unless otherwise stated.
     81 (Important exception: the Boolean operations ``or`` and ``and`` always return
     82 one of their operands.)
     83 
     84 
     85 .. _boolean:
     86 
     87 Boolean Operations --- :keyword:`and`, :keyword:`or`, :keyword:`not`
     88 ====================================================================
     89 
     90 .. index:: pair: Boolean; operations
     91 
     92 These are the Boolean operations, ordered by ascending priority:
     93 
     94 +-------------+---------------------------------+-------+
     95 | Operation   | Result                          | Notes |
     96 +=============+=================================+=======+
     97 | ``x or y``  | if *x* is false, then *y*, else | \(1)  |
     98 |             | *x*                             |       |
     99 +-------------+---------------------------------+-------+
    100 | ``x and y`` | if *x* is false, then *x*, else | \(2)  |
    101 |             | *y*                             |       |
    102 +-------------+---------------------------------+-------+
    103 | ``not x``   | if *x* is false, then ``True``, | \(3)  |
    104 |             | else ``False``                  |       |
    105 +-------------+---------------------------------+-------+
    106 
    107 .. index::
    108    operator: and
    109    operator: or
    110    operator: not
    111 
    112 Notes:
    113 
    114 (1)
    115    This is a short-circuit operator, so it only evaluates the second
    116    argument if the first one is :const:`False`.
    117 
    118 (2)
    119    This is a short-circuit operator, so it only evaluates the second
    120    argument if the first one is :const:`True`.
    121 
    122 (3)
    123    ``not`` has a lower priority than non-Boolean operators, so ``not a == b`` is
    124    interpreted as ``not (a == b)``, and ``a == not b`` is a syntax error.
    125 
    126 
    127 .. _stdcomparisons:
    128 
    129 Comparisons
    130 ===========
    131 
    132 .. index::
    133    pair: chaining; comparisons
    134    pair: operator; comparison
    135    operator: ==
    136    operator: <
    137    operator: <=
    138    operator: >
    139    operator: >=
    140    operator: !=
    141    operator: is
    142    operator: is not
    143 
    144 Comparison operations are supported by all objects.  They all have the same
    145 priority (which is higher than that of the Boolean operations). Comparisons can
    146 be chained arbitrarily; for example, ``x < y <= z`` is equivalent to ``x < y and
    147 y <= z``, except that *y* is evaluated only once (but in both cases *z* is not
    148 evaluated at all when ``x < y`` is found to be false).
    149 
    150 This table summarizes the comparison operations:
    151 
    152 +------------+-------------------------+-------+
    153 | Operation  | Meaning                 | Notes |
    154 +============+=========================+=======+
    155 | ``<``      | strictly less than      |       |
    156 +------------+-------------------------+-------+
    157 | ``<=``     | less than or equal      |       |
    158 +------------+-------------------------+-------+
    159 | ``>``      | strictly greater than   |       |
    160 +------------+-------------------------+-------+
    161 | ``>=``     | greater than or equal   |       |
    162 +------------+-------------------------+-------+
    163 | ``==``     | equal                   |       |
    164 +------------+-------------------------+-------+
    165 | ``!=``     | not equal               | \(1)  |
    166 +------------+-------------------------+-------+
    167 | ``is``     | object identity         |       |
    168 +------------+-------------------------+-------+
    169 | ``is not`` | negated object identity |       |
    170 +------------+-------------------------+-------+
    171 
    172 Notes:
    173 
    174 (1)
    175     ``!=`` can also be written ``<>``, but this is an obsolete usage
    176     kept for backwards compatibility only. New code should always use
    177     ``!=``.
    178 
    179 .. index::
    180    pair: object; numeric
    181    pair: objects; comparing
    182 
    183 Objects of different types, except different numeric types and different string
    184 types, never compare equal; such objects are ordered consistently but
    185 arbitrarily (so that sorting a heterogeneous array yields a consistent result).
    186 Furthermore, some types (for example, file objects) support only a degenerate
    187 notion of comparison where any two objects of that type are unequal.  Again,
    188 such objects are ordered arbitrarily but consistently. The ``<``, ``<=``, ``>``
    189 and ``>=`` operators will raise a :exc:`TypeError` exception when any operand is
    190 a complex number.
    191 
    192 .. index::
    193    single: __cmp__() (instance method)
    194    single: __eq__() (instance method)
    195    single: __ne__() (instance method)
    196    single: __lt__() (instance method)
    197    single: __le__() (instance method)
    198    single: __gt__() (instance method)
    199    single: __ge__() (instance method)
    200 
    201 Non-identical instances of a class normally compare as non-equal unless the
    202 class defines the :meth:`__eq__` method or the :meth:`__cmp__` method.
    203 
    204 Instances of a class cannot be ordered with respect to other instances of the
    205 same class, or other types of object, unless the class defines either enough of
    206 the rich comparison methods (:meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, and
    207 :meth:`__ge__`) or the :meth:`__cmp__` method.
    208 
    209 .. impl-detail::
    210 
    211    Objects of different types except numbers are ordered by their type names;
    212    objects of the same types that don't support proper comparison are ordered by
    213    their address.
    214 
    215 .. index::
    216    operator: in
    217    operator: not in
    218 
    219 Two more operations with the same syntactic priority, ``in`` and ``not in``, are
    220 supported only by sequence types (below).
    221 
    222 
    223 .. _typesnumeric:
    224 
    225 Numeric Types --- :class:`int`, :class:`float`, :class:`long`, :class:`complex`
    226 ===============================================================================
    227 
    228 .. index::
    229    object: numeric
    230    object: Boolean
    231    object: integer
    232    object: long integer
    233    object: floating point
    234    object: complex number
    235    pair: C; language
    236 
    237 There are four distinct numeric types: :dfn:`plain integers`, :dfn:`long
    238 integers`, :dfn:`floating point numbers`, and :dfn:`complex numbers`. In
    239 addition, Booleans are a subtype of plain integers. Plain integers (also just
    240 called :dfn:`integers`) are implemented using :c:type:`long` in C, which gives
    241 them at least 32 bits of precision (``sys.maxint`` is always set to the maximum
    242 plain integer value for the current platform, the minimum value is
    243 ``-sys.maxint - 1``).  Long integers have unlimited precision.  Floating point
    244 numbers are usually implemented using :c:type:`double` in C; information about
    245 the precision and internal representation of floating point numbers for the
    246 machine on which your program is running is available in
    247 :data:`sys.float_info`.  Complex numbers have a real and imaginary part, which
    248 are each a floating point number.  To extract these parts from a complex number
    249 *z*, use ``z.real`` and ``z.imag``. (The standard library includes additional
    250 numeric types, :mod:`fractions` that hold rationals, and :mod:`decimal` that
    251 hold floating-point numbers with user-definable precision.)
    252 
    253 .. index::
    254    pair: numeric; literals
    255    pair: integer; literals
    256    triple: long; integer; literals
    257    pair: floating point; literals
    258    pair: complex number; literals
    259    pair: hexadecimal; literals
    260    pair: octal; literals
    261 
    262 Numbers are created by numeric literals or as the result of built-in functions
    263 and operators.  Unadorned integer literals (including binary, hex, and octal
    264 numbers) yield plain integers unless the value they denote is too large to be
    265 represented as a plain integer, in which case they yield a long integer.
    266 Integer literals with an ``'L'`` or ``'l'`` suffix yield long integers (``'L'``
    267 is preferred because ``1l`` looks too much like eleven!).  Numeric literals
    268 containing a decimal point or an exponent sign yield floating point numbers.
    269 Appending ``'j'`` or ``'J'`` to a numeric literal yields an imaginary number
    270 (a complex number with a zero real part) which you can add to an integer or
    271 float to get a complex number with real and imaginary parts.
    272 
    273 .. index::
    274    single: arithmetic
    275    builtin: int
    276    builtin: long
    277    builtin: float
    278    builtin: complex
    279    operator: +
    280    operator: -
    281    operator: *
    282    operator: /
    283    operator: //
    284    operator: %
    285    operator: **
    286 
    287 Python fully supports mixed arithmetic: when a binary arithmetic operator has
    288 operands of different numeric types, the operand with the "narrower" type is
    289 widened to that of the other, where plain integer is narrower than long integer
    290 is narrower than floating point is narrower than complex. Comparisons between
    291 numbers of mixed type use the same rule. [2]_ The constructors :func:`int`,
    292 :func:`long`, :func:`float`, and :func:`complex` can be used to produce numbers
    293 of a specific type.
    294 
    295 All built-in numeric types support the following operations. See
    296 :ref:`power` and later sections for the operators' priorities.
    297 
    298 +--------------------+---------------------------------+--------+
    299 | Operation          | Result                          | Notes  |
    300 +====================+=================================+========+
    301 | ``x + y``          | sum of *x* and *y*              |        |
    302 +--------------------+---------------------------------+--------+
    303 | ``x - y``          | difference of *x* and *y*       |        |
    304 +--------------------+---------------------------------+--------+
    305 | ``x * y``          | product of *x* and *y*          |        |
    306 +--------------------+---------------------------------+--------+
    307 | ``x / y``          | quotient of *x* and *y*         | \(1)   |
    308 +--------------------+---------------------------------+--------+
    309 | ``x // y``         | (floored) quotient of *x* and   | (4)(5) |
    310 |                    | *y*                             |        |
    311 +--------------------+---------------------------------+--------+
    312 | ``x % y``          | remainder of ``x / y``          | \(4)   |
    313 +--------------------+---------------------------------+--------+
    314 | ``-x``             | *x* negated                     |        |
    315 +--------------------+---------------------------------+--------+
    316 | ``+x``             | *x* unchanged                   |        |
    317 +--------------------+---------------------------------+--------+
    318 | ``abs(x)``         | absolute value or magnitude of  | \(3)   |
    319 |                    | *x*                             |        |
    320 +--------------------+---------------------------------+--------+
    321 | ``int(x)``         | *x* converted to integer        | \(2)   |
    322 +--------------------+---------------------------------+--------+
    323 | ``long(x)``        | *x* converted to long integer   | \(2)   |
    324 +--------------------+---------------------------------+--------+
    325 | ``float(x)``       | *x* converted to floating point | \(6)   |
    326 +--------------------+---------------------------------+--------+
    327 | ``complex(re,im)`` | a complex number with real part |        |
    328 |                    | *re*, imaginary part *im*.      |        |
    329 |                    | *im* defaults to zero.          |        |
    330 +--------------------+---------------------------------+--------+
    331 | ``c.conjugate()``  | conjugate of the complex number |        |
    332 |                    | *c*. (Identity on real numbers) |        |
    333 +--------------------+---------------------------------+--------+
    334 | ``divmod(x, y)``   | the pair ``(x // y, x % y)``    | (3)(4) |
    335 +--------------------+---------------------------------+--------+
    336 | ``pow(x, y)``      | *x* to the power *y*            | (3)(7) |
    337 +--------------------+---------------------------------+--------+
    338 | ``x ** y``         | *x* to the power *y*            | \(7)   |
    339 +--------------------+---------------------------------+--------+
    340 
    341 .. index::
    342    triple: operations on; numeric; types
    343    single: conjugate() (complex number method)
    344 
    345 Notes:
    346 
    347 (1)
    348    .. index::
    349       pair: integer; division
    350       triple: long; integer; division
    351 
    352    For (plain or long) integer division, the result is an integer. The result is
    353    always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and
    354    (-1)/(-2) is 0.  Note that the result is a long integer if either operand is a
    355    long integer, regardless of the numeric value.
    356 
    357 (2)
    358    .. index::
    359       module: math
    360       single: floor() (in module math)
    361       single: ceil() (in module math)
    362       single: trunc() (in module math)
    363       pair: numeric; conversions
    364 
    365    Conversion from floats using :func:`int` or :func:`long` truncates toward
    366    zero like the related function, :func:`math.trunc`.  Use the function
    367    :func:`math.floor` to round downward and :func:`math.ceil` to round
    368    upward.
    369 
    370 (3)
    371    See :ref:`built-in-funcs` for a full description.
    372 
    373 (4)
    374    .. deprecated:: 2.3
    375       The floor division operator, the modulo operator, and the :func:`divmod`
    376       function are no longer defined for complex numbers.  Instead, convert to
    377       a floating point number using the :func:`abs` function if appropriate.
    378 
    379 (5)
    380    Also referred to as integer division.  The resultant value is a whole integer,
    381    though the result's type is not necessarily int.
    382 
    383 (6)
    384    float also accepts the strings "nan" and "inf" with an optional prefix "+"
    385    or "-" for Not a Number (NaN) and positive or negative infinity.
    386 
    387    .. versionadded:: 2.6
    388 
    389 (7)
    390    Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
    391    programming languages.
    392 
    393 All :class:`numbers.Real` types (:class:`int`, :class:`long`, and
    394 :class:`float`) also include the following operations:
    395 
    396 +--------------------+---------------------------------------------+
    397 | Operation          | Result                                      |
    398 +====================+=============================================+
    399 | :func:`math.trunc(\| *x* truncated to :class:`~numbers.Integral` |
    400 | x) <math.trunc>`   |                                             |
    401 +--------------------+---------------------------------------------+
    402 | :func:`round(x[,   | *x* rounded to *n* digits,                  |
    403 | n]) <round>`       | rounding ties away from zero. If *n*        |
    404 |                    | is omitted, it defaults to 0.               |
    405 +--------------------+---------------------------------------------+
    406 | :func:`math.floor(\| the greatest integer as a float <= *x*      |
    407 | x) <math.floor>`   |                                             |
    408 +--------------------+---------------------------------------------+
    409 | :func:`math.ceil(x)| the least integer as a float >= *x*         |
    410 | <math.ceil>`       |                                             |
    411 +--------------------+---------------------------------------------+
    412 
    413 .. XXXJH exceptions: overflow (when? what operations?) zerodivision
    414 
    415 
    416 .. _bitstring-ops:
    417 
    418 Bitwise Operations on Integer Types
    419 --------------------------------------
    420 
    421 .. index::
    422    triple: operations on; integer; types
    423    pair: bitwise; operations
    424    pair: shifting; operations
    425    pair: masking; operations
    426    operator: ^
    427    operator: &
    428    operator: <<
    429    operator: >>
    430 
    431 Bitwise operations only make sense for integers.  Negative numbers are treated
    432 as their 2's complement value (this assumes a sufficiently large number of bits
    433 that no overflow occurs during the operation).
    434 
    435 The priorities of the binary bitwise operations are all lower than the numeric
    436 operations and higher than the comparisons; the unary operation ``~`` has the
    437 same priority as the other unary numeric operations (``+`` and ``-``).
    438 
    439 This table lists the bitwise operations sorted in ascending priority:
    440 
    441 +------------+--------------------------------+----------+
    442 | Operation  | Result                         | Notes    |
    443 +============+================================+==========+
    444 | ``x | y``  | bitwise :dfn:`or` of *x* and   |          |
    445 |            | *y*                            |          |
    446 +------------+--------------------------------+----------+
    447 | ``x ^ y``  | bitwise :dfn:`exclusive or` of |          |
    448 |            | *x* and *y*                    |          |
    449 +------------+--------------------------------+----------+
    450 | ``x & y``  | bitwise :dfn:`and` of *x* and  |          |
    451 |            | *y*                            |          |
    452 +------------+--------------------------------+----------+
    453 | ``x << n`` | *x* shifted left by *n* bits   | (1)(2)   |
    454 +------------+--------------------------------+----------+
    455 | ``x >> n`` | *x* shifted right by *n* bits  | (1)(3)   |
    456 +------------+--------------------------------+----------+
    457 | ``~x``     | the bits of *x* inverted       |          |
    458 +------------+--------------------------------+----------+
    459 
    460 Notes:
    461 
    462 (1)
    463    Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
    464 
    465 (2)
    466    A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``.  A
    467    long integer is returned if the result exceeds the range of plain integers.
    468 
    469 (3)
    470    A right shift by *n* bits is equivalent to division by ``pow(2, n)``.
    471 
    472 
    473 Additional Methods on Integer Types
    474 -----------------------------------
    475 
    476 The integer types implement the :class:`numbers.Integral` :term:`abstract base
    477 class`. In addition, they provide one more method:
    478 
    479 .. method:: int.bit_length()
    480 .. method:: long.bit_length()
    481 
    482     Return the number of bits necessary to represent an integer in binary,
    483     excluding the sign and leading zeros::
    484 
    485         >>> n = -37
    486         >>> bin(n)
    487         '-0b100101'
    488         >>> n.bit_length()
    489         6
    490 
    491     More precisely, if ``x`` is nonzero, then ``x.bit_length()`` is the
    492     unique positive integer ``k`` such that ``2**(k-1) <= abs(x) < 2**k``.
    493     Equivalently, when ``abs(x)`` is small enough to have a correctly
    494     rounded logarithm, then ``k = 1 + int(log(abs(x), 2))``.
    495     If ``x`` is zero, then ``x.bit_length()`` returns ``0``.
    496 
    497     Equivalent to::
    498 
    499         def bit_length(self):
    500             s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    501             s = s.lstrip('-0b') # remove leading zeros and minus sign
    502             return len(s)       # len('100101') --> 6
    503 
    504     .. versionadded:: 2.7
    505 
    506 
    507 Additional Methods on Float
    508 ---------------------------
    509 
    510 The float type implements the :class:`numbers.Real` :term:`abstract base
    511 class`. float also has the following additional methods.
    512 
    513 .. method:: float.as_integer_ratio()
    514 
    515    Return a pair of integers whose ratio is exactly equal to the
    516    original float and with a positive denominator.  Raises
    517    :exc:`OverflowError` on infinities and a :exc:`ValueError` on
    518    NaNs.
    519 
    520    .. versionadded:: 2.6
    521 
    522 .. method:: float.is_integer()
    523 
    524    Return ``True`` if the float instance is finite with integral
    525    value, and ``False`` otherwise::
    526 
    527       >>> (-2.0).is_integer()
    528       True
    529       >>> (3.2).is_integer()
    530       False
    531 
    532    .. versionadded:: 2.6
    533 
    534 Two methods support conversion to
    535 and from hexadecimal strings.  Since Python's floats are stored
    536 internally as binary numbers, converting a float to or from a
    537 *decimal* string usually involves a small rounding error.  In
    538 contrast, hexadecimal strings allow exact representation and
    539 specification of floating-point numbers.  This can be useful when
    540 debugging, and in numerical work.
    541 
    542 
    543 .. method:: float.hex()
    544 
    545    Return a representation of a floating-point number as a hexadecimal
    546    string.  For finite floating-point numbers, this representation
    547    will always include a leading ``0x`` and a trailing ``p`` and
    548    exponent.
    549 
    550    .. versionadded:: 2.6
    551 
    552 
    553 .. method:: float.fromhex(s)
    554 
    555    Class method to return the float represented by a hexadecimal
    556    string *s*.  The string *s* may have leading and trailing
    557    whitespace.
    558 
    559    .. versionadded:: 2.6
    560 
    561 
    562 Note that :meth:`float.hex` is an instance method, while
    563 :meth:`float.fromhex` is a class method.
    564 
    565 A hexadecimal string takes the form::
    566 
    567    [sign] ['0x'] integer ['.' fraction] ['p' exponent]
    568 
    569 where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
    570 and ``fraction`` are strings of hexadecimal digits, and ``exponent``
    571 is a decimal integer with an optional leading sign.  Case is not
    572 significant, and there must be at least one hexadecimal digit in
    573 either the integer or the fraction.  This syntax is similar to the
    574 syntax specified in section 6.4.4.2 of the C99 standard, and also to
    575 the syntax used in Java 1.5 onwards.  In particular, the output of
    576 :meth:`float.hex` is usable as a hexadecimal floating-point literal in
    577 C or Java code, and hexadecimal strings produced by C's ``%a`` format
    578 character or Java's ``Double.toHexString`` are accepted by
    579 :meth:`float.fromhex`.
    580 
    581 
    582 Note that the exponent is written in decimal rather than hexadecimal,
    583 and that it gives the power of 2 by which to multiply the coefficient.
    584 For example, the hexadecimal string ``0x3.a7p10`` represents the
    585 floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
    586 ``3740.0``::
    587 
    588    >>> float.fromhex('0x3.a7p10')
    589    3740.0
    590 
    591 
    592 Applying the reverse conversion to ``3740.0`` gives a different
    593 hexadecimal string representing the same number::
    594 
    595    >>> float.hex(3740.0)
    596    '0x1.d380000000000p+11'
    597 
    598 
    599 .. _typeiter:
    600 
    601 Iterator Types
    602 ==============
    603 
    604 .. versionadded:: 2.2
    605 
    606 .. index::
    607    single: iterator protocol
    608    single: protocol; iterator
    609    single: sequence; iteration
    610    single: container; iteration over
    611 
    612 Python supports a concept of iteration over containers.  This is implemented
    613 using two distinct methods; these are used to allow user-defined classes to
    614 support iteration.  Sequences, described below in more detail, always support
    615 the iteration methods.
    616 
    617 One method needs to be defined for container objects to provide iteration
    618 support:
    619 
    620 .. XXX duplicated in reference/datamodel!
    621 
    622 .. method:: container.__iter__()
    623 
    624    Return an iterator object.  The object is required to support the iterator
    625    protocol described below.  If a container supports different types of
    626    iteration, additional methods can be provided to specifically request
    627    iterators for those iteration types.  (An example of an object supporting
    628    multiple forms of iteration would be a tree structure which supports both
    629    breadth-first and depth-first traversal.)  This method corresponds to the
    630    :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C
    631    API.
    632 
    633 The iterator objects themselves are required to support the following two
    634 methods, which together form the :dfn:`iterator protocol`:
    635 
    636 
    637 .. method:: iterator.__iter__()
    638 
    639    Return the iterator object itself.  This is required to allow both containers
    640    and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
    641    This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for
    642    Python objects in the Python/C API.
    643 
    644 
    645 .. method:: iterator.next()
    646 
    647    Return the next item from the container.  If there are no further items, raise
    648    the :exc:`StopIteration` exception.  This method corresponds to the
    649    :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the
    650    Python/C API.
    651 
    652 Python defines several iterator objects to support iteration over general and
    653 specific sequence types, dictionaries, and other more specialized forms.  The
    654 specific types are not important beyond their implementation of the iterator
    655 protocol.
    656 
    657 The intention of the protocol is that once an iterator's :meth:`~iterator.next` method
    658 raises :exc:`StopIteration`, it will continue to do so on subsequent calls.
    659 Implementations that do not obey this property are deemed broken.  (This
    660 constraint was added in Python 2.3; in Python 2.2, various iterators are broken
    661 according to this rule.)
    662 
    663 
    664 .. _generator-types:
    665 
    666 Generator Types
    667 ---------------
    668 
    669 Python's :term:`generator`\s provide a convenient way to implement the iterator
    670 protocol.  If a container object's :meth:`__iter__` method is implemented as a
    671 generator, it will automatically return an iterator object (technically, a
    672 generator object) supplying the :meth:`~iterator.__iter__` and
    673 :meth:`~iterator.next` methods.  More information about generators can be found
    674 in :ref:`the documentation for the yield expression <yieldexpr>`.
    675 
    676 
    677 .. _typesseq:
    678 
    679 Sequence Types --- :class:`str`, :class:`unicode`, :class:`list`, :class:`tuple`, :class:`bytearray`, :class:`buffer`, :class:`xrange`
    680 ======================================================================================================================================
    681 
    682 There are seven sequence types: strings, Unicode strings, lists, tuples,
    683 bytearrays, buffers, and xrange objects.
    684 
    685 For other containers see the built in :class:`dict` and :class:`set` classes,
    686 and the :mod:`collections` module.
    687 
    688 
    689 .. index::
    690    object: sequence
    691    object: string
    692    object: Unicode
    693    object: tuple
    694    object: list
    695    object: bytearray
    696    object: buffer
    697    object: xrange
    698 
    699 String literals are written in single or double quotes: ``'xyzzy'``,
    700 ``"frobozz"``.  See :ref:`strings` for more about string literals.
    701 Unicode strings are much like strings, but are specified in the syntax
    702 using a preceding ``'u'`` character: ``u'abc'``, ``u"def"``. In addition
    703 to the functionality described here, there are also string-specific
    704 methods described in the :ref:`string-methods` section. Lists are
    705 constructed with square brackets, separating items with commas: ``[a, b, c]``.
    706 Tuples are constructed by the comma operator (not within square
    707 brackets), with or without enclosing parentheses, but an empty tuple
    708 must have the enclosing parentheses, such as ``a, b, c`` or ``()``.  A
    709 single item tuple must have a trailing comma, such as ``(d,)``.
    710 
    711 Bytearray objects are created with the built-in function :func:`bytearray`.
    712 
    713 Buffer objects are not directly supported by Python syntax, but can be created
    714 by calling the built-in function :func:`buffer`.  They don't support
    715 concatenation or repetition.
    716 
    717 Objects of type xrange are similar to buffers in that there is no specific syntax to
    718 create them, but they are created using the :func:`xrange` function.  They don't
    719 support slicing, concatenation or repetition, and using ``in``, ``not in``,
    720 :func:`min` or :func:`max` on them is inefficient.
    721 
    722 Most sequence types support the following operations.  The ``in`` and ``not in``
    723 operations have the same priorities as the comparison operations.  The ``+`` and
    724 ``*`` operations have the same priority as the corresponding numeric operations.
    725 [3]_ Additional methods are provided for :ref:`typesseq-mutable`.
    726 
    727 This table lists the sequence operations sorted in ascending priority.
    728 In the table, *s* and *t* are sequences of the same type; *n*, *i* and *j* are integers:
    729 
    730 +------------------+--------------------------------+----------+
    731 | Operation        | Result                         | Notes    |
    732 +==================+================================+==========+
    733 | ``x in s``       | ``True`` if an item of *s* is  | \(1)     |
    734 |                  | equal to *x*, else ``False``   |          |
    735 +------------------+--------------------------------+----------+
    736 | ``x not in s``   | ``False`` if an item of *s* is | \(1)     |
    737 |                  | equal to *x*, else ``True``    |          |
    738 +------------------+--------------------------------+----------+
    739 | ``s + t``        | the concatenation of *s* and   | \(6)     |
    740 |                  | *t*                            |          |
    741 +------------------+--------------------------------+----------+
    742 | ``s * n, n * s`` | equivalent to adding *s* to    | \(2)     |
    743 |                  | itself *n* times               |          |
    744 +------------------+--------------------------------+----------+
    745 | ``s[i]``         | *i*\ th item of *s*, origin 0  | \(3)     |
    746 +------------------+--------------------------------+----------+
    747 | ``s[i:j]``       | slice of *s* from *i* to *j*   | (3)(4)   |
    748 +------------------+--------------------------------+----------+
    749 | ``s[i:j:k]``     | slice of *s* from *i* to *j*   | (3)(5)   |
    750 |                  | with step *k*                  |          |
    751 +------------------+--------------------------------+----------+
    752 | ``len(s)``       | length of *s*                  |          |
    753 +------------------+--------------------------------+----------+
    754 | ``min(s)``       | smallest item of *s*           |          |
    755 +------------------+--------------------------------+----------+
    756 | ``max(s)``       | largest item of *s*            |          |
    757 +------------------+--------------------------------+----------+
    758 | ``s.index(x)``   | index of the first occurrence  |          |
    759 |                  | of *x* in *s*                  |          |
    760 +------------------+--------------------------------+----------+
    761 | ``s.count(x)``   | total number of occurrences of |          |
    762 |                  | *x* in *s*                     |          |
    763 +------------------+--------------------------------+----------+
    764 
    765 Sequence types also support comparisons. In particular, tuples and lists
    766 are compared lexicographically by comparing corresponding
    767 elements. This means that to compare equal, every element must compare
    768 equal and the two sequences must be of the same type and have the same
    769 length. (For full details see :ref:`comparisons` in the language
    770 reference.)
    771 
    772 .. index::
    773    triple: operations on; sequence; types
    774    builtin: len
    775    builtin: min
    776    builtin: max
    777    pair: concatenation; operation
    778    pair: repetition; operation
    779    pair: subscript; operation
    780    pair: slice; operation
    781    pair: extended slice; operation
    782    operator: in
    783    operator: not in
    784 
    785 Notes:
    786 
    787 (1)
    788    When *s* is a string or Unicode string object the ``in`` and ``not in``
    789    operations act like a substring test.  In Python versions before 2.3, *x* had to
    790    be a string of length 1. In Python 2.3 and beyond, *x* may be a string of any
    791    length.
    792 
    793 (2)
    794    Values of *n* less than ``0`` are treated as ``0`` (which yields an empty
    795    sequence of the same type as *s*).  Note that items in the sequence *s*
    796    are not copied; they are referenced multiple times.  This often haunts
    797    new Python programmers; consider:
    798 
    799       >>> lists = [[]] * 3
    800       >>> lists
    801       [[], [], []]
    802       >>> lists[0].append(3)
    803       >>> lists
    804       [[3], [3], [3]]
    805 
    806    What has happened is that ``[[]]`` is a one-element list containing an empty
    807    list, so all three elements of ``[[]] * 3`` are references to this single empty
    808    list.  Modifying any of the elements of ``lists`` modifies this single list.
    809    You can create a list of different lists this way:
    810 
    811       >>> lists = [[] for i in range(3)]
    812       >>> lists[0].append(3)
    813       >>> lists[1].append(5)
    814       >>> lists[2].append(7)
    815       >>> lists
    816       [[3], [5], [7]]
    817 
    818    Further explanation is available in the FAQ entry
    819    :ref:`faq-multidimensional-list`.
    820 
    821 (3)
    822    If *i* or *j* is negative, the index is relative to the end of the string:
    823    ``len(s) + i`` or ``len(s) + j`` is substituted.  But note that ``-0`` is still
    824    ``0``.
    825 
    826 (4)
    827    The slice of *s* from *i* to *j* is defined as the sequence of items with index
    828    *k* such that ``i <= k < j``.  If *i* or *j* is greater than ``len(s)``, use
    829    ``len(s)``.  If *i* is omitted or ``None``, use ``0``.  If *j* is omitted or
    830    ``None``, use ``len(s)``.  If *i* is greater than or equal to *j*, the slice is
    831    empty.
    832 
    833 (5)
    834    The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
    835    items with index  ``x = i + n*k`` such that ``0 <= n < (j-i)/k``.  In other words,
    836    the indices are ``i``, ``i+k``, ``i+2*k``, ``i+3*k`` and so on, stopping when
    837    *j* is reached (but never including *j*).  If *i* or *j* is greater than
    838    ``len(s)``, use ``len(s)``.  If *i* or *j* are omitted or ``None``, they become
    839    "end" values (which end depends on the sign of *k*).  Note, *k* cannot be zero.
    840    If *k* is ``None``, it is treated like ``1``.
    841 
    842 (6)
    843    .. impl-detail::
    844 
    845       If *s* and *t* are both strings, some Python implementations such as
    846       CPython can usually perform an in-place optimization for assignments of
    847       the form ``s = s + t`` or ``s += t``.  When applicable, this optimization
    848       makes quadratic run-time much less likely.  This optimization is both
    849       version and implementation dependent.  For performance sensitive code, it
    850       is preferable to use the :meth:`str.join` method which assures consistent
    851       linear concatenation performance across versions and implementations.
    852 
    853    .. versionchanged:: 2.4
    854       Formerly, string concatenation never occurred in-place.
    855 
    856 
    857 .. _string-methods:
    858 
    859 String Methods
    860 --------------
    861 
    862 .. index:: pair: string; methods
    863 
    864 Below are listed the string methods which both 8-bit strings and
    865 Unicode objects support.  Some of them are also available on :class:`bytearray`
    866 objects.
    867 
    868 In addition, Python's strings support the sequence type methods
    869 described in the :ref:`typesseq` section. To output formatted strings
    870 use template strings or the ``%`` operator described in the
    871 :ref:`string-formatting` section. Also, see the :mod:`re` module for
    872 string functions based on regular expressions.
    873 
    874 .. method:: str.capitalize()
    875 
    876    Return a copy of the string with its first character capitalized and the
    877    rest lowercased.
    878 
    879    For 8-bit strings, this method is locale-dependent.
    880 
    881 
    882 .. method:: str.center(width[, fillchar])
    883 
    884    Return centered in a string of length *width*. Padding is done using the
    885    specified *fillchar* (default is a space).
    886 
    887    .. versionchanged:: 2.4
    888       Support for the *fillchar* argument.
    889 
    890 
    891 .. method:: str.count(sub[, start[, end]])
    892 
    893    Return the number of non-overlapping occurrences of substring *sub* in the
    894    range [*start*, *end*].  Optional arguments *start* and *end* are
    895    interpreted as in slice notation.
    896 
    897 
    898 .. method:: str.decode([encoding[, errors]])
    899 
    900    Decodes the string using the codec registered for *encoding*. *encoding*
    901    defaults to the default string encoding.  *errors* may be given to set a
    902    different error handling scheme.  The default is ``'strict'``, meaning that
    903    encoding errors raise :exc:`UnicodeError`.  Other possible values are
    904    ``'ignore'``, ``'replace'`` and any other name registered via
    905    :func:`codecs.register_error`, see section :ref:`codec-base-classes`.
    906 
    907    .. versionadded:: 2.2
    908 
    909    .. versionchanged:: 2.3
    910       Support for other error handling schemes added.
    911 
    912    .. versionchanged:: 2.7
    913       Support for keyword arguments added.
    914 
    915 .. method:: str.encode([encoding[,errors]])
    916 
    917    Return an encoded version of the string.  Default encoding is the current
    918    default string encoding.  *errors* may be given to set a different error
    919    handling scheme.  The default for *errors* is ``'strict'``, meaning that
    920    encoding errors raise a :exc:`UnicodeError`.  Other possible values are
    921    ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and
    922    any other name registered via :func:`codecs.register_error`, see section
    923    :ref:`codec-base-classes`. For a list of possible encodings, see section
    924    :ref:`standard-encodings`.
    925 
    926    .. versionadded:: 2.0
    927 
    928    .. versionchanged:: 2.3
    929       Support for ``'xmlcharrefreplace'`` and ``'backslashreplace'`` and other error
    930       handling schemes added.
    931 
    932    .. versionchanged:: 2.7
    933       Support for keyword arguments added.
    934 
    935 .. method:: str.endswith(suffix[, start[, end]])
    936 
    937    Return ``True`` if the string ends with the specified *suffix*, otherwise return
    938    ``False``.  *suffix* can also be a tuple of suffixes to look for.  With optional
    939    *start*, test beginning at that position.  With optional *end*, stop comparing
    940    at that position.
    941 
    942    .. versionchanged:: 2.5
    943       Accept tuples as *suffix*.
    944 
    945 
    946 .. method:: str.expandtabs([tabsize])
    947 
    948    Return a copy of the string where all tab characters are replaced by one or
    949    more spaces, depending on the current column and the given tab size.  Tab
    950    positions occur every *tabsize* characters (default is 8, giving tab
    951    positions at columns 0, 8, 16 and so on).  To expand the string, the current
    952    column is set to zero and the string is examined character by character.  If
    953    the character is a tab (``\t``), one or more space characters are inserted
    954    in the result until the current column is equal to the next tab position.
    955    (The tab character itself is not copied.)  If the character is a newline
    956    (``\n``) or return (``\r``), it is copied and the current column is reset to
    957    zero.  Any other character is copied unchanged and the current column is
    958    incremented by one regardless of how the character is represented when
    959    printed.
    960 
    961       >>> '01\t012\t0123\t01234'.expandtabs()
    962       '01      012     0123    01234'
    963       >>> '01\t012\t0123\t01234'.expandtabs(4)
    964       '01  012 0123    01234'
    965 
    966 
    967 .. method:: str.find(sub[, start[, end]])
    968 
    969    Return the lowest index in the string where substring *sub* is found within
    970    the slice ``s[start:end]``.  Optional arguments *start* and *end* are
    971    interpreted as in slice notation.  Return ``-1`` if *sub* is not found.
    972 
    973    .. note::
    974 
    975       The :meth:`~str.find` method should be used only if you need to know the
    976       position of *sub*.  To check if *sub* is a substring or not, use the
    977       :keyword:`in` operator::
    978 
    979          >>> 'Py' in 'Python'
    980          True
    981 
    982 
    983 .. method:: str.format(*args, **kwargs)
    984 
    985    Perform a string formatting operation.  The string on which this method is
    986    called can contain literal text or replacement fields delimited by braces
    987    ``{}``.  Each replacement field contains either the numeric index of a
    988    positional argument, or the name of a keyword argument.  Returns a copy of
    989    the string where each replacement field is replaced with the string value of
    990    the corresponding argument.
    991 
    992       >>> "The sum of 1 + 2 is {0}".format(1+2)
    993       'The sum of 1 + 2 is 3'
    994 
    995    See :ref:`formatstrings` for a description of the various formatting options
    996    that can be specified in format strings.
    997 
    998    This method of string formatting is the new standard in Python 3, and
    999    should be preferred to the ``%`` formatting described in
   1000    :ref:`string-formatting` in new code.
   1001 
   1002    .. versionadded:: 2.6
   1003 
   1004 
   1005 .. method:: str.index(sub[, start[, end]])
   1006 
   1007    Like :meth:`find`, but raise :exc:`ValueError` when the substring is not found.
   1008 
   1009 
   1010 .. method:: str.isalnum()
   1011 
   1012    Return true if all characters in the string are alphanumeric and there is at
   1013    least one character, false otherwise.
   1014 
   1015    For 8-bit strings, this method is locale-dependent.
   1016 
   1017 
   1018 .. method:: str.isalpha()
   1019 
   1020    Return true if all characters in the string are alphabetic and there is at least
   1021    one character, false otherwise.
   1022 
   1023    For 8-bit strings, this method is locale-dependent.
   1024 
   1025 
   1026 .. method:: str.isdigit()
   1027 
   1028    Return true if all characters in the string are digits and there is at least one
   1029    character, false otherwise.
   1030 
   1031    For 8-bit strings, this method is locale-dependent.
   1032 
   1033 
   1034 .. method:: str.islower()
   1035 
   1036    Return true if all cased characters [4]_ in the string are lowercase and there is at
   1037    least one cased character, false otherwise.
   1038 
   1039    For 8-bit strings, this method is locale-dependent.
   1040 
   1041 
   1042 .. method:: str.isspace()
   1043 
   1044    Return true if there are only whitespace characters in the string and there is
   1045    at least one character, false otherwise.
   1046 
   1047    For 8-bit strings, this method is locale-dependent.
   1048 
   1049 
   1050 .. method:: str.istitle()
   1051 
   1052    Return true if the string is a titlecased string and there is at least one
   1053    character, for example uppercase characters may only follow uncased characters
   1054    and lowercase characters only cased ones.  Return false otherwise.
   1055 
   1056    For 8-bit strings, this method is locale-dependent.
   1057 
   1058 
   1059 .. method:: str.isupper()
   1060 
   1061    Return true if all cased characters [4]_ in the string are uppercase and there is at
   1062    least one cased character, false otherwise.
   1063 
   1064    For 8-bit strings, this method is locale-dependent.
   1065 
   1066 
   1067 .. method:: str.join(iterable)
   1068 
   1069    Return a string which is the concatenation of the strings in the
   1070    :term:`iterable` *iterable*.  The separator between elements is the string
   1071    providing this method.
   1072 
   1073 
   1074 .. method:: str.ljust(width[, fillchar])
   1075 
   1076    Return the string left justified in a string of length *width*. Padding is done
   1077    using the specified *fillchar* (default is a space).  The original string is
   1078    returned if *width* is less than or equal to ``len(s)``.
   1079 
   1080    .. versionchanged:: 2.4
   1081       Support for the *fillchar* argument.
   1082 
   1083 
   1084 .. method:: str.lower()
   1085 
   1086    Return a copy of the string with all the cased characters [4]_ converted to
   1087    lowercase.
   1088 
   1089    For 8-bit strings, this method is locale-dependent.
   1090 
   1091 
   1092 .. method:: str.lstrip([chars])
   1093 
   1094    Return a copy of the string with leading characters removed.  The *chars*
   1095    argument is a string specifying the set of characters to be removed.  If omitted
   1096    or ``None``, the *chars* argument defaults to removing whitespace.  The *chars*
   1097    argument is not a prefix; rather, all combinations of its values are stripped:
   1098 
   1099       >>> '   spacious   '.lstrip()
   1100       'spacious   '
   1101       >>> 'www.example.com'.lstrip('cmowz.')
   1102       'example.com'
   1103 
   1104    .. versionchanged:: 2.2.2
   1105       Support for the *chars* argument.
   1106 
   1107 
   1108 .. method:: str.partition(sep)
   1109 
   1110    Split the string at the first occurrence of *sep*, and return a 3-tuple
   1111    containing the part before the separator, the separator itself, and the part
   1112    after the separator.  If the separator is not found, return a 3-tuple containing
   1113    the string itself, followed by two empty strings.
   1114 
   1115    .. versionadded:: 2.5
   1116 
   1117 
   1118 .. method:: str.replace(old, new[, count])
   1119 
   1120    Return a copy of the string with all occurrences of substring *old* replaced by
   1121    *new*.  If the optional argument *count* is given, only the first *count*
   1122    occurrences are replaced.
   1123 
   1124 
   1125 .. method:: str.rfind(sub [,start [,end]])
   1126 
   1127    Return the highest index in the string where substring *sub* is found, such
   1128    that *sub* is contained within ``s[start:end]``.  Optional arguments *start*
   1129    and *end* are interpreted as in slice notation.  Return ``-1`` on failure.
   1130 
   1131 
   1132 .. method:: str.rindex(sub[, start[, end]])
   1133 
   1134    Like :meth:`rfind` but raises :exc:`ValueError` when the substring *sub* is not
   1135    found.
   1136 
   1137 
   1138 .. method:: str.rjust(width[, fillchar])
   1139 
   1140    Return the string right justified in a string of length *width*. Padding is done
   1141    using the specified *fillchar* (default is a space). The original string is
   1142    returned if *width* is less than or equal to ``len(s)``.
   1143 
   1144    .. versionchanged:: 2.4
   1145       Support for the *fillchar* argument.
   1146 
   1147 
   1148 .. method:: str.rpartition(sep)
   1149 
   1150    Split the string at the last occurrence of *sep*, and return a 3-tuple
   1151    containing the part before the separator, the separator itself, and the part
   1152    after the separator.  If the separator is not found, return a 3-tuple containing
   1153    two empty strings, followed by the string itself.
   1154 
   1155    .. versionadded:: 2.5
   1156 
   1157 
   1158 .. method:: str.rsplit([sep [,maxsplit]])
   1159 
   1160    Return a list of the words in the string, using *sep* as the delimiter string.
   1161    If *maxsplit* is given, at most *maxsplit* splits are done, the *rightmost*
   1162    ones.  If *sep* is not specified or ``None``, any whitespace string is a
   1163    separator.  Except for splitting from the right, :meth:`rsplit` behaves like
   1164    :meth:`split` which is described in detail below.
   1165 
   1166    .. versionadded:: 2.4
   1167 
   1168 
   1169 .. method:: str.rstrip([chars])
   1170 
   1171    Return a copy of the string with trailing characters removed.  The *chars*
   1172    argument is a string specifying the set of characters to be removed.  If omitted
   1173    or ``None``, the *chars* argument defaults to removing whitespace.  The *chars*
   1174    argument is not a suffix; rather, all combinations of its values are stripped:
   1175 
   1176       >>> '   spacious   '.rstrip()
   1177       '   spacious'
   1178       >>> 'mississippi'.rstrip('ipz')
   1179       'mississ'
   1180 
   1181    .. versionchanged:: 2.2.2
   1182       Support for the *chars* argument.
   1183 
   1184 
   1185 .. method:: str.split([sep[, maxsplit]])
   1186 
   1187    Return a list of the words in the string, using *sep* as the delimiter
   1188    string.  If *maxsplit* is given, at most *maxsplit* splits are done (thus,
   1189    the list will have at most ``maxsplit+1`` elements).  If *maxsplit* is not
   1190    specified or ``-1``, then there is no limit on the number of splits
   1191    (all possible splits are made).
   1192 
   1193    If *sep* is given, consecutive delimiters are not grouped together and are
   1194    deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
   1195    ``['1', '', '2']``).  The *sep* argument may consist of multiple characters
   1196    (for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
   1197    Splitting an empty string with a specified separator returns ``['']``.
   1198 
   1199    If *sep* is not specified or is ``None``, a different splitting algorithm is
   1200    applied: runs of consecutive whitespace are regarded as a single separator,
   1201    and the result will contain no empty strings at the start or end if the
   1202    string has leading or trailing whitespace.  Consequently, splitting an empty
   1203    string or a string consisting of just whitespace with a ``None`` separator
   1204    returns ``[]``.
   1205 
   1206    For example, ``' 1  2   3  '.split()`` returns ``['1', '2', '3']``, and
   1207    ``'  1  2   3  '.split(None, 1)`` returns ``['1', '2   3  ']``.
   1208 
   1209 
   1210 .. index::
   1211    single: universal newlines; str.splitlines method
   1212 
   1213 .. method:: str.splitlines([keepends])
   1214 
   1215    Return a list of the lines in the string, breaking at line boundaries.
   1216    This method uses the :term:`universal newlines` approach to splitting lines.
   1217    Line breaks are not included in the resulting list unless *keepends* is
   1218    given and true.
   1219 
   1220    Python recognizes ``"\r"``, ``"\n"``, and ``"\r\n"`` as line boundaries for
   1221    8-bit strings.
   1222 
   1223    For example::
   1224 
   1225       >>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
   1226       ['ab c', '', 'de fg', 'kl']
   1227       >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(True)
   1228       ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
   1229 
   1230    Unlike :meth:`~str.split` when a delimiter string *sep* is given, this
   1231    method returns an empty list for the empty string, and a terminal line
   1232    break does not result in an extra line::
   1233 
   1234       >>> "".splitlines()
   1235       []
   1236       >>> "One line\n".splitlines()
   1237       ['One line']
   1238 
   1239    For comparison, ``split('\n')`` gives::
   1240 
   1241       >>> ''.split('\n')
   1242       ['']
   1243       >>> 'Two lines\n'.split('\n')
   1244       ['Two lines', '']
   1245 
   1246 .. method:: unicode.splitlines([keepends])
   1247 
   1248    Return a list of the lines in the string, like :meth:`str.splitlines`.
   1249    However, the Unicode method splits on the following line boundaries,
   1250    which are a superset of the :term:`universal newlines` recognized for
   1251    8-bit strings.
   1252 
   1253    +-----------------------+-----------------------------+
   1254    | Representation        | Description                 |
   1255    +=======================+=============================+
   1256    | ``\n``                | Line Feed                   |
   1257    +-----------------------+-----------------------------+
   1258    | ``\r``                | Carriage Return             |
   1259    +-----------------------+-----------------------------+
   1260    | ``\r\n``              | Carriage Return + Line Feed |
   1261    +-----------------------+-----------------------------+
   1262    | ``\v`` or ``\x0b``    | Line Tabulation             |
   1263    +-----------------------+-----------------------------+
   1264    | ``\f`` or ``\x0c``    | Form Feed                   |
   1265    +-----------------------+-----------------------------+
   1266    | ``\x1c``              | File Separator              |
   1267    +-----------------------+-----------------------------+
   1268    | ``\x1d``              | Group Separator             |
   1269    +-----------------------+-----------------------------+
   1270    | ``\x1e``              | Record Separator            |
   1271    +-----------------------+-----------------------------+
   1272    | ``\x85``              | Next Line (C1 Control Code) |
   1273    +-----------------------+-----------------------------+
   1274    | ``\u2028``            | Line Separator              |
   1275    +-----------------------+-----------------------------+
   1276    | ``\u2029``            | Paragraph Separator         |
   1277    +-----------------------+-----------------------------+
   1278 
   1279    .. versionchanged:: 2.7
   1280 
   1281       ``\v`` and ``\f`` added to list of line boundaries.
   1282 
   1283 
   1284 .. method:: str.startswith(prefix[, start[, end]])
   1285 
   1286    Return ``True`` if string starts with the *prefix*, otherwise return ``False``.
   1287    *prefix* can also be a tuple of prefixes to look for.  With optional *start*,
   1288    test string beginning at that position.  With optional *end*, stop comparing
   1289    string at that position.
   1290 
   1291    .. versionchanged:: 2.5
   1292       Accept tuples as *prefix*.
   1293 
   1294 
   1295 .. method:: str.strip([chars])
   1296 
   1297    Return a copy of the string with the leading and trailing characters removed.
   1298    The *chars* argument is a string specifying the set of characters to be removed.
   1299    If omitted or ``None``, the *chars* argument defaults to removing whitespace.
   1300    The *chars* argument is not a prefix or suffix; rather, all combinations of its
   1301    values are stripped:
   1302 
   1303       >>> '   spacious   '.strip()
   1304       'spacious'
   1305       >>> 'www.example.com'.strip('cmowz.')
   1306       'example'
   1307 
   1308    .. versionchanged:: 2.2.2
   1309       Support for the *chars* argument.
   1310 
   1311 
   1312 .. method:: str.swapcase()
   1313 
   1314    Return a copy of the string with uppercase characters converted to lowercase and
   1315    vice versa.
   1316 
   1317    For 8-bit strings, this method is locale-dependent.
   1318 
   1319 
   1320 .. method:: str.title()
   1321 
   1322    Return a titlecased version of the string where words start with an uppercase
   1323    character and the remaining characters are lowercase.
   1324 
   1325    The algorithm uses a simple language-independent definition of a word as
   1326    groups of consecutive letters.  The definition works in many contexts but
   1327    it means that apostrophes in contractions and possessives form word
   1328    boundaries, which may not be the desired result::
   1329 
   1330         >>> "they're bill's friends from the UK".title()
   1331         "They'Re Bill'S Friends From The Uk"
   1332 
   1333    A workaround for apostrophes can be constructed using regular expressions::
   1334 
   1335         >>> import re
   1336         >>> def titlecase(s):
   1337         ...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
   1338         ...                   lambda mo: mo.group(0)[0].upper() +
   1339         ...                              mo.group(0)[1:].lower(),
   1340         ...                   s)
   1341         ...
   1342         >>> titlecase("they're bill's friends.")
   1343         "They're Bill's Friends."
   1344 
   1345    For 8-bit strings, this method is locale-dependent.
   1346 
   1347 
   1348 .. method:: str.translate(table[, deletechars])
   1349 
   1350    Return a copy of the string where all characters occurring in the optional
   1351    argument *deletechars* are removed, and the remaining characters have been
   1352    mapped through the given translation table, which must be a string of length
   1353    256.
   1354 
   1355    You can use the :func:`~string.maketrans` helper function in the :mod:`string`
   1356    module to create a translation table. For string objects, set the *table*
   1357    argument to ``None`` for translations that only delete characters:
   1358 
   1359       >>> 'read this short text'.translate(None, 'aeiou')
   1360       'rd ths shrt txt'
   1361 
   1362    .. versionadded:: 2.6
   1363       Support for a ``None`` *table* argument.
   1364 
   1365    For Unicode objects, the :meth:`translate` method does not accept the optional
   1366    *deletechars* argument.  Instead, it returns a copy of the *s* where all
   1367    characters have been mapped through the given translation table which must be a
   1368    mapping of Unicode ordinals to Unicode ordinals, Unicode strings or ``None``.
   1369    Unmapped characters are left untouched. Characters mapped to ``None`` are
   1370    deleted.  Note, a more flexible approach is to create a custom character mapping
   1371    codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
   1372    example).
   1373 
   1374 
   1375 .. method:: str.upper()
   1376 
   1377    Return a copy of the string with all the cased characters [4]_ converted to
   1378    uppercase.  Note that ``str.upper().isupper()`` might be ``False`` if ``s``
   1379    contains uncased characters or if the Unicode category of the resulting
   1380    character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter, titlecase).
   1381 
   1382    For 8-bit strings, this method is locale-dependent.
   1383 
   1384 
   1385 .. method:: str.zfill(width)
   1386 
   1387    Return the numeric string left filled with zeros in a string of length
   1388    *width*.  A sign prefix is handled correctly.  The original string is
   1389    returned if *width* is less than or equal to ``len(s)``.
   1390 
   1391 
   1392    .. versionadded:: 2.2.2
   1393 
   1394 The following methods are present only on unicode objects:
   1395 
   1396 .. method:: unicode.isnumeric()
   1397 
   1398    Return ``True`` if there are only numeric characters in S, ``False``
   1399    otherwise. Numeric characters include digit characters, and all characters
   1400    that have the Unicode numeric value property, e.g. U+2155,
   1401    VULGAR FRACTION ONE FIFTH.
   1402 
   1403 .. method:: unicode.isdecimal()
   1404 
   1405    Return ``True`` if there are only decimal characters in S, ``False``
   1406    otherwise. Decimal characters include digit characters, and all characters
   1407    that can be used to form decimal-radix numbers, e.g. U+0660,
   1408    ARABIC-INDIC DIGIT ZERO.
   1409 
   1410 
   1411 .. _string-formatting:
   1412 
   1413 String Formatting Operations
   1414 ----------------------------
   1415 
   1416 .. index::
   1417    single: formatting, string (%)
   1418    single: interpolation, string (%)
   1419    single: string; formatting
   1420    single: string; interpolation
   1421    single: printf-style formatting
   1422    single: sprintf-style formatting
   1423    single: % formatting
   1424    single: % interpolation
   1425 
   1426 String and Unicode objects have one unique built-in operation: the ``%``
   1427 operator (modulo).  This is also known as the string *formatting* or
   1428 *interpolation* operator.  Given ``format % values`` (where *format* is a string
   1429 or Unicode object), ``%`` conversion specifications in *format* are replaced
   1430 with zero or more elements of *values*.  The effect is similar to the using
   1431 :c:func:`sprintf` in the C language.  If *format* is a Unicode object, or if any
   1432 of the objects being converted using the ``%s`` conversion are Unicode objects,
   1433 the result will also be a Unicode object.
   1434 
   1435 If *format* requires a single argument, *values* may be a single non-tuple
   1436 object. [5]_  Otherwise, *values* must be a tuple with exactly the number of
   1437 items specified by the format string, or a single mapping object (for example, a
   1438 dictionary).
   1439 
   1440 A conversion specifier contains two or more characters and has the following
   1441 components, which must occur in this order:
   1442 
   1443 #. The ``'%'`` character, which marks the start of the specifier.
   1444 
   1445 #. Mapping key (optional), consisting of a parenthesised sequence of characters
   1446    (for example, ``(somename)``).
   1447 
   1448 #. Conversion flags (optional), which affect the result of some conversion
   1449    types.
   1450 
   1451 #. Minimum field width (optional).  If specified as an ``'*'`` (asterisk), the
   1452    actual width is read from the next element of the tuple in *values*, and the
   1453    object to convert comes after the minimum field width and optional precision.
   1454 
   1455 #. Precision (optional), given as a ``'.'`` (dot) followed by the precision.  If
   1456    specified as ``'*'`` (an asterisk), the actual width is read from the next
   1457    element of the tuple in *values*, and the value to convert comes after the
   1458    precision.
   1459 
   1460 #. Length modifier (optional).
   1461 
   1462 #. Conversion type.
   1463 
   1464 When the right argument is a dictionary (or other mapping type), then the
   1465 formats in the string *must* include a parenthesised mapping key into that
   1466 dictionary inserted immediately after the ``'%'`` character. The mapping key
   1467 selects the value to be formatted from the mapping.  For example:
   1468 
   1469    >>> print '%(language)s has %(number)03d quote types.' % \
   1470    ...       {"language": "Python", "number": 2}
   1471    Python has 002 quote types.
   1472 
   1473 In this case no ``*`` specifiers may occur in a format (since they require a
   1474 sequential parameter list).
   1475 
   1476 The conversion flag characters are:
   1477 
   1478 +---------+---------------------------------------------------------------------+
   1479 | Flag    | Meaning                                                             |
   1480 +=========+=====================================================================+
   1481 | ``'#'`` | The value conversion will use the "alternate form" (where defined   |
   1482 |         | below).                                                             |
   1483 +---------+---------------------------------------------------------------------+
   1484 | ``'0'`` | The conversion will be zero padded for numeric values.              |
   1485 +---------+---------------------------------------------------------------------+
   1486 | ``'-'`` | The converted value is left adjusted (overrides the ``'0'``         |
   1487 |         | conversion if both are given).                                      |
   1488 +---------+---------------------------------------------------------------------+
   1489 | ``' '`` | (a space) A blank should be left before a positive number (or empty |
   1490 |         | string) produced by a signed conversion.                            |
   1491 +---------+---------------------------------------------------------------------+
   1492 | ``'+'`` | A sign character (``'+'`` or ``'-'``) will precede the conversion   |
   1493 |         | (overrides a "space" flag).                                         |
   1494 +---------+---------------------------------------------------------------------+
   1495 
   1496 A length modifier (``h``, ``l``, or ``L``) may be present, but is ignored as it
   1497 is not necessary for Python -- so e.g. ``%ld`` is identical to ``%d``.
   1498 
   1499 The conversion types are:
   1500 
   1501 +------------+-----------------------------------------------------+-------+
   1502 | Conversion | Meaning                                             | Notes |
   1503 +============+=====================================================+=======+
   1504 | ``'d'``    | Signed integer decimal.                             |       |
   1505 +------------+-----------------------------------------------------+-------+
   1506 | ``'i'``    | Signed integer decimal.                             |       |
   1507 +------------+-----------------------------------------------------+-------+
   1508 | ``'o'``    | Signed octal value.                                 | \(1)  |
   1509 +------------+-----------------------------------------------------+-------+
   1510 | ``'u'``    | Obsolete type -- it is identical to ``'d'``.        | \(7)  |
   1511 +------------+-----------------------------------------------------+-------+
   1512 | ``'x'``    | Signed hexadecimal (lowercase).                     | \(2)  |
   1513 +------------+-----------------------------------------------------+-------+
   1514 | ``'X'``    | Signed hexadecimal (uppercase).                     | \(2)  |
   1515 +------------+-----------------------------------------------------+-------+
   1516 | ``'e'``    | Floating point exponential format (lowercase).      | \(3)  |
   1517 +------------+-----------------------------------------------------+-------+
   1518 | ``'E'``    | Floating point exponential format (uppercase).      | \(3)  |
   1519 +------------+-----------------------------------------------------+-------+
   1520 | ``'f'``    | Floating point decimal format.                      | \(3)  |
   1521 +------------+-----------------------------------------------------+-------+
   1522 | ``'F'``    | Floating point decimal format.                      | \(3)  |
   1523 +------------+-----------------------------------------------------+-------+
   1524 | ``'g'``    | Floating point format. Uses lowercase exponential   | \(4)  |
   1525 |            | format if exponent is less than -4 or not less than |       |
   1526 |            | precision, decimal format otherwise.                |       |
   1527 +------------+-----------------------------------------------------+-------+
   1528 | ``'G'``    | Floating point format. Uses uppercase exponential   | \(4)  |
   1529 |            | format if exponent is less than -4 or not less than |       |
   1530 |            | precision, decimal format otherwise.                |       |
   1531 +------------+-----------------------------------------------------+-------+
   1532 | ``'c'``    | Single character (accepts integer or single         |       |
   1533 |            | character string).                                  |       |
   1534 +------------+-----------------------------------------------------+-------+
   1535 | ``'r'``    | String (converts any Python object using            | \(5)  |
   1536 |            | :ref:`repr() <func-repr>`).                         |       |
   1537 +------------+-----------------------------------------------------+-------+
   1538 | ``'s'``    | String (converts any Python object using            | \(6)  |
   1539 |            | :func:`str`).                                       |       |
   1540 +------------+-----------------------------------------------------+-------+
   1541 | ``'%'``    | No argument is converted, results in a ``'%'``      |       |
   1542 |            | character in the result.                            |       |
   1543 +------------+-----------------------------------------------------+-------+
   1544 
   1545 Notes:
   1546 
   1547 (1)
   1548    The alternate form causes a leading zero (``'0'``) to be inserted between
   1549    left-hand padding and the formatting of the number if the leading character
   1550    of the result is not already a zero.
   1551 
   1552 (2)
   1553    The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
   1554    the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
   1555    and the formatting of the number if the leading character of the result is not
   1556    already a zero.
   1557 
   1558 (3)
   1559    The alternate form causes the result to always contain a decimal point, even if
   1560    no digits follow it.
   1561 
   1562    The precision determines the number of digits after the decimal point and
   1563    defaults to 6.
   1564 
   1565 (4)
   1566    The alternate form causes the result to always contain a decimal point, and
   1567    trailing zeroes are not removed as they would otherwise be.
   1568 
   1569    The precision determines the number of significant digits before and after the
   1570    decimal point and defaults to 6.
   1571 
   1572 (5)
   1573    The ``%r`` conversion was added in Python 2.0.
   1574 
   1575    The precision determines the maximal number of characters used.
   1576 
   1577 (6)
   1578    If the object or format provided is a :class:`unicode` string, the resulting
   1579    string will also be :class:`unicode`.
   1580 
   1581    The precision determines the maximal number of characters used.
   1582 
   1583 (7)
   1584    See :pep:`237`.
   1585 
   1586 Since Python strings have an explicit length, ``%s`` conversions do not assume
   1587 that ``'\0'`` is the end of the string.
   1588 
   1589 .. XXX Examples?
   1590 
   1591 .. versionchanged:: 2.7
   1592    ``%f`` conversions for numbers whose absolute value is over 1e50 are no
   1593    longer replaced by ``%g`` conversions.
   1594 
   1595 .. index::
   1596    module: string
   1597    module: re
   1598 
   1599 Additional string operations are defined in standard modules :mod:`string` and
   1600 :mod:`re`.
   1601 
   1602 
   1603 .. _typesseq-xrange:
   1604 
   1605 XRange Type
   1606 -----------
   1607 
   1608 .. index:: object: xrange
   1609 
   1610 The :class:`xrange` type is an immutable sequence which is commonly used for
   1611 looping.  The advantage of the :class:`xrange` type is that an :class:`xrange`
   1612 object will always take the same amount of memory, no matter the size of the
   1613 range it represents.  There are no consistent performance advantages.
   1614 
   1615 XRange objects have very little behavior: they only support indexing, iteration,
   1616 and the :func:`len` function.
   1617 
   1618 
   1619 .. _typesseq-mutable:
   1620 
   1621 Mutable Sequence Types
   1622 ----------------------
   1623 
   1624 .. index::
   1625    triple: mutable; sequence; types
   1626    object: list
   1627 
   1628 List and :class:`bytearray` objects support additional operations that allow
   1629 in-place modification of the object. Other mutable sequence types (when added
   1630 to the language) should also support these operations. Strings and tuples
   1631 are immutable sequence types: such objects cannot be modified once created.
   1632 The following operations are defined on mutable sequence types (where *x* is
   1633 an arbitrary object):
   1634 
   1635 .. index::
   1636    triple: operations on; sequence; types
   1637    triple: operations on; list; type
   1638    pair: subscript; assignment
   1639    pair: slice; assignment
   1640    pair: extended slice; assignment
   1641    statement: del
   1642    single: append() (list method)
   1643    single: extend() (list method)
   1644    single: count() (list method)
   1645    single: index() (list method)
   1646    single: insert() (list method)
   1647    single: pop() (list method)
   1648    single: remove() (list method)
   1649    single: reverse() (list method)
   1650    single: sort() (list method)
   1651 
   1652 +------------------------------+--------------------------------+---------------------+
   1653 | Operation                    | Result                         | Notes               |
   1654 +==============================+================================+=====================+
   1655 | ``s[i] = x``                 | item *i* of *s* is replaced by |                     |
   1656 |                              | *x*                            |                     |
   1657 +------------------------------+--------------------------------+---------------------+
   1658 | ``s[i:j] = t``               | slice of *s* from *i* to *j*   |                     |
   1659 |                              | is replaced by the contents of |                     |
   1660 |                              | the iterable *t*               |                     |
   1661 +------------------------------+--------------------------------+---------------------+
   1662 | ``del s[i:j]``               | same as ``s[i:j] = []``        |                     |
   1663 +------------------------------+--------------------------------+---------------------+
   1664 | ``s[i:j:k] = t``             | the elements of ``s[i:j:k]``   | \(1)                |
   1665 |                              | are replaced by those of *t*   |                     |
   1666 +------------------------------+--------------------------------+---------------------+
   1667 | ``del s[i:j:k]``             | removes the elements of        |                     |
   1668 |                              | ``s[i:j:k]`` from the list     |                     |
   1669 +------------------------------+--------------------------------+---------------------+
   1670 | ``s.append(x)``              | same as ``s[len(s):len(s)] =   | \(2)                |
   1671 |                              | [x]``                          |                     |
   1672 +------------------------------+--------------------------------+---------------------+
   1673 | ``s.extend(t)`` or           | for the most part the same as  | \(3)                |
   1674 | ``s += t``                   | ``s[len(s):len(s)] = t``       |                     |
   1675 +------------------------------+--------------------------------+---------------------+
   1676 | ``s *= n``                   | updates *s* with its contents  | \(11)               |
   1677 |                              | repeated *n* times             |                     |
   1678 +------------------------------+--------------------------------+---------------------+
   1679 | ``s.count(x)``               | return number of *i*'s for     |                     |
   1680 |                              | which ``s[i] == x``            |                     |
   1681 +------------------------------+--------------------------------+---------------------+
   1682 | ``s.index(x[, i[, j]])``     | return smallest *k* such that  | \(4)                |
   1683 |                              | ``s[k] == x`` and ``i <= k <   |                     |
   1684 |                              | j``                            |                     |
   1685 +------------------------------+--------------------------------+---------------------+
   1686 | ``s.insert(i, x)``           | same as ``s[i:i] = [x]``       | \(5)                |
   1687 +------------------------------+--------------------------------+---------------------+
   1688 | ``s.pop([i])``               | same as ``x = s[i]; del s[i];  | \(6)                |
   1689 |                              | return x``                     |                     |
   1690 +------------------------------+--------------------------------+---------------------+
   1691 | ``s.remove(x)``              | same as ``del s[s.index(x)]``  | \(4)                |
   1692 +------------------------------+--------------------------------+---------------------+
   1693 | ``s.reverse()``              | reverses the items of *s* in   | \(7)                |
   1694 |                              | place                          |                     |
   1695 +------------------------------+--------------------------------+---------------------+
   1696 | ``s.sort([cmp[, key[,        | sort the items of *s* in place | (7)(8)(9)(10)       |
   1697 | reverse]]])``                |                                |                     |
   1698 +------------------------------+--------------------------------+---------------------+
   1699 
   1700 Notes:
   1701 
   1702 (1)
   1703    *t* must have the same length as the slice it is  replacing.
   1704 
   1705 (2)
   1706    The C implementation of Python has historically accepted multiple parameters and
   1707    implicitly joined them into a tuple; this no longer works in Python 2.0.  Use of
   1708    this misfeature has been deprecated since Python 1.4.
   1709 
   1710 (3)
   1711    *t* can be any iterable object.
   1712 
   1713 (4)
   1714    Raises :exc:`ValueError` when *x* is not found in *s*. When a negative index is
   1715    passed as the second or third parameter to the :meth:`index` method, the list
   1716    length is added, as for slice indices.  If it is still negative, it is truncated
   1717    to zero, as for slice indices.
   1718 
   1719    .. versionchanged:: 2.3
   1720       Previously, :meth:`index` didn't have arguments for specifying start and stop
   1721       positions.
   1722 
   1723 (5)
   1724    When a negative index is passed as the first parameter to the :meth:`insert`
   1725    method, the list length is added, as for slice indices.  If it is still
   1726    negative, it is truncated to zero, as for slice indices.
   1727 
   1728    .. versionchanged:: 2.3
   1729       Previously, all negative indices were truncated to zero.
   1730 
   1731 (6)
   1732    The :meth:`pop` method's optional argument *i* defaults to ``-1``, so that
   1733    by default the last item is removed and returned.
   1734 
   1735 (7)
   1736    The :meth:`sort` and :meth:`reverse` methods modify the list in place for
   1737    economy of space when sorting or reversing a large list.  To remind you that
   1738    they operate by side effect, they don't return the sorted or reversed list.
   1739 
   1740 (8)
   1741    The :meth:`sort` method takes optional arguments for controlling the
   1742    comparisons.
   1743 
   1744    *cmp* specifies a custom comparison function of two arguments (list items) which
   1745    should return a negative, zero or positive number depending on whether the first
   1746    argument is considered smaller than, equal to, or larger than the second
   1747    argument: ``cmp=lambda x,y: cmp(x.lower(), y.lower())``.  The default value
   1748    is ``None``.
   1749 
   1750    *key* specifies a function of one argument that is used to extract a comparison
   1751    key from each list element: ``key=str.lower``.  The default value is ``None``.
   1752 
   1753    *reverse* is a boolean value.  If set to ``True``, then the list elements are
   1754    sorted as if each comparison were reversed.
   1755 
   1756    In general, the *key* and *reverse* conversion processes are much faster than
   1757    specifying an equivalent *cmp* function.  This is because *cmp* is called
   1758    multiple times for each list element while *key* and *reverse* touch each
   1759    element only once.  Use :func:`functools.cmp_to_key` to convert an
   1760    old-style *cmp* function to a *key* function.
   1761 
   1762    .. versionchanged:: 2.3
   1763       Support for ``None`` as an equivalent to omitting *cmp* was added.
   1764 
   1765    .. versionchanged:: 2.4
   1766       Support for *key* and *reverse* was added.
   1767 
   1768 (9)
   1769    Starting with Python 2.3, the :meth:`sort` method is guaranteed to be stable.  A
   1770    sort is stable if it guarantees not to change the relative order of elements
   1771    that compare equal --- this is helpful for sorting in multiple passes (for
   1772    example, sort by department, then by salary grade).
   1773 
   1774 (10)
   1775    .. impl-detail::
   1776 
   1777       While a list is being sorted, the effect of attempting to mutate, or even
   1778       inspect, the list is undefined.  The C implementation of Python 2.3 and
   1779       newer makes the list appear empty for the duration, and raises
   1780       :exc:`ValueError` if it can detect that the list has been mutated during a
   1781       sort.
   1782 
   1783 (11)
   1784    The value *n* is an integer, or an object implementing
   1785    :meth:`~object.__index__`.  Zero and negative values of *n* clear
   1786    the sequence.  Items in the sequence are not copied; they are referenced
   1787    multiple times, as explained for ``s * n`` under :ref:`typesseq`.
   1788 
   1789 
   1790 .. _types-set:
   1791 
   1792 Set Types --- :class:`set`, :class:`frozenset`
   1793 ==============================================
   1794 
   1795 .. index:: object: set
   1796 
   1797 A :dfn:`set` object is an unordered collection of distinct :term:`hashable` objects.
   1798 Common uses include membership testing, removing duplicates from a sequence, and
   1799 computing mathematical operations such as intersection, union, difference, and
   1800 symmetric difference.
   1801 (For other containers see the built in :class:`dict`, :class:`list`,
   1802 and :class:`tuple` classes, and the :mod:`collections` module.)
   1803 
   1804 
   1805 .. versionadded:: 2.4
   1806 
   1807 Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
   1808 set``.  Being an unordered collection, sets do not record element position or
   1809 order of insertion.  Accordingly, sets do not support indexing, slicing, or
   1810 other sequence-like behavior.
   1811 
   1812 There are currently two built-in set types, :class:`set` and :class:`frozenset`.
   1813 The :class:`set` type is mutable --- the contents can be changed using methods
   1814 like :meth:`~set.add` and :meth:`~set.remove`.  Since it is mutable, it has no
   1815 hash value and cannot be used as either a dictionary key or as an element of
   1816 another set.  The :class:`frozenset` type is immutable and :term:`hashable` ---
   1817 its contents cannot be altered after it is created; it can therefore be used as
   1818 a dictionary key or as an element of another set.
   1819 
   1820 As of Python 2.7, non-empty sets (not frozensets) can be created by placing a
   1821 comma-separated list of elements within braces, for example: ``{'jack',
   1822 'sjoerd'}``, in addition to the :class:`set` constructor.
   1823 
   1824 The constructors for both classes work the same:
   1825 
   1826 .. class:: set([iterable])
   1827            frozenset([iterable])
   1828 
   1829    Return a new set or frozenset object whose elements are taken from
   1830    *iterable*.  The elements of a set must be :term:`hashable`.  To
   1831    represent sets of sets, the inner sets must be :class:`frozenset`
   1832    objects.  If *iterable* is not specified, a new empty set is
   1833    returned.
   1834 
   1835    Instances of :class:`set` and :class:`frozenset` provide the following
   1836    operations:
   1837 
   1838    .. describe:: len(s)
   1839 
   1840       Return the number of elements in set *s* (cardinality of *s*).
   1841 
   1842    .. describe:: x in s
   1843 
   1844       Test *x* for membership in *s*.
   1845 
   1846    .. describe:: x not in s
   1847 
   1848       Test *x* for non-membership in *s*.
   1849 
   1850    .. method:: isdisjoint(other)
   1851 
   1852       Return ``True`` if the set has no elements in common with *other*.  Sets are
   1853       disjoint if and only if their intersection is the empty set.
   1854 
   1855       .. versionadded:: 2.6
   1856 
   1857    .. method:: issubset(other)
   1858                set <= other
   1859 
   1860       Test whether every element in the set is in *other*.
   1861 
   1862    .. method:: set < other
   1863 
   1864       Test whether the set is a proper subset of *other*, that is,
   1865       ``set <= other and set != other``.
   1866 
   1867    .. method:: issuperset(other)
   1868                set >= other
   1869 
   1870       Test whether every element in *other* is in the set.
   1871 
   1872    .. method:: set > other
   1873 
   1874       Test whether the set is a proper superset of *other*, that is, ``set >=
   1875       other and set != other``.
   1876 
   1877    .. method:: union(*others)
   1878                set | other | ...
   1879 
   1880       Return a new set with elements from the set and all others.
   1881 
   1882       .. versionchanged:: 2.6
   1883          Accepts multiple input iterables.
   1884 
   1885    .. method:: intersection(*others)
   1886                set & other & ...
   1887 
   1888       Return a new set with elements common to the set and all others.
   1889 
   1890       .. versionchanged:: 2.6
   1891          Accepts multiple input iterables.
   1892 
   1893    .. method:: difference(*others)
   1894                set - other - ...
   1895 
   1896       Return a new set with elements in the set that are not in the others.
   1897 
   1898       .. versionchanged:: 2.6
   1899          Accepts multiple input iterables.
   1900 
   1901    .. method:: symmetric_difference(other)
   1902                set ^ other
   1903 
   1904       Return a new set with elements in either the set or *other* but not both.
   1905 
   1906    .. method:: copy()
   1907 
   1908       Return a new set with a shallow copy of *s*.
   1909 
   1910 
   1911    Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
   1912    :meth:`difference`, and :meth:`symmetric_difference`, :meth:`issubset`, and
   1913    :meth:`issuperset` methods will accept any iterable as an argument.  In
   1914    contrast, their operator based counterparts require their arguments to be
   1915    sets.  This precludes error-prone constructions like ``set('abc') & 'cbs'``
   1916    in favor of the more readable ``set('abc').intersection('cbs')``.
   1917 
   1918    Both :class:`set` and :class:`frozenset` support set to set comparisons. Two
   1919    sets are equal if and only if every element of each set is contained in the
   1920    other (each is a subset of the other). A set is less than another set if and
   1921    only if the first set is a proper subset of the second set (is a subset, but
   1922    is not equal). A set is greater than another set if and only if the first set
   1923    is a proper superset of the second set (is a superset, but is not equal).
   1924 
   1925    Instances of :class:`set` are compared to instances of :class:`frozenset`
   1926    based on their members.  For example, ``set('abc') == frozenset('abc')``
   1927    returns ``True`` and so does ``set('abc') in set([frozenset('abc')])``.
   1928 
   1929    The subset and equality comparisons do not generalize to a total ordering
   1930    function.  For example, any two non-empty disjoint sets are not equal and are not
   1931    subsets of each other, so *all* of the following return ``False``: ``a<b``,
   1932    ``a==b``, or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__`
   1933    method.
   1934 
   1935    Since sets only define partial ordering (subset relationships), the output of
   1936    the :meth:`list.sort` method is undefined for lists of sets.
   1937 
   1938    Set elements, like dictionary keys, must be :term:`hashable`.
   1939 
   1940    Binary operations that mix :class:`set` instances with :class:`frozenset`
   1941    return the type of the first operand.  For example: ``frozenset('ab') |
   1942    set('bc')`` returns an instance of :class:`frozenset`.
   1943 
   1944    The following table lists operations available for :class:`set` that do not
   1945    apply to immutable instances of :class:`frozenset`:
   1946 
   1947    .. method:: update(*others)
   1948                set |= other | ...
   1949 
   1950       Update the set, adding elements from all others.
   1951 
   1952       .. versionchanged:: 2.6
   1953          Accepts multiple input iterables.
   1954 
   1955    .. method:: intersection_update(*others)
   1956                set &= other & ...
   1957 
   1958       Update the set, keeping only elements found in it and all others.
   1959 
   1960       .. versionchanged:: 2.6
   1961          Accepts multiple input iterables.
   1962 
   1963    .. method:: difference_update(*others)
   1964                set -= other | ...
   1965 
   1966       Update the set, removing elements found in others.
   1967 
   1968       .. versionchanged:: 2.6
   1969          Accepts multiple input iterables.
   1970 
   1971    .. method:: symmetric_difference_update(other)
   1972                set ^= other
   1973 
   1974       Update the set, keeping only elements found in either set, but not in both.
   1975 
   1976    .. method:: add(elem)
   1977 
   1978       Add element *elem* to the set.
   1979 
   1980    .. method:: remove(elem)
   1981 
   1982       Remove element *elem* from the set.  Raises :exc:`KeyError` if *elem* is
   1983       not contained in the set.
   1984 
   1985    .. method:: discard(elem)
   1986 
   1987       Remove element *elem* from the set if it is present.
   1988 
   1989    .. method:: pop()
   1990 
   1991       Remove and return an arbitrary element from the set.  Raises
   1992       :exc:`KeyError` if the set is empty.
   1993 
   1994    .. method:: clear()
   1995 
   1996       Remove all elements from the set.
   1997 
   1998 
   1999    Note, the non-operator versions of the :meth:`update`,
   2000    :meth:`intersection_update`, :meth:`difference_update`, and
   2001    :meth:`symmetric_difference_update` methods will accept any iterable as an
   2002    argument.
   2003 
   2004    Note, the *elem* argument to the :meth:`__contains__`, :meth:`remove`, and
   2005    :meth:`discard` methods may be a set.  To support searching for an equivalent
   2006    frozenset, the *elem* set is temporarily mutated during the search and then
   2007    restored.  During the search, the *elem* set should not be read or mutated
   2008    since it does not have a meaningful value.
   2009 
   2010 
   2011 .. seealso::
   2012 
   2013    :ref:`comparison-to-builtin-set`
   2014       Differences between the :mod:`sets` module and the built-in set types.
   2015 
   2016 
   2017 .. _typesmapping:
   2018 
   2019 Mapping Types --- :class:`dict`
   2020 ===============================
   2021 
   2022 .. index::
   2023    object: mapping
   2024    object: dictionary
   2025    triple: operations on; mapping; types
   2026    triple: operations on; dictionary; type
   2027    statement: del
   2028    builtin: len
   2029 
   2030 A :term:`mapping` object maps :term:`hashable` values to arbitrary objects.
   2031 Mappings are mutable objects.  There is currently only one standard mapping
   2032 type, the :dfn:`dictionary`.  (For other containers see the built in
   2033 :class:`list`, :class:`set`, and :class:`tuple` classes, and the
   2034 :mod:`collections` module.)
   2035 
   2036 A dictionary's keys are *almost* arbitrary values.  Values that are not
   2037 :term:`hashable`, that is, values containing lists, dictionaries or other
   2038 mutable types (that are compared by value rather than by object identity) may
   2039 not be used as keys.  Numeric types used for keys obey the normal rules for
   2040 numeric comparison: if two numbers compare equal (such as ``1`` and ``1.0``)
   2041 then they can be used interchangeably to index the same dictionary entry.  (Note
   2042 however, that since computers store floating-point numbers as approximations it
   2043 is usually unwise to use them as dictionary keys.)
   2044 
   2045 Dictionaries can be created by placing a comma-separated list of ``key: value``
   2046 pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
   2047 'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.
   2048 
   2049 .. class:: dict(**kwarg)
   2050            dict(mapping, **kwarg)
   2051            dict(iterable, **kwarg)
   2052 
   2053    Return a new dictionary initialized from an optional positional argument
   2054    and a possibly empty set of keyword arguments.
   2055 
   2056    If no positional argument is given, an empty dictionary is created.
   2057    If a positional argument is given and it is a mapping object, a dictionary
   2058    is created with the same key-value pairs as the mapping object.  Otherwise,
   2059    the positional argument must be an :term:`iterable` object.  Each item in
   2060    the iterable must itself be an iterable with exactly two objects.  The
   2061    first object of each item becomes a key in the new dictionary, and the
   2062    second object the corresponding value.  If a key occurs more than once, the
   2063    last value for that key becomes the corresponding value in the new
   2064    dictionary.
   2065 
   2066    If keyword arguments are given, the keyword arguments and their values are
   2067    added to the dictionary created from the positional argument.  If a key
   2068    being added is already present, the value from the keyword argument
   2069    replaces the value from the positional argument.
   2070 
   2071    To illustrate, the following examples all return a dictionary equal to
   2072    ``{"one": 1, "two": 2, "three": 3}``::
   2073 
   2074       >>> a = dict(one=1, two=2, three=3)
   2075       >>> b = {'one': 1, 'two': 2, 'three': 3}
   2076       >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
   2077       >>> d = dict([('two', 2), ('one', 1), ('three', 3)])
   2078       >>> e = dict({'three': 3, 'one': 1, 'two': 2})
   2079       >>> a == b == c == d == e
   2080       True
   2081 
   2082    Providing keyword arguments as in the first example only works for keys that
   2083    are valid Python identifiers.  Otherwise, any valid keys can be used.
   2084 
   2085    .. versionadded:: 2.2
   2086 
   2087    .. versionchanged:: 2.3
   2088       Support for building a dictionary from keyword arguments added.
   2089 
   2090 
   2091    These are the operations that dictionaries support (and therefore, custom
   2092    mapping types should support too):
   2093 
   2094    .. describe:: len(d)
   2095 
   2096       Return the number of items in the dictionary *d*.
   2097 
   2098    .. describe:: d[key]
   2099 
   2100       Return the item of *d* with key *key*.  Raises a :exc:`KeyError` if *key*
   2101       is not in the map.
   2102 
   2103       .. index:: __missing__()
   2104 
   2105       If a subclass of dict defines a method :meth:`__missing__` and *key*
   2106       is not present, the ``d[key]`` operation calls that method with the key *key*
   2107       as argument.  The ``d[key]`` operation then returns or raises whatever is
   2108       returned or raised by the ``__missing__(key)`` call.
   2109       No other operations or methods invoke :meth:`__missing__`. If
   2110       :meth:`__missing__` is not defined, :exc:`KeyError` is raised.
   2111       :meth:`__missing__` must be a method; it cannot be an instance variable::
   2112 
   2113           >>> class Counter(dict):
   2114           ...     def __missing__(self, key):
   2115           ...         return 0
   2116           >>> c = Counter()
   2117           >>> c['red']
   2118           0
   2119           >>> c['red'] += 1
   2120           >>> c['red']
   2121           1
   2122 
   2123       The example above shows part of the implementation of
   2124       :class:`collections.Counter`.  A different ``__missing__`` method is used
   2125       by :class:`collections.defaultdict`.
   2126 
   2127       .. versionadded:: 2.5
   2128          Recognition of __missing__ methods of dict subclasses.
   2129 
   2130    .. describe:: d[key] = value
   2131 
   2132       Set ``d[key]`` to *value*.
   2133 
   2134    .. describe:: del d[key]
   2135 
   2136       Remove ``d[key]`` from *d*.  Raises a :exc:`KeyError` if *key* is not in the
   2137       map.
   2138 
   2139    .. describe:: key in d
   2140 
   2141       Return ``True`` if *d* has a key *key*, else ``False``.
   2142 
   2143       .. versionadded:: 2.2
   2144 
   2145    .. describe:: key not in d
   2146 
   2147       Equivalent to ``not key in d``.
   2148 
   2149       .. versionadded:: 2.2
   2150 
   2151    .. describe:: iter(d)
   2152 
   2153       Return an iterator over the keys of the dictionary.  This is a shortcut
   2154       for :meth:`iterkeys`.
   2155 
   2156    .. method:: clear()
   2157 
   2158       Remove all items from the dictionary.
   2159 
   2160    .. method:: copy()
   2161 
   2162       Return a shallow copy of the dictionary.
   2163 
   2164    .. method:: fromkeys(seq[, value])
   2165 
   2166       Create a new dictionary with keys from *seq* and values set to *value*.
   2167 
   2168       :func:`fromkeys` is a class method that returns a new dictionary. *value*
   2169       defaults to ``None``.
   2170 
   2171       .. versionadded:: 2.3
   2172 
   2173    .. method:: get(key[, default])
   2174 
   2175       Return the value for *key* if *key* is in the dictionary, else *default*.
   2176       If *default* is not given, it defaults to ``None``, so that this method
   2177       never raises a :exc:`KeyError`.
   2178 
   2179    .. method:: has_key(key)
   2180 
   2181       Test for the presence of *key* in the dictionary.  :meth:`has_key` is
   2182       deprecated in favor of ``key in d``.
   2183 
   2184    .. method:: items()
   2185 
   2186       Return a copy of the dictionary's list of ``(key, value)`` pairs.
   2187 
   2188       .. impl-detail::
   2189 
   2190          Keys and values are listed in an arbitrary order which is non-random,
   2191          varies across Python implementations, and depends on the dictionary's
   2192          history of insertions and deletions.
   2193 
   2194       If :meth:`items`, :meth:`keys`, :meth:`values`, :meth:`iteritems`,
   2195       :meth:`iterkeys`, and :meth:`itervalues` are called with no intervening
   2196       modifications to the dictionary, the lists will directly correspond.  This
   2197       allows the creation of ``(value, key)`` pairs using :func:`zip`: ``pairs =
   2198       zip(d.values(), d.keys())``.  The same relationship holds for the
   2199       :meth:`iterkeys` and :meth:`itervalues` methods: ``pairs =
   2200       zip(d.itervalues(), d.iterkeys())`` provides the same value for
   2201       ``pairs``. Another way to create the same list is ``pairs = [(v, k) for
   2202       (k, v) in d.iteritems()]``.
   2203 
   2204    .. method:: iteritems()
   2205 
   2206       Return an iterator over the dictionary's ``(key, value)`` pairs.  See the
   2207       note for :meth:`dict.items`.
   2208 
   2209       Using :meth:`iteritems` while adding or deleting entries in the dictionary
   2210       may raise a :exc:`RuntimeError` or fail to iterate over all entries.
   2211 
   2212       .. versionadded:: 2.2
   2213 
   2214    .. method:: iterkeys()
   2215 
   2216       Return an iterator over the dictionary's keys.  See the note for
   2217       :meth:`dict.items`.
   2218 
   2219       Using :meth:`iterkeys` while adding or deleting entries in the dictionary
   2220       may raise a :exc:`RuntimeError` or fail to iterate over all entries.
   2221 
   2222       .. versionadded:: 2.2
   2223 
   2224    .. method:: itervalues()
   2225 
   2226       Return an iterator over the dictionary's values.  See the note for
   2227       :meth:`dict.items`.
   2228 
   2229       Using :meth:`itervalues` while adding or deleting entries in the
   2230       dictionary may raise a :exc:`RuntimeError` or fail to iterate over all
   2231       entries.
   2232 
   2233       .. versionadded:: 2.2
   2234 
   2235    .. method:: keys()
   2236 
   2237       Return a copy of the dictionary's list of keys.  See the note for
   2238       :meth:`dict.items`.
   2239 
   2240    .. method:: pop(key[, default])
   2241 
   2242       If *key* is in the dictionary, remove it and return its value, else return
   2243       *default*.  If *default* is not given and *key* is not in the dictionary,
   2244       a :exc:`KeyError` is raised.
   2245 
   2246       .. versionadded:: 2.3
   2247 
   2248    .. method:: popitem()
   2249 
   2250       Remove and return an arbitrary ``(key, value)`` pair from the dictionary.
   2251 
   2252       :func:`popitem` is useful to destructively iterate over a dictionary, as
   2253       often used in set algorithms.  If the dictionary is empty, calling
   2254       :func:`popitem` raises a :exc:`KeyError`.
   2255 
   2256    .. method:: setdefault(key[, default])
   2257 
   2258       If *key* is in the dictionary, return its value.  If not, insert *key*
   2259       with a value of *default* and return *default*.  *default* defaults to
   2260       ``None``.
   2261 
   2262    .. method:: update([other])
   2263 
   2264       Update the dictionary with the key/value pairs from *other*, overwriting
   2265       existing keys.  Return ``None``.
   2266 
   2267       :func:`update` accepts either another dictionary object or an iterable of
   2268       key/value pairs (as tuples or other iterables of length two).  If keyword
   2269       arguments are specified, the dictionary is then updated with those
   2270       key/value pairs: ``d.update(red=1, blue=2)``.
   2271 
   2272       .. versionchanged:: 2.4
   2273           Allowed the argument to be an iterable of key/value pairs and allowed
   2274           keyword arguments.
   2275 
   2276    .. method:: values()
   2277 
   2278       Return a copy of the dictionary's list of values.  See the note for
   2279       :meth:`dict.items`.
   2280 
   2281    .. method:: viewitems()
   2282 
   2283       Return a new view of the dictionary's items (``(key, value)`` pairs).  See
   2284       below for documentation of view objects.
   2285 
   2286       .. versionadded:: 2.7
   2287 
   2288    .. method:: viewkeys()
   2289 
   2290       Return a new view of the dictionary's keys.  See below for documentation of
   2291       view objects.
   2292 
   2293       .. versionadded:: 2.7
   2294 
   2295    .. method:: viewvalues()
   2296 
   2297       Return a new view of the dictionary's values.  See below for documentation of
   2298       view objects.
   2299 
   2300       .. versionadded:: 2.7
   2301 
   2302    Dictionaries compare equal if and only if they have the same ``(key,
   2303    value)`` pairs.
   2304 
   2305 
   2306 .. _dict-views:
   2307 
   2308 Dictionary view objects
   2309 -----------------------
   2310 
   2311 The objects returned by :meth:`dict.viewkeys`, :meth:`dict.viewvalues` and
   2312 :meth:`dict.viewitems` are *view objects*.  They provide a dynamic view on the
   2313 dictionary's entries, which means that when the dictionary changes, the view
   2314 reflects these changes.
   2315 
   2316 Dictionary views can be iterated over to yield their respective data, and
   2317 support membership tests:
   2318 
   2319 .. describe:: len(dictview)
   2320 
   2321    Return the number of entries in the dictionary.
   2322 
   2323 .. describe:: iter(dictview)
   2324 
   2325    Return an iterator over the keys, values or items (represented as tuples of
   2326    ``(key, value)``) in the dictionary.
   2327 
   2328    Keys and values are iterated over in an arbitrary order which is non-random,
   2329    varies across Python implementations, and depends on the dictionary's history
   2330    of insertions and deletions. If keys, values and items views are iterated
   2331    over with no intervening modifications to the dictionary, the order of items
   2332    will directly correspond.  This allows the creation of ``(value, key)`` pairs
   2333    using :func:`zip`: ``pairs = zip(d.values(), d.keys())``.  Another way to
   2334    create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
   2335 
   2336    Iterating views while adding or deleting entries in the dictionary may raise
   2337    a :exc:`RuntimeError` or fail to iterate over all entries.
   2338 
   2339 .. describe:: x in dictview
   2340 
   2341    Return ``True`` if *x* is in the underlying dictionary's keys, values or
   2342    items (in the latter case, *x* should be a ``(key, value)`` tuple).
   2343 
   2344 
   2345 Keys views are set-like since their entries are unique and hashable.  If all
   2346 values are hashable, so that (key, value) pairs are unique and hashable, then
   2347 the items view is also set-like.  (Values views are not treated as set-like
   2348 since the entries are generally not unique.)  Then these set operations are
   2349 available ("other" refers either to another view or a set):
   2350 
   2351 .. describe:: dictview & other
   2352 
   2353    Return the intersection of the dictview and the other object as a new set.
   2354 
   2355 .. describe:: dictview | other
   2356 
   2357    Return the union of the dictview and the other object as a new set.
   2358 
   2359 .. describe:: dictview - other
   2360 
   2361    Return the difference between the dictview and the other object (all elements
   2362    in *dictview* that aren't in *other*) as a new set.
   2363 
   2364 .. describe:: dictview ^ other
   2365 
   2366    Return the symmetric difference (all elements either in *dictview* or
   2367    *other*, but not in both) of the dictview and the other object as a new set.
   2368 
   2369 
   2370 An example of dictionary view usage::
   2371 
   2372    >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
   2373    >>> keys = dishes.viewkeys()
   2374    >>> values = dishes.viewvalues()
   2375 
   2376    >>> # iteration
   2377    >>> n = 0
   2378    >>> for val in values:
   2379    ...     n += val
   2380    >>> print(n)
   2381    504
   2382 
   2383    >>> # keys and values are iterated over in the same order
   2384    >>> list(keys)
   2385    ['eggs', 'bacon', 'sausage', 'spam']
   2386    >>> list(values)
   2387    [2, 1, 1, 500]
   2388 
   2389    >>> # view objects are dynamic and reflect dict changes
   2390    >>> del dishes['eggs']
   2391    >>> del dishes['sausage']
   2392    >>> list(keys)
   2393    ['spam', 'bacon']
   2394 
   2395    >>> # set operations
   2396    >>> keys & {'eggs', 'bacon', 'salad'}
   2397    {'bacon'}
   2398 
   2399 
   2400 .. _bltin-file-objects:
   2401 
   2402 File Objects
   2403 ============
   2404 
   2405 .. index::
   2406    object: file
   2407    builtin: file
   2408    module: os
   2409    module: socket
   2410 
   2411 File objects are implemented using C's ``stdio`` package and can be
   2412 created with the built-in :func:`open` function.  File
   2413 objects are also returned by some other built-in functions and methods,
   2414 such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile`
   2415 method of socket objects. Temporary files can be created using the
   2416 :mod:`tempfile` module, and high-level file operations such as copying,
   2417 moving, and deleting files and directories can be achieved with the
   2418 :mod:`shutil` module.
   2419 
   2420 When a file operation fails for an I/O-related reason, the exception
   2421 :exc:`IOError` is raised.  This includes situations where the operation is not
   2422 defined for some reason, like :meth:`seek` on a tty device or writing a file
   2423 opened for reading.
   2424 
   2425 Files have the following methods:
   2426 
   2427 
   2428 .. method:: file.close()
   2429 
   2430    Close the file.  A closed file cannot be read or written any more. Any operation
   2431    which requires that the file be open will raise a :exc:`ValueError` after the
   2432    file has been closed.  Calling :meth:`close` more than once is allowed.
   2433 
   2434    As of Python 2.5, you can avoid having to call this method explicitly if you use
   2435    the :keyword:`with` statement.  For example, the following code will
   2436    automatically close *f* when the :keyword:`with` block is exited::
   2437 
   2438       from __future__ import with_statement # This isn't required in Python 2.6
   2439 
   2440       with open("hello.txt") as f:
   2441           for line in f:
   2442               print line,
   2443 
   2444    In older versions of Python, you would have needed to do this to get the same
   2445    effect::
   2446 
   2447       f = open("hello.txt")
   2448       try:
   2449           for line in f:
   2450               print line,
   2451       finally:
   2452           f.close()
   2453 
   2454    .. note::
   2455 
   2456       Not all "file-like" types in Python support use as a context manager for the
   2457       :keyword:`with` statement.  If your code is intended to work with any file-like
   2458       object, you can use the function :func:`contextlib.closing` instead of using
   2459       the object directly.
   2460 
   2461 
   2462 .. method:: file.flush()
   2463 
   2464    Flush the internal buffer, like ``stdio``'s :c:func:`fflush`.  This may be a
   2465    no-op on some file-like objects.
   2466 
   2467    .. note::
   2468 
   2469       :meth:`flush` does not necessarily write the file's data to disk.  Use
   2470       :meth:`flush` followed by :func:`os.fsync` to ensure this behavior.
   2471 
   2472 
   2473 .. method:: file.fileno()
   2474 
   2475    .. index::
   2476       pair: file; descriptor
   2477       module: fcntl
   2478 
   2479    Return the integer "file descriptor" that is used by the underlying
   2480    implementation to request I/O operations from the operating system.  This can be
   2481    useful for other, lower level interfaces that use file descriptors, such as the
   2482    :mod:`fcntl` module or :func:`os.read` and friends.
   2483 
   2484    .. note::
   2485 
   2486       File-like objects which do not have a real file descriptor should *not* provide
   2487       this method!
   2488 
   2489 
   2490 .. method:: file.isatty()
   2491 
   2492    Return ``True`` if the file is connected to a tty(-like) device, else ``False``.
   2493 
   2494    .. note::
   2495 
   2496       If a file-like object is not associated with a real file, this method should
   2497       *not* be implemented.
   2498 
   2499 
   2500 .. method:: file.next()
   2501 
   2502    A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
   2503    *f* is closed).  When a file is used as an iterator, typically in a
   2504    :keyword:`for` loop (for example, ``for line in f: print line.strip()``), the
   2505    :meth:`~file.next` method is called repeatedly.  This method returns the next input
   2506    line, or raises :exc:`StopIteration` when EOF is hit when the file is open for
   2507    reading (behavior is undefined when the file is open for writing).  In order to
   2508    make a :keyword:`for` loop the most efficient way of looping over the lines of a
   2509    file (a very common operation), the :meth:`~file.next` method uses a hidden read-ahead
   2510    buffer.  As a consequence of using a read-ahead buffer, combining :meth:`~file.next`
   2511    with other file methods (like :meth:`~file.readline`) does not work right.  However,
   2512    using :meth:`seek` to reposition the file to an absolute position will flush the
   2513    read-ahead buffer.
   2514 
   2515    .. versionadded:: 2.3
   2516 
   2517 
   2518 .. method:: file.read([size])
   2519 
   2520    Read at most *size* bytes from the file (less if the read hits EOF before
   2521    obtaining *size* bytes).  If the *size* argument is negative or omitted, read
   2522    all data until EOF is reached.  The bytes are returned as a string object.  An
   2523    empty string is returned when EOF is encountered immediately.  (For certain
   2524    files, like ttys, it makes sense to continue reading after an EOF is hit.)  Note
   2525    that this method may call the underlying C function :c:func:`fread` more than
   2526    once in an effort to acquire as close to *size* bytes as possible. Also note
   2527    that when in non-blocking mode, less data than was requested may be
   2528    returned, even if no *size* parameter was given.
   2529 
   2530    .. note::
   2531       This function is simply a wrapper for the underlying
   2532       :c:func:`fread` C function, and will behave the same in corner cases,
   2533       such as whether the EOF value is cached.
   2534 
   2535 
   2536 .. method:: file.readline([size])
   2537 
   2538    Read one entire line from the file.  A trailing newline character is kept in
   2539    the string (but may be absent when a file ends with an incomplete line). [6]_
   2540    If the *size* argument is present and non-negative, it is a maximum byte
   2541    count (including the trailing newline) and an incomplete line may be
   2542    returned. When *size* is not 0, an empty string is returned *only* when EOF
   2543    is encountered immediately.
   2544 
   2545    .. note::
   2546 
   2547       Unlike ``stdio``'s :c:func:`fgets`, the returned string contains null characters
   2548       (``'\0'``) if they occurred in the input.
   2549 
   2550 
   2551 .. method:: file.readlines([sizehint])
   2552 
   2553    Read until EOF using :meth:`~file.readline` and return a list containing the lines
   2554    thus read.  If the optional *sizehint* argument is present, instead of
   2555    reading up to EOF, whole lines totalling approximately *sizehint* bytes
   2556    (possibly after rounding up to an internal buffer size) are read.  Objects
   2557    implementing a file-like interface may choose to ignore *sizehint* if it
   2558    cannot be implemented, or cannot be implemented efficiently.
   2559 
   2560 
   2561 .. method:: file.xreadlines()
   2562 
   2563    This method returns the same thing as ``iter(f)``.
   2564 
   2565    .. versionadded:: 2.1
   2566 
   2567    .. deprecated:: 2.3
   2568       Use ``for line in file`` instead.
   2569 
   2570 
   2571 .. method:: file.seek(offset[, whence])
   2572 
   2573    Set the file's current position, like ``stdio``'s :c:func:`fseek`. The *whence*
   2574    argument is optional and defaults to  ``os.SEEK_SET`` or ``0`` (absolute file
   2575    positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the
   2576    current position) and ``os.SEEK_END`` or ``2``  (seek relative to the file's
   2577    end).  There is no return value.
   2578 
   2579    For example, ``f.seek(2, os.SEEK_CUR)`` advances the position by two and
   2580    ``f.seek(-3, os.SEEK_END)`` sets the position to the third to last.
   2581 
   2582    Note that if the file is opened for appending
   2583    (mode ``'a'`` or ``'a+'``), any :meth:`seek` operations will be undone at the
   2584    next write.  If the file is only opened for writing in append mode (mode
   2585    ``'a'``), this method is essentially a no-op, but it remains useful for files
   2586    opened in append mode with reading enabled (mode ``'a+'``).  If the file is
   2587    opened in text mode (without ``'b'``), only offsets returned by :meth:`tell` are
   2588    legal.  Use of other offsets causes undefined behavior.
   2589 
   2590    Note that not all file objects are seekable.
   2591 
   2592    .. versionchanged:: 2.6
   2593       Passing float values as offset has been deprecated.
   2594 
   2595 
   2596 .. method:: file.tell()
   2597 
   2598    Return the file's current position, like ``stdio``'s :c:func:`ftell`.
   2599 
   2600    .. note::
   2601 
   2602       On Windows, :meth:`tell` can return illegal values (after an :c:func:`fgets`)
   2603       when reading files with Unix-style line-endings. Use binary mode (``'rb'``) to
   2604       circumvent this problem.
   2605 
   2606 
   2607 .. method:: file.truncate([size])
   2608 
   2609    Truncate the file's size.  If the optional *size* argument is present, the file
   2610    is truncated to (at most) that size.  The size defaults to the current position.
   2611    The current file position is not changed.  Note that if a specified size exceeds
   2612    the file's current size, the result is platform-dependent:  possibilities
   2613    include that the file may remain unchanged, increase to the specified size as if
   2614    zero-filled, or increase to the specified size with undefined new content.
   2615    Availability:  Windows, many Unix variants.
   2616 
   2617 
   2618 .. method:: file.write(str)
   2619 
   2620    Write a string to the file.  There is no return value.  Due to buffering, the
   2621    string may not actually show up in the file until the :meth:`flush` or
   2622    :meth:`close` method is called.
   2623 
   2624 
   2625 .. method:: file.writelines(sequence)
   2626 
   2627    Write a sequence of strings to the file.  The sequence can be any iterable
   2628    object producing strings, typically a list of strings. There is no return value.
   2629    (The name is intended to match :meth:`readlines`; :meth:`writelines` does not
   2630    add line separators.)
   2631 
   2632 Files support the iterator protocol.  Each iteration returns the same result as
   2633 :meth:`~file.readline`, and iteration ends when the :meth:`~file.readline` method returns
   2634 an empty string.
   2635 
   2636 File objects also offer a number of other interesting attributes. These are not
   2637 required for file-like objects, but should be implemented if they make sense for
   2638 the particular object.
   2639 
   2640 
   2641 .. attribute:: file.closed
   2642 
   2643    bool indicating the current state of the file object.  This is a read-only
   2644    attribute; the :meth:`close` method changes the value. It may not be available
   2645    on all file-like objects.
   2646 
   2647 
   2648 .. attribute:: file.encoding
   2649 
   2650    The encoding that this file uses. When Unicode strings are written to a file,
   2651    they will be converted to byte strings using this encoding. In addition, when
   2652    the file is connected to a terminal, the attribute gives the encoding that the
   2653    terminal is likely to use (that  information might be incorrect if the user has
   2654    misconfigured the  terminal). The attribute is read-only and may not be present
   2655    on all file-like objects. It may also be ``None``, in which case the file uses
   2656    the system default encoding for converting Unicode strings.
   2657 
   2658    .. versionadded:: 2.3
   2659 
   2660 
   2661 .. attribute:: file.errors
   2662 
   2663    The Unicode error handler used along with the encoding.
   2664 
   2665    .. versionadded:: 2.6
   2666 
   2667 
   2668 .. attribute:: file.mode
   2669 
   2670    The I/O mode for the file.  If the file was created using the :func:`open`
   2671    built-in function, this will be the value of the *mode* parameter.  This is a
   2672    read-only attribute and may not be present on all file-like objects.
   2673 
   2674 
   2675 .. attribute:: file.name
   2676 
   2677    If the file object was created using :func:`open`, the name of the file.
   2678    Otherwise, some string that indicates the source of the file object, of the
   2679    form ``<...>``.  This is a read-only attribute and may not be present on all
   2680    file-like objects.
   2681 
   2682    .. index::
   2683       single: universal newlines; file.newlines attribute
   2684 
   2685 
   2686 .. attribute:: file.newlines
   2687 
   2688    If Python was built with :term:`universal newlines` enabled (the default) this
   2689    read-only attribute exists, and for files opened in universal newline read
   2690    mode it keeps track of the types of newlines encountered while reading the
   2691    file. The values it can take are ``'\r'``, ``'\n'``, ``'\r\n'``, ``None``
   2692    (unknown, no newlines read yet) or a tuple containing all the newline types
   2693    seen, to indicate that multiple newline conventions were encountered. For
   2694    files not opened in universal newlines read mode the value of this attribute
   2695    will be ``None``.
   2696 
   2697 
   2698 .. attribute:: file.softspace
   2699 
   2700    Boolean that indicates whether a space character needs to be printed before
   2701    another value when using the :keyword:`print` statement. Classes that are trying
   2702    to simulate a file object should also have a writable :attr:`softspace`
   2703    attribute, which should be initialized to zero.  This will be automatic for most
   2704    classes implemented in Python (care may be needed for objects that override
   2705    attribute access); types implemented in C will have to provide a writable
   2706    :attr:`softspace` attribute.
   2707 
   2708    .. note::
   2709 
   2710       This attribute is not used to control the :keyword:`print` statement, but to
   2711       allow the implementation of :keyword:`print` to keep track of its internal
   2712       state.
   2713 
   2714 
   2715 .. _typememoryview:
   2716 
   2717 memoryview type
   2718 ===============
   2719 
   2720 .. versionadded:: 2.7
   2721 
   2722 :class:`memoryview` objects allow Python code to access the internal data
   2723 of an object that supports the buffer protocol without copying.  Memory
   2724 is generally interpreted as simple bytes.
   2725 
   2726 .. class:: memoryview(obj)
   2727 
   2728    Create a :class:`memoryview` that references *obj*.  *obj* must support the
   2729    buffer protocol.  Built-in objects that support the buffer protocol include
   2730    :class:`str` and :class:`bytearray` (but not :class:`unicode`).
   2731 
   2732    A :class:`memoryview` has the notion of an *element*, which is the
   2733    atomic memory unit handled by the originating object *obj*.  For many
   2734    simple types such as :class:`str` and :class:`bytearray`, an element
   2735    is a single byte, but other third-party types may expose larger elements.
   2736 
   2737    ``len(view)`` returns the total number of elements in the memoryview,
   2738    *view*.  The :class:`~memoryview.itemsize` attribute will give you the
   2739    number of bytes in a single element.
   2740 
   2741    A :class:`memoryview` supports slicing to expose its data.  Taking a single
   2742    index will return a single element as a :class:`str` object.  Full
   2743    slicing will result in a subview::
   2744 
   2745       >>> v = memoryview('abcefg')
   2746       >>> v[1]
   2747       'b'
   2748       >>> v[-1]
   2749       'g'
   2750       >>> v[1:4]
   2751       <memory at 0x77ab28>
   2752       >>> v[1:4].tobytes()
   2753       'bce'
   2754 
   2755    If the object the memoryview is over supports changing its data, the
   2756    memoryview supports slice assignment::
   2757 
   2758       >>> data = bytearray('abcefg')
   2759       >>> v = memoryview(data)
   2760       >>> v.readonly
   2761       False
   2762       >>> v[0] = 'z'
   2763       >>> data
   2764       bytearray(b'zbcefg')
   2765       >>> v[1:4] = '123'
   2766       >>> data
   2767       bytearray(b'z123fg')
   2768       >>> v[2] = 'spam'
   2769       Traceback (most recent call last):
   2770         File "<stdin>", line 1, in <module>
   2771       ValueError: cannot modify size of memoryview object
   2772 
   2773    Notice how the size of the memoryview object cannot be changed.
   2774 
   2775    :class:`memoryview` has two methods:
   2776 
   2777    .. method:: tobytes()
   2778 
   2779       Return the data in the buffer as a bytestring (an object of class
   2780       :class:`str`). ::
   2781 
   2782          >>> m = memoryview("abc")
   2783          >>> m.tobytes()
   2784          'abc'
   2785 
   2786    .. method:: tolist()
   2787 
   2788       Return the data in the buffer as a list of integers. ::
   2789 
   2790          >>> memoryview("abc").tolist()
   2791          [97, 98, 99]
   2792 
   2793    There are also several readonly attributes available:
   2794 
   2795    .. attribute:: format
   2796 
   2797       A string containing the format (in :mod:`struct` module style) for each
   2798       element in the view.  This defaults to ``'B'``, a simple bytestring.
   2799 
   2800    .. attribute:: itemsize
   2801 
   2802       The size in bytes of each element of the memoryview.
   2803 
   2804    .. attribute:: shape
   2805 
   2806       A tuple of integers the length of :attr:`ndim` giving the shape of the
   2807       memory as an N-dimensional array.
   2808 
   2809    .. attribute:: ndim
   2810 
   2811       An integer indicating how many dimensions of a multi-dimensional array the
   2812       memory represents.
   2813 
   2814    .. attribute:: strides
   2815 
   2816       A tuple of integers the length of :attr:`ndim` giving the size in bytes to
   2817       access each element for each dimension of the array.
   2818 
   2819    .. attribute:: readonly
   2820 
   2821       A bool indicating whether the memory is read only.
   2822 
   2823    .. memoryview.suboffsets isn't documented because it only seems useful for C
   2824 
   2825 
   2826 .. _typecontextmanager:
   2827 
   2828 Context Manager Types
   2829 =====================
   2830 
   2831 .. versionadded:: 2.5
   2832 
   2833 .. index::
   2834    single: context manager
   2835    single: context management protocol
   2836    single: protocol; context management
   2837 
   2838 Python's :keyword:`with` statement supports the concept of a runtime context
   2839 defined by a context manager.  This is implemented using two separate methods
   2840 that allow user-defined classes to define a runtime context that is entered
   2841 before the statement body is executed and exited when the statement ends.
   2842 
   2843 The :dfn:`context management protocol` consists of a pair of methods that need
   2844 to be provided for a context manager object to define a runtime context:
   2845 
   2846 
   2847 .. method:: contextmanager.__enter__()
   2848 
   2849    Enter the runtime context and return either this object or another object
   2850    related to the runtime context. The value returned by this method is bound to
   2851    the identifier in the :keyword:`as` clause of :keyword:`with` statements using
   2852    this context manager.
   2853 
   2854    An example of a context manager that returns itself is a file object. File
   2855    objects return themselves from __enter__() to allow :func:`open` to be used as
   2856    the context expression in a :keyword:`with` statement.
   2857 
   2858    An example of a context manager that returns a related object is the one
   2859    returned by :func:`decimal.localcontext`. These managers set the active
   2860    decimal context to a copy of the original decimal context and then return the
   2861    copy. This allows changes to be made to the current decimal context in the body
   2862    of the :keyword:`with` statement without affecting code outside the
   2863    :keyword:`with` statement.
   2864 
   2865 
   2866 .. method:: contextmanager.__exit__(exc_type, exc_val, exc_tb)
   2867 
   2868    Exit the runtime context and return a Boolean flag indicating if any exception
   2869    that occurred should be suppressed. If an exception occurred while executing the
   2870    body of the :keyword:`with` statement, the arguments contain the exception type,
   2871    value and traceback information. Otherwise, all three arguments are ``None``.
   2872 
   2873    Returning a true value from this method will cause the :keyword:`with` statement
   2874    to suppress the exception and continue execution with the statement immediately
   2875    following the :keyword:`with` statement. Otherwise the exception continues
   2876    propagating after this method has finished executing. Exceptions that occur
   2877    during execution of this method will replace any exception that occurred in the
   2878    body of the :keyword:`with` statement.
   2879 
   2880    The exception passed in should never be reraised explicitly - instead, this
   2881    method should return a false value to indicate that the method completed
   2882    successfully and does not want to suppress the raised exception. This allows
   2883    context management code (such as ``contextlib.nested``) to easily detect whether
   2884    or not an :meth:`__exit__` method has actually failed.
   2885 
   2886 Python defines several context managers to support easy thread synchronisation,
   2887 prompt closure of files or other objects, and simpler manipulation of the active
   2888 decimal arithmetic context. The specific types are not treated specially beyond
   2889 their implementation of the context management protocol. See the
   2890 :mod:`contextlib` module for some examples.
   2891 
   2892 Python's :term:`generator`\s and the ``contextlib.contextmanager`` :term:`decorator`
   2893 provide a convenient way to implement these protocols.  If a generator function is
   2894 decorated with the ``contextlib.contextmanager`` decorator, it will return a
   2895 context manager implementing the necessary :meth:`__enter__` and
   2896 :meth:`__exit__` methods, rather than the iterator produced by an undecorated
   2897 generator function.
   2898 
   2899 Note that there is no specific slot for any of these methods in the type
   2900 structure for Python objects in the Python/C API. Extension types wanting to
   2901 define these methods must provide them as a normal Python accessible method.
   2902 Compared to the overhead of setting up the runtime context, the overhead of a
   2903 single class dictionary lookup is negligible.
   2904 
   2905 
   2906 .. _typesother:
   2907 
   2908 Other Built-in Types
   2909 ====================
   2910 
   2911 The interpreter supports several other kinds of objects. Most of these support
   2912 only one or two operations.
   2913 
   2914 
   2915 .. _typesmodules:
   2916 
   2917 Modules
   2918 -------
   2919 
   2920 The only special operation on a module is attribute access: ``m.name``, where
   2921 *m* is a module and *name* accesses a name defined in *m*'s symbol table.
   2922 Module attributes can be assigned to.  (Note that the :keyword:`import`
   2923 statement is not, strictly speaking, an operation on a module object; ``import
   2924 foo`` does not require a module object named *foo* to exist, rather it requires
   2925 an (external) *definition* for a module named *foo* somewhere.)
   2926 
   2927 A special attribute of every module is :attr:`~object.__dict__`. This is the
   2928 dictionary containing the module's symbol table. Modifying this dictionary will
   2929 actually change the module's symbol table, but direct assignment to the
   2930 :attr:`~object.__dict__` attribute is not possible (you can write
   2931 ``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but you can't write
   2932 ``m.__dict__ = {}``).  Modifying :attr:`~object.__dict__` directly is
   2933 not recommended.
   2934 
   2935 Modules built into the interpreter are written like this: ``<module 'sys'
   2936 (built-in)>``.  If loaded from a file, they are written as ``<module 'os' from
   2937 '/usr/local/lib/pythonX.Y/os.pyc'>``.
   2938 
   2939 
   2940 .. _typesobjects:
   2941 
   2942 Classes and Class Instances
   2943 ---------------------------
   2944 
   2945 See :ref:`objects` and :ref:`class` for these.
   2946 
   2947 
   2948 .. _typesfunctions:
   2949 
   2950 Functions
   2951 ---------
   2952 
   2953 Function objects are created by function definitions.  The only operation on a
   2954 function object is to call it: ``func(argument-list)``.
   2955 
   2956 There are really two flavors of function objects: built-in functions and
   2957 user-defined functions.  Both support the same operation (to call the function),
   2958 but the implementation is different, hence the different object types.
   2959 
   2960 See :ref:`function` for more information.
   2961 
   2962 
   2963 .. _typesmethods:
   2964 
   2965 Methods
   2966 -------
   2967 
   2968 .. index:: object: method
   2969 
   2970 Methods are functions that are called using the attribute notation. There are
   2971 two flavors: built-in methods (such as :meth:`append` on lists) and class
   2972 instance methods.  Built-in methods are described with the types that support
   2973 them.
   2974 
   2975 The implementation adds two special read-only attributes to class instance
   2976 methods: ``m.im_self`` is the object on which the method operates, and
   2977 ``m.im_func`` is the function implementing the method.  Calling ``m(arg-1,
   2978 arg-2, ..., arg-n)`` is completely equivalent to calling ``m.im_func(m.im_self,
   2979 arg-1, arg-2, ..., arg-n)``.
   2980 
   2981 Class instance methods are either *bound* or *unbound*, referring to whether the
   2982 method was accessed through an instance or a class, respectively.  When a method
   2983 is unbound, its ``im_self`` attribute will be ``None`` and if called, an
   2984 explicit ``self`` object must be passed as the first argument.  In this case,
   2985 ``self`` must be an instance of the unbound method's class (or a subclass of
   2986 that class), otherwise a :exc:`TypeError` is raised.
   2987 
   2988 Like function objects, methods objects support getting arbitrary attributes.
   2989 However, since method attributes are actually stored on the underlying function
   2990 object (``meth.im_func``), setting method attributes on either bound or unbound
   2991 methods is disallowed.  Attempting to set an attribute on a method results in
   2992 an :exc:`AttributeError` being raised.  In order to set a method attribute, you
   2993 need to explicitly set it on the underlying function object::
   2994 
   2995    >>> class C:
   2996    ...     def method(self):
   2997    ...         pass
   2998    ...
   2999    >>> c = C()
   3000    >>> c.method.whoami = 'my name is method'  # can't set on the method
   3001    Traceback (most recent call last):
   3002      File "<stdin>", line 1, in <module>
   3003    AttributeError: 'instancemethod' object has no attribute 'whoami'
   3004    >>> c.method.im_func.whoami = 'my name is method'
   3005    >>> c.method.whoami
   3006    'my name is method'
   3007 
   3008 
   3009 See :ref:`types` for more information.
   3010 
   3011 
   3012 .. index:: object; code, code object
   3013 
   3014 .. _bltin-code-objects:
   3015 
   3016 Code Objects
   3017 ------------
   3018 
   3019 .. index::
   3020    builtin: compile
   3021    single: func_code (function object attribute)
   3022 
   3023 Code objects are used by the implementation to represent "pseudo-compiled"
   3024 executable Python code such as a function body. They differ from function
   3025 objects because they don't contain a reference to their global execution
   3026 environment.  Code objects are returned by the built-in :func:`compile` function
   3027 and can be extracted from function objects through their :attr:`func_code`
   3028 attribute. See also the :mod:`code` module.
   3029 
   3030 .. index::
   3031    statement: exec
   3032    builtin: eval
   3033 
   3034 A code object can be executed or evaluated by passing it (instead of a source
   3035 string) to the :keyword:`exec` statement or the built-in :func:`eval` function.
   3036 
   3037 See :ref:`types` for more information.
   3038 
   3039 
   3040 .. _bltin-type-objects:
   3041 
   3042 Type Objects
   3043 ------------
   3044 
   3045 .. index::
   3046    builtin: type
   3047    module: types
   3048 
   3049 Type objects represent the various object types.  An object's type is accessed
   3050 by the built-in function :func:`type`.  There are no special operations on
   3051 types.  The standard module :mod:`types` defines names for all standard built-in
   3052 types.
   3053 
   3054 Types are written like this: ``<type 'int'>``.
   3055 
   3056 
   3057 .. _bltin-null-object:
   3058 
   3059 The Null Object
   3060 ---------------
   3061 
   3062 This object is returned by functions that don't explicitly return a value.  It
   3063 supports no special operations.  There is exactly one null object, named
   3064 ``None`` (a built-in name).
   3065 
   3066 It is written as ``None``.
   3067 
   3068 
   3069 .. _bltin-ellipsis-object:
   3070 
   3071 The Ellipsis Object
   3072 -------------------
   3073 
   3074 This object is used by extended slice notation (see :ref:`slicings`).  It
   3075 supports no special operations.  There is exactly one ellipsis object, named
   3076 :const:`Ellipsis` (a built-in name).
   3077 
   3078 It is written as ``Ellipsis``.  When in a subscript, it can also be written as
   3079 ``...``, for example ``seq[...]``.
   3080 
   3081 
   3082 The NotImplemented Object
   3083 -------------------------
   3084 
   3085 This object is returned from comparisons and binary operations when they are
   3086 asked to operate on types they don't support. See :ref:`comparisons` for more
   3087 information.
   3088 
   3089 It is written as ``NotImplemented``.
   3090 
   3091 
   3092 Boolean Values
   3093 --------------
   3094 
   3095 Boolean values are the two constant objects ``False`` and ``True``.  They are
   3096 used to represent truth values (although other values can also be considered
   3097 false or true).  In numeric contexts (for example when used as the argument to
   3098 an arithmetic operator), they behave like the integers 0 and 1, respectively.
   3099 The built-in function :func:`bool` can be used to convert any value to a
   3100 Boolean, if the value can be interpreted as a truth value (see section
   3101 :ref:`truth` above).
   3102 
   3103 .. index::
   3104    single: False
   3105    single: True
   3106    pair: Boolean; values
   3107 
   3108 They are written as ``False`` and ``True``, respectively.
   3109 
   3110 
   3111 .. _typesinternal:
   3112 
   3113 Internal Objects
   3114 ----------------
   3115 
   3116 See :ref:`types` for this information.  It describes stack frame objects,
   3117 traceback objects, and slice objects.
   3118 
   3119 
   3120 .. _specialattrs:
   3121 
   3122 Special Attributes
   3123 ==================
   3124 
   3125 The implementation adds a few special read-only attributes to several object
   3126 types, where they are relevant.  Some of these are not reported by the
   3127 :func:`dir` built-in function.
   3128 
   3129 
   3130 .. attribute:: object.__dict__
   3131 
   3132    A dictionary or other mapping object used to store an object's (writable)
   3133    attributes.
   3134 
   3135 
   3136 .. attribute:: object.__methods__
   3137 
   3138    .. deprecated:: 2.2
   3139       Use the built-in function :func:`dir` to get a list of an object's attributes.
   3140       This attribute is no longer available.
   3141 
   3142 
   3143 .. attribute:: object.__members__
   3144 
   3145    .. deprecated:: 2.2
   3146       Use the built-in function :func:`dir` to get a list of an object's attributes.
   3147       This attribute is no longer available.
   3148 
   3149 
   3150 .. attribute:: instance.__class__
   3151 
   3152    The class to which a class instance belongs.
   3153 
   3154 
   3155 .. attribute:: class.__bases__
   3156 
   3157    The tuple of base classes of a class object.
   3158 
   3159 
   3160 .. attribute:: definition.__name__
   3161 
   3162    The name of the class, type, function, method, descriptor, or
   3163    generator instance.
   3164 
   3165 
   3166 The following attributes are only supported by :term:`new-style class`\ es.
   3167 
   3168 .. attribute:: class.__mro__
   3169 
   3170    This attribute is a tuple of classes that are considered when looking for
   3171    base classes during method resolution.
   3172 
   3173 
   3174 .. method:: class.mro()
   3175 
   3176    This method can be overridden by a metaclass to customize the method
   3177    resolution order for its instances.  It is called at class instantiation, and
   3178    its result is stored in :attr:`~class.__mro__`.
   3179 
   3180 
   3181 .. method:: class.__subclasses__
   3182 
   3183    Each new-style class keeps a list of weak references to its immediate
   3184    subclasses.  This method returns a list of all those references still alive.
   3185    Example::
   3186 
   3187       >>> int.__subclasses__()
   3188       [<type 'bool'>]
   3189 
   3190 
   3191 .. rubric:: Footnotes
   3192 
   3193 .. [1] Additional information on these special methods may be found in the Python
   3194    Reference Manual (:ref:`customization`).
   3195 
   3196 .. [2] As a consequence, the list ``[1, 2]`` is considered equal to ``[1.0, 2.0]``, and
   3197    similarly for tuples.
   3198 
   3199 .. [3] They must have since the parser can't tell the type of the operands.
   3200 
   3201 .. [4] Cased characters are those with general category property being one of
   3202    "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt" (Letter, titlecase).
   3203 
   3204 .. [5] To format only a tuple you should therefore provide a singleton tuple whose only
   3205    element is the tuple to be formatted.
   3206 
   3207 .. [6] The advantage of leaving the newline on is that returning an empty string is
   3208    then an unambiguous EOF indication.  It is also possible (in cases where it
   3209    might matter, for example, if you want to make an exact copy of a file while
   3210    scanning its lines) to tell whether the last line of a file ended in a newline
   3211    or not (yes this happens!).
   3212