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