[SciPy-dev] Suppressing of numpy __mul__, __div__ etc

Dmitrey tmp50@ukr....
Sun Dec 13 02:38:42 CST 2009


On Sat, Dec 12, 2009 at 4:52 PM, Robert Kern wrote:  
> We could also define a standard attribute that could mark such classes instead of requiring a mixin subclass.  
  
I had thought about the idea, but I suspect it will work slower:  
  
    def __mul__(self, i):  
        return asarray(multiply(self, i)) if not getattr(i, '_numpyLeftOperatorsOverloaded', False) else i.__rmul__(self)  
vs  
    def __mul__(self, i):  
        return asarray(multiply(self, i)) if not isinstance(i,CNumpyLeftOperatorOverloaded) else i.__rmul__(self)  
  
But it doesn't matter for me essentially which way to chose; so which one do you agree to be implemented?  
  
  On Sat, Dec 12, 2009 at 4:52 PM, Charles R Harris  
<charlesr.harris@gmail.com> wrote:  >Instead of converting with np.array or np.asarray, we could check whether the instances in the function arguments implement the required interface, and if yes just use the methods without converting to arrays.  
  
Do you mean something like  
    def __mul__(self, i):  
        return asarray(multiply(self, i)) if not hasattr(i, '__rmul__', False) else i.__rmul__(self)  
?  
I guess this is wrong idea, because sometimes user will want to get result that is masked array of i instances. BTW, lots of data types have its own __rmul__ (Python lists, numbers, even numpy arrays and matrices), so I guess it will not work as expected (as it is done now) for them.  
  
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-dev/attachments/20091213/405e4ada/attachment.html 


More information about the SciPy-Dev mailing list