Python 的“__get*__”和“__get*__”有什么区别?和“_del*__”方法?

发布于 2024-12-29 07:17:13 字数 368 浏览 2 评论 0 原文

我几个月前刚刚开始学习 Python,我试图理解不同的 __get*__ 方法之间的差异:

__get__
__getattr__
__getattribute__
__getitem___

以及它们的 __del*__ 等效项:

__del__
__delattr__
__delete__
__delitem__

什么是这些之间的区别?我什么时候应该使用其中一种而不是另一种?大多数 __get*__ 方法都有 __set*__ 等效项,但没有 __setattribute__ ,这是否有特定原因?

I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods:

__get__
__getattr__
__getattribute__
__getitem___

And their __del*__ equivalents:

__del__
__delattr__
__delete__
__delitem__

What are the differences between these? When should I use one over the other? Is there a specific reason why most of the __get*__ methods have __set*__ equivalents, but there is no __setattribute__?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

荆棘i 2025-01-05 07:17:13

您可以从文档索引轻松访问您列出的每种方法的文档。

无论如何,这可能是一个小小的扩展参考:

__get____set____del__ 是描述符

“简而言之,描述符是一种方法自定义引用模型上的属性时会发生的情况。” [官方文档链接]

它们得到了很好的解释,所以这里有一些参考:

__getattr____getattribute____setattr____delattr__

可以定义这些方法来自定义属性访问的含义(使用、赋值或删除类实例的 x.name)。 [官方文档链接]

示例 1:

class Foo:
    def __init__(self):
        self.x = 10
    def __getattr__(self, name):
        return name

f = Foo()
f.x    # -> 10
f.bar   # -> 'bar'

示例 2:

class Foo:
    def __init__(self):
        self.x = 10
    def __getattr__(self,name):
        return name
    def __getattribute__(self, name):
        if name == 'bar':
            raise AttributeError
        return 'getattribute'

f = Foo()
f.x    # -> 'getattribute'
f.baz    # -> 'getattribute'
f.bar    # -> 'bar'

__getitem____setitem____delitem__

是以下方法: 可以定义为实现容器对象。 [官方文档链接]

示例:

class MyColors:
    def __init__(self):
        self._colors = {'yellow': 1, 'red': 2, 'blue': 3}
    def __getitem__(self, name):
        return self._colors.get(name, 100)

colors = MyColors()
colors['yellow']   # -> 1
colors['brown']    # -> 100

我希望这足以让您有一个总体了解。

The documentation for every method that you listed is easly reachable from the documentation index .

Anyway this may be a little extended reference:

__get__, __set__ and __del__ are descriptors

"In a nutshell, a descriptor is a way to customize what happens when you reference an attribute on a model." [official doc link]

They are well explained around, so here there are some references:

__getattr__, __getattribute__, __setattr__, __delattr__

Are methods that can be defined to customize the meaning of attribute access (use of, assignment to, or deletion of x.name) for class instances. [official doc link]

Example 1:

class Foo:
    def __init__(self):
        self.x = 10
    def __getattr__(self, name):
        return name

f = Foo()
f.x    # -> 10
f.bar   # -> 'bar'

Example 2:

class Foo:
    def __init__(self):
        self.x = 10
    def __getattr__(self,name):
        return name
    def __getattribute__(self, name):
        if name == 'bar':
            raise AttributeError
        return 'getattribute'

f = Foo()
f.x    # -> 'getattribute'
f.baz    # -> 'getattribute'
f.bar    # -> 'bar'

__getitem__, __setitem__, __delitem__

Are methods that can be defined to implement container objects. [official doc link]

Example:

class MyColors:
    def __init__(self):
        self._colors = {'yellow': 1, 'red': 2, 'blue': 3}
    def __getitem__(self, name):
        return self._colors.get(name, 100)

colors = MyColors()
colors['yellow']   # -> 1
colors['brown']    # -> 100

I hope this is enough to give you a general idea.

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