[Numpy-discussion] coercion for array-like objects for ufuncs

Tim Hochberg tim.hochberg at ieee.org
Wed Sep 5 11:54:01 EDT 2001

Hi Chris,

I believe that all you should have to do is define an __array__ function
similar to that in UserArray;

    def __array__(self,t=None):
        if t: return asarray(self.array,t)
        return asarray(self.array)

Replace self.array with whatever you are calling your contained array.



----- Original Message -----
From: "Chris Myers" <myers at tc.cornell.edu>
To: <numpy-discussion at lists.sourceforge.net>
Sent: Wednesday, September 05, 2001 11:39 AM
Subject: [Numpy-discussion] coercion for array-like objects for ufuncs

> I have a Python object that contains a NumPy array (among other
> things), and I'd like it to behave like an array in certain
> circumstances.  (For example, I can define __getitem__ on the class so
> that the underlying NumPy array is indexed.)
> I'd like to be able to act on such an object with a ufunc, but am
> stymied.  For example, if y is an instance of this array-like class of
> mine, then I get a type error if I try a reduction on it:
> >>> Numeric.minimum.reduce(y)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: function not supported for these types, and can't coerce to
supported types
> Is there a way I can effect this coercion?  I guess I'd prefer not to
> have to inherit from UserArray.
> Thanks,
> Chris
> ==========================================================================
> Chris Myers
> Cornell Theory Center
> --------------------------------------------------------------------------
> 636 Rhodes Hall              email: myers at tc.cornell.edu
> Cornell University           phone: (607) 255-5894 / fax: (607) 254-8888
> Ithaca, NY 14853             http://www.tc.cornell.edu/~myers
> ==========================================================================
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list