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

      • <bdo id='vbqqg'></bdo><ul id='vbqqg'></ul>
      <legend id='vbqqg'><style id='vbqqg'><dir id='vbqqg'><q id='vbqqg'></q></dir></style></legend>
    1. <tfoot id='vbqqg'></tfoot>

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

      1. 在 Python 中分配类布尔值

        时间:2023-08-29
          <tbody id='4wYHP'></tbody>
      2. <legend id='4wYHP'><style id='4wYHP'><dir id='4wYHP'><q id='4wYHP'></q></dir></style></legend>

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

              <small id='4wYHP'></small><noframes id='4wYHP'>

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

                  问题描述

                  Python 中的 If 语句允许您执行以下操作:

                  If statements in Python allow you to do something like:

                     if not x:
                         print "X is false."
                  

                  如果您使用的是空列表、空字典、None、0 等,则此方法有效,但如果您有自己的自定义类怎么办?你能为那个类分配一个 false 值,以便在相同的条件样式中,它会返回 false 吗?

                  This works if you're using an empty list, an empty dictionary, None, 0, etc, but what if you have your own custom class? Can you assign a false value for that class so that in the same style of conditional, it will return false?

                  推荐答案

                  你需要实现 __nonzero__ 方法.这应该返回 True 或 False 以确定真值:

                  You need to implement the __nonzero__ method on your class. This should return True or False to determine the truth value:

                  class MyClass(object):
                      def __init__(self, val):
                          self.val = val
                      def __nonzero__(self):
                          return self.val != 0  #This is an example, you can use any condition
                  
                  x = MyClass(0)
                  if not x:
                      print 'x is false'
                  

                  如果未定义 __nonzero__,则实现将调用 __len__ 并且如果实例返回非零值,则该实例将被视为 True.如果 __len__ 也没有定义,所有实例都将被视为 True.

                  If __nonzero__ has not been defined, the implementation will call __len__ and the instance will be considered True if it returned a nonzero value. If __len__ hasn't been defined either, all instances will be considered True.

                  在 Python 3 中,__bool__ 代替 __nonzero__.

                  In Python 3, __bool__ is used instead of __nonzero__.

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

                2. <legend id='LvxBh'><style id='LvxBh'><dir id='LvxBh'><q id='LvxBh'></q></dir></style></legend>
                3. <small id='LvxBh'></small><noframes id='LvxBh'>

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

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

                        <tbody id='LvxBh'></tbody>
                    1. <tfoot id='LvxBh'></tfoot>