• <small id='9y18o'></small><noframes id='9y18o'>

    <legend id='9y18o'><style id='9y18o'><dir id='9y18o'><q id='9y18o'></q></dir></style></legend>
      <bdo id='9y18o'></bdo><ul id='9y18o'></ul>

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

    1. <tfoot id='9y18o'></tfoot>

        如何返回属性的默认值?

        时间:2023-09-13
        <tfoot id='zJpXN'></tfoot>

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

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

                    <tbody id='zJpXN'></tbody>
                  本文介绍了如何返回属性的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个对象 myobject,它可能返回 None.如果返回None,则不会返回属性id:

                  I have an object myobject, which might return None. If it returns None, it won't return an attribute id:

                  a = myobject.id
                  

                  所以当 myobject 为 None 时,上面的语句会导致 AttributeError:

                  So when myobject is None, the stament above results in a AttributeError:

                  AttributeError: 'NoneType' object has no attribute 'id'
                  

                  如果 myobject 为 None,那么我希望 a 等于 None.如何在一行语句中避免此异常,例如:

                  If myobject is None, then I want a to be equal to None. How do I avoid this exception in one line statement, such as:

                  a = default(myobject.id, None)
                  

                  推荐答案

                  你应该使用 getattr 包装器,而不是直接检索 id 的值.

                  a = getattr(myobject, 'id', None)
                  

                  这就像说我想从对象myobject中检索属性id,但是如果里面没有属性id对象 myobject,然后返回 None."但它确实有效.

                  This is like saying "I would like to retrieve the attribute id from the object myobject, but if there is no attribute id inside the object myobject, then return None instead." But it does it efficiently.

                  有些对象还支持以下形式的getattr访问:

                  Some objects also support the following form of getattr access:

                  a = myobject.getattr('id', None)
                  

                  根据 OP 要求,'deep getattr':

                  def deepgetattr(obj, attr):
                      """Recurses through an attribute chain to get the ultimate value."""
                      return reduce(getattr, attr.split('.'), obj)
                  # usage: 
                  print deepgetattr(universe, 'galaxy.solarsystem.planet.name')
                  

                  简单解释:

                  Reduce 就像一个就地递归函数.在这种情况下,它的作用是从 obj (universe)开始,然后为您尝试使用 getattr 访问的每个属性递归地深入,所以在您的问题中它会是像这样:

                  Reduce is like an in-place recursive function. What it does in this case is start with the obj (universe) and then recursively get deeper for each attribute you try to access using getattr, so in your question it would be like this:

                  a = getattr(getattr(myobject, 'id', None), 'number', None)

                  这篇关于如何返回属性的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何向networkx中的边缘添加新属性? 下一篇:Python 类与模块属性

                  相关文章

                  最新文章

                  <tfoot id='AngHQ'></tfoot>

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

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

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

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