如何从 Python 的 AST 获取变量的类型?
假设我想从我从某些源代码生成的 AST 树中获取所有变量的类型 - 我将如何去做呢? 例如,假设在我的源代码中我有类似 i = 5
的内容。我如何从抽象语法树中确定 i
的类型是整数?
我尝试了 type() 函数;但是,它在这种情况下不起作用。
Suppose I want to get the type of all variables from the AST tree that I have generated from some source code -- how would I go about doing that?
For example, suppose in my source code I have something like i = 5
. How would I determine, from the abstract syntax tree, that the type of i
is integer?
I tried the type()
function; however, it does not work in this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他帖子中所解释的,如果不对语法树进行大量分析,就没有简单的方法可以实现这一点,而 python ast 模块对此没有提供任何设施。
您仍然可以使用 logilab 的 astng1,它是 pylint2 并提供静态推理功能。
这是一个简单的例子:
当然,您必须挖掘 api 以获得更多实际用途。您仍然可以编写 [电子邮件受保护] 寻求帮助。
As explained in other posts, there isn't easy way to achieve this without heavy analysis of the syntax tree, for which python ast module provides no facilities.
You can still use logilab's astng1, which is the basis for pylint2 and provides static inference capabilities.
Here is a quick example :
Of course you'll have to dig the api for more real-life usage. You can still write [email protected] for help on this.
不能,因为 Python 的变量没有类型。 值有类型。
这就是动态类型的工作原理。
You can't, because Python's variables don't have a type. Values have types.
That's how dynamic typing works.
正如其他发帖者所指出的,这在动态类型语言中并不那么容易。您不能像在 C 或 Java 中那样将赋值追溯到静态类型声明。
然而,人们通常可以合理地确定类型。
据推测,范围规则允许人们确定在提出问题时可以访问/更新/绑定哪个 i(或哪一组 i)(“代码中此时的类型是什么?”)。然后可以对可能分配的所有值进行分析(一种特别简单的情况是当 i 仅绑定到函数定义时)。这些类型的类型格中的上限是 i 的“类型”。是的,在某些情况下它可能是“任何东西”,但在大多数编写良好的程序中,即使动态变量也具有程序员预期的“窄”类型,并且通常是原始语言类型(例如,呃,“int”)。或者程序员将无法合理地编写算法(什么,你的数组索引有时不是整数?)。
您需要对程序进行某种保守分析来确定此上限类型。 (显然,您可以进行简单的分析,并得出无用的结论:变量可以是“任何”类型)。我认为这是一个不能令人满意的答案。
进行所有这些分析的机制相当复杂(您需要全局流分析并确定可以动态加载哪些内容才能很好地完成此任务),并且我怀疑 Python 的 AST 包是否可以做到这一点。
As other posters have noted, this isn't so easy in a dynamically typed language. You can't just trace the assignment back to a static type declaration, as you can in C or Java.
However, one can often make a reasonable determination of the type.
Presumably the scoping rules allow one to determine which i (or which set of i's) might be accessed/updated/bound where the question is asked ("what the type of at this point in the code?"). Then one can do an analysis of all the values that might be assigned (a particularly trivial case is when i is bound only to a function definition). The upper bound in the type lattice on those types is the "type" of i. Yes, it might be "anything" in some cases, but in most well-written programs even dynamic variables have a "narrow" type intended by the programmer, and often its a primitive langauge type (like, er, "int"). Or the programmer wouldn't be able to reasonably write an algorithm (What, your array index isn't an integer sometimes?).
You need to do some kind of conservative analysis of the program to determine this upperbound type. (You can obviously do the trivial analysis, and conclude useless that a variable can be "any" type). I think that's an unsatisfactory answer.
The machinery to do all this analysis is pretty complicated (you need global flow analysis and some determination of what can be dynamically loaded to do this really well) and I doubt if Python's AST package does it.