mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-92788: Add docs for ast.Module
, ast.Expression
, and others (#101055)
This commit is contained in:
parent
a8210b6df1
commit
33608fd67d
1 changed files with 101 additions and 0 deletions
|
@ -146,6 +146,102 @@ Node classes
|
||||||
Snakes <https://greentreesnakes.readthedocs.io/en/latest/>`__ project and
|
Snakes <https://greentreesnakes.readthedocs.io/en/latest/>`__ project and
|
||||||
all its contributors.
|
all its contributors.
|
||||||
|
|
||||||
|
|
||||||
|
.. _ast-root-nodes:
|
||||||
|
|
||||||
|
Root nodes
|
||||||
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
.. class:: Module(body, type_ignores)
|
||||||
|
|
||||||
|
A Python module, as with :ref:`file input <file-input>`.
|
||||||
|
Node type generated by :func:`ast.parse` in the default ``"exec"`` *mode*.
|
||||||
|
|
||||||
|
*body* is a :class:`list` of the module's :ref:`ast-statements`.
|
||||||
|
|
||||||
|
*type_ignores* is a :class:`list` of the module's type ignore comments;
|
||||||
|
see :func:`ast.parse` for more details.
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
|
>>> print(ast.dump(ast.parse('x = 1'), indent=4))
|
||||||
|
Module(
|
||||||
|
body=[
|
||||||
|
Assign(
|
||||||
|
targets=[
|
||||||
|
Name(id='x', ctx=Store())],
|
||||||
|
value=Constant(value=1))],
|
||||||
|
type_ignores=[])
|
||||||
|
|
||||||
|
|
||||||
|
.. class:: Expression(body)
|
||||||
|
|
||||||
|
A single Python :ref:`expression input <expression-input>`.
|
||||||
|
Node type generated by :func:`ast.parse` when *mode* is ``"eval"``.
|
||||||
|
|
||||||
|
*body* is a single node,
|
||||||
|
one of the :ref:`expression types <ast-expressions>`.
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
|
>>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
|
||||||
|
Expression(
|
||||||
|
body=Constant(value=123))
|
||||||
|
|
||||||
|
|
||||||
|
.. class:: Interactive(body)
|
||||||
|
|
||||||
|
A single :ref:`interactive input <interactive>`, like in :ref:`tut-interac`.
|
||||||
|
Node type generated by :func:`ast.parse` when *mode* is ``"single"``.
|
||||||
|
|
||||||
|
*body* is a :class:`list` of :ref:`statement nodes <ast-statements>`.
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
|
>>> print(ast.dump(ast.parse('x = 1; y = 2', mode='single'), indent=4))
|
||||||
|
Interactive(
|
||||||
|
body=[
|
||||||
|
Assign(
|
||||||
|
targets=[
|
||||||
|
Name(id='x', ctx=Store())],
|
||||||
|
value=Constant(value=1)),
|
||||||
|
Assign(
|
||||||
|
targets=[
|
||||||
|
Name(id='y', ctx=Store())],
|
||||||
|
value=Constant(value=2))])
|
||||||
|
|
||||||
|
|
||||||
|
.. class:: FunctionType(argtypes, returns)
|
||||||
|
|
||||||
|
A representation of an old-style type comments for functions,
|
||||||
|
as Python versions prior to 3.5 didn't support :pep:`484` annotations.
|
||||||
|
Node type generated by :func:`ast.parse` when *mode* is ``"func_type"``.
|
||||||
|
|
||||||
|
Such type comments would look like this::
|
||||||
|
|
||||||
|
def sum_two_number(a, b):
|
||||||
|
# type: (int, int) -> int
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
*argtypes* is a :class:`list` of :ref:`expression nodes <ast-expressions>`.
|
||||||
|
|
||||||
|
*returns* is a single :ref:`expression node <ast-expressions>`.
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
|
>>> print(ast.dump(ast.parse('(int, str) -> List[int]', mode='func_type'), indent=4))
|
||||||
|
FunctionType(
|
||||||
|
argtypes=[
|
||||||
|
Name(id='int', ctx=Load()),
|
||||||
|
Name(id='str', ctx=Load())],
|
||||||
|
returns=Subscript(
|
||||||
|
value=Name(id='List', ctx=Load()),
|
||||||
|
slice=Name(id='int', ctx=Load()),
|
||||||
|
ctx=Load()))
|
||||||
|
|
||||||
|
.. versionadded:: 3.8
|
||||||
|
|
||||||
|
|
||||||
Literals
|
Literals
|
||||||
^^^^^^^^
|
^^^^^^^^
|
||||||
|
|
||||||
|
@ -344,6 +440,8 @@ Variables
|
||||||
type_ignores=[])
|
type_ignores=[])
|
||||||
|
|
||||||
|
|
||||||
|
.. _ast-expressions:
|
||||||
|
|
||||||
Expressions
|
Expressions
|
||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|
||||||
|
@ -735,6 +833,9 @@ Comprehensions
|
||||||
ifs=[],
|
ifs=[],
|
||||||
is_async=1)]))
|
is_async=1)]))
|
||||||
|
|
||||||
|
|
||||||
|
.. _ast-statements:
|
||||||
|
|
||||||
Statements
|
Statements
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue