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

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

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

    2. <legend id='KzBAJ'><style id='KzBAJ'><dir id='KzBAJ'><q id='KzBAJ'></q></dir></style></legend>

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

        Python中对象的布尔值

        Boolean value of objects in Python(Python中对象的布尔值)
        1. <small id='Vt4Nf'></small><noframes id='Vt4Nf'>

          <legend id='Vt4Nf'><style id='Vt4Nf'><dir id='Vt4Nf'><q id='Vt4Nf'></q></dir></style></legend>
            <tbody id='Vt4Nf'></tbody>

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

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

                • 本文介绍了Python中对象的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  众所周知,Python 对对象有布尔值:如果一个类有一个 __len__ 方法,那么 __len__() 碰巧返回 0 的每个实例都会被评估为布尔值 False(例如,空列表).

                  As we know, Python has boolean values for objects: If a class has a __len__ method, every instance of it for which __len__() happens to return 0 will be evaluated as a boolean False (for example, the empty list).

                  事实上,如果每个可迭代的空自定义对象出现在布尔表达式中,它都会被评估为 False.

                  In fact, every iterable, empty custom object is evaluated as False if it appears in boolean expression.

                  现在假设我有一个带有属性bar 的类foo.我如何定义它的真值,例如,如果 bar % 2 == 0 和 False 否则它将被评估为 True?

                  Now suppose I have a class foo with attribute bar. How can I define its truth value, so that, say, it will be evaluated to True if bar % 2 == 0 and False otherwise?

                  例如:

                  myfoo = foo()
                  myfoo.bar = 3
                  def a(foo):
                      if foo:
                          print "spam"
                      else:
                          print "eggs"
                  

                  所以,a(myfoo) 应该打印 eggs".

                  推荐答案

                  在 Python 2 中,使用 __nonzero__:

                  请参阅 Python 2 文档以了解 __nonzero__.

                  class foo(object):
                      def __nonzero__( self) :
                          return self.bar % 2 == 0
                  
                  def a(foo):
                      if foo:
                          print "spam"
                      else:
                          print "eggs"
                  
                  def main():
                      myfoo = foo()
                      myfoo.bar = 3
                      a(myfoo)
                  
                  if __name__ == "__main__":
                      main()
                  

                  这篇关于Python中对象的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    • <bdo id='dKjJR'></bdo><ul id='dKjJR'></ul>
                          <tbody id='dKjJR'></tbody>

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

                          <tfoot id='dKjJR'></tfoot>

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

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