给定一个 AST,是否有用于获取源代码的工作库? [英] Given an AST, is there a working library for getting the source?

查看:22
本文介绍了给定一个 AST,是否有用于获取源代码的工作库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将给定的 Python 抽象语法树 (AST) 转换为源代码?

Is there a way to convert a given Python abstract syntax tree (AST) to a source code?

这里是一个很好的例子如何使用 Python 的 ast 模块,特别是 NodeTransformer.我一直在寻找一种方法将生成的 AST 转换回源代码,以便可以直观地检查更改.

Here is a good example of how to use Python's ast module, specifically a NodeTransformer. I was looking for a way to convert the resulting AST back to source, so the changes can be inspected visually.

推荐答案

Python 源代码树包含以下实现:unparse.py Demo/parser 目录

The Python source tree contains an implementation of this: unparse.py in the Demo/parser directory

编者注:随着 ast.unparse() 在 Python 3.9 中,unparse.py 已被移除,因此上面的链接已更新为指向 3.8.

Editor's note: With the introduction of ast.unparse() in Python 3.9, unparse.py has been removed, so the above link has been updated to point to 3.8.

这篇关于给定一个 AST,是否有用于获取源代码的工作库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆