如何处理 python 查找:make.up.a.dot.separated.name.and.use.it.until.destroyed = 777

发布于 2024-08-08 00:58:32 字数 204 浏览 2 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-08-15 00:58:32
#!/usr/bin/env python

class Make:
    def __getattr__(self, name):
        self.__dict__[name] = Make()
        return self.__dict__[name]

make = Make()

make.a.dot.separated.name = 666
make.anything.i.want = 777

print make.a.dot.separated.name
print make.anything.i.want

当未找到命名值时,将调用特殊的 __getattr__ 方法。 make.anything.i.want 行最终执行的操作相当于:

m1 = make.anything    # calls make.__getattr__("anything")
m2 = m1.i             # calls m1.__getattr__("i")
m2.want = 777

上面的实现使用这些对 __getattr__ 的调用来创建 Make 链> 每次访问未知属性时都会对象。这允许点访问嵌套任意深度,直到最终赋值,此时分配了实际值。

Python 文档 - 自定义属性访问

object.__getattr__(self, name)

当属性查找在通常的位置没有找到属性时调用(即它不是实例属性,也没有在 self 的类树中找到)。 name 是属性名称。此方法应返回(计算的)属性值或引发 AttributeError 异常。

请注意,如果通过正常机制找到该属性,则不会调用 __getattr__()。 (这是 __getattr__()__setattr__() 之间故意不对称的。)这样做既是出于效率原因,也是因为 __getattr__()将无法访问实例的其他属性。请注意,至少对于实例变量,您可以通过不在实例属性字典中插入任何值(而是将它们插入另一个对象中)来伪造完全控制。请参阅下面的 __getattribute__() 方法,了解在新型类中实际获得完全控制的方法。

object.__setattr__(自身、名称、值)

尝试分配属性时调用。这被调用而不是正常的机制(即将值存储在实例字典中)。 name 是属性名称,value 是要分配给它的值。

如果__setattr__()想要分配给实例属性,它不应该简单地执行self.name = value——这会导致对其自身的递归调用。相反,它应该将值插入到实例属性的字典中,例如,self.__dict__[name] = value。对于新式类,不应访问实例字典,而应调用基类的同名方法,例如,object.__setattr__(self, name, value)

#!/usr/bin/env python

class Make:
    def __getattr__(self, name):
        self.__dict__[name] = Make()
        return self.__dict__[name]

make = Make()

make.a.dot.separated.name = 666
make.anything.i.want = 777

print make.a.dot.separated.name
print make.anything.i.want

The special __getattr__ method is called when a named value isn't found. The line make.anything.i.want ends up doing the equivalent of:

m1 = make.anything    # calls make.__getattr__("anything")
m2 = m1.i             # calls m1.__getattr__("i")
m2.want = 777

The above implementation uses these calls to __getattr__ to create a chain of Make 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:

object.__getattr__(self, name)

Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception.

Note that if the attribute is found through the normal mechanism, __getattr__() is not called. (This is an intentional asymmetry between __getattr__() and __setattr__().) This is done both for efficiency reasons and because otherwise __getattr__() would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the __getattribute__() method below for a way to actually get total control in new-style classes.

object.__setattr__(self, name, value)

Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). name is the attribute name, value is the value to be assigned to it.

If __setattr__() wants to assign to an instance attribute, it should not simply execute self.name = value — this would cause a recursive call to itself. Instead, it should insert the value in the dictionary of instance attributes, e.g., self.__dict__[name] = value. For new-style classes, rather than accessing the instance dictionary, it should call the base class method with the same name, for example, object.__setattr__(self, name, value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文