[Numpy-discussion] numarray unfriendly to user defined types

Todd Miller jmiller at stsci.edu
Tue Sep 23 03:35:01 EDT 2003

On Mon, 2003-09-22 at 19:27, Tim Hochberg wrote:

> I actually have no idea how you plan to make keyword arguments work 
> here, perhaps you could explain that in more detail. Metaclasses are 
> overkill, but a mixin, marker class could be used. That is, when 
> designing a class for use with numarray, one would derive a class from a 
> marker class in numarray::
>    class MyArrayLikeClass(numarray.DeferToMe):
>        ....
> Hmmm. That's not too bad. Todd, what do you think about using this logic::
>     def __mul__(self, operand):
>         if isinstance(operand, DeferToMe):
>             operand.__rmul__(self)
>         else:
>             self.__mul__(operand)

I like the core idea a lot.  My only doubt is whether forcing the use of
inheritance is appropriate / a good thing.  We might also consider
spelling it like:

class MyArrayLikeClass:
	_numarray_defer_to_me = True

class NumArray:
	def __mul__(self, operand):
		if hasattr(operand, "_numarray_defer_to_me"):
			return operand.__rmul__(self)
			return ufunc.multiply(self, operand)

> The only case where I see a potential problem is an old-style 
> C-extenstion that can't be subclassed. I think that might be a case of 
> YAGNI though. 

Sounds YAGNI to me.

> Avoiding registration is appealing.

Good to have this seconded.

> -tim

All in all, great ideas!


Todd Miller <jmiller at stsci.edu>

More information about the NumPy-Discussion mailing list