.. doctest::
- >>> print(ast.dump(ast.parse("123"), indent=4))
- Module(
- body=[
- Expr(
- value=Constant(value=123, kind=None))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
+ Expression(
+ body=Constant(value=123, kind=None))
.. class:: FormattedValue(value, conversion, format_spec)
.. doctest::
- >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"'), indent=4))
- Module(
- body=[
- Expr(
- value=JoinedStr(
- values=[
- Constant(value='sin(', kind=None),
- FormattedValue(
- value=Name(id='a', ctx=Load()),
- conversion=-1,
- format_spec=None),
- Constant(value=') is ', kind=None),
- FormattedValue(
- value=Call(
- func=Name(id='sin', ctx=Load()),
- args=[
- Name(id='a', ctx=Load())],
- keywords=[]),
- conversion=-1,
- format_spec=JoinedStr(
- values=[
- Constant(value='.3', kind=None)]))]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4))
+ Expression(
+ body=JoinedStr(
+ values=[
+ Constant(value='sin(', kind=None),
+ FormattedValue(
+ value=Name(id='a', ctx=Load()),
+ conversion=-1,
+ format_spec=None),
+ Constant(value=') is ', kind=None),
+ FormattedValue(
+ value=Call(
+ func=Name(id='sin', ctx=Load()),
+ args=[
+ Name(id='a', ctx=Load())],
+ keywords=[]),
+ conversion=-1,
+ format_spec=JoinedStr(
+ values=[
+ Constant(value='.3', kind=None)]))]))
.. class:: List(elts, ctx)
.. doctest::
- >>> print(ast.dump(ast.parse("[1, 2, 3]"), indent=4))
- Module(
- body=[
- Expr(
- value=List(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)],
- ctx=Load()))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("(1, 2, 3)"), indent=4))
- Module(
- body=[
- Expr(
- value=Tuple(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)],
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4))
+ Expression(
+ body=List(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)],
+ ctx=Load()))
+ >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
+ Expression(
+ body=Tuple(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)],
+ ctx=Load()))
.. class:: Set(elts)
.. doctest::
- >>> print(ast.dump(ast.parse("{1, 2, 3}"), indent=4))
- Module(
- body=[
- Expr(
- value=Set(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4))
+ Expression(
+ body=Set(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)]))
.. class:: Dict(keys, values)
.. doctest::
- >>> print(ast.dump(ast.parse("{'a':1, **d}"), indent=4))
- Module(
- body=[
- Expr(
- value=Dict(
- keys=[
- Constant(value='a', kind=None),
- None],
- values=[
- Constant(value=1, kind=None),
- Name(id='d', ctx=Load())]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4))
+ Expression(
+ body=Dict(
+ keys=[
+ Constant(value='a', kind=None),
+ None],
+ values=[
+ Constant(value=1, kind=None),
+ Name(id='d', ctx=Load())]))
Variables
.. doctest::
- >>> print(ast.dump(ast.parse("not x"), indent=4))
- Module(
- body=[
- Expr(
- value=UnaryOp(
- op=Not(),
- operand=Name(id='x', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4))
+ Expression(
+ body=UnaryOp(
+ op=Not(),
+ operand=Name(id='x', ctx=Load())))
.. class:: BinOp(left, op, right)
.. doctest::
- >>> print(ast.dump(ast.parse("x + y"), indent=4))
- Module(
- body=[
- Expr(
- value=BinOp(
- left=Name(id='x', ctx=Load()),
- op=Add(),
- right=Name(id='y', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4))
+ Expression(
+ body=BinOp(
+ left=Name(id='x', ctx=Load()),
+ op=Add(),
+ right=Name(id='y', ctx=Load())))
.. class:: Add
.. doctest::
- >>> print(ast.dump(ast.parse("x or y"), indent=4))
- Module(
- body=[
- Expr(
- value=BoolOp(
- op=Or(),
- values=[
- Name(id='x', ctx=Load()),
- Name(id='y', ctx=Load())]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4))
+ Expression(
+ body=BoolOp(
+ op=Or(),
+ values=[
+ Name(id='x', ctx=Load()),
+ Name(id='y', ctx=Load())]))
.. class:: And
.. doctest::
- >>> print(ast.dump(ast.parse("1 < a < 10"), indent=4))
- Module(
- body=[
- Expr(
- value=Compare(
- left=Constant(value=1, kind=None),
- ops=[
- Lt(),
- Lt()],
- comparators=[
- Name(id='a', ctx=Load()),
- Constant(value=10, kind=None)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
+ Expression(
+ body=Compare(
+ left=Constant(value=1, kind=None),
+ ops=[
+ LtE(),
+ Lt()],
+ comparators=[
+ Name(id='a', ctx=Load()),
+ Constant(value=10, kind=None)]))
.. class:: Eq
.. doctest::
- >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)'), indent=4))
- Module(
- body=[
- Expr(
- value=Call(
- func=Name(id='func', ctx=Load()),
- args=[
- Name(id='a', ctx=Load()),
- Starred(
- value=Name(id='d', ctx=Load()),
- ctx=Load())],
- keywords=[
- keyword(
- arg='b',
- value=Name(id='c', ctx=Load())),
- keyword(
- arg=None,
- value=Name(id='e', ctx=Load()))]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4))
+ Expression(
+ body=Call(
+ func=Name(id='func', ctx=Load()),
+ args=[
+ Name(id='a', ctx=Load()),
+ Starred(
+ value=Name(id='d', ctx=Load()),
+ ctx=Load())],
+ keywords=[
+ keyword(
+ arg='b',
+ value=Name(id='c', ctx=Load())),
+ keyword(
+ arg=None,
+ value=Name(id='e', ctx=Load()))]))
.. class:: keyword(arg, value)
.. doctest::
- >>> print(ast.dump(ast.parse("a if b else c"), indent=4))
- Module(
- body=[
- Expr(
- value=IfExp(
- test=Name(id='b', ctx=Load()),
- body=Name(id='a', ctx=Load()),
- orelse=Name(id='c', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4))
+ Expression(
+ body=IfExp(
+ test=Name(id='b', ctx=Load()),
+ body=Name(id='a', ctx=Load()),
+ orelse=Name(id='c', ctx=Load())))
.. class:: Attribute(value, attr, ctx)
.. doctest::
- >>> print(ast.dump(ast.parse('snake.colour'), indent=4))
- Module(
- body=[
- Expr(
- value=Attribute(
- value=Name(id='snake', ctx=Load()),
- attr='colour',
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4))
+ Expression(
+ body=Attribute(
+ value=Name(id='snake', ctx=Load()),
+ attr='colour',
+ ctx=Load()))
.. class:: NamedExpr(target, value)
.. doctest::
- >>> print(ast.dump(ast.parse("(x := 4)"), indent=4))
- Module(
- body=[
- Expr(
- value=NamedExpr(
- target=Name(id='x', ctx=Store()),
- value=Constant(value=4, kind=None)))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4))
+ Expression(
+ body=NamedExpr(
+ target=Name(id='x', ctx=Store()),
+ value=Constant(value=4, kind=None)))
Subscripting
.. doctest::
- >>> print(ast.dump(ast.parse('l[1]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=Index(
- value=Constant(value=1, kind=None)),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=Index(
+ value=Constant(value=1, kind=None)),
+ ctx=Load()))
.. class:: Slice(lower, upper, step)
.. doctest::
- >>> print(ast.dump(ast.parse('l[1:2]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=Slice(
- lower=Constant(value=1, kind=None),
- upper=Constant(value=2, kind=None),
- step=None),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=Slice(
+ lower=Constant(value=1, kind=None),
+ upper=Constant(value=2, kind=None),
+ step=None),
+ ctx=Load()))
.. class:: ExtSlice(dims)
.. doctest::
- >>> print(ast.dump(ast.parse('l[1:2, 3]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=ExtSlice(
- dims=[
- Slice(
- lower=Constant(value=1, kind=None),
- upper=Constant(value=2, kind=None),
- step=None),
- Index(
- value=Constant(value=3, kind=None))]),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=ExtSlice(
+ dims=[
+ Slice(
+ lower=Constant(value=1, kind=None),
+ upper=Constant(value=2, kind=None),
+ step=None),
+ Index(
+ value=Constant(value=3, kind=None))]),
+ ctx=Load()))
Comprehensions
.. doctest::
- >>> print(ast.dump(ast.parse("[x for x in numbers]"), indent=4))
- Module(
- body=[
- Expr(
- value=ListComp(
- elt=Name(id='x', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("{x: x**2 for x in numbers}"), indent=4))
- Module(
- body=[
- Expr(
- value=DictComp(
- key=Name(id='x', ctx=Load()),
- value=BinOp(
- left=Name(id='x', ctx=Load()),
- op=Pow(),
- right=Constant(value=2, kind=None)),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("{x for x in numbers}"), indent=4))
- Module(
- body=[
- Expr(
- value=SetComp(
- elt=Name(id='x', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4))
+ Expression(
+ body=ListComp(
+ elt=Name(id='x', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
+ >>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4))
+ Expression(
+ body=DictComp(
+ key=Name(id='x', ctx=Load()),
+ value=BinOp(
+ left=Name(id='x', ctx=Load()),
+ op=Pow(),
+ right=Constant(value=2, kind=None)),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
+ >>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4))
+ Expression(
+ body=SetComp(
+ elt=Name(id='x', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
.. class:: comprehension(target, iter, ifs, is_async)
.. doctest::
- >>> print(ast.dump(ast.parse("[ord(c) for line in file for c in line]", mode='eval'),
+ >>> print(ast.dump(ast.parse('[ord(c) for line in file for c in line]', mode='eval'),
... indent=4)) # Multiple comprehensions in one.
Expression(
body=ListComp(
ifs=[],
is_async=0)]))
- >>> print(ast.dump(ast.parse("(n**2 for n in it if n>5 if n<10)", mode='eval'),
+ >>> print(ast.dump(ast.parse('(n**2 for n in it if n>5 if n<10)', mode='eval'),
... indent=4)) # generator comprehension
Expression(
body=GeneratorExp(
Constant(value=10, kind=None)])],
is_async=0)]))
- >>> print(ast.dump(ast.parse("async def f():"
- ... " return [i async for i in soc]"),
+ >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
... indent=4)) # Async comprehension
- Module(
- body=[
- AsyncFunctionDef(
- name='f',
- args=arguments(
- posonlyargs=[],
- args=[],
- vararg=None,
- kwonlyargs=[],
- kw_defaults=[],
- kwarg=None,
- defaults=[]),
- body=[
- Return(
- value=ListComp(
- elt=Name(id='i', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='i', ctx=Store()),
- iter=Name(id='soc', ctx=Load()),
- ifs=[],
- is_async=1)]))],
- decorator_list=[],
- returns=None,
- type_comment=None)],
- type_ignores=[])
+ Expression(
+ body=ListComp(
+ elt=Name(id='i', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='i', ctx=Store()),
+ iter=Name(id='soc', ctx=Load()),
+ ifs=[],
+ is_async=1)]))
Statements
^^^^^^^^^^
.. doctest::
- >>> print(ast.dump(ast.parse("a = b = 1"), indent=4)) # Multiple assignment
+ >>> print(ast.dump(ast.parse('a = b = 1'), indent=4)) # Multiple assignment
Module(
body=[
Assign(
type_comment=None)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a,b = c"), indent=4)) # Unpacking
+ >>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking
Module(
body=[
Assign(
.. doctest::
- >>> print(ast.dump(ast.parse("c: int"), indent=4))
+ >>> print(ast.dump(ast.parse('c: int'), indent=4))
Module(
body=[
AnnAssign(
simple=1)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("(a): int = 1"), indent=4)) # Annotation with parenthesis
+ >>> print(ast.dump(ast.parse('(a): int = 1'), indent=4)) # Annotation with parenthesis
Module(
body=[
AnnAssign(
simple=0)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a.b: int"), indent=4)) # Attribute annotation
+ >>> print(ast.dump(ast.parse('a.b: int'), indent=4)) # Attribute annotation
Module(
body=[
AnnAssign(
simple=0)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a[1]: int"), indent=4)) # Subscript annotation
+ >>> print(ast.dump(ast.parse('a[1]: int'), indent=4)) # Subscript annotation
Module(
body=[
AnnAssign(
.. doctest::
- >>> print(ast.dump(ast.parse("x += 2"), indent=4))
+ >>> print(ast.dump(ast.parse('x += 2'), indent=4))
Module(
body=[
AugAssign(
.. doctest::
- >>> print(ast.dump(ast.parse("raise x from y"), indent=4))
+ >>> print(ast.dump(ast.parse('raise x from y'), indent=4))
Module(
body=[
Raise(
.. doctest::
- >>> print(ast.dump(ast.parse("assert x,y"), indent=4))
+ >>> print(ast.dump(ast.parse('assert x,y'), indent=4))
Module(
body=[
Assert(
.. doctest::
- >>> print(ast.dump(ast.parse("del x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('del x,y,z'), indent=4))
Module(
body=[
Delete(
.. doctest::
- >>> print(ast.dump(ast.parse("pass"), indent=4))
+ >>> print(ast.dump(ast.parse('pass'), indent=4))
Module(
body=[
Pass()],
.. doctest::
- >>> print(ast.dump(ast.parse("import x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('import x,y,z'), indent=4))
Module(
body=[
Import(
.. doctest::
- >>> print(ast.dump(ast.parse("from y import x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('from y import x,y,z'), indent=4))
Module(
body=[
ImportFrom(
.. doctest::
- >>> print(ast.dump(ast.parse("from ..foo.bar import a as b, c"), indent=4))
+ >>> print(ast.dump(ast.parse('from ..foo.bar import a as b, c'), indent=4))
Module(
body=[
ImportFrom(
.. doctest::
- >>> print(ast.dump(ast.parse("lambda x,y: ..."), indent=4))
+ >>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
Module(
body=[
Expr(
.. doctest::
- >>> print(ast.dump(ast.parse("return 4"), indent=4))
+ >>> print(ast.dump(ast.parse('return 4'), indent=4))
Module(
body=[
Return(
.. doctest::
- >>> print(ast.dump(ast.parse("yield x"), indent=4))
+ >>> print(ast.dump(ast.parse('yield x'), indent=4))
Module(
body=[
Expr(
value=Name(id='x', ctx=Load())))],
type_ignores=[])
- >>> print(ast.dump(ast.parse("yield from x"), indent=4))
+ >>> print(ast.dump(ast.parse('yield from x'), indent=4))
Module(
body=[
Expr(
.. doctest::
- >>> print(ast.dump(ast.parse("global x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('global x,y,z'), indent=4))
Module(
body=[
Global(
'z'])],
type_ignores=[])
- >>> print(ast.dump(ast.parse("nonlocal x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('nonlocal x,y,z'), indent=4))
Module(
body=[
Nonlocal(