如何处理 python 查找:make.up.a.dot.separated.name.and.use.it.until.destroyed = 777
我是一名 Python 新手,非常渴望尝试 Python 的点名称查找过程。如何在“make.py”中编写类或函数以便这些赋值语句成功工作?
import make
make.a.dot.separated.name = 666
make.something.else.up = 123
make.anything.i.want = 777
I'm a Python newbie with a very particular itch to experiment with Python's dot-name-lookup process. How do I code either a class or function in "make.py" so that these assignment statements work succesfully?
import make
make.a.dot.separated.name = 666
make.something.else.up = 123
make.anything.i.want = 777
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当未找到命名值时,将调用特殊的
__getattr__
方法。make.anything.i.want
行最终执行的操作相当于:上面的实现使用这些对
__getattr__
的调用来创建Make
链> 每次访问未知属性时都会对象。这允许点访问嵌套任意深度,直到最终赋值,此时分配了实际值。Python 文档 - 自定义属性访问:
The special
__getattr__
method is called when a named value isn't found. The linemake.anything.i.want
ends up doing the equivalent of:The above implementation uses these calls to
__getattr__
to create a chain ofMake
objects each time an unknown property is accessed. This allows the dot accesses to be nested arbitrarily deep until the final assignment at which point a real value is assigned.Python documentation - customizing attribute access: