On this page
ast.get_docstring
Prev: ast.fix_missing_locations | Up: ast | Next: ast.get_source_segment
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.get_docstring
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import get_docstring
Signature & Overview
ast.get_docstring(node, clean=True)
Return the docstring for the given node or None if no docstring can be found. If the node provided does not have docstrings a TypeError will be raised.
Usage
import ast
tree = ast.parse('def f():\n """Hello."""\n return 1')
assert ast.get_docstring(tree.body[0]) == "Hello."
Gotchas
- Static docstring extraction without importing/executing the file: Works on
Module,ClassDef,FunctionDef, andAsyncFunctionDef.
REPL Inspect
from ast import get_docstring
help(get_docstring)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/fix_missing_locations Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/get_source_segment Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/Add Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/alias Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/And Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AnnAssign Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AST Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast/arg shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/arguments shared tags: #ast, #stdlib