blob: 01735643dbb9b8f468518645a7c5a4d36e8144bf [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001:mod:`ast` --- Abstract Syntax Trees
2====================================
Georg Brandl0c77a822008-06-10 16:37:50 +00003
4.. module:: ast
5 :synopsis: Abstract Syntax Tree classes and manipulation.
6
7.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
8.. sectionauthor:: Georg Brandl <georg@python.org>
9
Pablo Galindo114081f2020-03-02 03:14:06 +000010.. testsetup::
11
12 import ast
13
Raymond Hettinger10480942011-01-10 03:26:08 +000014**Source code:** :source:`Lib/ast.py`
Georg Brandl0c77a822008-06-10 16:37:50 +000015
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000016--------------
17
Georg Brandl0c77a822008-06-10 16:37:50 +000018The :mod:`ast` module helps Python applications to process trees of the Python
19abstract syntax grammar. The abstract syntax itself might change with each
20Python release; this module helps to find out programmatically what the current
21grammar looks like.
22
Benjamin Petersonec9199b2008-11-08 17:05:00 +000023An abstract syntax tree can be generated by passing :data:`ast.PyCF_ONLY_AST` as
Georg Brandl22b34312009-07-26 14:54:51 +000024a flag to the :func:`compile` built-in function, or using the :func:`parse`
Georg Brandl0c77a822008-06-10 16:37:50 +000025helper provided in this module. The result will be a tree of objects whose
Benjamin Petersonec9199b2008-11-08 17:05:00 +000026classes all inherit from :class:`ast.AST`. An abstract syntax tree can be
27compiled into a Python code object using the built-in :func:`compile` function.
Georg Brandl0c77a822008-06-10 16:37:50 +000028
Georg Brandl0c77a822008-06-10 16:37:50 +000029
Pablo Galindo114081f2020-03-02 03:14:06 +000030.. _abstract-grammar:
31
32Abstract Grammar
33----------------
34
35The abstract grammar is currently defined as follows:
36
37.. literalinclude:: ../../Parser/Python.asdl
38 :language: none
39
40
Georg Brandl0c77a822008-06-10 16:37:50 +000041Node classes
42------------
43
44.. class:: AST
45
46 This is the base of all AST node classes. The actual node classes are
47 derived from the :file:`Parser/Python.asdl` file, which is reproduced
48 :ref:`below <abstract-grammar>`. They are defined in the :mod:`_ast` C
49 module and re-exported in :mod:`ast`.
50
51 There is one class defined for each left-hand side symbol in the abstract
52 grammar (for example, :class:`ast.stmt` or :class:`ast.expr`). In addition,
53 there is one class defined for each constructor on the right-hand side; these
54 classes inherit from the classes for the left-hand side trees. For example,
55 :class:`ast.BinOp` inherits from :class:`ast.expr`. For production rules
56 with alternatives (aka "sums"), the left-hand side class is abstract: only
57 instances of specific constructor nodes are ever created.
58
Serhiy Storchaka913876d2018-10-28 13:41:26 +020059 .. index:: single: ? (question mark); in AST grammar
60 .. index:: single: * (asterisk); in AST grammar
61
Georg Brandl0c77a822008-06-10 16:37:50 +000062 .. attribute:: _fields
63
64 Each concrete class has an attribute :attr:`_fields` which gives the names
65 of all child nodes.
66
67 Each instance of a concrete class has one attribute for each child node,
68 of the type as defined in the grammar. For example, :class:`ast.BinOp`
69 instances have an attribute :attr:`left` of type :class:`ast.expr`.
70
71 If these attributes are marked as optional in the grammar (using a
72 question mark), the value might be ``None``. If the attributes can have
73 zero-or-more values (marked with an asterisk), the values are represented
74 as Python lists. All possible attributes must be present and have valid
75 values when compiling an AST with :func:`compile`.
76
77 .. attribute:: lineno
78 col_offset
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000079 end_lineno
80 end_col_offset
Georg Brandl0c77a822008-06-10 16:37:50 +000081
82 Instances of :class:`ast.expr` and :class:`ast.stmt` subclasses have
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000083 :attr:`lineno`, :attr:`col_offset`, :attr:`lineno`, and :attr:`col_offset`
84 attributes. The :attr:`lineno` and :attr:`end_lineno` are the first and
85 last line numbers of source text span (1-indexed so the first line is line 1)
86 and the :attr:`col_offset` and :attr:`end_col_offset` are the corresponding
87 UTF-8 byte offsets of the first and last tokens that generated the node.
88 The UTF-8 offset is recorded because the parser uses UTF-8 internally.
89
90 Note that the end positions are not required by the compiler and are
91 therefore optional. The end offset is *after* the last symbol, for example
92 one can get the source segment of a one-line expression node using
93 ``source_line[node.col_offset : node.end_col_offset]``.
Georg Brandl0c77a822008-06-10 16:37:50 +000094
95 The constructor of a class :class:`ast.T` parses its arguments as follows:
96
97 * If there are positional arguments, there must be as many as there are items
98 in :attr:`T._fields`; they will be assigned as attributes of these names.
99 * If there are keyword arguments, they will set the attributes of the same
100 names to the given values.
101
102 For example, to create and populate an :class:`ast.UnaryOp` node, you could
103 use ::
104
105 node = ast.UnaryOp()
106 node.op = ast.USub()
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300107 node.operand = ast.Constant()
108 node.operand.value = 5
Georg Brandl0c77a822008-06-10 16:37:50 +0000109 node.operand.lineno = 0
110 node.operand.col_offset = 0
111 node.lineno = 0
112 node.col_offset = 0
113
114 or the more compact ::
115
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300116 node = ast.UnaryOp(ast.USub(), ast.Constant(5, lineno=0, col_offset=0),
Georg Brandl0c77a822008-06-10 16:37:50 +0000117 lineno=0, col_offset=0)
118
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200119.. versionchanged:: 3.8
120
121 Class :class:`ast.Constant` is now used for all constants.
122
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300123.. deprecated:: 3.8
124
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200125 Old classes :class:`ast.Num`, :class:`ast.Str`, :class:`ast.Bytes`,
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300126 :class:`ast.NameConstant` and :class:`ast.Ellipsis` are still available,
Serhiy Storchaka85a2eef2020-02-17 11:03:00 +0200127 but they will be removed in future Python releases. In the meanwhile,
128 instantiating them will return an instance of a different class.
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300129
Pablo Galindo114081f2020-03-02 03:14:06 +0000130Literals
131^^^^^^^^
Georg Brandl0c77a822008-06-10 16:37:50 +0000132
Pablo Galindo114081f2020-03-02 03:14:06 +0000133.. class:: Constant(value)
Georg Brandl0c77a822008-06-10 16:37:50 +0000134
Pablo Galindo114081f2020-03-02 03:14:06 +0000135 A constant value. The ``value`` attribute of the ``Constant`` literal contains the
136 Python object it represents. The values represented can be simple types
137 such as a number, string or ``None``, but also immutable container types
138 (tuples and frozensets) if all of their elements are constant.
Georg Brandl0c77a822008-06-10 16:37:50 +0000139
Pablo Galindo114081f2020-03-02 03:14:06 +0000140 .. doctest::
Georg Brandl0c77a822008-06-10 16:37:50 +0000141
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000142 >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
143 Expression(
144 body=Constant(value=123, kind=None))
Pablo Galindo114081f2020-03-02 03:14:06 +0000145
146
147.. class:: FormattedValue(value, conversion, format_spec)
148
149 Node representing a single formatting field in an f-string. If the string
150 contains a single formatting field and nothing else the node can be
151 isolated otherwise it appears in :class:`JoinedStr`.
152
153 * ``value`` is any expression node (such as a literal, a variable, or a
154 function call).
155 * ``conversion`` is an integer:
156
157 * -1: no formatting
158 * 115: ``!s`` string formatting
159 * 114: ``!r`` repr formatting
160 * 97: ``!a`` ascii formatting
161
162 * ``format_spec`` is a :class:`JoinedStr` node representing the formatting
163 of the value, or ``None`` if no format was specified. Both
164 ``conversion`` and ``format_spec`` can be set at the same time.
165
166
167.. class:: JoinedStr(values)
168
169 An f-string, comprising a series of :class:`FormattedValue` and :class:`Constant`
170 nodes.
171
172 .. doctest::
173
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000174 >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4))
175 Expression(
176 body=JoinedStr(
177 values=[
178 Constant(value='sin(', kind=None),
179 FormattedValue(
180 value=Name(id='a', ctx=Load()),
181 conversion=-1,
182 format_spec=None),
183 Constant(value=') is ', kind=None),
184 FormattedValue(
185 value=Call(
186 func=Name(id='sin', ctx=Load()),
187 args=[
188 Name(id='a', ctx=Load())],
189 keywords=[]),
190 conversion=-1,
191 format_spec=JoinedStr(
192 values=[
193 Constant(value='.3', kind=None)]))]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000194
195
196.. class:: List(elts, ctx)
197 Tuple(elts, ctx)
198
199 A list or tuple. ``elts`` holds a list of nodes representing the elements.
200 ``ctx`` is :class:`Store` if the container is an assignment target (i.e.
201 ``(x,y)=something``), and :class:`Load` otherwise.
202
203 .. doctest::
204
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000205 >>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4))
206 Expression(
207 body=List(
208 elts=[
209 Constant(value=1, kind=None),
210 Constant(value=2, kind=None),
211 Constant(value=3, kind=None)],
212 ctx=Load()))
213 >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
214 Expression(
215 body=Tuple(
216 elts=[
217 Constant(value=1, kind=None),
218 Constant(value=2, kind=None),
219 Constant(value=3, kind=None)],
220 ctx=Load()))
Pablo Galindo114081f2020-03-02 03:14:06 +0000221
222
223.. class:: Set(elts)
224
225 A set. ``elts`` holds a list of nodes representing the set's elements.
226
227 .. doctest::
228
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000229 >>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4))
230 Expression(
231 body=Set(
232 elts=[
233 Constant(value=1, kind=None),
234 Constant(value=2, kind=None),
235 Constant(value=3, kind=None)]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000236
237
238.. class:: Dict(keys, values)
239
240 A dictionary. ``keys`` and ``values`` hold lists of nodes representing the
241 keys and the values respectively, in matching order (what would be returned
242 when calling :code:`dictionary.keys()` and :code:`dictionary.values()`).
243
244 When doing dictionary unpacking using dictionary literals the expression to be
245 expanded goes in the ``values`` list, with a ``None`` at the corresponding
246 position in ``keys``.
247
248 .. doctest::
249
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000250 >>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4))
251 Expression(
252 body=Dict(
253 keys=[
254 Constant(value='a', kind=None),
255 None],
256 values=[
257 Constant(value=1, kind=None),
258 Name(id='d', ctx=Load())]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000259
260
261Variables
262^^^^^^^^^
263
264.. class:: Name(id, ctx)
265
266 A variable name. ``id`` holds the name as a string, and ``ctx`` is one of
267 the following types.
268
269
270.. class:: Load()
271 Store()
272 Del()
273
274 Variable references can be used to load the value of a variable, to assign
275 a new value to it, or to delete it. Variable references are given a context
276 to distinguish these cases.
277
278 .. doctest::
279
280 >>> print(ast.dump(ast.parse('a'), indent=4))
281 Module(
282 body=[
283 Expr(
284 value=Name(id='a', ctx=Load()))],
285 type_ignores=[])
286
287 >>> print(ast.dump(ast.parse('a = 1'), indent=4))
288 Module(
289 body=[
290 Assign(
291 targets=[
292 Name(id='a', ctx=Store())],
293 value=Constant(value=1, kind=None),
294 type_comment=None)],
295 type_ignores=[])
296
297 >>> print(ast.dump(ast.parse('del a'), indent=4))
298 Module(
299 body=[
300 Delete(
301 targets=[
302 Name(id='a', ctx=Del())])],
303 type_ignores=[])
304
305
306.. class:: Starred(value, ctx)
307
308 A ``*var`` variable reference. ``value`` holds the variable, typically a
309 :class:`Name` node. This type must be used when building a :class:`Call`
310 node with ``*args``.
311
312 .. doctest::
313
314 >>> print(ast.dump(ast.parse('a, *b = it'), indent=4))
315 Module(
316 body=[
317 Assign(
318 targets=[
319 Tuple(
320 elts=[
321 Name(id='a', ctx=Store()),
322 Starred(
323 value=Name(id='b', ctx=Store()),
324 ctx=Store())],
325 ctx=Store())],
326 value=Name(id='it', ctx=Load()),
327 type_comment=None)],
328 type_ignores=[])
329
330
331Expressions
332^^^^^^^^^^^
333
334.. class:: Expr(value)
335
336 When an expression, such as a function call, appears as a statement by itself
337 with its return value not used or stored, it is wrapped in this container.
338 ``value`` holds one of the other nodes in this section, a :class:`Constant`, a
339 :class:`Name`, a :class:`Lambda`, a :class:`Yield` or :class:`YieldFrom` node.
340
341 .. doctest::
342
343 >>> print(ast.dump(ast.parse('-a'), indent=4))
344 Module(
345 body=[
346 Expr(
347 value=UnaryOp(
348 op=USub(),
349 operand=Name(id='a', ctx=Load())))],
350 type_ignores=[])
351
352
353.. class:: UnaryOp(op, operand)
354
355 A unary operation. ``op`` is the operator, and ``operand`` any expression
356 node.
357
358
359.. class:: UAdd
360 USub
361 Not
362 Invert
363
364 Unary operator tokens. :class:`Not` is the ``not`` keyword, :class:`Invert`
365 is the ``~`` operator.
366
367 .. doctest::
368
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000369 >>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4))
370 Expression(
371 body=UnaryOp(
372 op=Not(),
373 operand=Name(id='x', ctx=Load())))
Pablo Galindo114081f2020-03-02 03:14:06 +0000374
375
376.. class:: BinOp(left, op, right)
377
378 A binary operation (like addition or division). ``op`` is the operator, and
379 ``left`` and ``right`` are any expression nodes.
380
381 .. doctest::
382
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000383 >>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4))
384 Expression(
385 body=BinOp(
386 left=Name(id='x', ctx=Load()),
387 op=Add(),
388 right=Name(id='y', ctx=Load())))
Pablo Galindo114081f2020-03-02 03:14:06 +0000389
390
391.. class:: Add
392 Sub
393 Mult
394 Div
395 FloorDiv
396 Mod
397 Pow
398 LShift
399 RShift
400 BitOr
401 BitXor
402 BitAnd
403 MatMult
404
405 Binary operator tokens.
406
407
408.. class:: BoolOp(op, values)
409
410 A boolean operation, 'or' or 'and'. ``op`` is :class:`Or` or :class:`And`.
411 ``values`` are the values involved. Consecutive operations with the same
412 operator, such as ``a or b or c``, are collapsed into one node with several
413 values.
414
415 This doesn't include ``not``, which is a :class:`UnaryOp`.
416
417 .. doctest::
418
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000419 >>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4))
420 Expression(
421 body=BoolOp(
422 op=Or(),
423 values=[
424 Name(id='x', ctx=Load()),
425 Name(id='y', ctx=Load())]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000426
427
428.. class:: And
429 Or
430
431 Boolean operator tokens.
432
433
434.. class:: Compare(left, ops, comparators)
435
436 A comparison of two or more values. ``left`` is the first value in the
437 comparison, ``ops`` the list of operators, and ``comparators`` the list
438 of values after the first element in the comparison.
439
440 .. doctest::
441
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000442 >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
443 Expression(
444 body=Compare(
445 left=Constant(value=1, kind=None),
446 ops=[
447 LtE(),
448 Lt()],
449 comparators=[
450 Name(id='a', ctx=Load()),
451 Constant(value=10, kind=None)]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000452
453
454.. class:: Eq
455 NotEq
456 Lt
457 LtE
458 Gt
459 GtE
460 Is
461 IsNot
462 In
463 NotIn
464
465 Comparison operator tokens.
466
467
468.. class:: Call(func, args, keywords, starargs, kwargs)
469
470 A function call. ``func`` is the function, which will often be a
471 :class:`Name` or :class:`Attribute` object. Of the arguments:
472
473 * ``args`` holds a list of the arguments passed by position.
474 * ``keywords`` holds a list of :class:`keyword` objects representing
475 arguments passed by keyword.
476
477 When creating a ``Call`` node, ``args`` and ``keywords`` are required, but
478 they can be empty lists. ``starargs`` and ``kwargs`` are optional.
479
480 .. doctest::
481
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000482 >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4))
483 Expression(
484 body=Call(
485 func=Name(id='func', ctx=Load()),
486 args=[
487 Name(id='a', ctx=Load()),
488 Starred(
489 value=Name(id='d', ctx=Load()),
490 ctx=Load())],
491 keywords=[
492 keyword(
493 arg='b',
494 value=Name(id='c', ctx=Load())),
495 keyword(
496 arg=None,
497 value=Name(id='e', ctx=Load()))]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000498
499
500.. class:: keyword(arg, value)
501
502 A keyword argument to a function call or class definition. ``arg`` is a raw
503 string of the parameter name, ``value`` is a node to pass in.
504
505
506.. class:: IfExp(test, body, orelse)
507
508 An expression such as ``a if b else c``. Each field holds a single node, so
509 in the following example, all three are :class:`Name` nodes.
510
511 .. doctest::
512
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000513 >>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4))
514 Expression(
515 body=IfExp(
516 test=Name(id='b', ctx=Load()),
517 body=Name(id='a', ctx=Load()),
518 orelse=Name(id='c', ctx=Load())))
Pablo Galindo114081f2020-03-02 03:14:06 +0000519
520
521.. class:: Attribute(value, attr, ctx)
522
523 Attribute access, e.g. ``d.keys``. ``value`` is a node, typically a
524 :class:`Name`. ``attr`` is a bare string giving the name of the attribute,
525 and ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del` according to how
526 the attribute is acted on.
527
528 .. doctest::
529
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000530 >>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4))
531 Expression(
532 body=Attribute(
533 value=Name(id='snake', ctx=Load()),
534 attr='colour',
535 ctx=Load()))
Pablo Galindo114081f2020-03-02 03:14:06 +0000536
537
538.. class:: NamedExpr(target, value)
539
540 A named expression. This AST node is produced by the assignment expressions
541 operator (also known as the walrus operator). As opposed to the :class:`Assign`
542 node in which the first argument can be multiple nodes, in this case both
543 ``target`` and ``value`` must be single nodes.
544
545 .. doctest::
546
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000547 >>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4))
548 Expression(
549 body=NamedExpr(
550 target=Name(id='x', ctx=Store()),
551 value=Constant(value=4, kind=None)))
Pablo Galindo114081f2020-03-02 03:14:06 +0000552
553
554Subscripting
555~~~~~~~~~~~~
556
557.. class:: Subscript(value, slice, ctx)
558
559 A subscript, such as ``l[1]``. ``value`` is the object, often a
560 :class:`Name`. ``slice`` is one of :class:`Index`, :class:`Slice` or
561 :class:`ExtSlice`. ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del`
562 according to the action performed with the subscript.
563
564
565.. class:: Index(value)
566
567 Simple subscripting with a single value
568
569 .. doctest::
570
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000571 >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4))
572 Expression(
573 body=Subscript(
574 value=Name(id='l', ctx=Load()),
575 slice=Index(
576 value=Constant(value=1, kind=None)),
577 ctx=Load()))
Pablo Galindo114081f2020-03-02 03:14:06 +0000578
579
580.. class:: Slice(lower, upper, step)
581
582 Regular slicing (on the form x:y).
583
584 .. doctest::
585
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000586 >>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4))
587 Expression(
588 body=Subscript(
589 value=Name(id='l', ctx=Load()),
590 slice=Slice(
591 lower=Constant(value=1, kind=None),
592 upper=Constant(value=2, kind=None),
593 step=None),
594 ctx=Load()))
Pablo Galindo114081f2020-03-02 03:14:06 +0000595
596
597.. class:: ExtSlice(dims)
598
599 Advanced slicing. ``dims`` holds a list of :class:`Slice` and
600 :class:`Index` nodes
601
602 .. doctest::
603
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000604 >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
605 Expression(
606 body=Subscript(
607 value=Name(id='l', ctx=Load()),
608 slice=ExtSlice(
609 dims=[
610 Slice(
611 lower=Constant(value=1, kind=None),
612 upper=Constant(value=2, kind=None),
613 step=None),
614 Index(
615 value=Constant(value=3, kind=None))]),
616 ctx=Load()))
Pablo Galindo114081f2020-03-02 03:14:06 +0000617
618
619Comprehensions
620~~~~~~~~~~~~~~
621
622.. class:: ListComp(elt, generators)
623 SetComp(elt, generators)
624 GeneratorExp(elt, generators)
625 DictComp(key, value, generators)
626
627 List and set comprehensions, generator expressions, and dictionary
628 comprehensions. ``elt`` (or ``key`` and ``value``) is a single node
629 representing the part that will be evaluated for each item.
630
631 ``generators`` is a list of :class:`comprehension` nodes.
632
633 .. doctest::
634
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000635 >>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4))
636 Expression(
637 body=ListComp(
638 elt=Name(id='x', ctx=Load()),
639 generators=[
640 comprehension(
641 target=Name(id='x', ctx=Store()),
642 iter=Name(id='numbers', ctx=Load()),
643 ifs=[],
644 is_async=0)]))
645 >>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4))
646 Expression(
647 body=DictComp(
648 key=Name(id='x', ctx=Load()),
649 value=BinOp(
650 left=Name(id='x', ctx=Load()),
651 op=Pow(),
652 right=Constant(value=2, kind=None)),
653 generators=[
654 comprehension(
655 target=Name(id='x', ctx=Store()),
656 iter=Name(id='numbers', ctx=Load()),
657 ifs=[],
658 is_async=0)]))
659 >>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4))
660 Expression(
661 body=SetComp(
662 elt=Name(id='x', ctx=Load()),
663 generators=[
664 comprehension(
665 target=Name(id='x', ctx=Store()),
666 iter=Name(id='numbers', ctx=Load()),
667 ifs=[],
668 is_async=0)]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000669
670
671.. class:: comprehension(target, iter, ifs, is_async)
672
673 One ``for`` clause in a comprehension. ``target`` is the reference to use for
674 each element - typically a :class:`Name` or :class:`Tuple` node. ``iter``
675 is the object to iterate over. ``ifs`` is a list of test expressions: each
676 ``for`` clause can have multiple ``ifs``.
677
678 ``is_async`` indicates a comprehension is asynchronous (using an
679 ``async for`` instead of ``for``). The value is an integer (0 or 1).
680
681 .. doctest::
682
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000683 >>> print(ast.dump(ast.parse('[ord(c) for line in file for c in line]', mode='eval'),
Pablo Galindo114081f2020-03-02 03:14:06 +0000684 ... indent=4)) # Multiple comprehensions in one.
685 Expression(
686 body=ListComp(
687 elt=Call(
688 func=Name(id='ord', ctx=Load()),
689 args=[
690 Name(id='c', ctx=Load())],
691 keywords=[]),
692 generators=[
693 comprehension(
694 target=Name(id='line', ctx=Store()),
695 iter=Name(id='file', ctx=Load()),
696 ifs=[],
697 is_async=0),
698 comprehension(
699 target=Name(id='c', ctx=Store()),
700 iter=Name(id='line', ctx=Load()),
701 ifs=[],
702 is_async=0)]))
703
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000704 >>> print(ast.dump(ast.parse('(n**2 for n in it if n>5 if n<10)', mode='eval'),
Pablo Galindo114081f2020-03-02 03:14:06 +0000705 ... indent=4)) # generator comprehension
706 Expression(
707 body=GeneratorExp(
708 elt=BinOp(
709 left=Name(id='n', ctx=Load()),
710 op=Pow(),
711 right=Constant(value=2, kind=None)),
712 generators=[
713 comprehension(
714 target=Name(id='n', ctx=Store()),
715 iter=Name(id='it', ctx=Load()),
716 ifs=[
717 Compare(
718 left=Name(id='n', ctx=Load()),
719 ops=[
720 Gt()],
721 comparators=[
722 Constant(value=5, kind=None)]),
723 Compare(
724 left=Name(id='n', ctx=Load()),
725 ops=[
726 Lt()],
727 comparators=[
728 Constant(value=10, kind=None)])],
729 is_async=0)]))
730
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000731 >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
Pablo Galindo114081f2020-03-02 03:14:06 +0000732 ... indent=4)) # Async comprehension
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000733 Expression(
734 body=ListComp(
735 elt=Name(id='i', ctx=Load()),
736 generators=[
737 comprehension(
738 target=Name(id='i', ctx=Store()),
739 iter=Name(id='soc', ctx=Load()),
740 ifs=[],
741 is_async=1)]))
Pablo Galindo114081f2020-03-02 03:14:06 +0000742
743Statements
744^^^^^^^^^^
745
746.. class:: Assign(targets, value, type_comment)
747
748 An assignment. ``targets`` is a list of nodes, and ``value`` is a single node.
749
750 Multiple nodes in ``targets`` represents assigning the same value to each.
751 Unpacking is represented by putting a :class:`Tuple` or :class:`List`
752 within ``targets``.
753
754 .. attribute:: type_comment
755
756 ``type_comment`` is an optional string with the type annotation as a comment.
757
758 .. doctest::
759
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000760 >>> print(ast.dump(ast.parse('a = b = 1'), indent=4)) # Multiple assignment
Pablo Galindo114081f2020-03-02 03:14:06 +0000761 Module(
762 body=[
763 Assign(
764 targets=[
765 Name(id='a', ctx=Store()),
766 Name(id='b', ctx=Store())],
767 value=Constant(value=1, kind=None),
768 type_comment=None)],
769 type_ignores=[])
770
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000771 >>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking
Pablo Galindo114081f2020-03-02 03:14:06 +0000772 Module(
773 body=[
774 Assign(
775 targets=[
776 Tuple(
777 elts=[
778 Name(id='a', ctx=Store()),
779 Name(id='b', ctx=Store())],
780 ctx=Store())],
781 value=Name(id='c', ctx=Load()),
782 type_comment=None)],
783 type_ignores=[])
784
785
786.. class:: AnnAssign(target, annotation, value, simple)
787
788 An assignment with a type annotation. ``target`` is a single node and can
789 be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`.
790 ``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name`
791 node. ``value`` is a single optional node. ``simple`` is a boolean integer
792 set to True for a :class:`Name` node in ``target`` that do not appear in
793 between parenthesis and are hence pure names and not expressions.
794
795 .. doctest::
796
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000797 >>> print(ast.dump(ast.parse('c: int'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000798 Module(
799 body=[
800 AnnAssign(
801 target=Name(id='c', ctx=Store()),
802 annotation=Name(id='int', ctx=Load()),
803 value=None,
804 simple=1)],
805 type_ignores=[])
806
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000807 >>> print(ast.dump(ast.parse('(a): int = 1'), indent=4)) # Annotation with parenthesis
Pablo Galindo114081f2020-03-02 03:14:06 +0000808 Module(
809 body=[
810 AnnAssign(
811 target=Name(id='a', ctx=Store()),
812 annotation=Name(id='int', ctx=Load()),
813 value=Constant(value=1, kind=None),
814 simple=0)],
815 type_ignores=[])
816
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000817 >>> print(ast.dump(ast.parse('a.b: int'), indent=4)) # Attribute annotation
Pablo Galindo114081f2020-03-02 03:14:06 +0000818 Module(
819 body=[
820 AnnAssign(
821 target=Attribute(
822 value=Name(id='a', ctx=Load()),
823 attr='b',
824 ctx=Store()),
825 annotation=Name(id='int', ctx=Load()),
826 value=None,
827 simple=0)],
828 type_ignores=[])
829
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000830 >>> print(ast.dump(ast.parse('a[1]: int'), indent=4)) # Subscript annotation
Pablo Galindo114081f2020-03-02 03:14:06 +0000831 Module(
832 body=[
833 AnnAssign(
834 target=Subscript(
835 value=Name(id='a', ctx=Load()),
836 slice=Index(
837 value=Constant(value=1, kind=None)),
838 ctx=Store()),
839 annotation=Name(id='int', ctx=Load()),
840 value=None,
841 simple=0)],
842 type_ignores=[])
843
844
845.. class:: AugAssign(target, op, value)
846
847 Augmented assignment, such as ``a += 1``. In the following example,
848 ``target`` is a :class:`Name` node for ``x`` (with the :class:`Store`
849 context), ``op`` is :class:`Add`, and ``value`` is a :class:`Constant` with
850 value for 1.
851
852 The ``target`` attribute connot be of class :class:`Tuple` or :class:`List`,
853 unlike the targets of :class:`Assign`.
854
855 .. doctest::
856
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000857 >>> print(ast.dump(ast.parse('x += 2'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000858 Module(
859 body=[
860 AugAssign(
861 target=Name(id='x', ctx=Store()),
862 op=Add(),
863 value=Constant(value=2, kind=None))],
864 type_ignores=[])
865
866
867.. class:: Raise(exc, cause)
868
869 A ``raise`` statement. ``exc`` is the exception object to be raised, normally a
870 :class:`Call` or :class:`Name`, or ``None`` for a standalone ``raise``.
871 ``cause`` is the optional part for ``y`` in ``raise x from y``.
872
873 .. doctest::
874
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000875 >>> print(ast.dump(ast.parse('raise x from y'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000876 Module(
877 body=[
878 Raise(
879 exc=Name(id='x', ctx=Load()),
880 cause=Name(id='y', ctx=Load()))],
881 type_ignores=[])
882
883
884.. class:: Assert(test, msg)
885
886 An assertion. ``test`` holds the condition, such as a :class:`Compare` node.
887 ``msg`` holds the failure message.
888
889 .. doctest::
890
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000891 >>> print(ast.dump(ast.parse('assert x,y'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000892 Module(
893 body=[
894 Assert(
895 test=Name(id='x', ctx=Load()),
896 msg=Name(id='y', ctx=Load()))],
897 type_ignores=[])
898
899
900.. class:: Delete(targets)
901
902 Represents a ``del`` statement. ``targets`` is a list of nodes, such as
903 :class:`Name`, :class:`Attribute` or :class:`Subscript` nodes.
904
905 .. doctest::
906
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000907 >>> print(ast.dump(ast.parse('del x,y,z'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000908 Module(
909 body=[
910 Delete(
911 targets=[
912 Name(id='x', ctx=Del()),
913 Name(id='y', ctx=Del()),
914 Name(id='z', ctx=Del())])],
915 type_ignores=[])
916
917
918.. class:: Pass()
919
920 A ``pass`` statement.
921
922 .. doctest::
923
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000924 >>> print(ast.dump(ast.parse('pass'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000925 Module(
926 body=[
927 Pass()],
928 type_ignores=[])
929
930
931Other statements which are only applicable inside functions or loops are
932described in other sections.
933
934Imports
935~~~~~~~
936
937.. class:: Import(names)
938
939 An import statement. ``names`` is a list of :class:`alias` nodes.
940
941 .. doctest::
942
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000943 >>> print(ast.dump(ast.parse('import x,y,z'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000944 Module(
945 body=[
946 Import(
947 names=[
948 alias(name='x', asname=None),
949 alias(name='y', asname=None),
950 alias(name='z', asname=None)])],
951 type_ignores=[])
952
953
954.. class:: ImportFrom(module, names, level)
955
956 Represents ``from x import y``. ``module`` is a raw string of the 'from' name,
957 without any leading dots, or ``None`` for statements such as ``from . import foo``.
958 ``level`` is an integer holding the level of the relative import (0 means
959 absolute import).
960
961 .. doctest::
962
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000963 >>> print(ast.dump(ast.parse('from y import x,y,z'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000964 Module(
965 body=[
966 ImportFrom(
967 module='y',
968 names=[
969 alias(name='x', asname=None),
970 alias(name='y', asname=None),
971 alias(name='z', asname=None)],
972 level=0)],
973 type_ignores=[])
974
975
976.. class:: alias(name, asname)
977
978 Both parameters are raw strings of the names. ``asname`` can be ``None`` if
979 the regular name is to be used.
980
981 .. doctest::
982
Pablo Galindo02f64cb2020-03-07 18:22:58 +0000983 >>> print(ast.dump(ast.parse('from ..foo.bar import a as b, c'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +0000984 Module(
985 body=[
986 ImportFrom(
987 module='foo.bar',
988 names=[
989 alias(name='a', asname='b'),
990 alias(name='c', asname=None)],
991 level=2)],
992 type_ignores=[])
993
994Control flow
995^^^^^^^^^^^^
996
997.. note::
998 Optional clauses such as ``else`` are stored as an empty list if they're
999 not present.
1000
1001.. class:: If(test, body, orelse)
1002
1003 An ``if`` statement. ``test`` holds a single node, such as a :class:`Compare`
1004 node. ``body`` and ``orelse`` each hold a list of nodes.
1005
1006 ``elif`` clauses don't have a special representation in the AST, but rather
1007 appear as extra :class:`If` nodes within the ``orelse`` section of the
1008 previous one.
1009
1010 .. doctest::
1011
1012 >>> print(ast.dump(ast.parse("""
1013 ... if x:
1014 ... ...
1015 ... elif y:
1016 ... ...
1017 ... else:
1018 ... ...
1019 ... """), indent=4))
1020 Module(
1021 body=[
1022 If(
1023 test=Name(id='x', ctx=Load()),
1024 body=[
1025 Expr(
1026 value=Constant(value=Ellipsis, kind=None))],
1027 orelse=[
1028 If(
1029 test=Name(id='y', ctx=Load()),
1030 body=[
1031 Expr(
1032 value=Constant(value=Ellipsis, kind=None))],
1033 orelse=[
1034 Expr(
1035 value=Constant(value=Ellipsis, kind=None))])])],
1036 type_ignores=[])
1037
1038
1039.. class:: For(target, iter, body, orelse, type_comment)
1040
1041 A ``for`` loop. ``target`` holds the variable(s) the loop assigns to, as a
1042 single :class:`Name`, :class:`Tuple` or :class:`List` node. ``iter`` holds
1043 the item to be looped over, again as a single node. ``body`` and ``orelse``
1044 contain lists of nodes to execute. Those in ``orelse`` are executed if the
1045 loop finishes normally, rather than via a ``break`` statement.
1046
1047 .. attribute:: type_comment
1048
1049 ``type_comment`` is an optional string with the type annotation as a comment.
1050
1051 .. doctest::
1052
1053 >>> print(ast.dump(ast.parse("""
1054 ... for x in y:
1055 ... ...
1056 ... else:
1057 ... ...
1058 ... """), indent=4))
1059 Module(
1060 body=[
1061 For(
1062 target=Name(id='x', ctx=Store()),
1063 iter=Name(id='y', ctx=Load()),
1064 body=[
1065 Expr(
1066 value=Constant(value=Ellipsis, kind=None))],
1067 orelse=[
1068 Expr(
1069 value=Constant(value=Ellipsis, kind=None))],
1070 type_comment=None)],
1071 type_ignores=[])
1072
1073
1074.. class:: While(test, body, orelse)
1075
1076 A ``while`` loop. ``test`` holds the condition, such as a :class:`Compare`
1077 node.
1078
1079 .. doctest::
1080
1081 >> print(ast.dump(ast.parse("""
1082 ... while x:
1083 ... ...
1084 ... else:
1085 ... ...
1086 ... """), indent=4))
1087 Module(
1088 body=[
1089 While(
1090 test=Name(id='x', ctx=Load()),
1091 body=[
1092 Expr(
1093 value=Constant(value=Ellipsis, kind=None))],
1094 orelse=[
1095 Expr(
1096 value=Constant(value=Ellipsis, kind=None))])],
1097 type_ignores=[])
1098
1099
1100.. class:: Break
1101 Continue
1102
1103 The ``break`` and ``continue`` statements.
1104
1105 .. doctest::
1106
1107 >>> print(ast.dump(ast.parse("""\
1108 ... for a in b:
1109 ... if a > 5:
1110 ... break
1111 ... else:
1112 ... continue
1113 ...
1114 ... """), indent=4))
1115 Module(
1116 body=[
1117 For(
1118 target=Name(id='a', ctx=Store()),
1119 iter=Name(id='b', ctx=Load()),
1120 body=[
1121 If(
1122 test=Compare(
1123 left=Name(id='a', ctx=Load()),
1124 ops=[
1125 Gt()],
1126 comparators=[
1127 Constant(value=5, kind=None)]),
1128 body=[
1129 Break()],
1130 orelse=[
1131 Continue()])],
1132 orelse=[],
1133 type_comment=None)],
1134 type_ignores=[])
1135
1136
1137.. class:: Try(body, handlers, orelse, finalbody)
1138
1139 ``try`` blocks. All attributes are list of nodes to execute, except for
1140 ``handlers``, which is a list of :class:`ExceptHandler` nodes.
1141
1142 .. doctest::
1143
1144 >>> print(ast.dump(ast.parse("""
1145 ... try:
1146 ... ...
1147 ... except Exception:
1148 ... ...
1149 ... except OtherException as e:
1150 ... ...
1151 ... else:
1152 ... ...
1153 ... finally:
1154 ... ...
1155 ... """), indent=4))
1156 Module(
1157 body=[
1158 Try(
1159 body=[
1160 Expr(
1161 value=Constant(value=Ellipsis, kind=None))],
1162 handlers=[
1163 ExceptHandler(
1164 type=Name(id='Exception', ctx=Load()),
1165 name=None,
1166 body=[
1167 Expr(
1168 value=Constant(value=Ellipsis, kind=None))]),
1169 ExceptHandler(
1170 type=Name(id='OtherException', ctx=Load()),
1171 name='e',
1172 body=[
1173 Expr(
1174 value=Constant(value=Ellipsis, kind=None))])],
1175 orelse=[
1176 Expr(
1177 value=Constant(value=Ellipsis, kind=None))],
1178 finalbody=[
1179 Expr(
1180 value=Constant(value=Ellipsis, kind=None))])],
1181 type_ignores=[])
1182
1183
1184.. class:: ExceptHandler(type, name, body)
1185
1186 A single ``except`` clause. ``type`` is the exception type it will match,
1187 typically a :class:`Name` node (or ``None`` for a catch-all ``except:`` clause).
1188 ``name`` is a raw string for the name to hold the exception, or ``None`` if
1189 the clause doesn't have ``as foo``. ``body`` is a list of nodes.
1190
1191 .. doctest::
1192
1193 >>> print(ast.dump(ast.parse("""\
1194 ... try:
1195 ... a + 1
1196 ... except TypeError:
1197 ... pass
1198 ... """), indent=4))
1199 Module(
1200 body=[
1201 Try(
1202 body=[
1203 Expr(
1204 value=BinOp(
1205 left=Name(id='a', ctx=Load()),
1206 op=Add(),
1207 right=Constant(value=1, kind=None)))],
1208 handlers=[
1209 ExceptHandler(
1210 type=Name(id='TypeError', ctx=Load()),
1211 name=None,
1212 body=[
1213 Pass()])],
1214 orelse=[],
1215 finalbody=[])],
1216 type_ignores=[])
1217
1218
1219.. class:: With(items, body, type_comment)
1220
1221 A ``with`` block. ``items`` is a list of :class:`withitem` nodes representing
1222 the context managers, and ``body`` is the indented block inside the context.
1223
1224 .. attribute:: type_comment
1225
1226 ``type_comment`` is an optional string with the type annotation as a comment.
1227
1228
1229.. class:: withitem(context_expr, optional_vars)
1230
1231 A single context manager in a ``with`` block. ``context_expr`` is the context
1232 manager, often a :class:`Call` node. ``optional_vars`` is a :class:`Name`,
1233 :class:`Tuple` or :class:`List` for the ``as foo`` part, or ``None`` if that
1234 isn't used.
1235
1236 .. doctest::
1237
1238 >>> print(ast.dump(ast.parse("""\
1239 ... with a as b, c as d:
1240 ... something(b, d)
1241 ... """), indent=4))
1242 Module(
1243 body=[
1244 With(
1245 items=[
1246 withitem(
1247 context_expr=Name(id='a', ctx=Load()),
1248 optional_vars=Name(id='b', ctx=Store())),
1249 withitem(
1250 context_expr=Name(id='c', ctx=Load()),
1251 optional_vars=Name(id='d', ctx=Store()))],
1252 body=[
1253 Expr(
1254 value=Call(
1255 func=Name(id='something', ctx=Load()),
1256 args=[
1257 Name(id='b', ctx=Load()),
1258 Name(id='d', ctx=Load())],
1259 keywords=[]))],
1260 type_comment=None)],
1261 type_ignores=[])
1262
1263
1264Function and class definitions
1265^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1266
1267.. class:: FunctionDef(name, args, body, decorator_list, returns, type_comment)
1268
1269 A function definition.
1270
1271 * ``name`` is a raw string of the function name.
1272 * ``args`` is a :class:`arguments` node.
1273 * ``body`` is the list of nodes inside the function.
1274 * ``decorator_list`` is the list of decorators to be applied, stored outermost
1275 first (i.e. the first in the list will be applied last).
1276 * ``returns`` is the return annotation.
1277
1278 .. attribute:: type_comment
1279
1280 ``type_comment`` is an optional string with the type annotation as a comment.
1281
1282
1283.. class:: Lambda(args, body)
1284
1285 ``lambda`` is a minimal function definition that can be used inside an
1286 expression. Unlike :class:`FunctionDef`, ``body`` holds a single node.
1287
1288 .. doctest::
1289
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001290 >>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001291 Module(
1292 body=[
1293 Expr(
1294 value=Lambda(
1295 args=arguments(
1296 posonlyargs=[],
1297 args=[
1298 arg(arg='x', annotation=None, type_comment=None),
1299 arg(arg='y', annotation=None, type_comment=None)],
1300 vararg=None,
1301 kwonlyargs=[],
1302 kw_defaults=[],
1303 kwarg=None,
1304 defaults=[]),
1305 body=Constant(value=Ellipsis, kind=None)))],
1306 type_ignores=[])
1307
1308
1309.. class:: arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults)
1310
1311 The arguments for a function.
1312
1313 * ``posonlyargs``, ``args`` and ``kwonlyargs`` are lists of :class:`arg` nodes.
1314 * ``vararg`` and ``kwarg`` are single :class:`arg` nodes, referring to the
1315 ``*args, **kwargs`` parameters.
1316 * ``kw_defaults`` is a list of default values for keyword-only arguments. If
1317 one is ``None``, the corresponding argument is required.
1318 * ``defaults`` is a list of default values for arguments that can be passed
1319 positionally. If there are fewer defaults, they correspond to the last n
1320 arguments.
1321
1322
1323.. class:: arg(arg, annotation, type_comment)
1324
1325 A single argument in a list. ``arg`` is a raw string of the argument
1326 name, ``annotation`` is its annotation, such as a :class:`Str` or
1327 :class:`Name` node.
1328
1329 .. attribute:: type_comment
1330
1331 ``type_comment`` is an optional string with the type annotation as a comment
1332
1333 .. doctest::
1334
1335 >>> print(ast.dump(ast.parse("""\
1336 ... @decorator1
1337 ... @decorator2
1338 ... def f(a: 'annotation', b=1, c=2, *d, e, f=3, **g) -> 'return annotation':
1339 ... pass
1340 ... """), indent=4))
1341 Module(
1342 body=[
1343 FunctionDef(
1344 name='f',
1345 args=arguments(
1346 posonlyargs=[],
1347 args=[
1348 arg(
1349 arg='a',
1350 annotation=Constant(value='annotation', kind=None),
1351 type_comment=None),
1352 arg(arg='b', annotation=None, type_comment=None),
1353 arg(arg='c', annotation=None, type_comment=None)],
1354 vararg=arg(arg='d', annotation=None, type_comment=None),
1355 kwonlyargs=[
1356 arg(arg='e', annotation=None, type_comment=None),
1357 arg(arg='f', annotation=None, type_comment=None)],
1358 kw_defaults=[
1359 None,
1360 Constant(value=3, kind=None)],
1361 kwarg=arg(arg='g', annotation=None, type_comment=None),
1362 defaults=[
1363 Constant(value=1, kind=None),
1364 Constant(value=2, kind=None)]),
1365 body=[
1366 Pass()],
1367 decorator_list=[
1368 Name(id='decorator1', ctx=Load()),
1369 Name(id='decorator2', ctx=Load())],
1370 returns=Constant(value='return annotation', kind=None),
1371 type_comment=None)],
1372 type_ignores=[])
1373
1374
1375.. class:: Return(value)
1376
1377 A ``return`` statement.
1378
1379 .. doctest::
1380
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001381 >>> print(ast.dump(ast.parse('return 4'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001382 Module(
1383 body=[
1384 Return(
1385 value=Constant(value=4, kind=None))],
1386 type_ignores=[])
1387
1388
1389.. class:: Yield(value)
1390 YieldFrom(value)
1391
1392 A ``yield`` or ``yield from`` expression. Because these are expressions, they
1393 must be wrapped in a :class:`Expr` node if the value sent back is not used.
1394
1395 .. doctest::
1396
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001397 >>> print(ast.dump(ast.parse('yield x'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001398 Module(
1399 body=[
1400 Expr(
1401 value=Yield(
1402 value=Name(id='x', ctx=Load())))],
1403 type_ignores=[])
1404
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001405 >>> print(ast.dump(ast.parse('yield from x'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001406 Module(
1407 body=[
1408 Expr(
1409 value=YieldFrom(
1410 value=Name(id='x', ctx=Load())))],
1411 type_ignores=[])
1412
1413
1414.. class:: Global(names)
1415 Nonlocal(names)
1416
1417 ``global`` and ``nonlocal`` statements. ``names`` is a list of raw strings.
1418
1419 .. doctest::
1420
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001421 >>> print(ast.dump(ast.parse('global x,y,z'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001422 Module(
1423 body=[
1424 Global(
1425 names=[
1426 'x',
1427 'y',
1428 'z'])],
1429 type_ignores=[])
1430
Pablo Galindo02f64cb2020-03-07 18:22:58 +00001431 >>> print(ast.dump(ast.parse('nonlocal x,y,z'), indent=4))
Pablo Galindo114081f2020-03-02 03:14:06 +00001432 Module(
1433 body=[
1434 Nonlocal(
1435 names=[
1436 'x',
1437 'y',
1438 'z'])],
1439 type_ignores=[])
1440
1441
1442.. class:: ClassDef(name, bases, keywords, starargs, kwargs, body, decorator_list)
1443
1444 A class definition.
1445
1446 * ``name`` is a raw string for the class name
1447 * ``bases`` is a list of nodes for explicitly specified base classes.
1448 * ``keywords`` is a list of :class:`keyword` nodes, principally for 'metaclass'.
1449 Other keywords will be passed to the metaclass, as per `PEP-3115
1450 <http://www.python.org/dev/peps/pep-3115/>`_.
1451 * ``starargs`` and ``kwargs`` are each a single node, as in a function call.
1452 starargs will be expanded to join the list of base classes, and kwargs will
1453 be passed to the metaclass.
1454 * ``body`` is a list of nodes representing the code within the class
1455 definition.
1456 * ``decorator_list`` is a list of nodes, as in :class:`FunctionDef`.
1457
1458 .. doctest::
1459
1460 >>> print(ast.dump(ast.parse("""\
1461 ... @decorator1
1462 ... @decorator2
1463 ... class Foo(base1, base2, metaclass=meta):
1464 ... pass
1465 ... """), indent=4))
1466 Module(
1467 body=[
1468 ClassDef(
1469 name='Foo',
1470 bases=[
1471 Name(id='base1', ctx=Load()),
1472 Name(id='base2', ctx=Load())],
1473 keywords=[
1474 keyword(
1475 arg='metaclass',
1476 value=Name(id='meta', ctx=Load()))],
1477 body=[
1478 Pass()],
1479 decorator_list=[
1480 Name(id='decorator1', ctx=Load()),
1481 Name(id='decorator2', ctx=Load())])],
1482 type_ignores=[])
1483
1484Async and await
1485^^^^^^^^^^^^^^^
1486
1487.. class:: AsyncFunctionDef(name, args, body, decorator_list, returns, type_comment)
1488
1489 An ``async def`` function definition. Has the same fields as
1490 :class:`FunctionDef`.
1491
1492
1493.. class:: Await(value)
1494
1495 An ``await`` expression. ``value`` is what it waits for.
1496 Only valid in the body of an :class:`AsyncFunctionDef`.
1497
1498.. doctest::
1499
1500 >>> print(ast.dump(ast.parse("""\
1501 ... async def f():
1502 ... await other_func()
1503 ... """), indent=4))
1504 Module(
1505 body=[
1506 AsyncFunctionDef(
1507 name='f',
1508 args=arguments(
1509 posonlyargs=[],
1510 args=[],
1511 vararg=None,
1512 kwonlyargs=[],
1513 kw_defaults=[],
1514 kwarg=None,
1515 defaults=[]),
1516 body=[
1517 Expr(
1518 value=Await(
1519 value=Call(
1520 func=Name(id='other_func', ctx=Load()),
1521 args=[],
1522 keywords=[])))],
1523 decorator_list=[],
1524 returns=None,
1525 type_comment=None)],
1526 type_ignores=[])
1527
1528
1529.. class:: AsyncFor(target, iter, body, orelse, type_comment)
1530 AsyncWith(items, body, type_comment)
1531
1532 ``async for`` loops and ``async with`` context managers. They have the same
1533 fields as :class:`For` and :class:`With`, respectively. Only valid in the
1534 body of an :class:`AsyncFunctionDef`.
Georg Brandl0c77a822008-06-10 16:37:50 +00001535
1536
1537:mod:`ast` Helpers
1538------------------
1539
Martin Panter2e4571a2015-11-14 01:07:43 +00001540Apart from the node classes, the :mod:`ast` module defines these utility functions
Georg Brandl0c77a822008-06-10 16:37:50 +00001541and classes for traversing abstract syntax trees:
1542
Guido van Rossum10b55c12019-06-11 17:23:12 -07001543.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
Georg Brandl0c77a822008-06-10 16:37:50 +00001544
Terry Reedyfeac6242011-01-24 21:36:03 +00001545 Parse the source into an AST node. Equivalent to ``compile(source,
Benjamin Petersonec9199b2008-11-08 17:05:00 +00001546 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl0c77a822008-06-10 16:37:50 +00001547
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001548 If ``type_comments=True`` is given, the parser is modified to check
1549 and return type comments as specified by :pep:`484` and :pep:`526`.
1550 This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the
1551 flags passed to :func:`compile()`. This will report syntax errors
1552 for misplaced type comments. Without this flag, type comments will
1553 be ignored, and the ``type_comment`` field on selected AST nodes
1554 will always be ``None``. In addition, the locations of ``# type:
1555 ignore`` comments will be returned as the ``type_ignores``
1556 attribute of :class:`Module` (otherwise it is always an empty list).
1557
1558 In addition, if ``mode`` is ``'func_type'``, the input syntax is
1559 modified to correspond to :pep:`484` "signature type comments",
1560 e.g. ``(str, int) -> List[str]``.
1561
Guido van Rossum10b55c12019-06-11 17:23:12 -07001562 Also, setting ``feature_version`` to a tuple ``(major, minor)``
1563 will attempt to parse using that Python version's grammar.
1564 Currently ``major`` must equal to ``3``. For example, setting
1565 ``feature_version=(3, 4)`` will allow the use of ``async`` and
1566 ``await`` as variable names. The lowest supported version is
1567 ``(3, 4)``; the highest is ``sys.version_info[0:2]``.
Guido van Rossum495da292019-03-07 12:38:08 -08001568
Brett Cannon7a7f1002018-03-09 12:03:22 -08001569 .. warning::
1570 It is possible to crash the Python interpreter with a
1571 sufficiently large/complex string due to stack depth limitations
1572 in Python's AST compiler.
1573
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001574 .. versionchanged:: 3.8
Guido van Rossum495da292019-03-07 12:38:08 -08001575 Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001576
Georg Brandl48310cd2009-01-03 21:18:54 +00001577
Pablo Galindo27fc3b62019-11-24 23:02:40 +00001578.. function:: unparse(ast_obj)
1579
1580 Unparse an :class:`ast.AST` object and generate a string with code
1581 that would produce an equivalent :class:`ast.AST` object if parsed
1582 back with :func:`ast.parse`.
1583
1584 .. warning::
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001585 The produced code string will not necessarily be equal to the original
Pablo Galindo27fc3b62019-11-24 23:02:40 +00001586 code that generated the :class:`ast.AST` object.
1587
1588 .. versionadded:: 3.9
1589
1590
Georg Brandl0c77a822008-06-10 16:37:50 +00001591.. function:: literal_eval(node_or_string)
1592
Georg Brandlb9b389e2014-11-05 20:20:28 +01001593 Safely evaluate an expression node or a string containing a Python literal or
1594 container display. The string or node provided may only consist of the
1595 following Python literal structures: strings, bytes, numbers, tuples, lists,
1596 dicts, sets, booleans, and ``None``.
Georg Brandl0c77a822008-06-10 16:37:50 +00001597
Georg Brandlb9b389e2014-11-05 20:20:28 +01001598 This can be used for safely evaluating strings containing Python values from
1599 untrusted sources without the need to parse the values oneself. It is not
1600 capable of evaluating arbitrarily complex expressions, for example involving
1601 operators or indexing.
Georg Brandl0c77a822008-06-10 16:37:50 +00001602
Brett Cannon7a7f1002018-03-09 12:03:22 -08001603 .. warning::
1604 It is possible to crash the Python interpreter with a
1605 sufficiently large/complex string due to stack depth limitations
1606 in Python's AST compiler.
1607
Georg Brandl492f3fc2010-07-11 09:41:21 +00001608 .. versionchanged:: 3.2
Georg Brandl85f21772010-07-13 06:38:10 +00001609 Now allows bytes and set literals.
Georg Brandl492f3fc2010-07-11 09:41:21 +00001610
Raymond Hettinger4fcf5c12020-01-02 22:21:18 -07001611 .. versionchanged:: 3.9
1612 Now supports creating empty sets with ``'set()'``.
1613
Georg Brandl0c77a822008-06-10 16:37:50 +00001614
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001615.. function:: get_docstring(node, clean=True)
Georg Brandl0c77a822008-06-10 16:37:50 +00001616
1617 Return the docstring of the given *node* (which must be a
INADA Naokicb41b272017-02-23 00:31:59 +09001618 :class:`FunctionDef`, :class:`AsyncFunctionDef`, :class:`ClassDef`,
1619 or :class:`Module` node), or ``None`` if it has no docstring.
1620 If *clean* is true, clean up the docstring's indentation with
1621 :func:`inspect.cleandoc`.
1622
1623 .. versionchanged:: 3.5
1624 :class:`AsyncFunctionDef` is now supported.
1625
Georg Brandl0c77a822008-06-10 16:37:50 +00001626
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001627.. function:: get_source_segment(source, node, *, padded=False)
1628
1629 Get source code segment of the *source* that generated *node*.
1630 If some location information (:attr:`lineno`, :attr:`end_lineno`,
1631 :attr:`col_offset`, or :attr:`end_col_offset`) is missing, return ``None``.
1632
1633 If *padded* is ``True``, the first line of a multi-line statement will
1634 be padded with spaces to match its original position.
1635
1636 .. versionadded:: 3.8
1637
1638
Georg Brandl0c77a822008-06-10 16:37:50 +00001639.. function:: fix_missing_locations(node)
1640
1641 When you compile a node tree with :func:`compile`, the compiler expects
1642 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
1643 them. This is rather tedious to fill in for generated nodes, so this helper
1644 adds these attributes recursively where not already set, by setting them to
1645 the values of the parent node. It works recursively starting at *node*.
1646
1647
1648.. function:: increment_lineno(node, n=1)
1649
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001650 Increment the line number and end line number of each node in the tree
1651 starting at *node* by *n*. This is useful to "move code" to a different
1652 location in a file.
Georg Brandl0c77a822008-06-10 16:37:50 +00001653
1654
1655.. function:: copy_location(new_node, old_node)
1656
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001657 Copy source location (:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`,
1658 and :attr:`end_col_offset`) from *old_node* to *new_node* if possible,
1659 and return *new_node*.
Georg Brandl0c77a822008-06-10 16:37:50 +00001660
1661
1662.. function:: iter_fields(node)
1663
1664 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
1665 that is present on *node*.
1666
1667
1668.. function:: iter_child_nodes(node)
1669
1670 Yield all direct child nodes of *node*, that is, all fields that are nodes
1671 and all items of fields that are lists of nodes.
1672
1673
1674.. function:: walk(node)
1675
Georg Brandl619e7ba2011-01-09 07:38:51 +00001676 Recursively yield all descendant nodes in the tree starting at *node*
1677 (including *node* itself), in no specified order. This is useful if you only
1678 want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +00001679
1680
1681.. class:: NodeVisitor()
1682
1683 A node visitor base class that walks the abstract syntax tree and calls a
1684 visitor function for every node found. This function may return a value
Georg Brandl36ab1ef2009-01-03 21:17:04 +00001685 which is forwarded by the :meth:`visit` method.
Georg Brandl0c77a822008-06-10 16:37:50 +00001686
1687 This class is meant to be subclassed, with the subclass adding visitor
1688 methods.
1689
1690 .. method:: visit(node)
1691
1692 Visit a node. The default implementation calls the method called
1693 :samp:`self.visit_{classname}` where *classname* is the name of the node
1694 class, or :meth:`generic_visit` if that method doesn't exist.
1695
1696 .. method:: generic_visit(node)
1697
1698 This visitor calls :meth:`visit` on all children of the node.
Georg Brandl48310cd2009-01-03 21:18:54 +00001699
Georg Brandl0c77a822008-06-10 16:37:50 +00001700 Note that child nodes of nodes that have a custom visitor method won't be
1701 visited unless the visitor calls :meth:`generic_visit` or visits them
1702 itself.
1703
1704 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
1705 during traversal. For this a special visitor exists
1706 (:class:`NodeTransformer`) that allows modifications.
1707
Serhiy Storchakac3ea41e2019-08-26 10:13:19 +03001708 .. deprecated:: 3.8
1709
1710 Methods :meth:`visit_Num`, :meth:`visit_Str`, :meth:`visit_Bytes`,
1711 :meth:`visit_NameConstant` and :meth:`visit_Ellipsis` are deprecated
1712 now and will not be called in future Python versions. Add the
1713 :meth:`visit_Constant` method to handle all constant nodes.
1714
Georg Brandl0c77a822008-06-10 16:37:50 +00001715
1716.. class:: NodeTransformer()
1717
1718 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
1719 allows modification of nodes.
1720
Georg Brandl36ab1ef2009-01-03 21:17:04 +00001721 The :class:`NodeTransformer` will walk the AST and use the return value of
1722 the visitor methods to replace or remove the old node. If the return value
1723 of the visitor method is ``None``, the node will be removed from its
1724 location, otherwise it is replaced with the return value. The return value
1725 may be the original node in which case no replacement takes place.
Georg Brandl0c77a822008-06-10 16:37:50 +00001726
1727 Here is an example transformer that rewrites all occurrences of name lookups
1728 (``foo``) to ``data['foo']``::
1729
1730 class RewriteName(NodeTransformer):
1731
1732 def visit_Name(self, node):
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +03001733 return Subscript(
Georg Brandl0c77a822008-06-10 16:37:50 +00001734 value=Name(id='data', ctx=Load()),
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001735 slice=Index(value=Constant(value=node.id)),
Georg Brandl0c77a822008-06-10 16:37:50 +00001736 ctx=node.ctx
1737 ), node)
1738
1739 Keep in mind that if the node you're operating on has child nodes you must
1740 either transform the child nodes yourself or call the :meth:`generic_visit`
1741 method for the node first.
1742
1743 For nodes that were part of a collection of statements (that applies to all
1744 statement nodes), the visitor may also return a list of nodes rather than
1745 just a single node.
1746
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +03001747 If :class:`NodeTransformer` introduces new nodes (that weren't part of
1748 original tree) without giving them location information (such as
1749 :attr:`lineno`), :func:`fix_missing_locations` should be called with
1750 the new sub-tree to recalculate the location information::
1751
1752 tree = ast.parse('foo', mode='eval')
1753 new_tree = fix_missing_locations(RewriteName().visit(tree))
1754
Georg Brandl0c77a822008-06-10 16:37:50 +00001755 Usually you use the transformer like this::
1756
1757 node = YourTransformer().visit(node)
1758
1759
Serhiy Storchaka850573b2019-09-09 19:33:13 +03001760.. function:: dump(node, annotate_fields=True, include_attributes=False, *, indent=None)
Georg Brandl0c77a822008-06-10 16:37:50 +00001761
1762 Return a formatted dump of the tree in *node*. This is mainly useful for
Serhiy Storchakae64f9482019-08-29 09:30:23 +03001763 debugging purposes. If *annotate_fields* is true (by default),
1764 the returned string will show the names and the values for fields.
1765 If *annotate_fields* is false, the result string will be more compact by
1766 omitting unambiguous field names. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001767 numbers and column offsets are not dumped by default. If this is wanted,
Serhiy Storchakae64f9482019-08-29 09:30:23 +03001768 *include_attributes* can be set to true.
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001769
Serhiy Storchaka850573b2019-09-09 19:33:13 +03001770 If *indent* is a non-negative integer or string, then the tree will be
1771 pretty-printed with that indent level. An indent level
1772 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
1773 selects the single line representation. Using a positive integer indent
1774 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
1775 that string is used to indent each level.
1776
1777 .. versionchanged:: 3.9
1778 Added the *indent* option.
1779
1780
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001781.. _ast-cli:
1782
1783Command-Line Usage
1784------------------
1785
1786.. versionadded:: 3.9
1787
1788The :mod:`ast` module can be executed as a script from the command line.
1789It is as simple as:
1790
1791.. code-block:: sh
1792
1793 python -m ast [-m <mode>] [-a] [infile]
1794
1795The following options are accepted:
1796
1797.. program:: ast
1798
1799.. cmdoption:: -h, --help
1800
1801 Show the help message and exit.
1802
1803.. cmdoption:: -m <mode>
1804 --mode <mode>
1805
1806 Specify what kind of code must be compiled, like the *mode* argument
1807 in :func:`parse`.
1808
Batuhan Taşkaya814d6872019-12-16 21:23:27 +03001809.. cmdoption:: --no-type-comments
1810
1811 Don't parse type comments.
1812
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001813.. cmdoption:: -a, --include-attributes
1814
1815 Include attributes such as line numbers and column offsets.
1816
Batuhan Taşkaya814d6872019-12-16 21:23:27 +03001817.. cmdoption:: -i <indent>
1818 --indent <indent>
1819
1820 Indentation of nodes in AST (number of spaces).
1821
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001822If :file:`infile` is specified its contents are parsed to AST and dumped
1823to stdout. Otherwise, the content is read from stdin.
1824
1825
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001826.. seealso::
1827
Sanyam Khurana338cd832018-01-20 05:55:37 +05301828 `Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external documentation resource, has good
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001829 details on working with Python ASTs.