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

      <bdo id='RRBbM'></bdo><ul id='RRBbM'></ul>

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

        如何在 python 中覆盖类属性访问?

        时间:2023-09-13
      1. <tfoot id='HOMVD'></tfoot>

          <tbody id='HOMVD'></tbody>

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

        • <legend id='HOMVD'><style id='HOMVD'><dir id='HOMVD'><q id='HOMVD'></q></dir></style></legend>
            • <bdo id='HOMVD'></bdo><ul id='HOMVD'></ul>

                • <i id='HOMVD'><tr id='HOMVD'><dt id='HOMVD'><q id='HOMVD'><span id='HOMVD'><b id='HOMVD'><form id='HOMVD'><ins id='HOMVD'></ins><ul id='HOMVD'></ul><sub id='HOMVD'></sub></form><legend id='HOMVD'></legend><bdo id='HOMVD'><pre id='HOMVD'><center id='HOMVD'></center></pre></bdo></b><th id='HOMVD'></th></span></q></dt></tr></i><div id='HOMVD'><tfoot id='HOMVD'></tfoot><dl id='HOMVD'><fieldset id='HOMVD'></fieldset></dl></div>
                  本文介绍了如何在 python 中覆盖类属性访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在 python 中覆盖类属性访问?

                  How can I override class attribute access in python?

                  附:有没有办法单独保留对类属性的常规访问,但在缺少属性时调用更具体的异常?

                  P.S. Is there a way to leave regular access to class attributes alone but calling a more specific exception on missing attribute?

                  推荐答案

                  __getattr__ 属性在实例/类/父类上不存在时调用魔术方法.您可以使用它为缺少的属性引发特殊异常:

                  The __getattr__ magic method is called when the attribute doesn't exist on the instance / class / parent classes. You'd use it to raise a special exception for a missing attribute:

                  class Foo(object):
                      def __getattr__(self, attr):
                          # only called when self.attr doesn't exist
                          raise MyCustonException(attr)
                  

                  如果要自定义访问类属性,需要在元类/类型上定义__getattr__:

                  If you want to customize access to class attributes, you need to define __getattr__ on the metaclass / type:

                  class BooType(type):
                      def __getattr__(self, attr):
                          print attr
                          return attr
                  
                  class Boo(object):
                      __metaclass__ = BooType
                  
                  boo = Boo()
                  Boo.asd # prints asd
                  boo.asd # raises an AttributeError like normal
                  

                  如果您想自定义 all 属性访问,请使用 __getattribute__ 魔术方法.

                  If you want to customize all attribute access, use the __getattribute__ magic method.

                  这篇关于如何在 python 中覆盖类属性访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么从类和实例中获取属性的查找过程不同? 下一篇:为什么 __get__ 获取所有者而 __set__ 和 __delete__ 不

                  相关文章

                  最新文章

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

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

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

                      <legend id='d4uHn'><style id='d4uHn'><dir id='d4uHn'><q id='d4uHn'></q></dir></style></legend>

                      <tfoot id='d4uHn'></tfoot>