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