Skip to content

Commit 6266689

Browse files
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 6cba6e1 commit 6266689

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
@@ -1870,6 +1870,7 @@ class Helper:
18701870
':': 'SLICINGS DICTIONARYLITERALS',
18711871
'@': 'def class',
18721872
'\\': 'STRINGS',
1873+
':=': 'ASSIGNMENTEXPRESSIONS',
18731874
'_': 'PRIVATENAMES',
18741875
'__': 'PRIVATENAMES SPECIALMETHODS',
18751876
'`': 'BACKQUOTES',
@@ -1963,6 +1964,7 @@ class Helper:
19631964
'ASSERTION': 'assert',
19641965
'ASSIGNMENT': ('assignment', 'AUGMENTEDASSIGNMENT'),
19651966
'AUGMENTEDASSIGNMENT': ('augassign', 'NUMBERMETHODS'),
1967+
'ASSIGNMENTEXPRESSIONS': ('assignment-expressions', ''),
19661968
'DELETION': 'del',
19671969
'RETURNING': 'return',
19681970
'IMPORTING': 'import',

Lib/pydoc_data/topics.py

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

0 commit comments

Comments
 (0)