• <bdo id='vcobb'></bdo><ul id='vcobb'></ul>

    1. <tfoot id='vcobb'></tfoot>

    2. <legend id='vcobb'><style id='vcobb'><dir id='vcobb'><q id='vcobb'></q></dir></style></legend>
    3. <i id='vcobb'><tr id='vcobb'><dt id='vcobb'><q id='vcobb'><span id='vcobb'><b id='vcobb'><form id='vcobb'><ins id='vcobb'></ins><ul id='vcobb'></ul><sub id='vcobb'></sub></form><legend id='vcobb'></legend><bdo id='vcobb'><pre id='vcobb'><center id='vcobb'></center></pre></bdo></b><th id='vcobb'></th></span></q></dt></tr></i><div id='vcobb'><tfoot id='vcobb'></tfoot><dl id='vcobb'><fieldset id='vcobb'></fieldset></dl></div>
      1. <small id='vcobb'></small><noframes id='vcobb'>

        为什么 __get__ 获取所有者而 __set__ 和 __delete__ 不

        时间:2023-09-13

          <tbody id='9YPDG'></tbody>
        • <small id='9YPDG'></small><noframes id='9YPDG'>

            <i id='9YPDG'><tr id='9YPDG'><dt id='9YPDG'><q id='9YPDG'><span id='9YPDG'><b id='9YPDG'><form id='9YPDG'><ins id='9YPDG'></ins><ul id='9YPDG'></ul><sub id='9YPDG'></sub></form><legend id='9YPDG'></legend><bdo id='9YPDG'><pre id='9YPDG'><center id='9YPDG'></center></pre></bdo></b><th id='9YPDG'></th></span></q></dt></tr></i><div id='9YPDG'><tfoot id='9YPDG'></tfoot><dl id='9YPDG'><fieldset id='9YPDG'></fieldset></dl></div>
              <tfoot id='9YPDG'></tfoot>

                • <bdo id='9YPDG'></bdo><ul id='9YPDG'></ul>
                  <legend id='9YPDG'><style id='9YPDG'><dir id='9YPDG'><q id='9YPDG'></q></dir></style></legend>
                • 本文介绍了为什么 __get__ 获取所有者而 __set__ 和 __delete__ 不获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  来自 Python 数据模型文档:p><块引用>

                  object.__get__(self, instance, owner=None)

                  调用以获取所有者类的属性(类属性访问)或该类的实例(实例属性访问).可选的 owner 参数是所有者类,而 instance 是访问属性的实例,或者 None 访问属性时所有者.

                  此方法应返回计算的属性值或引发 AttributeError 异常.

                  PEP 252 指定 __get__() 可以用一个或两个参数调用.Python 自己的内置描述符支持此规范;但是,某些第三方工具可能具有需要两个参数的描述符.Python 自己的 __getattribute__() 实现总是传入两个参数,无论它们是否需要.

                  object.__set__(self, instance, value)

                  调用以将所有者类的实例 instance 上的属性设置为新值 value.

                  注意,添加 __set__()__delete__() 会将描述符的种类更改为数据描述符".有关详细信息,请参阅调用描述符.

                  object.__delete__(self, instance)

                  调用来删除所有者类的实例instance上的属性.

                  为什么 __get__ 使用 owner__set____delete__ 没有?

                  是不是意味着当一个描述符同时提供__get____set__时,

                  • 无论是属于所有者类的实例还是属于所有者类,我们都可以得到一个属性,
                  • 我们可以设置和删除属于所有者类实例的属性,但不能在属于所有者类时设置和删除?

                  我的问题实际上是这个的一部分.

                  解决方案

                  owner 主要用于获取类本身的属性,而不是实例.当您检索实例的属性时,owner 参数是多余的,因为它只是 type(instance).

                  __set__ 不适用于设置类本身的属性,所以 owner 没有用.

                  From the Python data model documentation:

                  object.__get__(self, instance, owner=None)

                  Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). The optional owner argument is the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner.

                  This method should return the computed attribute value or raise an AttributeError exception.

                  PEP 252 specifies that __get__() is callable with one or two arguments. Python’s own built-in descriptors support this specification; however, it is likely that some third-party tools have descriptors that require both arguments. Python’s own __getattribute__() implementation always passes in both arguments whether they are required or not.

                  object.__set__(self, instance, value)

                  Called to set the attribute on an instance instance of the owner class to a new value, value.

                  Note, adding __set__() or __delete__() changes the kind of descriptor to a "data descriptor". See Invoking Descriptors for more details.

                  object.__delete__(self, instance)

                  Called to delete the attribute on an instance instance of the owner class.

                  Why does __get__ take an owner while __set__ and __delete__ do not?

                  Does it mean that when a descriptor supplies both __get__ and __set__,

                  • we can get an attribute no matter whether it belongs to an instance of the owner class or to the owner class,
                  • we can set and delete an attribute when it belongs to an instance of the owner class but not when it belongs to the owner class?

                  My question is actually part of this one.

                  解决方案

                  owner mostly exists for getting the attribute on the class itself, rather than an instance. When you're retrieving the attribute on an instance, the owner argument is redundant, since it's just type(instance).

                  __set__ doesn't apply to setting the attribute on the class itself, so it has no use for owner.

                  这篇关于为什么 __get__ 获取所有者而 __set__ 和 __delete__ 不获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 python 中覆盖类属性访问? 下一篇:AttributeError:“FreqDist"对象没有属性“inc"

                  相关文章

                  最新文章

                  <tfoot id='YBYMw'></tfoot>

                  <small id='YBYMw'></small><noframes id='YBYMw'>

                    1. <i id='YBYMw'><tr id='YBYMw'><dt id='YBYMw'><q id='YBYMw'><span id='YBYMw'><b id='YBYMw'><form id='YBYMw'><ins id='YBYMw'></ins><ul id='YBYMw'></ul><sub id='YBYMw'></sub></form><legend id='YBYMw'></legend><bdo id='YBYMw'><pre id='YBYMw'><center id='YBYMw'></center></pre></bdo></b><th id='YBYMw'></th></span></q></dt></tr></i><div id='YBYMw'><tfoot id='YBYMw'></tfoot><dl id='YBYMw'><fieldset id='YBYMw'></fieldset></dl></div>
                        <bdo id='YBYMw'></bdo><ul id='YBYMw'></ul>
                    2. <legend id='YBYMw'><style id='YBYMw'><dir id='YBYMw'><q id='YBYMw'></q></dir></style></legend>