Python 按右侧对象处理操作(_r*__方法)

Python 按右侧对象处理操作(_r*__方法),python,operator-overloading,operators,Python,Operator Overloading,Operators,当numpy.array位于右侧时,numpy如何处理操作 >>> [1,2,3]+numpy.array([1,2,3]) array([2, 4, 6]) 我认为list应该尝试将array(使用list.\uuuu add\uuuu方法)添加到自身中,然后失败 @M4rtini答案的附加示例:\uuuuuu radd\uuuuuu在\uuuuu add\uuuuuu失败且对象类型不同时调用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu cl

numpy.array
位于右侧时,
numpy
如何处理操作

>>> [1,2,3]+numpy.array([1,2,3])
array([2, 4, 6])
我认为
list
应该尝试将
array
(使用
list.\uuuu add\uuuu
方法)添加到自身中,然后失败


@M4rtini答案的附加示例:
\uuuuuu radd\uuuuuu
\uuuuu add\uuuuuu
失败且对象类型不同时调用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class A():
    def __radd__(self, other):
        return "result"
print(A()+A()) #fail with TypeError

调用这些方法来实现二进制算术运算 (+,-,*,/,%,divmod(),pow(),**,&,^,|)带反射 (交换)操作数仅当左侧 操作数不支持相应的操作和操作数 属于不同的类型。[2]


\uuuuu radd\uuuuu
\uuuu add\uuuuuuuu>失败时调用get。它的可能副本可能不是直接副本,但那里的答案解释了这一点behavior@M4rtini更新评论请不要为同一类型的两个对象调用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,Python文档中有没有规定失败时调用
\uuuuuuuuuuuuuuuuuuuuuuuuu,它不是用相同类型的对象调用的?我好几天都没弄明白:)说得很清楚,是我的错
class A(object):
    def __radd__(self, other):
        print ("__radd__ called of A")
        return "result of A"

class B(object):
    def __radd__(self, other):
        print ("__radd__ called of B")
        return "result of B"


print (B()+A())
print (A()+B())

>>__radd__ called of A
>>result of A
>>__radd__ called of B
>>result of B
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rdiv__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other)
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)