了解 __getattr__ 和 __getattribute__ 之间的区别 [英] Understanding the difference between __getattr__ and __getattribute__

查看:22
本文介绍了了解 __getattr__ 和 __getattribute__ 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 __getattr____getattribute__ 之间的区别,但是,我失败了.

I am trying to understand the difference between __getattr__ and __getattribute__, however, I am failing at it.

堆栈溢出问题的答案__getattr____getattribute__ 之间的区别说:

The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says:

__getattribute__ 在查看实际属性之前被调用对象,因此可能很棘手正确执行.你可以在无限递归很容易.

__getattribute__ is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily.

我完全不知道那是什么意思.

I have absolutely no idea what that means.

接着说:

你几乎肯定想要__getattr__.

为什么?

我读到如果 __getattribute__ 失败,__getattr__ 会被调用.那么为什么有两种不同的方法做同样的事情呢?如果我的代码实现了新的样式类,我应该使用什么?

I read that if __getattribute__ fails, __getattr__ is called. So why are there two different methods doing the same thing? If my code implements the new style classes, what should I use?

我正在寻找一些代码示例来解决这个问题.我已尽我所能在谷歌上搜索,但我找到的答案并没有彻底讨论这个问题.

I am looking for some code examples to clear this question. I have Googled to best of my ability, but the answers that I found don't discuss the problem thoroughly.

如果有任何文档,我准备阅读.

If there is any documentation, I am ready to read that.

推荐答案

先了解一些基础知识.

对于对象,你需要处理它们的属性.通常,我们做instance.attribute.有时我们需要更多的控制(当我们事先不知道属性的名称时).

Some basics first.

With objects, you need to deal with their attributes. Ordinarily, we do instance.attribute. Sometimes we need more control (when we do not know the name of the attribute in advance).

例如,instance.attribute 将变成 getattr(instance, attribute_name).使用此模型,我们可以通过将 attribute_name 作为字符串提供来获取属性.

For example, instance.attribute would become getattr(instance, attribute_name). Using this model, we can get the attribute by supplying the attribute_name as a string.

您还可以告诉一个类如何处理它没有明确管理的属性,并通过 __getattr__ 方法来做到这一点.

You can also tell a class how to deal with attributes which it doesn't explicitly manage and do that via __getattr__ method.

每当您请求尚未定义的属性时,Python 都会调用此方法,因此您可以定义如何处理它.

Python will call this method whenever you request an attribute that hasn't already been defined, so you can define what to do with it.

经典用例:

class A(dict):
    def __getattr__(self, name):
       return self[name]
a = A()
# Now a.somekey will give a['somekey']

警告和__getattribute__的使用

如果您需要捕获每个属性不管它是否存在,请改用 __getattribute__.不同之处在于 __getattr__ 只会为实际不存在的属性调用.如果您直接设置一个属性,引用该属性将检索它而不调用 __getattr__.

Caveats and use of __getattribute__

If you need to catch every attribute regardless whether it exists or not, use __getattribute__ instead. The difference is that __getattr__ only gets called for attributes that don't actually exist. If you set an attribute directly, referencing that attribute will retrieve it without calling __getattr__.

__getattribute__ 一直被调用.

这篇关于了解 __getattr__ 和 __getattribute__ 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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