]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-81263: Add assignment expressions to `help` (#124641)
authorEmily Morehouse <emily@python.org>
Fri, 27 Sep 2024 20:59:26 +0000 (13:59 -0700)
committerGitHub <noreply@github.com>
Fri, 27 Sep 2024 20:59:26 +0000 (13:59 -0700)
* Add assignment expression (:=) to `help`

* Update index for Assignment Expressions to include pair of `assignment; expression`

Doc/reference/expressions.rst
Lib/pydoc.py
Lib/pydoc_data/topics.py

index f734221a2cdec556ad07898d485f18668c921a06..ab72ad49d041e14e1e7bb51f4b351b14f48e05cf 100644 (file)
@@ -1807,6 +1807,7 @@ returns a boolean value regardless of the type of its argument
    single: assignment expression
    single: walrus operator
    single: named expression
+   pair: assignment; expression
 
 Assignment expressions
 ======================
index d376592d69d40d6746677acf92d32c88a174f0ee..eec7b0770f56cadba878598de75e7f6cd23324a2 100644 (file)
@@ -1870,6 +1870,7 @@ class Helper:
         ':': 'SLICINGS DICTIONARYLITERALS',
         '@': 'def class',
         '\\': 'STRINGS',
+        ':=': 'ASSIGNMENTEXPRESSIONS',
         '_': 'PRIVATENAMES',
         '__': 'PRIVATENAMES SPECIALMETHODS',
         '`': 'BACKQUOTES',
@@ -1963,6 +1964,7 @@ class Helper:
         'ASSERTION': 'assert',
         'ASSIGNMENT': ('assignment', 'AUGMENTEDASSIGNMENT'),
         'AUGMENTEDASSIGNMENT': ('augassign', 'NUMBERMETHODS'),
+        'ASSIGNMENTEXPRESSIONS': ('assignment-expressions', ''),
         'DELETION': 'del',
         'RETURNING': 'return',
         'IMPORTING': 'import',
index 4643df80e44aafa928ede86b69102ddb88930d18..97bb4eb52f43864b0900e7d3c6e461c2480d233e 100644 (file)
@@ -416,6 +416,34 @@ topics = {'assert': 'The "assert" statement\n'
                'some expressions (like un-parenthesized tuple expressions) '
                'caused a\n'
                'syntax error.\n',
+ 'assignment-expressions': 'Assignment expressions\n'
+          '**********************\n'
+          '\n'
+          'An assignment expression (sometimes also called a “named expression”'
+          '\nor “walrus”) assigns an expression to an identifier, while also\n'
+          'returning the value of the expression.\n'
+          '\n'
+          'One common use case is when handling matched regular expressions:\n'
+          '\n'
+          '   if matching := pattern.search(data):\n'
+          '      do_something(matching)\n'
+          '\n'
+          'Or, when processing a file stream in chunks:\n'
+          '\n'
+          '   while chunk := file.read(9000):\n'
+          '      process(chunk)\n'
+          '\n'
+          'Assignment expressions must be surrounded by parentheses when used as\n'
+          'expression statements and when used as sub-expressions in slicing,\n'
+          'conditional, lambda, keyword-argument, and comprehension-if\n'
+          'expressions and in assert, with, and assignment statements. In all\n'
+          'other places where they can be used, parentheses are not required,\n'
+          'including in if and while statements.\n'
+          '\n'
+          'Added in version 3.8.\n'
+          'See also:\n'
+          '\n'
+          '  **PEP 572** - Assignment Expressions\n',
  'async': 'Coroutines\n'
           '**********\n'
           '\n'