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

        python isinstance vs hasattr vs try/except:什么更好?

        时间:2023-09-13
        1. <small id='U1gpe'></small><noframes id='U1gpe'>

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

                  <tfoot id='U1gpe'></tfoot>
                  本文介绍了python isinstance vs hasattr vs try/except:什么更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我试图找出不同方法之间的权衡,以确定是否使用对象 obj 可以执行操作 do_stuff().据我了解,有三种方法可以确定这是否可行:

                  I am trying to figure out the tradeoffs between different approaches of determining whether or not with object obj you can perform action do_stuff(). As I understand, there are three ways of determining if this is possible:

                  # Way 1
                  if isinstance(obj, Foo):
                      obj.do_stuff()
                  
                  # Way 2
                  if hasattr(obj, 'do_stuff'):
                      obj.do_stuff()
                  
                  # Way 3
                  try:
                      obj.do_stuff()
                  except:
                      print 'Do something else'
                  

                  首选方法是什么(为什么)?

                  Which is the preferred method (and why)?

                  推荐答案

                  我相信最后一种方法通常是 Python 程序员首选的,因为 座右铭 在 Python 社区中教授:请求原谅比请求许可更容易"(EAFP).

                  I believe that the last method is generally preferred by Python coders because of a motto taught in the Python community: "Easier to ask for forgiveness than permission" (EAFP).

                  简而言之,座右铭的意思是避免在做某事之前检查您是否可以做某事.相反,只需运行该操作.如果失败,请适当处理.

                  In a nutshell, the motto means to avoid checking if you can do something before you do it. Instead, just run the operation. If it fails, handle it appropriately.

                  另外,第三种方法还有一个额外的好处,就是明确操作应该起作用.

                  Also, the third method has the added advantage of making it clear that the operation should work.

                  话虽如此,您确实应该避免使用像这样的纯 except.这样做将捕获任何/所有异常,甚至是不相关的异常.相反,最好专门捕获异常.

                  With that said, you really should avoid using a bare except like that. Doing so will capture any/all exceptions, even the unrelated ones. Instead, it is best to capture exceptions specifically.

                  在这里,您需要捕获 AttributeError:

                  Here, you will want to capture for an AttributeError:

                  try:
                      obj.do_stuff()   # Try to invoke do_stuff
                  except AttributeError:
                      print 'Do something else'  # If unsuccessful, do something else
                  

                  这篇关于python isinstance vs hasattr vs try/except:什么更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python:如何使对象属性引用调用方法 下一篇:为什么我不能直接向任何 python 对象添加属性?

                  相关文章

                  最新文章

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

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

                  1. <legend id='Nf358'><style id='Nf358'><dir id='Nf358'><q id='Nf358'></q></dir></style></legend>