[SciPy-Dev] Numpy special methods and NotImplemented vs. TypeError

Hoyt Koepke hoytak@stat.washington....
Tue Mar 8 15:57:33 CST 2011


Hello,

I've been working on implementing a matrix-like class of symbolic
objects that have to interact seamlessly with numpy arrays.  It's a
wrapper for CPlex's Concert library, which effectively represents
variables in an optimization problem as symbolic C++ objects that can
be used to build the model.  I'm trying to wrap this to work with
numpy -- I'd be happy to share my code when it's done -- and I'm
running into a few problems.

The main issue I'm working against is that when ndarray's special
methods, e.g. __add__, __gt__, etc. encounter a type they don't
understand, they raise a TypeError instead of a NotImplemented
exception.  This is a problem as the corresponding right-operator
methods (__radd__, __le__) are only called under three conditions
(http://docs.python.org/reference/datamodel.html#object.__radd__)

1. When the left object doesn't implement the left operator.
2. When the left object raises a NotImplemented exception.
3. When the right object is a subclass of the left object.  In this
case, the left operator method is never called.

Consider working with A + X, where A is an ndarray and X is an array
representing my symbolic objects, and let's suppose my matrix class
implements __radd__ sensibly.  Obviously, 1. doesn't apply here.  If I
go with 2., a TypeError is raised, which blows things apart.  So the
only solution I have is 3.

However, it isn't possible for me to use the underlying array
machinery by, for example, keeping a numpy array of python objects,
each wrapping a single symbolic variable.  This is due to space /
speed constraints and the fact that what I'm really doing is wrapping
a given CPlex array class of these symbolic objects.  Thus, if I
subclass ndarray, the underlying array is meaningless -- while I do
implement slicing and various other array-like methods, the only
reason I'm subclassing ndarray is (3).  The big problem with this is
that many of the attributes and methods of ndarray don't make sense
for my class, so I have to basically cover them up by overriding them
with methods that simply raise exceptions.  Doable, but far from
ideal.

So my main question is this:  Is there any reason that ndarrays throw
a TypeError in this case instead of NotImplemented?  It seems like the
latter would be more standards compliant.

Thanks!
--Hoyt


++++++++++++++++++++++++++++++++++++++++++++++++
+ Hoyt Koepke
+ University of Washington Department of Statistics
+ http://www.stat.washington.edu/~hoytak/
+ hoytak@gmail.com
++++++++++++++++++++++++++++++++++++++++++


More information about the SciPy-Dev mailing list