Skip to content

Commit 0a125d9

Browse files
[3.13] gh-81263: Add assignment expressions to help (GH-124641) (#124713)
gh-81263: Add assignment expressions to `help` (#124641) * Add assignment expression (:=) to `help` * Update index for Assignment Expressions to include pair of `assignment; expression`
1 parent 5c5f027 commit 0a125d9

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Doc/reference/expressions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,7 @@ returns a boolean value regardless of the type of its argument
18071807
single: assignment expression
18081808
single: walrus operator
18091809
single: named expression
1810+
pair: assignment; expression
18101811

18111812
Assignment expressions
18121813
======================

Lib/pydoc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,7 @@ class Helper:
18611861
':': 'SLICINGS DICTIONARYLITERALS',
18621862
'@': 'def class',
18631863
'\\': 'STRINGS',
1864+
':=': 'ASSIGNMENTEXPRESSIONS',
18641865
'_': 'PRIVATENAMES',
18651866
'__': 'PRIVATENAMES SPECIALMETHODS',
18661867
'`': 'BACKQUOTES',
@@ -1954,6 +1955,7 @@ class Helper:
19541955
'ASSERTION': 'assert',
19551956
'ASSIGNMENT': ('assignment', 'AUGMENTEDASSIGNMENT'),
19561957
'AUGMENTEDASSIGNMENT': ('augassign', 'NUMBERMETHODS'),
1958+
'ASSIGNMENTEXPRESSIONS': ('assignment-expressions', ''),
19571959
'DELETION': 'del',
19581960
'RETURNING': 'return',
19591961
'IMPORTING': 'import',

Lib/pydoc_data/topics.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,34 @@
421421
'some expressions (like un-parenthesized tuple expressions) '
422422
'caused a\n'
423423
'syntax error.\n',
424+
'assignment-expressions': 'Assignment expressions\n'
425+
'**********************\n'
426+
'\n'
427+
'An assignment expression (sometimes also called a “named expression”'
428+
'\nor “walrus”) assigns an expression to an identifier, while also\n'
429+
'returning the value of the expression.\n'
430+
'\n'
431+
'One common use case is when handling matched regular expressions:\n'
432+
'\n'
433+
' if matching := pattern.search(data):\n'
434+
' do_something(matching)\n'
435+
'\n'
436+
'Or, when processing a file stream in chunks:\n'
437+
'\n'
438+
' while chunk := file.read(9000):\n'
439+
' process(chunk)\n'
440+
'\n'
441+
'Assignment expressions must be surrounded by parentheses when used as\n'
442+
'expression statements and when used as sub-expressions in slicing,\n'
443+
'conditional, lambda, keyword-argument, and comprehension-if\n'
444+
'expressions and in assert, with, and assignment statements. In all\n'
445+
'other places where they can be used, parentheses are not required,\n'
446+
'including in if and while statements.\n'
447+
'\n'
448+
'Added in version 3.8.\n'
449+
'See also:\n'
450+
'\n'
451+
' **PEP 572** - Assignment Expressions\n',
424452
'async': 'Coroutines\n'
425453
'**********\n'
426454
'\n'

0 commit comments

Comments
 (0)