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

查看:560
本文介绍了如何在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.

推荐答案

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

$ 2 b
$ b

在Python 2中,或

in Python 2, or

super().__setattr__(name, value)


$ b Python 3中的$ b

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)

$ 2 b
$ b

在Python 2或

on Python 2 or

return super().__getattribute__(name)

提高 AttributeError 跳过默认处理并转到 __ getattr __ ,或只生成<$调用代码中的c $ c> 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天全站免登陆