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

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

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

    1. hasattr() 与 try-except 块处理不存在的属性

      时间:2023-09-13
        <tbody id='DR63i'></tbody>

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

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

          • <bdo id='DR63i'></bdo><ul id='DR63i'></ul>
            • <tfoot id='DR63i'></tfoot>

                <legend id='DR63i'><style id='DR63i'><dir id='DR63i'><q id='DR63i'></q></dir></style></legend>
              1. 本文介绍了hasattr() 与 try-except 块处理不存在的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                if hasattr(obj, 'attribute'):
                    # do somthing
                

                try:
                    # access obj.attribute
                except AttributeError, e:
                    # deal with AttributeError
                

                应该首选哪个以及为什么?

                Which should be preferred and why?

                推荐答案

                hasattr 在内部快速执行与 try/except 块相同的任务:这是一个非常具体的,经过优化的单任务工具,因此在适用时应优先于非常通用的替代方案.

                hasattr internally and rapidly performs the same task as the try/except block: it's a very specific, optimized, one-task tool and thus should be preferred, when applicable, to the very general-purpose alternative.

                这篇关于hasattr() 与 try-except 块处理不存在的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么属性名称不能是 Python 关键字? 下一篇:向python对象添加属性

                相关文章

                最新文章

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

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

                  <tfoot id='mws53'></tfoot>

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