blob: ea3057867b0f0c925d3ff085de52e069509180de [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 Galindo114081f2020-03-02 03:14:06 +0000142 >>> print(ast.dump(ast.parse("123"), indent=4))
143 Module(
144 body=[
145 Expr(
146 value=Constant(value=123, kind=None))],
147 type_ignores=[])
148
149
150.. class:: FormattedValue(value, conversion, format_spec)
151
152 Node representing a single formatting field in an f-string. If the string
153 contains a single formatting field and nothing else the node can be
154 isolated otherwise it appears in :class:`JoinedStr`.
155
156 * ``value`` is any expression node (such as a literal, a variable, or a
157 function call).
158 * ``conversion`` is an integer:
159
160 * -1: no formatting
161 * 115: ``!s`` string formatting
162 * 114: ``!r`` repr formatting
163 * 97: ``!a`` ascii formatting
164
165 * ``format_spec`` is a :class:`JoinedStr` node representing the formatting
166 of the value, or ``None`` if no format was specified. Both
167 ``conversion`` and ``format_spec`` can be set at the same time.
168
169
170.. class:: JoinedStr(values)
171
172 An f-string, comprising a series of :class:`FormattedValue` and :class:`Constant`
173 nodes.
174
175 .. doctest::
176
177 >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"'), indent=4))
178 Module(
179 body=[
180 Expr(
181 value=JoinedStr(
182 values=[
183 Constant(value='sin(', kind=None),
184 FormattedValue(
185 value=Name(id='a', ctx=Load()),
186 conversion=-1,
187 format_spec=None),
188 Constant(value=') is ', kind=None),
189 FormattedValue(
190 value=Call(
191 func=Name(id='sin', ctx=Load()),
192 args=[
193 Name(id='a', ctx=Load())],
194 keywords=[]),
195 conversion=-1,
196 format_spec=JoinedStr(
197 values=[
198 Constant(value='.3', kind=None)]))]))],
199 type_ignores=[])
200
201
202.. class:: List(elts, ctx)
203 Tuple(elts, ctx)
204
205 A list or tuple. ``elts`` holds a list of nodes representing the elements.
206 ``ctx`` is :class:`Store` if the container is an assignment target (i.e.
207 ``(x,y)=something``), and :class:`Load` otherwise.
208
209 .. doctest::
210
211 >>> print(ast.dump(ast.parse("[1, 2, 3]"), indent=4))
212 Module(
213 body=[
214 Expr(
215 value=List(
216 elts=[
217 Constant(value=1, kind=None),
218 Constant(value=2, kind=None),
219 Constant(value=3, kind=None)],
220 ctx=Load()))],
221 type_ignores=[])
222
223 >>> print(ast.dump(ast.parse("(1, 2, 3)"), indent=4))
224 Module(
225 body=[
226 Expr(
227 value=Tuple(
228 elts=[
229 Constant(value=1, kind=None),
230 Constant(value=2, kind=None),
231 Constant(value=3, kind=None)],
232 ctx=Load()))],
233 type_ignores=[])
234
235
236.. class:: Set(elts)
237
238 A set. ``elts`` holds a list of nodes representing the set's elements.
239
240 .. doctest::
241
242 >>> print(ast.dump(ast.parse("{1, 2, 3}"), indent=4))
243 Module(
244 body=[
245 Expr(
246 value=Set(
247 elts=[
248 Constant(value=1, kind=None),
249 Constant(value=2, kind=None),
250 Constant(value=3, kind=None)]))],
251 type_ignores=[])
252
253
254.. class:: Dict(keys, values)
255
256 A dictionary. ``keys`` and ``values`` hold lists of nodes representing the
257 keys and the values respectively, in matching order (what would be returned
258 when calling :code:`dictionary.keys()` and :code:`dictionary.values()`).
259
260 When doing dictionary unpacking using dictionary literals the expression to be
261 expanded goes in the ``values`` list, with a ``None`` at the corresponding
262 position in ``keys``.
263
264 .. doctest::
265
266 >>> print(ast.dump(ast.parse("{'a':1, **d}"), indent=4))
267 Module(
268 body=[
269 Expr(
270 value=Dict(
271 keys=[
272 Constant(value='a', kind=None),
273 None],
274 values=[
275 Constant(value=1, kind=None),
276 Name(id='d', ctx=Load())]))],
277 type_ignores=[])
278
279
280Variables
281^^^^^^^^^
282
283.. class:: Name(id, ctx)
284
285 A variable name. ``id`` holds the name as a string, and ``ctx`` is one of
286 the following types.
287
288
289.. class:: Load()
290 Store()
291 Del()
292
293 Variable references can be used to load the value of a variable, to assign
294 a new value to it, or to delete it. Variable references are given a context
295 to distinguish these cases.
296
297 .. doctest::
298
299 >>> print(ast.dump(ast.parse('a'), indent=4))
300 Module(
301 body=[
302 Expr(
303 value=Name(id='a', ctx=Load()))],
304 type_ignores=[])
305
306 >>> print(ast.dump(ast.parse('a = 1'), indent=4))
307 Module(
308 body=[
309 Assign(
310 targets=[
311 Name(id='a', ctx=Store())],
312 value=Constant(value=1, kind=None),
313 type_comment=None)],
314 type_ignores=[])
315
316 >>> print(ast.dump(ast.parse('del a'), indent=4))
317 Module(
318 body=[
319 Delete(
320 targets=[
321 Name(id='a', ctx=Del())])],
322 type_ignores=[])
323
324
325.. class:: Starred(value, ctx)
326
327 A ``*var`` variable reference. ``value`` holds the variable, typically a
328 :class:`Name` node. This type must be used when building a :class:`Call`
329 node with ``*args``.
330
331 .. doctest::
332
333 >>> print(ast.dump(ast.parse('a, *b = it'), indent=4))
334 Module(
335 body=[
336 Assign(
337 targets=[
338 Tuple(
339 elts=[
340 Name(id='a', ctx=Store()),
341 Starred(
342 value=Name(id='b', ctx=Store()),
343 ctx=Store())],
344 ctx=Store())],
345 value=Name(id='it', ctx=Load()),
346 type_comment=None)],
347 type_ignores=[])
348
349
350Expressions
351^^^^^^^^^^^
352
353.. class:: Expr(value)
354
355 When an expression, such as a function call, appears as a statement by itself
356 with its return value not used or stored, it is wrapped in this container.
357 ``value`` holds one of the other nodes in this section, a :class:`Constant`, a
358 :class:`Name`, a :class:`Lambda`, a :class:`Yield` or :class:`YieldFrom` node.
359
360 .. doctest::
361
362 >>> print(ast.dump(ast.parse('-a'), indent=4))
363 Module(
364 body=[
365 Expr(
366 value=UnaryOp(
367 op=USub(),
368 operand=Name(id='a', ctx=Load())))],
369 type_ignores=[])
370
371
372.. class:: UnaryOp(op, operand)
373
374 A unary operation. ``op`` is the operator, and ``operand`` any expression
375 node.
376
377
378.. class:: UAdd
379 USub
380 Not
381 Invert
382
383 Unary operator tokens. :class:`Not` is the ``not`` keyword, :class:`Invert`
384 is the ``~`` operator.
385
386 .. doctest::
387
388 >>> print(ast.dump(ast.parse("not x"), indent=4))
389 Module(
390 body=[
391 Expr(
392 value=UnaryOp(
393 op=Not(),
394 operand=Name(id='x', ctx=Load())))],
395 type_ignores=[])
396
397
398.. class:: BinOp(left, op, right)
399
400 A binary operation (like addition or division). ``op`` is the operator, and
401 ``left`` and ``right`` are any expression nodes.
402
403 .. doctest::
404
405 >>> print(ast.dump(ast.parse("x + y"), indent=4))
406 Module(
407 body=[
408 Expr(
409 value=BinOp(
410 left=Name(id='x', ctx=Load()),
411 op=Add(),
412 right=Name(id='y', ctx=Load())))],
413 type_ignores=[])
414
415
416.. class:: Add
417 Sub
418 Mult
419 Div
420 FloorDiv
421 Mod
422 Pow
423 LShift
424 RShift
425 BitOr
426 BitXor
427 BitAnd
428 MatMult
429
430 Binary operator tokens.
431
432
433.. class:: BoolOp(op, values)
434
435 A boolean operation, 'or' or 'and'. ``op`` is :class:`Or` or :class:`And`.
436 ``values`` are the values involved. Consecutive operations with the same
437 operator, such as ``a or b or c``, are collapsed into one node with several
438 values.
439
440 This doesn't include ``not``, which is a :class:`UnaryOp`.
441
442 .. doctest::
443
444 >>> print(ast.dump(ast.parse("x or y"), indent=4))
445 Module(
446 body=[
447 Expr(
448 value=BoolOp(
449 op=Or(),
450 values=[
451 Name(id='x', ctx=Load()),
452 Name(id='y', ctx=Load())]))],
453 type_ignores=[])
454
455
456.. class:: And
457 Or
458
459 Boolean operator tokens.
460
461
462.. class:: Compare(left, ops, comparators)
463
464 A comparison of two or more values. ``left`` is the first value in the
465 comparison, ``ops`` the list of operators, and ``comparators`` the list
466 of values after the first element in the comparison.
467
468 .. doctest::
469
470 >>> print(ast.dump(ast.parse("1 < a < 10"), indent=4))
471 Module(
472 body=[
473 Expr(
474 value=Compare(
475 left=Constant(value=1, kind=None),
476 ops=[
477 Lt(),
478 Lt()],
479 comparators=[
480 Name(id='a', ctx=Load()),
481 Constant(value=10, kind=None)]))],
482 type_ignores=[])
483
484
485.. class:: Eq
486 NotEq
487 Lt
488 LtE
489 Gt
490 GtE
491 Is
492 IsNot
493 In
494 NotIn
495
496 Comparison operator tokens.
497
498
499.. class:: Call(func, args, keywords, starargs, kwargs)
500
501 A function call. ``func`` is the function, which will often be a
502 :class:`Name` or :class:`Attribute` object. Of the arguments:
503
504 * ``args`` holds a list of the arguments passed by position.
505 * ``keywords`` holds a list of :class:`keyword` objects representing
506 arguments passed by keyword.
507
508 When creating a ``Call`` node, ``args`` and ``keywords`` are required, but
509 they can be empty lists. ``starargs`` and ``kwargs`` are optional.
510
511 .. doctest::
512
513 >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)'), indent=4))
514 Module(
515 body=[
516 Expr(
517 value=Call(
518 func=Name(id='func', ctx=Load()),
519 args=[
520 Name(id='a', ctx=Load()),
521 Starred(
522 value=Name(id='d', ctx=Load()),
523 ctx=Load())],
524 keywords=[
525 keyword(
526 arg='b',
527 value=Name(id='c', ctx=Load())),
528 keyword(
529 arg=None,
530 value=Name(id='e', ctx=Load()))]))],
531 type_ignores=[])
532
533
534.. class:: keyword(arg, value)
535
536 A keyword argument to a function call or class definition. ``arg`` is a raw
537 string of the parameter name, ``value`` is a node to pass in.
538
539
540.. class:: IfExp(test, body, orelse)
541
542 An expression such as ``a if b else c``. Each field holds a single node, so
543 in the following example, all three are :class:`Name` nodes.
544
545 .. doctest::
546
547 >>> print(ast.dump(ast.parse("a if b else c"), indent=4))
548 Module(
549 body=[
550 Expr(
551 value=IfExp(
552 test=Name(id='b', ctx=Load()),
553 body=Name(id='a', ctx=Load()),
554 orelse=Name(id='c', ctx=Load())))],
555 type_ignores=[])
556
557
558.. class:: Attribute(value, attr, ctx)
559
560 Attribute access, e.g. ``d.keys``. ``value`` is a node, typically a
561 :class:`Name`. ``attr`` is a bare string giving the name of the attribute,
562 and ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del` according to how
563 the attribute is acted on.
564
565 .. doctest::
566
567 >>> print(ast.dump(ast.parse('snake.colour'), indent=4))
568 Module(
569 body=[
570 Expr(
571 value=Attribute(
572 value=Name(id='snake', ctx=Load()),
573 attr='colour',
574 ctx=Load()))],
575 type_ignores=[])
576
577
578.. class:: NamedExpr(target, value)
579
580 A named expression. This AST node is produced by the assignment expressions
581 operator (also known as the walrus operator). As opposed to the :class:`Assign`
582 node in which the first argument can be multiple nodes, in this case both
583 ``target`` and ``value`` must be single nodes.
584
585 .. doctest::
586
587 >>> print(ast.dump(ast.parse("(x := 4)"), indent=4))
588 Module(
589 body=[
590 Expr(
591 value=NamedExpr(
592 target=Name(id='x', ctx=Store()),
593 value=Constant(value=4, kind=None)))],
594 type_ignores=[])
595
596
597Subscripting
598~~~~~~~~~~~~
599
600.. class:: Subscript(value, slice, ctx)
601
602 A subscript, such as ``l[1]``. ``value`` is the object, often a
603 :class:`Name`. ``slice`` is one of :class:`Index`, :class:`Slice` or
604 :class:`ExtSlice`. ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del`
605 according to the action performed with the subscript.
606
607
608.. class:: Index(value)
609
610 Simple subscripting with a single value
611
612 .. doctest::
613
614 >>> print(ast.dump(ast.parse('l[1]'), indent=4))
615 Module(
616 body=[
617 Expr(
618 value=Subscript(
619 value=Name(id='l', ctx=Load()),
620 slice=Index(
621 value=Constant(value=1, kind=None)),
622 ctx=Load()))],
623 type_ignores=[])
624
625
626.. class:: Slice(lower, upper, step)
627
628 Regular slicing (on the form x:y).
629
630 .. doctest::
631
632 >>> print(ast.dump(ast.parse('l[1:2]'), indent=4))
633 Module(
634 body=[
635 Expr(
636 value=Subscript(
637 value=Name(id='l', ctx=Load()),
638 slice=Slice(
639 lower=Constant(value=1, kind=None),
640 upper=Constant(value=2, kind=None),
641 step=None),
642 ctx=Load()))],
643 type_ignores=[])
644
645
646.. class:: ExtSlice(dims)
647
648 Advanced slicing. ``dims`` holds a list of :class:`Slice` and
649 :class:`Index` nodes
650
651 .. doctest::
652
653 >>> print(ast.dump(ast.parse('l[1:2, 3]'), indent=4))
654 Module(
655 body=[
656 Expr(
657 value=Subscript(
658 value=Name(id='l', ctx=Load()),
659 slice=ExtSlice(
660 dims=[
661 Slice(
662 lower=Constant(value=1, kind=None),
663 upper=Constant(value=2, kind=None),
664 step=None),
665 Index(
666 value=Constant(value=3, kind=None))]),
667 ctx=Load()))],
668 type_ignores=[])
669
670
671Comprehensions
672~~~~~~~~~~~~~~
673
674.. class:: ListComp(elt, generators)
675 SetComp(elt, generators)
676 GeneratorExp(elt, generators)
677 DictComp(key, value, generators)
678
679 List and set comprehensions, generator expressions, and dictionary
680 comprehensions. ``elt`` (or ``key`` and ``value``) is a single node
681 representing the part that will be evaluated for each item.
682
683 ``generators`` is a list of :class:`comprehension` nodes.
684
685 .. doctest::
686
687 >>> print(ast.dump(ast.parse("[x for x in numbers]"), indent=4))
688 Module(
689 body=[
690 Expr(
691 value=ListComp(
692 elt=Name(id='x', ctx=Load()),
693 generators=[
694 comprehension(
695 target=Name(id='x', ctx=Store()),
696 iter=Name(id='numbers', ctx=Load()),
697 ifs=[],
698 is_async=0)]))],
699 type_ignores=[])
700
701 >>> print(ast.dump(ast.parse("{x: x**2 for x in numbers}"), indent=4))
702 Module(
703 body=[
704 Expr(
705 value=DictComp(
706 key=Name(id='x', ctx=Load()),
707 value=BinOp(
708 left=Name(id='x', ctx=Load()),
709 op=Pow(),
710 right=Constant(value=2, kind=None)),
711 generators=[
712 comprehension(
713 target=Name(id='x', ctx=Store()),
714 iter=Name(id='numbers', ctx=Load()),
715 ifs=[],
716 is_async=0)]))],
717 type_ignores=[])
718
719 >>> print(ast.dump(ast.parse("{x for x in numbers}"), indent=4))
720 Module(
721 body=[
722 Expr(
723 value=SetComp(
724 elt=Name(id='x', ctx=Load()),
725 generators=[
726 comprehension(
727 target=Name(id='x', ctx=Store()),
728 iter=Name(id='numbers', ctx=Load()),
729 ifs=[],
730 is_async=0)]))],
731 type_ignores=[])
732
733
734.. class:: comprehension(target, iter, ifs, is_async)
735
736 One ``for`` clause in a comprehension. ``target`` is the reference to use for
737 each element - typically a :class:`Name` or :class:`Tuple` node. ``iter``
738 is the object to iterate over. ``ifs`` is a list of test expressions: each
739 ``for`` clause can have multiple ``ifs``.
740
741 ``is_async`` indicates a comprehension is asynchronous (using an
742 ``async for`` instead of ``for``). The value is an integer (0 or 1).
743
744 .. doctest::
745
746 >>> print(ast.dump(ast.parse("[ord(c) for line in file for c in line]", mode='eval'),
747 ... indent=4)) # Multiple comprehensions in one.
748 Expression(
749 body=ListComp(
750 elt=Call(
751 func=Name(id='ord', ctx=Load()),
752 args=[
753 Name(id='c', ctx=Load())],
754 keywords=[]),
755 generators=[
756 comprehension(
757 target=Name(id='line', ctx=Store()),
758 iter=Name(id='file', ctx=Load()),
759 ifs=[],
760 is_async=0),
761 comprehension(
762 target=Name(id='c', ctx=Store()),
763 iter=Name(id='line', ctx=Load()),
764 ifs=[],
765 is_async=0)]))
766
767 >>> print(ast.dump(ast.parse("(n**2 for n in it if n>5 if n<10)", mode='eval'),
768 ... indent=4)) # generator comprehension
769 Expression(
770 body=GeneratorExp(
771 elt=BinOp(
772 left=Name(id='n', ctx=Load()),
773 op=Pow(),
774 right=Constant(value=2, kind=None)),
775 generators=[
776 comprehension(
777 target=Name(id='n', ctx=Store()),
778 iter=Name(id='it', ctx=Load()),
779 ifs=[
780 Compare(
781 left=Name(id='n', ctx=Load()),
782 ops=[
783 Gt()],
784 comparators=[
785 Constant(value=5, kind=None)]),
786 Compare(
787 left=Name(id='n', ctx=Load()),
788 ops=[
789 Lt()],
790 comparators=[
791 Constant(value=10, kind=None)])],
792 is_async=0)]))
793
794 >>> print(ast.dump(ast.parse("async def f():"
795 ... " return [i async for i in soc]"),
796 ... indent=4)) # Async comprehension
797 Module(
798 body=[
799 AsyncFunctionDef(
800 name='f',
801 args=arguments(
802 posonlyargs=[],
803 args=[],
804 vararg=None,
805 kwonlyargs=[],
806 kw_defaults=[],
807 kwarg=None,
808 defaults=[]),
809 body=[
810 Return(
811 value=ListComp(
812 elt=Name(id='i', ctx=Load()),
813 generators=[
814 comprehension(
815 target=Name(id='i', ctx=Store()),
816 iter=Name(id='soc', ctx=Load()),
817 ifs=[],
818 is_async=1)]))],
819 decorator_list=[],
820 returns=None,
821 type_comment=None)],
822 type_ignores=[])
823
824Statements
825^^^^^^^^^^
826
827.. class:: Assign(targets, value, type_comment)
828
829 An assignment. ``targets`` is a list of nodes, and ``value`` is a single node.
830
831 Multiple nodes in ``targets`` represents assigning the same value to each.
832 Unpacking is represented by putting a :class:`Tuple` or :class:`List`
833 within ``targets``.
834
835 .. attribute:: type_comment
836
837 ``type_comment`` is an optional string with the type annotation as a comment.
838
839 .. doctest::
840
841 >>> print(ast.dump(ast.parse("a = b = 1"), indent=4)) # Multiple assignment
842 Module(
843 body=[
844 Assign(
845 targets=[
846 Name(id='a', ctx=Store()),
847 Name(id='b', ctx=Store())],
848 value=Constant(value=1, kind=None),
849 type_comment=None)],
850 type_ignores=[])
851
852 >>> print(ast.dump(ast.parse("a,b = c"), indent=4)) # Unpacking
853 Module(
854 body=[
855 Assign(
856 targets=[
857 Tuple(
858 elts=[
859 Name(id='a', ctx=Store()),
860 Name(id='b', ctx=Store())],
861 ctx=Store())],
862 value=Name(id='c', ctx=Load()),
863 type_comment=None)],
864 type_ignores=[])
865
866
867.. class:: AnnAssign(target, annotation, value, simple)
868
869 An assignment with a type annotation. ``target`` is a single node and can
870 be a :class:`Name`, a :class:`Attribute` or a :class:`Subscript`.
871 ``annotation`` is the annotation, such as a :class:`Constant` or :class:`Name`
872 node. ``value`` is a single optional node. ``simple`` is a boolean integer
873 set to True for a :class:`Name` node in ``target`` that do not appear in
874 between parenthesis and are hence pure names and not expressions.
875
876 .. doctest::
877
878 >>> print(ast.dump(ast.parse("c: int"), indent=4))
879 Module(
880 body=[
881 AnnAssign(
882 target=Name(id='c', ctx=Store()),
883 annotation=Name(id='int', ctx=Load()),
884 value=None,
885 simple=1)],
886 type_ignores=[])
887
888 >>> print(ast.dump(ast.parse("(a): int = 1"), indent=4)) # Annotation with parenthesis
889 Module(
890 body=[
891 AnnAssign(
892 target=Name(id='a', ctx=Store()),
893 annotation=Name(id='int', ctx=Load()),
894 value=Constant(value=1, kind=None),
895 simple=0)],
896 type_ignores=[])
897
898 >>> print(ast.dump(ast.parse("a.b: int"), indent=4)) # Attribute annotation
899 Module(
900 body=[
901 AnnAssign(
902 target=Attribute(
903 value=Name(id='a', ctx=Load()),
904 attr='b',
905 ctx=Store()),
906 annotation=Name(id='int', ctx=Load()),
907 value=None,
908 simple=0)],
909 type_ignores=[])
910
911 >>> print(ast.dump(ast.parse("a[1]: int"), indent=4)) # Subscript annotation
912 Module(
913 body=[
914 AnnAssign(
915 target=Subscript(
916 value=Name(id='a', ctx=Load()),
917 slice=Index(
918 value=Constant(value=1, kind=None)),
919 ctx=Store()),
920 annotation=Name(id='int', ctx=Load()),
921 value=None,
922 simple=0)],
923 type_ignores=[])
924
925
926.. class:: AugAssign(target, op, value)
927
928 Augmented assignment, such as ``a += 1``. In the following example,
929 ``target`` is a :class:`Name` node for ``x`` (with the :class:`Store`
930 context), ``op`` is :class:`Add`, and ``value`` is a :class:`Constant` with
931 value for 1.
932
933 The ``target`` attribute connot be of class :class:`Tuple` or :class:`List`,
934 unlike the targets of :class:`Assign`.
935
936 .. doctest::
937
938 >>> print(ast.dump(ast.parse("x += 2"), indent=4))
939 Module(
940 body=[
941 AugAssign(
942 target=Name(id='x', ctx=Store()),
943 op=Add(),
944 value=Constant(value=2, kind=None))],
945 type_ignores=[])
946
947
948.. class:: Raise(exc, cause)
949
950 A ``raise`` statement. ``exc`` is the exception object to be raised, normally a
951 :class:`Call` or :class:`Name`, or ``None`` for a standalone ``raise``.
952 ``cause`` is the optional part for ``y`` in ``raise x from y``.
953
954 .. doctest::
955
956 >>> print(ast.dump(ast.parse("raise x from y"), indent=4))
957 Module(
958 body=[
959 Raise(
960 exc=Name(id='x', ctx=Load()),
961 cause=Name(id='y', ctx=Load()))],
962 type_ignores=[])
963
964
965.. class:: Assert(test, msg)
966
967 An assertion. ``test`` holds the condition, such as a :class:`Compare` node.
968 ``msg`` holds the failure message.
969
970 .. doctest::
971
972 >>> print(ast.dump(ast.parse("assert x,y"), indent=4))
973 Module(
974 body=[
975 Assert(
976 test=Name(id='x', ctx=Load()),
977 msg=Name(id='y', ctx=Load()))],
978 type_ignores=[])
979
980
981.. class:: Delete(targets)
982
983 Represents a ``del`` statement. ``targets`` is a list of nodes, such as
984 :class:`Name`, :class:`Attribute` or :class:`Subscript` nodes.
985
986 .. doctest::
987
988 >>> print(ast.dump(ast.parse("del x,y,z"), indent=4))
989 Module(
990 body=[
991 Delete(
992 targets=[
993 Name(id='x', ctx=Del()),
994 Name(id='y', ctx=Del()),
995 Name(id='z', ctx=Del())])],
996 type_ignores=[])
997
998
999.. class:: Pass()
1000
1001 A ``pass`` statement.
1002
1003 .. doctest::
1004
1005 >>> print(ast.dump(ast.parse("pass"), indent=4))
1006 Module(
1007 body=[
1008 Pass()],
1009 type_ignores=[])
1010
1011
1012Other statements which are only applicable inside functions or loops are
1013described in other sections.
1014
1015Imports
1016~~~~~~~
1017
1018.. class:: Import(names)
1019
1020 An import statement. ``names`` is a list of :class:`alias` nodes.
1021
1022 .. doctest::
1023
1024 >>> print(ast.dump(ast.parse("import x,y,z"), indent=4))
1025 Module(
1026 body=[
1027 Import(
1028 names=[
1029 alias(name='x', asname=None),
1030 alias(name='y', asname=None),
1031 alias(name='z', asname=None)])],
1032 type_ignores=[])
1033
1034
1035.. class:: ImportFrom(module, names, level)
1036
1037 Represents ``from x import y``. ``module`` is a raw string of the 'from' name,
1038 without any leading dots, or ``None`` for statements such as ``from . import foo``.
1039 ``level`` is an integer holding the level of the relative import (0 means
1040 absolute import).
1041
1042 .. doctest::
1043
1044 >>> print(ast.dump(ast.parse("from y import x,y,z"), indent=4))
1045 Module(
1046 body=[
1047 ImportFrom(
1048 module='y',
1049 names=[
1050 alias(name='x', asname=None),
1051 alias(name='y', asname=None),
1052 alias(name='z', asname=None)],
1053 level=0)],
1054 type_ignores=[])
1055
1056
1057.. class:: alias(name, asname)
1058
1059 Both parameters are raw strings of the names. ``asname`` can be ``None`` if
1060 the regular name is to be used.
1061
1062 .. doctest::
1063
1064 >>> print(ast.dump(ast.parse("from ..foo.bar import a as b, c"), indent=4))
1065 Module(
1066 body=[
1067 ImportFrom(
1068 module='foo.bar',
1069 names=[
1070 alias(name='a', asname='b'),
1071 alias(name='c', asname=None)],
1072 level=2)],
1073 type_ignores=[])
1074
1075Control flow
1076^^^^^^^^^^^^
1077
1078.. note::
1079 Optional clauses such as ``else`` are stored as an empty list if they're
1080 not present.
1081
1082.. class:: If(test, body, orelse)
1083
1084 An ``if`` statement. ``test`` holds a single node, such as a :class:`Compare`
1085 node. ``body`` and ``orelse`` each hold a list of nodes.
1086
1087 ``elif`` clauses don't have a special representation in the AST, but rather
1088 appear as extra :class:`If` nodes within the ``orelse`` section of the
1089 previous one.
1090
1091 .. doctest::
1092
1093 >>> print(ast.dump(ast.parse("""
1094 ... if x:
1095 ... ...
1096 ... elif y:
1097 ... ...
1098 ... else:
1099 ... ...
1100 ... """), indent=4))
1101 Module(
1102 body=[
1103 If(
1104 test=Name(id='x', ctx=Load()),
1105 body=[
1106 Expr(
1107 value=Constant(value=Ellipsis, kind=None))],
1108 orelse=[
1109 If(
1110 test=Name(id='y', ctx=Load()),
1111 body=[
1112 Expr(
1113 value=Constant(value=Ellipsis, kind=None))],
1114 orelse=[
1115 Expr(
1116 value=Constant(value=Ellipsis, kind=None))])])],
1117 type_ignores=[])
1118
1119
1120.. class:: For(target, iter, body, orelse, type_comment)
1121
1122 A ``for`` loop. ``target`` holds the variable(s) the loop assigns to, as a
1123 single :class:`Name`, :class:`Tuple` or :class:`List` node. ``iter`` holds
1124 the item to be looped over, again as a single node. ``body`` and ``orelse``
1125 contain lists of nodes to execute. Those in ``orelse`` are executed if the
1126 loop finishes normally, rather than via a ``break`` statement.
1127
1128 .. attribute:: type_comment
1129
1130 ``type_comment`` is an optional string with the type annotation as a comment.
1131
1132 .. doctest::
1133
1134 >>> print(ast.dump(ast.parse("""
1135 ... for x in y:
1136 ... ...
1137 ... else:
1138 ... ...
1139 ... """), indent=4))
1140 Module(
1141 body=[
1142 For(
1143 target=Name(id='x', ctx=Store()),
1144 iter=Name(id='y', ctx=Load()),
1145 body=[
1146 Expr(
1147 value=Constant(value=Ellipsis, kind=None))],
1148 orelse=[
1149 Expr(
1150 value=Constant(value=Ellipsis, kind=None))],
1151 type_comment=None)],
1152 type_ignores=[])
1153
1154
1155.. class:: While(test, body, orelse)
1156
1157 A ``while`` loop. ``test`` holds the condition, such as a :class:`Compare`
1158 node.
1159
1160 .. doctest::
1161
1162 >> print(ast.dump(ast.parse("""
1163 ... while x:
1164 ... ...
1165 ... else:
1166 ... ...
1167 ... """), indent=4))
1168 Module(
1169 body=[
1170 While(
1171 test=Name(id='x', ctx=Load()),
1172 body=[
1173 Expr(
1174 value=Constant(value=Ellipsis, kind=None))],
1175 orelse=[
1176 Expr(
1177 value=Constant(value=Ellipsis, kind=None))])],
1178 type_ignores=[])
1179
1180
1181.. class:: Break
1182 Continue
1183
1184 The ``break`` and ``continue`` statements.
1185
1186 .. doctest::
1187
1188 >>> print(ast.dump(ast.parse("""\
1189 ... for a in b:
1190 ... if a > 5:
1191 ... break
1192 ... else:
1193 ... continue
1194 ...
1195 ... """), indent=4))
1196 Module(
1197 body=[
1198 For(
1199 target=Name(id='a', ctx=Store()),
1200 iter=Name(id='b', ctx=Load()),
1201 body=[
1202 If(
1203 test=Compare(
1204 left=Name(id='a', ctx=Load()),
1205 ops=[
1206 Gt()],
1207 comparators=[
1208 Constant(value=5, kind=None)]),
1209 body=[
1210 Break()],
1211 orelse=[
1212 Continue()])],
1213 orelse=[],
1214 type_comment=None)],
1215 type_ignores=[])
1216
1217
1218.. class:: Try(body, handlers, orelse, finalbody)
1219
1220 ``try`` blocks. All attributes are list of nodes to execute, except for
1221 ``handlers``, which is a list of :class:`ExceptHandler` nodes.
1222
1223 .. doctest::
1224
1225 >>> print(ast.dump(ast.parse("""
1226 ... try:
1227 ... ...
1228 ... except Exception:
1229 ... ...
1230 ... except OtherException as e:
1231 ... ...
1232 ... else:
1233 ... ...
1234 ... finally:
1235 ... ...
1236 ... """), indent=4))
1237 Module(
1238 body=[
1239 Try(
1240 body=[
1241 Expr(
1242 value=Constant(value=Ellipsis, kind=None))],
1243 handlers=[
1244 ExceptHandler(
1245 type=Name(id='Exception', ctx=Load()),
1246 name=None,
1247 body=[
1248 Expr(
1249 value=Constant(value=Ellipsis, kind=None))]),
1250 ExceptHandler(
1251 type=Name(id='OtherException', ctx=Load()),
1252 name='e',
1253 body=[
1254 Expr(
1255 value=Constant(value=Ellipsis, kind=None))])],
1256 orelse=[
1257 Expr(
1258 value=Constant(value=Ellipsis, kind=None))],
1259 finalbody=[
1260 Expr(
1261 value=Constant(value=Ellipsis, kind=None))])],
1262 type_ignores=[])
1263
1264
1265.. class:: ExceptHandler(type, name, body)
1266
1267 A single ``except`` clause. ``type`` is the exception type it will match,
1268 typically a :class:`Name` node (or ``None`` for a catch-all ``except:`` clause).
1269 ``name`` is a raw string for the name to hold the exception, or ``None`` if
1270 the clause doesn't have ``as foo``. ``body`` is a list of nodes.
1271
1272 .. doctest::
1273
1274 >>> print(ast.dump(ast.parse("""\
1275 ... try:
1276 ... a + 1
1277 ... except TypeError:
1278 ... pass
1279 ... """), indent=4))
1280 Module(
1281 body=[
1282 Try(
1283 body=[
1284 Expr(
1285 value=BinOp(
1286 left=Name(id='a', ctx=Load()),
1287 op=Add(),
1288 right=Constant(value=1, kind=None)))],
1289 handlers=[
1290 ExceptHandler(
1291 type=Name(id='TypeError', ctx=Load()),
1292 name=None,
1293 body=[
1294 Pass()])],
1295 orelse=[],
1296 finalbody=[])],
1297 type_ignores=[])
1298
1299
1300.. class:: With(items, body, type_comment)
1301
1302 A ``with`` block. ``items`` is a list of :class:`withitem` nodes representing
1303 the context managers, and ``body`` is the indented block inside the context.
1304
1305 .. attribute:: type_comment
1306
1307 ``type_comment`` is an optional string with the type annotation as a comment.
1308
1309
1310.. class:: withitem(context_expr, optional_vars)
1311
1312 A single context manager in a ``with`` block. ``context_expr`` is the context
1313 manager, often a :class:`Call` node. ``optional_vars`` is a :class:`Name`,
1314 :class:`Tuple` or :class:`List` for the ``as foo`` part, or ``None`` if that
1315 isn't used.
1316
1317 .. doctest::
1318
1319 >>> print(ast.dump(ast.parse("""\
1320 ... with a as b, c as d:
1321 ... something(b, d)
1322 ... """), indent=4))
1323 Module(
1324 body=[
1325 With(
1326 items=[
1327 withitem(
1328 context_expr=Name(id='a', ctx=Load()),
1329 optional_vars=Name(id='b', ctx=Store())),
1330 withitem(
1331 context_expr=Name(id='c', ctx=Load()),
1332 optional_vars=Name(id='d', ctx=Store()))],
1333 body=[
1334 Expr(
1335 value=Call(
1336 func=Name(id='something', ctx=Load()),
1337 args=[
1338 Name(id='b', ctx=Load()),
1339 Name(id='d', ctx=Load())],
1340 keywords=[]))],
1341 type_comment=None)],
1342 type_ignores=[])
1343
1344
1345Function and class definitions
1346^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1347
1348.. class:: FunctionDef(name, args, body, decorator_list, returns, type_comment)
1349
1350 A function definition.
1351
1352 * ``name`` is a raw string of the function name.
1353 * ``args`` is a :class:`arguments` node.
1354 * ``body`` is the list of nodes inside the function.
1355 * ``decorator_list`` is the list of decorators to be applied, stored outermost
1356 first (i.e. the first in the list will be applied last).
1357 * ``returns`` is the return annotation.
1358
1359 .. attribute:: type_comment
1360
1361 ``type_comment`` is an optional string with the type annotation as a comment.
1362
1363
1364.. class:: Lambda(args, body)
1365
1366 ``lambda`` is a minimal function definition that can be used inside an
1367 expression. Unlike :class:`FunctionDef`, ``body`` holds a single node.
1368
1369 .. doctest::
1370
1371 >>> print(ast.dump(ast.parse("lambda x,y: ..."), indent=4))
1372 Module(
1373 body=[
1374 Expr(
1375 value=Lambda(
1376 args=arguments(
1377 posonlyargs=[],
1378 args=[
1379 arg(arg='x', annotation=None, type_comment=None),
1380 arg(arg='y', annotation=None, type_comment=None)],
1381 vararg=None,
1382 kwonlyargs=[],
1383 kw_defaults=[],
1384 kwarg=None,
1385 defaults=[]),
1386 body=Constant(value=Ellipsis, kind=None)))],
1387 type_ignores=[])
1388
1389
1390.. class:: arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults)
1391
1392 The arguments for a function.
1393
1394 * ``posonlyargs``, ``args`` and ``kwonlyargs`` are lists of :class:`arg` nodes.
1395 * ``vararg`` and ``kwarg`` are single :class:`arg` nodes, referring to the
1396 ``*args, **kwargs`` parameters.
1397 * ``kw_defaults`` is a list of default values for keyword-only arguments. If
1398 one is ``None``, the corresponding argument is required.
1399 * ``defaults`` is a list of default values for arguments that can be passed
1400 positionally. If there are fewer defaults, they correspond to the last n
1401 arguments.
1402
1403
1404.. class:: arg(arg, annotation, type_comment)
1405
1406 A single argument in a list. ``arg`` is a raw string of the argument
1407 name, ``annotation`` is its annotation, such as a :class:`Str` or
1408 :class:`Name` node.
1409
1410 .. attribute:: type_comment
1411
1412 ``type_comment`` is an optional string with the type annotation as a comment
1413
1414 .. doctest::
1415
1416 >>> print(ast.dump(ast.parse("""\
1417 ... @decorator1
1418 ... @decorator2
1419 ... def f(a: 'annotation', b=1, c=2, *d, e, f=3, **g) -> 'return annotation':
1420 ... pass
1421 ... """), indent=4))
1422 Module(
1423 body=[
1424 FunctionDef(
1425 name='f',
1426 args=arguments(
1427 posonlyargs=[],
1428 args=[
1429 arg(
1430 arg='a',
1431 annotation=Constant(value='annotation', kind=None),
1432 type_comment=None),
1433 arg(arg='b', annotation=None, type_comment=None),
1434 arg(arg='c', annotation=None, type_comment=None)],
1435 vararg=arg(arg='d', annotation=None, type_comment=None),
1436 kwonlyargs=[
1437 arg(arg='e', annotation=None, type_comment=None),
1438 arg(arg='f', annotation=None, type_comment=None)],
1439 kw_defaults=[
1440 None,
1441 Constant(value=3, kind=None)],
1442 kwarg=arg(arg='g', annotation=None, type_comment=None),
1443 defaults=[
1444 Constant(value=1, kind=None),
1445 Constant(value=2, kind=None)]),
1446 body=[
1447 Pass()],
1448 decorator_list=[
1449 Name(id='decorator1', ctx=Load()),
1450 Name(id='decorator2', ctx=Load())],
1451 returns=Constant(value='return annotation', kind=None),
1452 type_comment=None)],
1453 type_ignores=[])
1454
1455
1456.. class:: Return(value)
1457
1458 A ``return`` statement.
1459
1460 .. doctest::
1461
1462 >>> print(ast.dump(ast.parse("return 4"), indent=4))
1463 Module(
1464 body=[
1465 Return(
1466 value=Constant(value=4, kind=None))],
1467 type_ignores=[])
1468
1469
1470.. class:: Yield(value)
1471 YieldFrom(value)
1472
1473 A ``yield`` or ``yield from`` expression. Because these are expressions, they
1474 must be wrapped in a :class:`Expr` node if the value sent back is not used.
1475
1476 .. doctest::
1477
1478 >>> print(ast.dump(ast.parse("yield x"), indent=4))
1479 Module(
1480 body=[
1481 Expr(
1482 value=Yield(
1483 value=Name(id='x', ctx=Load())))],
1484 type_ignores=[])
1485
1486 >>> print(ast.dump(ast.parse("yield from x"), indent=4))
1487 Module(
1488 body=[
1489 Expr(
1490 value=YieldFrom(
1491 value=Name(id='x', ctx=Load())))],
1492 type_ignores=[])
1493
1494
1495.. class:: Global(names)
1496 Nonlocal(names)
1497
1498 ``global`` and ``nonlocal`` statements. ``names`` is a list of raw strings.
1499
1500 .. doctest::
1501
1502 >>> print(ast.dump(ast.parse("global x,y,z"), indent=4))
1503 Module(
1504 body=[
1505 Global(
1506 names=[
1507 'x',
1508 'y',
1509 'z'])],
1510 type_ignores=[])
1511
1512 >>> print(ast.dump(ast.parse("nonlocal x,y,z"), indent=4))
1513 Module(
1514 body=[
1515 Nonlocal(
1516 names=[
1517 'x',
1518 'y',
1519 'z'])],
1520 type_ignores=[])
1521
1522
1523.. class:: ClassDef(name, bases, keywords, starargs, kwargs, body, decorator_list)
1524
1525 A class definition.
1526
1527 * ``name`` is a raw string for the class name
1528 * ``bases`` is a list of nodes for explicitly specified base classes.
1529 * ``keywords`` is a list of :class:`keyword` nodes, principally for 'metaclass'.
1530 Other keywords will be passed to the metaclass, as per `PEP-3115
1531 <http://www.python.org/dev/peps/pep-3115/>`_.
1532 * ``starargs`` and ``kwargs`` are each a single node, as in a function call.
1533 starargs will be expanded to join the list of base classes, and kwargs will
1534 be passed to the metaclass.
1535 * ``body`` is a list of nodes representing the code within the class
1536 definition.
1537 * ``decorator_list`` is a list of nodes, as in :class:`FunctionDef`.
1538
1539 .. doctest::
1540
1541 >>> print(ast.dump(ast.parse("""\
1542 ... @decorator1
1543 ... @decorator2
1544 ... class Foo(base1, base2, metaclass=meta):
1545 ... pass
1546 ... """), indent=4))
1547 Module(
1548 body=[
1549 ClassDef(
1550 name='Foo',
1551 bases=[
1552 Name(id='base1', ctx=Load()),
1553 Name(id='base2', ctx=Load())],
1554 keywords=[
1555 keyword(
1556 arg='metaclass',
1557 value=Name(id='meta', ctx=Load()))],
1558 body=[
1559 Pass()],
1560 decorator_list=[
1561 Name(id='decorator1', ctx=Load()),
1562 Name(id='decorator2', ctx=Load())])],
1563 type_ignores=[])
1564
1565Async and await
1566^^^^^^^^^^^^^^^
1567
1568.. class:: AsyncFunctionDef(name, args, body, decorator_list, returns, type_comment)
1569
1570 An ``async def`` function definition. Has the same fields as
1571 :class:`FunctionDef`.
1572
1573
1574.. class:: Await(value)
1575
1576 An ``await`` expression. ``value`` is what it waits for.
1577 Only valid in the body of an :class:`AsyncFunctionDef`.
1578
1579.. doctest::
1580
1581 >>> print(ast.dump(ast.parse("""\
1582 ... async def f():
1583 ... await other_func()
1584 ... """), indent=4))
1585 Module(
1586 body=[
1587 AsyncFunctionDef(
1588 name='f',
1589 args=arguments(
1590 posonlyargs=[],
1591 args=[],
1592 vararg=None,
1593 kwonlyargs=[],
1594 kw_defaults=[],
1595 kwarg=None,
1596 defaults=[]),
1597 body=[
1598 Expr(
1599 value=Await(
1600 value=Call(
1601 func=Name(id='other_func', ctx=Load()),
1602 args=[],
1603 keywords=[])))],
1604 decorator_list=[],
1605 returns=None,
1606 type_comment=None)],
1607 type_ignores=[])
1608
1609
1610.. class:: AsyncFor(target, iter, body, orelse, type_comment)
1611 AsyncWith(items, body, type_comment)
1612
1613 ``async for`` loops and ``async with`` context managers. They have the same
1614 fields as :class:`For` and :class:`With`, respectively. Only valid in the
1615 body of an :class:`AsyncFunctionDef`.
Georg Brandl0c77a822008-06-10 16:37:50 +00001616
1617
1618:mod:`ast` Helpers
1619------------------
1620
Martin Panter2e4571a2015-11-14 01:07:43 +00001621Apart from the node classes, the :mod:`ast` module defines these utility functions
Georg Brandl0c77a822008-06-10 16:37:50 +00001622and classes for traversing abstract syntax trees:
1623
Guido van Rossum10b55c12019-06-11 17:23:12 -07001624.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
Georg Brandl0c77a822008-06-10 16:37:50 +00001625
Terry Reedyfeac6242011-01-24 21:36:03 +00001626 Parse the source into an AST node. Equivalent to ``compile(source,
Benjamin Petersonec9199b2008-11-08 17:05:00 +00001627 filename, mode, ast.PyCF_ONLY_AST)``.
Georg Brandl0c77a822008-06-10 16:37:50 +00001628
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001629 If ``type_comments=True`` is given, the parser is modified to check
1630 and return type comments as specified by :pep:`484` and :pep:`526`.
1631 This is equivalent to adding :data:`ast.PyCF_TYPE_COMMENTS` to the
1632 flags passed to :func:`compile()`. This will report syntax errors
1633 for misplaced type comments. Without this flag, type comments will
1634 be ignored, and the ``type_comment`` field on selected AST nodes
1635 will always be ``None``. In addition, the locations of ``# type:
1636 ignore`` comments will be returned as the ``type_ignores``
1637 attribute of :class:`Module` (otherwise it is always an empty list).
1638
1639 In addition, if ``mode`` is ``'func_type'``, the input syntax is
1640 modified to correspond to :pep:`484` "signature type comments",
1641 e.g. ``(str, int) -> List[str]``.
1642
Guido van Rossum10b55c12019-06-11 17:23:12 -07001643 Also, setting ``feature_version`` to a tuple ``(major, minor)``
1644 will attempt to parse using that Python version's grammar.
1645 Currently ``major`` must equal to ``3``. For example, setting
1646 ``feature_version=(3, 4)`` will allow the use of ``async`` and
1647 ``await`` as variable names. The lowest supported version is
1648 ``(3, 4)``; the highest is ``sys.version_info[0:2]``.
Guido van Rossum495da292019-03-07 12:38:08 -08001649
Brett Cannon7a7f1002018-03-09 12:03:22 -08001650 .. warning::
1651 It is possible to crash the Python interpreter with a
1652 sufficiently large/complex string due to stack depth limitations
1653 in Python's AST compiler.
1654
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001655 .. versionchanged:: 3.8
Guido van Rossum495da292019-03-07 12:38:08 -08001656 Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001657
Georg Brandl48310cd2009-01-03 21:18:54 +00001658
Pablo Galindo27fc3b62019-11-24 23:02:40 +00001659.. function:: unparse(ast_obj)
1660
1661 Unparse an :class:`ast.AST` object and generate a string with code
1662 that would produce an equivalent :class:`ast.AST` object if parsed
1663 back with :func:`ast.parse`.
1664
1665 .. warning::
Gurupad Hegde6c7bb382019-12-28 17:16:02 -05001666 The produced code string will not necessarily be equal to the original
Pablo Galindo27fc3b62019-11-24 23:02:40 +00001667 code that generated the :class:`ast.AST` object.
1668
1669 .. versionadded:: 3.9
1670
1671
Georg Brandl0c77a822008-06-10 16:37:50 +00001672.. function:: literal_eval(node_or_string)
1673
Georg Brandlb9b389e2014-11-05 20:20:28 +01001674 Safely evaluate an expression node or a string containing a Python literal or
1675 container display. The string or node provided may only consist of the
1676 following Python literal structures: strings, bytes, numbers, tuples, lists,
1677 dicts, sets, booleans, and ``None``.
Georg Brandl0c77a822008-06-10 16:37:50 +00001678
Georg Brandlb9b389e2014-11-05 20:20:28 +01001679 This can be used for safely evaluating strings containing Python values from
1680 untrusted sources without the need to parse the values oneself. It is not
1681 capable of evaluating arbitrarily complex expressions, for example involving
1682 operators or indexing.
Georg Brandl0c77a822008-06-10 16:37:50 +00001683
Brett Cannon7a7f1002018-03-09 12:03:22 -08001684 .. warning::
1685 It is possible to crash the Python interpreter with a
1686 sufficiently large/complex string due to stack depth limitations
1687 in Python's AST compiler.
1688
Georg Brandl492f3fc2010-07-11 09:41:21 +00001689 .. versionchanged:: 3.2
Georg Brandl85f21772010-07-13 06:38:10 +00001690 Now allows bytes and set literals.
Georg Brandl492f3fc2010-07-11 09:41:21 +00001691
Raymond Hettinger4fcf5c12020-01-02 22:21:18 -07001692 .. versionchanged:: 3.9
1693 Now supports creating empty sets with ``'set()'``.
1694
Georg Brandl0c77a822008-06-10 16:37:50 +00001695
Amaury Forgeot d'Arcfdfe62d2008-06-17 20:36:03 +00001696.. function:: get_docstring(node, clean=True)
Georg Brandl0c77a822008-06-10 16:37:50 +00001697
1698 Return the docstring of the given *node* (which must be a
INADA Naokicb41b272017-02-23 00:31:59 +09001699 :class:`FunctionDef`, :class:`AsyncFunctionDef`, :class:`ClassDef`,
1700 or :class:`Module` node), or ``None`` if it has no docstring.
1701 If *clean* is true, clean up the docstring's indentation with
1702 :func:`inspect.cleandoc`.
1703
1704 .. versionchanged:: 3.5
1705 :class:`AsyncFunctionDef` is now supported.
1706
Georg Brandl0c77a822008-06-10 16:37:50 +00001707
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001708.. function:: get_source_segment(source, node, *, padded=False)
1709
1710 Get source code segment of the *source* that generated *node*.
1711 If some location information (:attr:`lineno`, :attr:`end_lineno`,
1712 :attr:`col_offset`, or :attr:`end_col_offset`) is missing, return ``None``.
1713
1714 If *padded* is ``True``, the first line of a multi-line statement will
1715 be padded with spaces to match its original position.
1716
1717 .. versionadded:: 3.8
1718
1719
Georg Brandl0c77a822008-06-10 16:37:50 +00001720.. function:: fix_missing_locations(node)
1721
1722 When you compile a node tree with :func:`compile`, the compiler expects
1723 :attr:`lineno` and :attr:`col_offset` attributes for every node that supports
1724 them. This is rather tedious to fill in for generated nodes, so this helper
1725 adds these attributes recursively where not already set, by setting them to
1726 the values of the parent node. It works recursively starting at *node*.
1727
1728
1729.. function:: increment_lineno(node, n=1)
1730
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001731 Increment the line number and end line number of each node in the tree
1732 starting at *node* by *n*. This is useful to "move code" to a different
1733 location in a file.
Georg Brandl0c77a822008-06-10 16:37:50 +00001734
1735
1736.. function:: copy_location(new_node, old_node)
1737
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001738 Copy source location (:attr:`lineno`, :attr:`col_offset`, :attr:`end_lineno`,
1739 and :attr:`end_col_offset`) from *old_node* to *new_node* if possible,
1740 and return *new_node*.
Georg Brandl0c77a822008-06-10 16:37:50 +00001741
1742
1743.. function:: iter_fields(node)
1744
1745 Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
1746 that is present on *node*.
1747
1748
1749.. function:: iter_child_nodes(node)
1750
1751 Yield all direct child nodes of *node*, that is, all fields that are nodes
1752 and all items of fields that are lists of nodes.
1753
1754
1755.. function:: walk(node)
1756
Georg Brandl619e7ba2011-01-09 07:38:51 +00001757 Recursively yield all descendant nodes in the tree starting at *node*
1758 (including *node* itself), in no specified order. This is useful if you only
1759 want to modify nodes in place and don't care about the context.
Georg Brandl0c77a822008-06-10 16:37:50 +00001760
1761
1762.. class:: NodeVisitor()
1763
1764 A node visitor base class that walks the abstract syntax tree and calls a
1765 visitor function for every node found. This function may return a value
Georg Brandl36ab1ef2009-01-03 21:17:04 +00001766 which is forwarded by the :meth:`visit` method.
Georg Brandl0c77a822008-06-10 16:37:50 +00001767
1768 This class is meant to be subclassed, with the subclass adding visitor
1769 methods.
1770
1771 .. method:: visit(node)
1772
1773 Visit a node. The default implementation calls the method called
1774 :samp:`self.visit_{classname}` where *classname* is the name of the node
1775 class, or :meth:`generic_visit` if that method doesn't exist.
1776
1777 .. method:: generic_visit(node)
1778
1779 This visitor calls :meth:`visit` on all children of the node.
Georg Brandl48310cd2009-01-03 21:18:54 +00001780
Georg Brandl0c77a822008-06-10 16:37:50 +00001781 Note that child nodes of nodes that have a custom visitor method won't be
1782 visited unless the visitor calls :meth:`generic_visit` or visits them
1783 itself.
1784
1785 Don't use the :class:`NodeVisitor` if you want to apply changes to nodes
1786 during traversal. For this a special visitor exists
1787 (:class:`NodeTransformer`) that allows modifications.
1788
Serhiy Storchakac3ea41e2019-08-26 10:13:19 +03001789 .. deprecated:: 3.8
1790
1791 Methods :meth:`visit_Num`, :meth:`visit_Str`, :meth:`visit_Bytes`,
1792 :meth:`visit_NameConstant` and :meth:`visit_Ellipsis` are deprecated
1793 now and will not be called in future Python versions. Add the
1794 :meth:`visit_Constant` method to handle all constant nodes.
1795
Georg Brandl0c77a822008-06-10 16:37:50 +00001796
1797.. class:: NodeTransformer()
1798
1799 A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
1800 allows modification of nodes.
1801
Georg Brandl36ab1ef2009-01-03 21:17:04 +00001802 The :class:`NodeTransformer` will walk the AST and use the return value of
1803 the visitor methods to replace or remove the old node. If the return value
1804 of the visitor method is ``None``, the node will be removed from its
1805 location, otherwise it is replaced with the return value. The return value
1806 may be the original node in which case no replacement takes place.
Georg Brandl0c77a822008-06-10 16:37:50 +00001807
1808 Here is an example transformer that rewrites all occurrences of name lookups
1809 (``foo``) to ``data['foo']``::
1810
1811 class RewriteName(NodeTransformer):
1812
1813 def visit_Name(self, node):
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +03001814 return Subscript(
Georg Brandl0c77a822008-06-10 16:37:50 +00001815 value=Name(id='data', ctx=Load()),
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001816 slice=Index(value=Constant(value=node.id)),
Georg Brandl0c77a822008-06-10 16:37:50 +00001817 ctx=node.ctx
1818 ), node)
1819
1820 Keep in mind that if the node you're operating on has child nodes you must
1821 either transform the child nodes yourself or call the :meth:`generic_visit`
1822 method for the node first.
1823
1824 For nodes that were part of a collection of statements (that applies to all
1825 statement nodes), the visitor may also return a list of nodes rather than
1826 just a single node.
1827
Batuhan Taşkaya6680f4a2020-01-12 23:38:53 +03001828 If :class:`NodeTransformer` introduces new nodes (that weren't part of
1829 original tree) without giving them location information (such as
1830 :attr:`lineno`), :func:`fix_missing_locations` should be called with
1831 the new sub-tree to recalculate the location information::
1832
1833 tree = ast.parse('foo', mode='eval')
1834 new_tree = fix_missing_locations(RewriteName().visit(tree))
1835
Georg Brandl0c77a822008-06-10 16:37:50 +00001836 Usually you use the transformer like this::
1837
1838 node = YourTransformer().visit(node)
1839
1840
Serhiy Storchaka850573b2019-09-09 19:33:13 +03001841.. function:: dump(node, annotate_fields=True, include_attributes=False, *, indent=None)
Georg Brandl0c77a822008-06-10 16:37:50 +00001842
1843 Return a formatted dump of the tree in *node*. This is mainly useful for
Serhiy Storchakae64f9482019-08-29 09:30:23 +03001844 debugging purposes. If *annotate_fields* is true (by default),
1845 the returned string will show the names and the values for fields.
1846 If *annotate_fields* is false, the result string will be more compact by
1847 omitting unambiguous field names. Attributes such as line
Benjamin Petersondcf97b92008-07-02 17:30:14 +00001848 numbers and column offsets are not dumped by default. If this is wanted,
Serhiy Storchakae64f9482019-08-29 09:30:23 +03001849 *include_attributes* can be set to true.
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001850
Serhiy Storchaka850573b2019-09-09 19:33:13 +03001851 If *indent* is a non-negative integer or string, then the tree will be
1852 pretty-printed with that indent level. An indent level
1853 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
1854 selects the single line representation. Using a positive integer indent
1855 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
1856 that string is used to indent each level.
1857
1858 .. versionchanged:: 3.9
1859 Added the *indent* option.
1860
1861
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001862.. _ast-cli:
1863
1864Command-Line Usage
1865------------------
1866
1867.. versionadded:: 3.9
1868
1869The :mod:`ast` module can be executed as a script from the command line.
1870It is as simple as:
1871
1872.. code-block:: sh
1873
1874 python -m ast [-m <mode>] [-a] [infile]
1875
1876The following options are accepted:
1877
1878.. program:: ast
1879
1880.. cmdoption:: -h, --help
1881
1882 Show the help message and exit.
1883
1884.. cmdoption:: -m <mode>
1885 --mode <mode>
1886
1887 Specify what kind of code must be compiled, like the *mode* argument
1888 in :func:`parse`.
1889
Batuhan Taşkaya814d6872019-12-16 21:23:27 +03001890.. cmdoption:: --no-type-comments
1891
1892 Don't parse type comments.
1893
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001894.. cmdoption:: -a, --include-attributes
1895
1896 Include attributes such as line numbers and column offsets.
1897
Batuhan Taşkaya814d6872019-12-16 21:23:27 +03001898.. cmdoption:: -i <indent>
1899 --indent <indent>
1900
1901 Indentation of nodes in AST (number of spaces).
1902
Serhiy Storchaka832e8642019-09-09 23:36:13 +03001903If :file:`infile` is specified its contents are parsed to AST and dumped
1904to stdout. Otherwise, the content is read from stdin.
1905
1906
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001907.. seealso::
1908
Sanyam Khurana338cd832018-01-20 05:55:37 +05301909 `Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external documentation resource, has good
Senthil Kumaranf3695bf2016-01-06 21:26:53 -08001910 details on working with Python ASTs.