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:
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@tc.cornell.edu Cornell University phone: (607) 255-5894 / fax: (607) 254-8888 Ithaca, NY 14853 http://www.tc.cornell.edu/~myers ==========================================================================

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

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. Regards, -tim ----- Original Message ----- From: "Chris Myers" <myers@tc.cornell.edu> To: <numpy-discussion@lists.sourceforge.net> Sent: Wednesday, September 05, 2001 11:39 AM Subject: [Numpy-discussion] coercion for array-like objects for ufuncs
participants (3)
-
Chris Myers
-
Tim Hochberg
-
Travis Oliphant