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

    1. <small id='xZQOC'></small><noframes id='xZQOC'>

          <bdo id='xZQOC'></bdo><ul id='xZQOC'></ul>
      1. <legend id='xZQOC'><style id='xZQOC'><dir id='xZQOC'><q id='xZQOC'></q></dir></style></legend>

        为什么调用 Python 的“魔术方法"不像对应的运

        时间:2023-08-05
        <tfoot id='4RDNG'></tfoot>
        <legend id='4RDNG'><style id='4RDNG'><dir id='4RDNG'><q id='4RDNG'></q></dir></style></legend>

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

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

                  <bdo id='4RDNG'></bdo><ul id='4RDNG'></ul>
                • 本文介绍了为什么调用 Python 的“魔术方法"不像对应的运算符那样进行类型转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我从整数中减去浮点数时(例如 1-2.0),Python 会进行隐式类型转换(我认为).但是当我使用魔术方法 __sub__ 调用我认为是相同的操作时,它突然不再存在了.

                  When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.

                  我在这里缺少什么?当我为自己的类重载运算符时,除了将输入显式转换为我需要的任何类型之外,还有其他方法吗?

                  What am I missing here? When I overload operators for my own classes, is there a way around this other than explicitly casting input to whatever type I need?

                  a=1
                  a.__sub__(2.)
                  # returns NotImplemented
                  a.__rsub__(2.)
                  # returns NotImplemented
                  # yet, of course:
                  a-2.
                  # returns -1.0
                  

                  推荐答案

                  a - b 不仅仅是 a.__sub__(b).如果 a 无法处理该操作,它也会尝试 b.__rsub__(a),在 1 - 2. 的情况下,它是float 的 __rsub__ 处理操作.

                  a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.

                  >>> (2.).__rsub__(1)
                  -1.0
                  

                  您运行了 a.__rsub__(2.),但这是错误的 __rsub__.您需要右侧操作数的 __rsub__,而不是左侧操作数.

                  You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.

                  减法运算符没有内置隐式类型转换.float.__rsub__ 必须手动处理整数.如果您想在自己的运算符实现中进行类型转换,您也必须手动处理.

                  There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.

                  这篇关于为什么调用 Python 的“魔术方法"不像对应的运算符那样进行类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在所有 pandas 列中将字符串转换为浮点数,这是可 下一篇:将 numpy 列表数组转换为 numpy 数组

                  相关文章

                  最新文章

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

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

                    <tfoot id='WlciZ'></tfoot>

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