[SciPy-User] subclassing ndarray : i want slice to return > ndarray, not subclass

Tom Aldcroft aldcroft@head.cfa.harvard....
Thu May 24 11:09:42 CDT 2012

On Thu, May 24, 2012 at 6:40 AM, M Daoust <m_daoust@hotmail.com> wrote:
>> > Subclassing ndarray is a very tricky business -- I did it once and
>> > regretted having done it for years, because there's so much you can't do
>> > etc.. You're almost certainly better off with embedding an array as an
>> > attribute, and then forward properties etc. to it.
>> Yes, it's almost always the wrong thing...
> I've sub-classes numpy.ndarray in some of my personal code, so I'm just
> hoping to understand why this is the wrong choice before I dig the hole any
> deeper.
> So it's only wrong because it's hard to do right, and you can't do certain
> things? (which things?)
> If you want a object to act mostly like a  numpy.ndarray, making a sub-class
> *should* be the right answer.
> Because isn't forwarding lookups to a wrapped class  just a
> re-implementation of the sub-classing mechanism?
> Isn't it also tricky to get that "wrapping subclassing" right?
> If you're going to sub-class something, it would be nice to use the built in
> sub-class machinery, for clarity... etc...
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

After seeing this thread I brought this point up on the
numpy-discussion list.  Here is a link to the thread:


- Tom

More information about the SciPy-User mailing list