Python 左侧二元算子的numpy强制问题

Python 左侧二元算子的numpy强制问题,python,numpy,coercion,Python,Numpy,Coercion,我正在实现一个类似数组的对象,它应该可以与标准numpy数组进行互操作。我刚刚遇到了一个恼人的问题,范围缩小到以下几点: class MyArray( object ): def __rmul__( self, other ): return MyArray() # value not important for current purpose from numpy import array print array([1,2,3]) * MyArray() 这将产生以下输出: [

我正在实现一个类似数组的对象,它应该可以与标准numpy数组进行互操作。我刚刚遇到了一个恼人的问题,范围缩小到以下几点:

class MyArray( object ):
  def __rmul__( self, other ):
    return MyArray() # value not important for current purpose

from numpy import array
print array([1,2,3]) * MyArray()
这将产生以下输出:

[<__main__.MyArray instance at 0x91903ec>
 <__main__.MyArray instance at 0x919038c>
 <__main__.MyArray instance at 0x919042c>]
[
]
显然,不像我所希望的那样调用
MyArray()。\uuuurmul\uuuuuuu(array([1,2,3])
,而是为数组中的每个单独元素调用
\uurmul\uuuuuu
,并将结果包装在对象数组中。在我看来,这似乎不符合python的标准。更重要的是,它使我的左乘法变得无用

有人知道怎么解决这个问题吗


(我原以为a可以使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
来修复它,但链接的文档解释说,不再调用该函数来响应二进制运算符…

事实证明,numpy为这个问题提供了一个简单的修复方法。以下代码按预期工作

class MyArray( object ):
  __array_priority__ = 1. # <- fixes the problem
  def __rmul__( self, other ):
    return MyArray()
类MyArray(对象):
__数组优先级=1#