如何在 Python 中的新样式类上正确覆盖 __setattr__ 和 __getattribute__? [英] How do I properly override __setattr__ and __getattribute__ on new-style classes in Python?

查看:29
本文介绍了如何在 Python 中的新样式类上正确覆盖 __setattr__ 和 __getattribute__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖我的 Python 类的 __getattribute____setattr__ 方法.我的用例是通常的用例:我有一些我想要处理的特殊名称,并且我想要其他任何东西的默认行为.对于 __getattribute__,似乎我可以通过提高 AttributeError 来请求默认行为.但是,如何在 __setattr__ 中实现相同的目标?这是一个简单的例子,实现一个具有不可变字段A"、B"和C"的类.

I want to override my Python class's __getattribute__ and __setattr__ methods. My use case is the usual one: I have a few special names that I want to handle, and I want the default behavior for anything else. For __getattribute__, it seems that I can request the default behavior simply by raising AttributeError. However, how can I achieve the same in __setattr__? Here is a trivial example, implementing a class with immutable fields "A", "B", and "C".

class ABCImmutable(SomeSuperclass):
    def __getattribute__(self, name):
        if name in ("A", "B", "C"):
            return "Immutable value of %s" % name
        else:
            # This should trigger the default behavior for any other
            # attribute name.
            raise AttributeError()

    def __setattr__(self, name, value):
        if name in ("A", "B", "C"):
            raise AttributeError("%s is an immutable attribute.")
        else:
            # How do I request the default behavior?
            ???

什么代替问号?对于旧式类,答案显然是 self.__dict__[name] = value,但文档表明这对于新式类是错误的.

What goes in place of the question marks? With old-style classes, the answer was apparently self.__dict__[name] = value, but documentation indicates that this is wrong for new-style classes.

推荐答案

It's

super(ABCImmutable, self).__setattr__(name, value)

在 Python 2 中,或

in Python 2, or

super().__setattr__(name, value)

在 Python 3 中.

in Python 3.

此外,提高 AttributeError 不是不是您退回到 __getattribute__ 的默认行为的方式.你用

Also, raising AttributeError is not how you fall back to the default behavior for __getattribute__. You fall back to the default with

return super(ABCImmutable, self).__getattribute__(name)

在 Python 2 或

on Python 2 or

return super().__getattribute__(name)

在 Python 3 上.

on Python 3.

Raising AttributeError 跳过默认处理并转到 __getattr__,或者如果没有 ,则只在调用代码中产生一个 AttributeError__getattr__.

Raising AttributeError skips the default handling and goes to __getattr__, or just produces an AttributeError in the calling code if there's no __getattr__.

请参阅有关自定义属性访问的文档.

这篇关于如何在 Python 中的新样式类上正确覆盖 __setattr__ 和 __getattribute__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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