Python AST:几个语义不清楚,例如expr_context [英] Python AST: several semantics unclear, e.g. expr_context

查看:23
本文介绍了Python AST:几个语义不清楚,例如expr_context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 ast 文档之外还有什么关于ast 模块?

特别是,我想知道 expr_context(及其所有可能的值)究竟意味着什么.

Esp., I am wondering what expr_context (and all its possible values) exactly means.

另外,AssignAugAssign 有什么区别?

Also, what is the difference between Assign and AugAssign?

此外,在对局部变量进行赋值时,是否可以引用真实的 Python 对象而不是其名称?我自己正在构建一个 AST,我有一些我想在 AST 中访问的 Python 对象.另一种方法是为它们引入一些虚拟临时变量名称,并将该虚拟变量名称添加到 globals() 范围以供稍后编译的函数使用,但这对我来说似乎有些糟糕(缓慢而笨拙).

Also, it is possible to reference to a real Python object instead of its name when doing an assignment to a local variable? I am building an AST myself and I have some Python objects which I want to access to in the AST. The alternative would be to introduce some dummy temp var name for them and add that dummy var name to the globals() scope for the later compiled function but that seems somewhat bad (slow and hacky) to me.

推荐答案

我会尽量自己回答.

经过更多的测试和猜测:

After some more testing and guessing:

expr_context 是定义 Name 的地方,例如如果它在左侧 (Store, AugStore)、右侧 (Load, AugLoad),在 del (Del) 或从 FunctionDefLambda (参数).

expr_context is where the Name is defined, e.g. if it is in an assignment on the left side (Store, AugStore), right side (Load, AugLoad), in a del (Del) or in an argument list like from FunctionDef or Lambda (Param).

AugAssign 就像 a = a b.Assign 只是一个简单的a = b.

AugAssign is like a = a <op> b. Assign is just a simple a = b.

我还没有找到引用真正 Python 对象的方法,而且似乎没有.

I haven't found a way to reference to a real Python object and it seems like there is none.

这篇关于Python AST:几个语义不清楚,例如expr_context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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