Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="node794.html" /> | |
13 | <link rel="parent" href="node792.html" /> | |
14 | <link rel="next" href="module-compiler.visitor.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>19.3.3 Examples</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="19.3.2 Assignment nodes" | |
24 | href="node794.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="19.3 Python Abstract Syntax" | |
27 | href="node792.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="19.4 Using Visitors to" | |
30 | href="module-compiler.visitor.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="node794.html">19.3.2 Assignment nodes</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="node792.html">19.3 Python Abstract Syntax</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-compiler.visitor.html">19.4 Using Visitors to</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0021330000000000000000"> | |
55 | 19.3.3 Examples</A> | |
56 | </H2> | |
57 | ||
58 | <P> | |
59 | This section shows several simple examples of ASTs for Python source | |
60 | code. The examples demonstrate how to use the <tt class="function">parse()</tt> | |
61 | function, what the repr of an AST looks like, and how to access | |
62 | attributes of an AST node. | |
63 | ||
64 | <P> | |
65 | The first module defines a single function. Assume it is stored in | |
66 | <span class="file">/tmp/doublelib.py</span>. | |
67 | ||
68 | <P> | |
69 | <div class="verbatim"><pre> | |
70 | """This is an example module. | |
71 | ||
72 | This is the docstring. | |
73 | """ | |
74 | ||
75 | def double(x): | |
76 | "Return twice the argument" | |
77 | return x * 2 | |
78 | </pre></div> | |
79 | ||
80 | <P> | |
81 | In the interactive interpreter session below, I have reformatted the | |
82 | long AST reprs for readability. The AST reprs use unqualified class | |
83 | names. If you want to create an instance from a repr, you must import | |
84 | the class names from the <tt class="module">compiler.ast</tt> module. | |
85 | ||
86 | <P> | |
87 | <div class="verbatim"><pre> | |
88 | >>> import compiler | |
89 | >>> mod = compiler.parseFile("/tmp/doublelib.py") | |
90 | >>> mod | |
91 | Module('This is an example module.\n\nThis is the docstring.\n', | |
92 | Stmt([Function(None, 'double', ['x'], [], 0, | |
93 | 'Return twice the argument', | |
94 | Stmt([Return(Mul((Name('x'), Const(2))))]))])) | |
95 | >>> from compiler.ast import * | |
96 | >>> Module('This is an example module.\n\nThis is the docstring.\n', | |
97 | ... Stmt([Function(None, 'double', ['x'], [], 0, | |
98 | ... 'Return twice the argument', | |
99 | ... Stmt([Return(Mul((Name('x'), Const(2))))]))])) | |
100 | Module('This is an example module.\n\nThis is the docstring.\n', | |
101 | Stmt([Function(None, 'double', ['x'], [], 0, | |
102 | 'Return twice the argument', | |
103 | Stmt([Return(Mul((Name('x'), Const(2))))]))])) | |
104 | >>> mod.doc | |
105 | 'This is an example module.\n\nThis is the docstring.\n' | |
106 | >>> for node in mod.node.nodes: | |
107 | ... print node | |
108 | ... | |
109 | Function(None, 'double', ['x'], [], 0, 'Return twice the argument', | |
110 | Stmt([Return(Mul((Name('x'), Const(2))))])) | |
111 | >>> func = mod.node.nodes[0] | |
112 | >>> func.code | |
113 | Stmt([Return(Mul((Name('x'), Const(2))))]) | |
114 | </pre></div> | |
115 | ||
116 | <P> | |
117 | ||
118 | <DIV CLASS="navigation"> | |
119 | <div class='online-navigation'> | |
120 | <p></p><hr /> | |
121 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
122 | <tr> | |
123 | <td class='online-navigation'><a rel="prev" title="19.3.2 Assignment nodes" | |
124 | href="node794.html"><img src='../icons/previous.png' | |
125 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
126 | <td class='online-navigation'><a rel="parent" title="19.3 Python Abstract Syntax" | |
127 | href="node792.html"><img src='../icons/up.png' | |
128 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
129 | <td class='online-navigation'><a rel="next" title="19.4 Using Visitors to" | |
130 | href="module-compiler.visitor.html"><img src='../icons/next.png' | |
131 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
132 | <td align="center" width="100%">Python Library Reference</td> | |
133 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
134 | href="contents.html"><img src='../icons/contents.png' | |
135 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
136 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
137 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
138 | <td class='online-navigation'><a rel="index" title="Index" | |
139 | href="genindex.html"><img src='../icons/index.png' | |
140 | border='0' height='32' alt='Index' width='32' /></A></td> | |
141 | </tr></table> | |
142 | <div class='online-navigation'> | |
143 | <b class="navlabel">Previous:</b> | |
144 | <a class="sectref" rel="prev" href="node794.html">19.3.2 Assignment nodes</A> | |
145 | <b class="navlabel">Up:</b> | |
146 | <a class="sectref" rel="parent" href="node792.html">19.3 Python Abstract Syntax</A> | |
147 | <b class="navlabel">Next:</b> | |
148 | <a class="sectref" rel="next" href="module-compiler.visitor.html">19.4 Using Visitors to</A> | |
149 | </div> | |
150 | </div> | |
151 | <hr /> | |
152 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
153 | </DIV> | |
154 | <!--End of Navigation Panel--> | |
155 | <ADDRESS> | |
156 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
157 | </ADDRESS> | |
158 | </BODY> | |
159 | </HTML> |