diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2008-03-31 04:42:11 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2008-03-31 04:42:11 (GMT) |
commit | 207c9f3c5c0c41f329e7b7a2aeb8f2f7579dcbc9 (patch) | |
tree | 9e913d7d95ef67358b2596299022bd2c0e6ecbb1 /Doc | |
parent | db4115ffc063f20da2c6078bb93187ee8753d4ec (diff) | |
download | cpython-207c9f3c5c0c41f329e7b7a2aeb8f2f7579dcbc9.zip cpython-207c9f3c5c0c41f329e7b7a2aeb8f2f7579dcbc9.tar.gz cpython-207c9f3c5c0c41f329e7b7a2aeb8f2f7579dcbc9.tar.bz2 |
Merged revisions 62049,62054 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62049 | georg.brandl | 2008-03-30 00:01:47 -0700 (Sun, 30 Mar 2008) | 2 lines
#2505: allow easier creation of AST nodes.
........
r62054 | georg.brandl | 2008-03-30 12:43:27 -0700 (Sun, 30 Mar 2008) | 2 lines
Fix error message -- "expects either 0 or 0 arguments"
........
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/_ast.rst | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/Doc/library/_ast.rst b/Doc/library/_ast.rst index 518798e..cb7e44e 100644 --- a/Doc/library/_ast.rst +++ b/Doc/library/_ast.rst @@ -44,9 +44,32 @@ node. The utf8 offset is recorded because the parser uses utf8 internally. If these attributes are marked as optional in the grammar (using a question mark), the value might be ``None``. If the attributes can have zero-or-more values (marked with an asterisk), the values are represented as Python lists. +All possible attributes must be present and have valid values when compiling an +AST with :func:`compile`. + +The constructor of a class ``_ast.T`` parses their arguments as follows: + +* If there are positional arguments, there must be as many as there are items in + ``T._fields``; they will be assigned as attributes of these names. +* If there are keyword arguments, they will set the attributes of the same names + to the given values. + +For example, to create and populate a ``UnaryOp`` node, you could use :: + + node = _ast.UnaryOp() + node.op = _ast.USub() + node.operand = _ast.Num() + node.operand.n = 5 + node.operand.lineno = 0 + node.operand.col_offset = 0 + node.lineno = 0 + node.col_offset = 0 + +or the more compact :: + + node = _ast.UnaryOp(_ast.USub(), _ast.Num(5, lineno=0, col_offset=0), + lineno=0, col_offset=0) -The constructors of all ``_ast`` classes don't take arguments; instead, if you -create instances, you must assign the required attributes separately. Abstract Grammar |