source
stringclasses
1 value
version
stringclasses
1 value
module
stringclasses
43 values
function
stringclasses
307 values
input
stringlengths
3
496
expected
stringlengths
0
40.5k
signature
stringclasses
0 values
cpython
cfcd524
pydoc_data.topics
with
>>> print(sys.exception())
None
null
cpython
cfcd524
pydoc_data.topics
with
>>> try: ... raise TypeError ... except: ... print(repr(sys.exception())) ... try: ... raise ValueError ... except: ... print(repr(sys.exception())) ... print(repr(sys.exception())) ...
TypeError() ValueError() TypeError()
null
cpython
cfcd524
pydoc_data.topics
with
>>> print(sys.exception())
None "except*" clause ---------------- The "except*" clause(s) specify one or more handlers for groups of exceptions ("BaseExceptionGroup" instances). A "try" statement can have either "except" or "except*" clauses, but not both. The exception type for matching is mandatory in the case of "except*", so "except*:" is...
null
cpython
cfcd524
pydoc_data.topics
with
>>> try: ... raise ExceptionGroup("eg", ... [ValueError(1), TypeError(2), OSError(3), OSError(4)]) ... except* TypeError as e: ... print(f'caught {type(e)} with nested {e.exceptions}') ... except* OSError as e: ... print(f'caught {type(e)} with nested {e.exceptions}') ...
caught <class 'ExceptionGroup'> with nested (TypeError(2),) caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4)) + Exception Group Traceback (most recent call last): | File "<doctest default[0]>", line 2, in <module> | raise ExceptionGroup("eg", | [ValueError(1), TypeError(2), OSEr...
null
cpython
cfcd524
pydoc_data.topics
with
>>> try: ... raise BlockingIOError ... except* BlockingIOError as e: ... print(repr(e)) ...
ExceptionGroup('', (BlockingIOError())) "break", "continue" and "return" cannot appear in an "except*" clause. "else" clause ------------- The optional "else" clause is executed if the control flow leaves the "try" suite, no exception was raised, and no "return", "continue", or "break" statement was executed. Exce...
null
cpython
cfcd524
pydoc_data.topics
with
>>> flag = False
null
cpython
cfcd524
pydoc_data.topics
with
>>> match (100, 200): ... case (100, 300): # Mismatch: 200 != 300 ... print('Case 1') ... case (100, 200) if flag: # Successful match, but guard fails ... print('Case 2') ... case (100, y): # Matches and binds y to 200 ... print(f'Case 3, y: {y}') ... case _: # Pattern not attempted...
Case 3, y: 200 In this case, "if flag" is a guard. Read more about that in the next section. Guards ------ guard: "if" `!named_expression` A "guard" (which is part of the "case") must succeed for code inside the "case" block to execute. It takes the form: "if" followed by an expression. The logical flow of a "c...
null
cpython
cfcd524
pydoc_data.topics
with
>>> class A: pass
null
cpython
cfcd524
pydoc_data.topics
with
>>> class B: pass
null
cpython
cfcd524
pydoc_data.topics
with
>>> class C(A, B): pass
defines a class "C" that inherits from classes "A" and "B". The *method resolution order* (MRO) is the order in which base classes are searched when looking up an attribute on a class. See The Python 2.3 Method Resolution Order for a description of how Python determines the MRO for a class. Multiple inheritance is no...
null
cpython
cfcd524
pydoc_data.topics
with
>>> class SubBool(bool): # TypeError ... pass
Traceback (most recent call last): ... TypeError: type 'bool' is not an acceptable base type In the resolved MRO of a class, the class’s bases appear in the order they were specified in the class’s bases list. Additionally, the MRO always lists a child class before any of its bases. A class definition will fail if ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Base: pass
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Child(Base): pass
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Grandchild(Base, Child): pass # TypeError
Traceback (most recent call last): ... TypeError: Cannot create a consistent method resolution order (MRO) for bases Base, Child In the MRO of "Grandchild", "Base" must appear before "Child" because it is first in the base class list, but it must also appear after "Child" because it is a parent of "Child". This is ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Solid1: ... __slots__ = ("solid1",)
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Solid2: ... __slots__ = ("solid2",)
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> class SolidChild(Solid1): ... __slots__ = ("solid_child",)
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> class C1: # solid base is `object` ... pass
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> # OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
null
cpython
cfcd524
pydoc_data.topics
with
>>> class C2(Solid1, C1): # solid base is `Solid1` ... pass
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> # OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
null
cpython
cfcd524
pydoc_data.topics
with
>>> class C3(SolidChild, Solid1): # solid base is `SolidChild` ... pass
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> # Error: solid bases are `Solid1` and `Solid2`, but neither is a subclass of the other.
null
cpython
cfcd524
pydoc_data.topics
with
>>> class C4(Solid1, Solid2): # error: no single solid base ... pass
Traceback (most recent call last): ... TypeError: multiple bases have instance lay-out conflict Coroutines ========== Added in version 3.5. Coroutine function definition ----------------------------- async_funcdef: [decorators] "async" "def" funcname "(" [parameter_list] ")" ["->" expression] ":"...
null
cpython
cfcd524
pydoc_data.topics
with
>>> from __future__ import annotations
null
cpython
cfcd524
pydoc_data.topics
with
>>> def f(param: annotation): ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> f.__annotations__
{'param': 'annotation'} This future statement will be deprecated and removed in a future version of Python, but not before Python 3.13 reaches its end of life (see **PEP 749**). When it is used, introspection tools like "annotationlib.get_annotations()" and "typing.get_type_hints()" are less likely to be able to resol...
null
cpython
cfcd524
pydoc_data.topics
with
>>> import pdb
null
cpython
cfcd524
pydoc_data.topics
with
>>> def f(x): ... print(1 / x)
null
cpython
cfcd524
pydoc_data.topics
with
>>> pdb.run("f(2)")
> <string>(1)<module>() (Pdb) continue 0.5
null
cpython
cfcd524
pydoc_data.topics
with
>>>
The typical usage to inspect a crashed program is:
null
cpython
cfcd524
pydoc_data.topics
with
>>> import pdb
null
cpython
cfcd524
pydoc_data.topics
with
>>> def f(x): ... print(1 / x) ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> f(0)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f ZeroDivisionError: division by zero
null
cpython
cfcd524
pydoc_data.topics
with
>>> pdb.pm()
> <stdin>(2)f() (Pdb) p x 0 (Pdb) Changed in version 3.13: The implementation of **PEP 667** means that name assignments made via "pdb" will immediately affect the active scope, even when running inside an *optimized scope*. The module defines the following functions; each enters the debugger in a slightly different ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> type Alias = 1/0
null
cpython
cfcd524
pydoc_data.topics
with
>>> Alias.__value__
Traceback (most recent call last): ... ZeroDivisionError: division by zero
null
cpython
cfcd524
pydoc_data.topics
with
>>> def func[T: 1/0](): pass
null
cpython
cfcd524
pydoc_data.topics
with
>>> T = func.__type_params__[0]
null
cpython
cfcd524
pydoc_data.topics
with
>>> T.__bound__
Traceback (most recent call last): ... ZeroDivisionError: division by zero Here the exception is raised only when the "__value__" attribute of the type alias or the "__bound__" attribute of the type variable is accessed. This behavior is primarily useful for references to types that have not yet been defined when t...
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra' Accessing arguments by name:
null
cpython
cfcd524
pydoc_data.topics
with
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
null
cpython
cfcd524
pydoc_data.topics
with
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
null
cpython
cfcd524
pydoc_data.topics
with
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W' Accessing arguments’ attributes:
null
cpython
cfcd524
pydoc_data.topics
with
>>> c = 3-5j
null
cpython
cfcd524
pydoc_data.topics
with
>>> ('The complex number {0} is formed from the real part {0.real} ' ... 'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
null
cpython
cfcd524
pydoc_data.topics
with
>>> class Point: ... def __init__(self, x, y): ... self.x, self.y = x, y ... def __str__(self): ... return 'Point({self.x}, {self.y})'.format(self=self) ...
null
cpython
cfcd524
pydoc_data.topics
with
>>> str(Point(4, 2))
'Point(4, 2)' Accessing arguments’ items:
null
cpython
cfcd524
pydoc_data.topics
with
>>> coord = (3, 5)
null
cpython
cfcd524
pydoc_data.topics
with
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5' Replacing "%s" and "%r":
null
cpython
cfcd524
pydoc_data.topics
with
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2" Aligning the text and specifying a width:
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:<30}'.format('left aligned')
'left aligned '
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:>30}'.format('right aligned')
' right aligned'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:^30}'.format('centered')
' centered '
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********' Replacing "%+f", "%-f", and "% f" and specifying a sign:
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000' Replacing "%x" and "%o" and converting the value to different bases:
null
cpython
cfcd524
pydoc_data.topics
with
>>> # format also supports binary numbers
null
cpython
cfcd524
pydoc_data.topics
with
>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)
'int: 42; hex: 2a; oct: 52; bin: 101010'
null
cpython
cfcd524
pydoc_data.topics
with
>>> # with 0x, 0o, or 0b as prefix:
null
cpython
cfcd524
pydoc_data.topics
with
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' Using the comma or the underscore as a digit group separator:
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:,}'.format(1234567890)
'1,234,567,890'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:_}'.format(1234567890)
'1_234_567_890'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:_b}'.format(1234567890)
'100_1001_1001_0110_0000_0010_1101_0010'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:_x}'.format(1234567890)
'4996_02d2'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:_}'.format(123456789.123456789)
'123_456_789.12345679'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:.,}'.format(123456789.123456789)
'123456789.123,456,79'
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:,._}'.format(123456789.123456789)
'123,456,789.123_456_79' Expressing a percentage:
null
cpython
cfcd524
pydoc_data.topics
with
>>> points = 19
null
cpython
cfcd524
pydoc_data.topics
with
>>> total = 22
null
cpython
cfcd524
pydoc_data.topics
with
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%' Using type-specific formatting:
null
cpython
cfcd524
pydoc_data.topics
with
>>> import datetime
null
cpython
cfcd524
pydoc_data.topics
with
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58' Nesting arguments and more complex examples:
null
cpython
cfcd524
pydoc_data.topics
with
>>> for align, text in zip('<^>', ['left', 'center', 'right']): ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) ...
'left<<<<<<<<<<<<' '^^^^^center^^^^^' '>>>>>>>>>>>right'
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> octets = [192, 168, 0, 1]
null
cpython
cfcd524
pydoc_data.topics
with
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'
null
cpython
cfcd524
pydoc_data.topics
with
>>> int(_, 16)
3232235521
null
cpython
cfcd524
pydoc_data.topics
with
>>>
null
cpython
cfcd524
pydoc_data.topics
with
>>> width = 5
null
cpython
cfcd524
pydoc_data.topics
with
>>> for num in range(5,12): ... for base in 'dXob': ... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ') ... print() ...
5 5 5 101 6 6 6 110 7 7 7 111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 ''', 'function': r'''Function definitions ******************** A function definition defines a user-defined function object (see section The standard ...
null
cpython
cfcd524
uuid
__module__
>>> import uuid
# make a UUID based on the host ID and current time
null
cpython
cfcd524
uuid
__module__
>>> uuid.uuid1() # doctest: +SKIP
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') # make a UUID using an MD5 hash of a namespace UUID and a name
null
cpython
cfcd524
uuid
__module__
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') # make a random UUID
null
cpython
cfcd524
uuid
__module__
>>> uuid.uuid4() # doctest: +SKIP
UUID('16fd2706-8baf-433b-82eb-8c7fada847da') # make a UUID using a SHA-1 hash of a namespace UUID and a name
null
cpython
cfcd524
uuid
__module__
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') # make a UUID from a string of hex digits (braces and hyphens ignored)
null
cpython
cfcd524
uuid
__module__
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
# convert a UUID to a string of hex digits in standard form
null
cpython
cfcd524
uuid
__module__
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f' # get the raw 16 bytes of the UUID
null