cut
stringclasses
1 value
version
stringclasses
10 values
idiom_id
stringclasses
53 values
pep
stringclasses
53 values
introduced_in
stringclasses
11 values
form
stringclasses
2 values
file
stringclasses
887 values
code
stringclasses
916 values
source
stringclasses
1 value
origin
stringlengths
15
17
origin_revision
stringclasses
10 values
license
stringclasses
1 value
train
3.13
readonly-items
705
3.13
modern
readonly-items/modern_19.py
class A(TypedDict): x: ReadOnly[int] class B(TypedDict): x: ReadOnly[str] class C(TypedDict): x: int | str def copy_and_modify(a: A) -> C: c: C = copy.copy(a) if not c['x']: c['x'] = "N/A" return c def merge_and_modify(a: A, b: B) -> C: c: C = a | b if not c['x']: c['...
pydocs
pep-0705.rst:474
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
readonly-items
705
3.13
modern
readonly-items/modern_20.py
@final class D(TypedDict): field: ReadOnly[Collection[str]] @final class E(TypedDict): field: list[str] e: E = { "field": ["value1", "value2"] } d: D = e # Error?
pydocs
pep-0705.rst:551
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_01.py
def narrower(x: I) -> TypeIs[R]: ... def func1(val: A): if narrower(val): assert_type(val, NP) else: assert_type(val, NN)
pydocs
pep-0742.rst:147
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_02.py
from typing import TypeIs, assert_type def is_str(x: object) -> TypeIs[str]: return isinstance(x, str) def f(x: str | int) -> None: if is_str(x): assert_type(x, str) else: assert_type(x, int)
pydocs
pep-0742.rst:173
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_03.py
from collections.abc import Awaitable from typing import Any, TypeIs, assert_type import inspect def isawaitable(x: object) -> TypeIs[Awaitable[Any]]: return inspect.isawaitable(x) def f(x: Awaitable[int] | int) -> None: if isawaitable(x): # Type checkers may also infer the more precise type #...
pydocs
pep-0742.rst:187
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_04.py
from typing import TypeIs def is_str(x: int) -> TypeIs[str]: # Type checker error ...
pydocs
pep-0742.rst:204
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_05.py
def takes_narrower(x: int | str, narrower: Callable[[object], TypeIs[int]]): if narrower(x): print(x + 1) # x is an int else: print("Hello " + x) # x is a str def is_bool(x: object) -> TypeIs[bool]: return isinstance(x, bool) takes_narrower(1, is_bool) # Error: is_bool is not a TypeIs[i...
pydocs
pep-0742.rst:222
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_06.py
from typing import TypeIs, Literal type Direction = Literal["N", "E", "S", "W"] def is_direction(x: str) -> TypeIs[Direction]: return x in {"N", "E", "S", "W"} def maybe_direction(x: str) -> None: if is_direction(x): print(f"{x} is a cardinal direction") else: print(f"{x} is not a cardina...
pydocs
pep-0742.rst:280
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_07.py
from typing import TypeIs # Correct def good_typeis(x: object) -> TypeIs[int]: return isinstance(x, int) # Incorrect: does not return True for all ints def bad_typeis1(x: object) -> TypeIs[int]: return isinstance(x, int) and x > 0 # Incorrect: returns True for some non-ints def bad_typeis2(x: object) -> Type...
pydocs
pep-0742.rst:307
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_08.py
from typing import TypedDict, TypeIs class Point(TypedDict): x: int y: int def is_point(x: object) -> TypeIs[Point]: return ( isinstance(x, dict) and all(isinstance(key, str) for key in x) and "x" in x and "y" in x and isinstance(x["x"], int) and isinstance(...
pydocs
pep-0742.rst:339
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
type-is
742
3.13
modern
type-is/modern_09.py
from typing import TypeGuard, TypeIs, reveal_type, final class Base: ... class Child(Base): ... @final class Unrelated: ... def is_base_typeguard(x: object) -> TypeGuard[Base]: return isinstance(x, Base) def is_base_typeis(x: object) -> TypeIs[Base]: return isinstance(x, Base) def use_typeguard(x: Child | U...
pydocs
pep-0742.rst:388
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_01.py
import datetime name = 'Fred' age = 50 anniversary = datetime.date(1991, 10, 12) f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.' f'He said his name is {name!r}.'
pydocs
pep-0498.rst:36
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_01.py
msg = 'disk failure' 'error: %s' % msg
pydocs
pep-0498.rst:62
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_02.py
msg = ('disk failure', 32) 'error: %s' % msg
pydocs
pep-0498.rst:68
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_03.py
'error: %s' % (msg,)
pydocs
pep-0498.rst:76
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_04.py
value = 4 * 20 'The value is {value}.'.format(value=value)
pydocs
pep-0498.rst:90
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_05.py
'The value is {}.'.format(value)
pydocs
pep-0498.rst:98
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_02.py
f'The value is {value}.'
pydocs
pep-0498.rst:103
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_03.py
value = 1234 f'input={value:#06x}'
pydocs
pep-0498.rst:118
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_04.py
date = datetime.date(1991, 10, 12) f'{date} was on a {date:%A}'
pydocs
pep-0498.rst:125
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_06.py
def outer(x): def inner(): return 'x={x}'.format_map(locals()) return inner outer(42)()
pydocs
pep-0498.rst:137
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_07.py
def outer(x): def inner(): x return 'x={x}'.format_map(locals()) return inner outer(42)()
pydocs
pep-0498.rst:152
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_05.py
f'{"quoted string"}'
pydocs
pep-0498.rst:254
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_06.py
f'{{ {4*10} }}' f'{{{4*10}}}'
pydocs
pep-0498.rst:263
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_07.py
fr'x={4*10}\n'
pydocs
pep-0498.rst:271
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_08.py
f"abc {a['x']} def"
pydocs
pep-0498.rst:281
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_09.py
f'''abc {a['x']} def'''
pydocs
pep-0498.rst:285
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_10.py
f'abc{expr1:spec1}{expr2!r:spec2}def{expr3}ghi'
pydocs
pep-0498.rst:297
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_11.py
def foo(): return 20 f'result={foo()}'
pydocs
pep-0498.rst:317
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_12.py
x = 0 f'''{x +1}''' d = {0: 'zero'} f'''{d[0 ]}'''
pydocs
pep-0498.rst:334
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_13.py
width = 10 precision = 4 value = decimal.Decimal('12.34567') f'result: {value:{width}.{precision}}'
pydocs
pep-0498.rst:350
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_14.py
x = 10 y = 'hi' 'a' 'b' f'{x}' '{c}' f'str<{y:^4}>' 'd' 'e'
pydocs
pep-0498.rst:368
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_15.py
d = {0:10, 1:20} for i in range(3): print(f'{i}:{d[i]}')
pydocs
pep-0498.rst:413
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_16.py
for x in (32, 100, 'fifty'): print(f'x = {x:+3}')
pydocs
pep-0498.rst:425
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_17.py
def fn(l, incr): result = l[0] l[0] += incr return result lst = [0] f'{fn(lst,2)} {fn(lst,3)}' f'{fn(lst,2)} {fn(lst,3)}' lst
pydocs
pep-0498.rst:447
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_18.py
f'Column={col_idx+1}' f'number of items: {len(items)}'
pydocs
pep-0498.rst:521
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_19.py
col_number = col_idx+1 f'Column={col_number}' n_items = len(items) f'number of items: {n_items}'
pydocs
pep-0498.rst:526
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_20.py
f'mapping is { {a:b for (a, b) in ((1, 2), (3, 4))} }'
pydocs
pep-0498.rst:535
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_08.py
d = {'a': 10, 'b': 20} 'a={d[a]}'.format(d=d)
pydocs
pep-0498.rst:554
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_21.py
f'a={d["a"]}'
pydocs
pep-0498.rst:564
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_22.py
a = 'b' f'a={d[a]}'
pydocs
pep-0498.rst:570
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
legacy
fstring/legacy_09.py
'{i[";]}'.format(i={'";':4})
pydocs
pep-0498.rst:580
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_23.py
header = 'Subject' fr'{header}:\s+'
pydocs
pep-0498.rst:600
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_24.py
a = 'some string' f'{a!r}'
pydocs
pep-0498.rst:635
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_25.py
f'{repr(a)}'
pydocs
pep-0498.rst:641
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_26.py
f'{(lambda x: x*2)(3)}'
pydocs
pep-0498.rst:664
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_27.py
extra = '{},waiters:{}'.format(extra, len(self._waiters)) extra = f'{extra},waiters:{len(self._waiters)}'
pydocs
pep-0498.rst:686
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_28.py
message.append(" [line {0:2d}]".format(lineno)) message.append(f" [line {lineno:2d}]")
pydocs
pep-0498.rst:691
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_29.py
methoddef_name = "{}_METHODDEF".format(c_basename.upper()) methoddef_name = f"{c_basename.upper()}_METHODDEF"
pydocs
pep-0498.rst:696
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
fstring
498
3.6
modern
fstring/modern_30.py
print("Usage: {0} [{1}]".format(sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr) print(f"Usage: {sys.argv[0]} [{'|'.join('--'+opt for opt in valid_opts)}]", file=sys.stderr)
pydocs
pep-0498.rst:701
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_01.py
match = re.match(data) group = match.group(1) if match else None
pydocs
pep-0572.rst:87
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_02.py
group = re.match(data).group(1) if re.match(data) else None
pydocs
pep-0572.rst:92
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_03.py
match1 = pattern1.match(data) match2 = pattern2.match(data) if match1: result = match1.group(1) elif match2: result = match2.group(2) else: result = None
pydocs
pep-0572.rst:97
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_04.py
match1 = pattern1.match(data) if match1: result = match1.group(1) else: match2 = pattern2.match(data) if match2: result = match2.group(2) else: result = None
pydocs
pep-0572.rst:110
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_01.py
# Handle a matched regex if (match := pattern.search(data)) is not None: ... # Do something with match # A loop that can't be trivially rewritten using 2-arg iter() while chunk := file.read(8192): process(chunk) # Reuse a value that's expensive to compute [y := f(x), y**2, y**3] # Share a subexpression be...
pydocs
pep-0572.rst:133
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_02.py
f'{(x:=10)}' # Valid, uses assignment expression x = 10 f'{x:=10}' # Valid, passes '=10' to formatter
pydocs
pep-0572.rst:222
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_03.py
if any((comment := line).startswith('#') for line in lines): print("First comment:", comment) else: print("There are no comments") if all((nonblank := line).strip() == '' for line in lines): print("All lines are blank") else: print("First non-blank line:", nonblank)
pydocs
pep-0572.rst:257
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_04.py
# Compute partial sums in a list comprehension total = 0 partial_sums = [total := total + v for v in values] print("Total:", total)
pydocs
pep-0572.rst:270
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_05.py
[[(j := j) for i in range(5)] for j in range(5)] # INVALID [i := 0 for i, j in stuff] # INVALID [i+1 for i in (i := stuff)] # INVALID
pydocs
pep-0572.rst:287
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_06.py
[False and (i := 0) for i, j in stuff] # INVALID [i for i, j in stuff if True or (j := 1)] # INVALID
pydocs
pep-0572.rst:299
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_07.py
[i+1 for i in (j := stuff)] # INVALID [i+1 for i in range(2) for j in (k := stuff)] # INVALID [i+1 for i in [j for j in (k := stuff)]] # INVALID [i+1 for i in (lambda: (j := stuff))()] # INVALID
pydocs
pep-0572.rst:316
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_08.py
class Example: [(j := i) for i in range(5)] # INVALID
pydocs
pep-0572.rst:327
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_09.py
# Valid if any(len(longline := line) >= 100 for line in lines): print("Extremely long line:", longline)
pydocs
pep-0572.rst:386
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_10.py
x = 1, 2 # Sets x to (1, 2) (x := 1, 2) # Sets x to 1
pydocs
pep-0572.rst:433
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_05.py
env_base = os.environ.get("PYTHONUSERBASE", None) if env_base: return env_base
pydocs
pep-0572.rst:491
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_11.py
if env_base := os.environ.get("PYTHONUSERBASE", None): return env_base
pydocs
pep-0572.rst:497
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_06.py
if self._is_special: ans = self._check_nans(context=context) if ans: return ans
pydocs
pep-0572.rst:507
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_12.py
if self._is_special and (ans := self._check_nans(context=context)): return ans
pydocs
pep-0572.rst:514
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_07.py
reductor = dispatch_table.get(cls) if reductor: rv = reductor(x) else: reductor = getattr(x, "__reduce_ex__", None) if reductor: rv = reductor(4) else: reductor = getattr(x, "__reduce__", None) if reductor: rv = reductor() else: raise Error( ...
pydocs
pep-0572.rst:525
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_13.py
if reductor := dispatch_table.get(cls): rv = reductor(x) elif reductor := getattr(x, "__reduce_ex__", None): rv = reductor(4) elif reductor := getattr(x, "__reduce__", None): rv = reductor() else: raise Error("un(deep)copyable object of type %s" % cls)
pydocs
pep-0572.rst:542
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_08.py
s = _format_time(self._hour, self._minute, self._second, self._microsecond, timespec) tz = self._tzstr() if tz: s += tz return s
pydocs
pep-0572.rst:559
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_14.py
s = _format_time(self._hour, self._minute, self._second, self._microsecond, timespec) if tz := self._tzstr(): s += tz return s
pydocs
pep-0572.rst:569
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_09.py
while True: line = fp.readline() if not line: break m = define_rx.match(line) if m: n, v = m.group(1, 2) try: v = int(v) except ValueError: pass vars[n] = v else: m = undef_rx.match(line) if m: vars[m.group(1...
pydocs
pep-0572.rst:585
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_15.py
while line := fp.readline(): if m := define_rx.match(line): n, v = m.group(1, 2) try: v = int(v) except ValueError: pass vars[n] = v elif m := undef_rx.match(line): vars[m.group(1)] = 0
pydocs
pep-0572.rst:604
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_16.py
results = [(x, y, x/y) for x in input_data if (y := f(x)) > 0]
pydocs
pep-0572.rst:622
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_17.py
stuff = [[y := f(x), x/y] for x in range(5)]
pydocs
pep-0572.rst:627
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_18.py
# Loop-and-a-half while (command := input("> ")) != "quit": print("You entered:", command) # Capturing regular expression match objects # See, for instance, Lib/pydoc.py, which uses a multiline spelling # of this effect if match := re.search(pat, text): print("Found:", match.group(0)) # The same syntax chains ...
pydocs
pep-0572.rst:639
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_19.py
if pid := os.fork(): ... # Parent code else: ... # Child code
pydocs
pep-0572.rst:669
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_20.py
pos = -1 while pos := buffer.find(search_term, pos + 1) >= 0: ...
pydocs
pep-0572.rst:862
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_21.py
(point := (x, y)) (point := x, y)
pydocs
pep-0572.rst:882
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_22.py
foo(x := 1, y) foo(x := (1, y))
pydocs
pep-0572.rst:892
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_23.py
# Top level in if if match := pattern.match(line): return match.group(1) # Short call len(lines := f.readlines())
pydocs
pep-0572.rst:907
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_10.py
mylast = mylast[1] yield mylast[0]
pydocs
pep-0572.rst:996
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_24.py
yield (mylast := mylast[1])[0]
pydocs
pep-0572.rst:1001
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_11.py
while True: old = total total += term if old == total: return total term *= mx2 / (i*(i+1)) i += 2
pydocs
pep-0572.rst:1009
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_25.py
while total != (total := total + term): term *= mx2 / (i*(i+1)) i += 2 return total
pydocs
pep-0572.rst:1019
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_12.py
result = solution(xs, n) if result: ... # use result
pydocs
pep-0572.rst:1037
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_26.py
if result := solution(xs, n): ... # use result
pydocs
pep-0572.rst:1044
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_13.py
reductor = dispatch_table.get(cls) if reductor: rv = reductor(x) else: reductor = getattr(x, "__reduce_ex__", None) if reductor: rv = reductor(4) else: reductor = getattr(x, "__reduce__", None) if reductor: rv = reductor() else: raise Error("un(sha...
pydocs
pep-0572.rst:1057
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_27.py
if reductor := dispatch_table.get(cls): rv = reductor(x) elif reductor := getattr(x, "__reduce_ex__", None): rv = reductor(4) elif reductor := getattr(x, "__reduce__", None): rv = reductor() else: raise Error("un(shallow)copyable object of type %s" % cls)
pydocs
pep-0572.rst:1074
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_14.py
diff = x - x_base if diff: g = gcd(diff, n) if g > 1: return g
pydocs
pep-0572.rst:1091
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_28.py
if (diff := x - x_base) and (g := gcd(diff, n)) > 1: return g
pydocs
pep-0572.rst:1099
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_29.py
while a > (d := x // a**(n-1)): a = ((n-1)*a + d) // n return a
pydocs
pep-0572.rst:1122
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
legacy
walrus/legacy_15.py
while True: d = x // a**(n-1) if a <= d: break a = ((n-1)*a + d) // n return a
pydocs
pep-0572.rst:1140
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_30.py
def f(): a = [TARGET := EXPR for VAR in ITERABLE]
pydocs
pep-0572.rst:1192
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_31.py
def f(): global TARGET a = [TARGET := EXPR for VAR in ITERABLE]
pydocs
pep-0572.rst:1211
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_32.py
def g(): TARGET = ... def f(): nonlocal TARGET a = [TARGET := EXPR for VAR in ITERABLE]
pydocs
pep-0572.rst:1230
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_33.py
def f(): a = [[TARGET := i for i in range(3)] for j in range(2)] # I.e., a = [[0, 1, 2], [0, 1, 2]] print(TARGET) # prints 2
pydocs
pep-0572.rst:1253
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
walrus
572
3.8
modern
walrus/modern_34.py
a = 42 def f(): # `a` is local to `f`, but remains unbound # until the caller executes this genexp: yield ((a := i) for i in range(3)) yield lambda: a + 100 print("done") try: print(f"`a` is bound to {a}") assert False except UnboundLocalError: print("`a` is not yet b...
pydocs
pep-0572.rst:1284
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0
train
3.13
pattern-matching
634
3.10
modern
pattern-matching/modern_01.py
match command.split(): case [action, obj]: ... # interpret action, obj
pydocs
pep-0636.rst:66
fb134c39c42a8d461083b42b0507f0c6aee2d0d4
CC0-1.0