[Python-ideas] Retain string form of AST Numbers

Chris Angelico rosuav at gmail.com
Sat Apr 5 23:55:03 CEST 2014


On Sun, Apr 6, 2014 at 3:39 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> What about strings?
>
>>>> print(ast.dump(ast.parse(r"x = '''\x0a\u000d''' r'x'")))
> Module(body=[Assign(targets=[Name(id='x', ctx=Store())],
> value=Str(s='\n\rx'))])
>
> Tuples?
>
>>>> print(ast.dump(ast.parse("x = (1, 2,)")))
> Module(body=[Assign(targets=[Name(id='x', ctx=Store())],
> value=Tuple(elts=[Num(n=1), Num(n=2)], ctx=Load()))])

I don't have any concrete use cases for those, but the same could be
done for every type of node as a general mechanism for recreating a
file more exactly. Nick linked to a tracker issue with some discussion
on docstrings, which could benefit from that. Or maybe it would be
better to avoid the AST altogether and do a source-level translation,
as mentioned in the blog post he also linked to.

ChrisA


More information about the Python-ideas mailing list