• <legend id='t40Ae'><style id='t40Ae'><dir id='t40Ae'><q id='t40Ae'></q></dir></style></legend>
      <tfoot id='t40Ae'></tfoot>

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

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

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

        Python bool() 函数能否为无效参数引发异常?

        时间:2023-09-01

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

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

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

                  <tbody id='eTHo2'></tbody>

                  <tfoot id='eTHo2'></tfoot>
                1. 本文介绍了Python bool() 函数能否为无效参数引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果参数不能为 int()float() 等函数,则可能引发异常 (ValueError)转换为适当的数字类型.因此,如果有可能将无效参数传递给它们,则将它们包含在 try-except 中通常是一种好习惯.

                  但由于 Python 在真实性"方面的灵活性,我想不出任何可能传递给 bool() 函数的值会引发异常.即使您完全不带参数调用它,该函数也会完成并返回 False.

                  我是否纠正了 bool() 不会引发异常,只要您传递的参数不超过一个?因此,将调用包含在 try-except 中是没有意义的?

                  解决方案

                  bool__bool__ 不返回 True 时报错错误.

                  >>>类布尔失败:... def __bool__(self):...返回虚假"...>>>布尔(布尔失败())[...]TypeError: __bool__ 应该返回 bool,返回 str

                  不过,没有任何内置类型如此疯狂.

                  DSM 发表了非常有价值的评论:流行的 numpy 库中有 bool 会产生错误的示例.

                  >>>将 numpy 导入为 np>>>a = np.array([[1],[2]])>>>布尔(一)[...]ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()

                  user2357112 指出了以下极端情况.

                  <块引用>

                  标准的、近乎通用的 stdlib 示例,例如:一个死对象的 weakref.proxy 将引发几乎所有操作的 ReferenceError,包括 bool.

                  >>>导入弱引用>>>Foo类:...     经过...>>>bool(weakref.proxy(Foo()))[...]ReferenceError:弱引用对象不再存在

                  这不是 bool 独有的,任何使用其 dead 参数的函数都可能抛出此错误,例如 myfunc = lambda x: print(x).p>

                  It's possible for functions like int() or float() to raise an exception (ValueError) if the argument can't be converted to the appropriate numeric type. Hence, it's generally good practice to enclose these in a try-except if there's a possibility of an invalid argument being passed to them.

                  But with Python's flexibility when it comes to "truthiness," I can't think of any possible value you might pass to the bool() function that would raise an exception. Even if you call it with no argument at all, the function completes and returns False.

                  Am I correct that bool() just won't raise an exception, as long as you pass it no more than one argument? And as a result, there's no point in enclosing the call in a try-except?

                  解决方案

                  bool complains when __bool__ does not return True or False.

                  >>> class BoolFail:
                  ...     def __bool__(self):
                  ...         return 'bogus'
                  ... 
                  >>> bool(BoolFail())
                  [...]
                  TypeError: __bool__ should return bool, returned str
                  

                  No builtin types are this insane, though.

                  DSM made a very valuable comment: the popular numpy library has examples where bool will produce an error.

                  >>> import numpy as np
                  >>> a = np.array([[1],[2]])
                  >>> bool(a)
                  [...]
                  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
                  

                  user2357112 pointed out the following corner case.

                  Standard, near-universal stdlib example for things like this: a weakref.proxy to a dead object will raise a ReferenceError for almost any operation, including bool.

                  >>> import weakref
                  >>> class Foo:
                  ...     pass
                  ... 
                  >>> bool(weakref.proxy(Foo()))
                  [...]
                  ReferenceError: weakly-referenced object no longer exists
                  

                  This is not unique to bool, any function that uses its dead argument might throw this error, for example myfunc = lambda x: print(x).

                  这篇关于Python bool() 函数能否为无效参数引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在由切片和两个布尔数组索引的 numpy 数组中设置值 下一篇:Python中列表的布尔值

                  相关文章

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

                    <legend id='lxQFN'><style id='lxQFN'><dir id='lxQFN'><q id='lxQFN'></q></dir></style></legend>
                    1. <small id='lxQFN'></small><noframes id='lxQFN'>

                        <bdo id='lxQFN'></bdo><ul id='lxQFN'></ul>
                    2. <tfoot id='lxQFN'></tfoot>