python - Python3:访问一个 dunder 变量
问题描述
我知道 Python dunder 变量是什么,而且我知道名称修饰。
但由于某种原因,我无法访问以下代码片段中的 dunder 变量:
for node in ast.find_all((Call,)):
# Check if the identifier match the extension name
if node.node.identifier == 'myapp.ext.MyExtension':
# I want to access node.__meta
print("==> type(node) = %s" % type(nod))
print("==> node.__dict__ = %s" % node.__dict__")
哪个打印:
==> type(node) = <class 'jinja2.nodes.Call'>
==> node.__dict__ = {
'kwargs': [],
# ... a bunch of other attributes
# The __meta attribute below is what I want to access
'__meta': {'type': 'checkbox', 'value': Const(value='checked'), 'name': Const(value='agree'), 'class': Const(value='bold')}
}
由于该node
变量是Call
该类的一个实例,并且我想访问它的__meta
属性,根据名称修饰,我必须这样做node._Call__meta
,但我收到一个错误:
`'Call' object has no attribute '_Call__meta'`
我究竟做错了什么?
解决方案
__dict__
您似乎对名称修饰和交互方式有一点误解。__dict__
条目不受名称修饰的影响 - 如果您在 中看到名称'__meta'
,__dict__
则该属性按字面意思命名__meta
(而不是_Call__meta
)。
您可以通过这个小演示自己确认这一点:
class Demo:
__meta = 5
print('__meta' in vars(Demo)) # False
print('_Demo__meta' in vars(Demo)) # True
有两种方法可以访问此__meta
属性:
- 如果您的代码不在类中,则可以使用
node.__meta
. 这在课堂上不起作用,因为那样__meta
会被命名。 - 或者,您可以直接从
__dict__
with中获取属性node.__dict__['__meta']
。
推荐阅读
- apache-kafka - 在集群中清理 Kafka 主题
- java - 如何使用 JDBC 以安全模式(kerberos+ssl)连接配置单元?
- r - 如何使用参数化过滤器与来自 power bi 的 r 数据进行通信
- continuous-integration - Circle CI 无法添加项目,它也不存在于工作流中
- node.js - 通过在猫鼬中填充匹配结果来查找文档
- javascript - WebStorm 在 TypeScript 上下文中不显示纯 JS 库的自动完成
- android - 可滚动的 LinearLayout 高度限制
- android - DialogFragment 显示具有不同“片段”的步骤
- makefile - 在 Win10 64 位中“制作”OMNet5.4 时出错 (1803)
- json - 将任意 json 数据读取到 javafx 树视图中,并且只显示其中任何数组的第一个元素