[Numpy-discussion] subclassing ndarray subtleties??

Tom Aldcroft aldcroft at head.cfa.harvard.edu
Tue May 22 16:36:06 EDT 2012


On Tue, May 22, 2012 at 4:07 PM, Dan Goodman <dg.gmane at thesamovar.net> wrote:
> On 22/05/2012 18:20, Nathaniel Smith wrote:
>> I don't know of anything that the docs are lacking in particular. It's
>> just that subclassing in general is basically a special form of
>> monkey-patching: you have this ecosystem of cooperating methods, and
>> then you're inserting some arbitrary changes in the middle of it.
>> Making it all work in general requires that you carefully think
>> through how all the different pieces of the ndarray API interact, and
>> the ndarray API is very large and complicated. The __getslice__ thing
>> is one example of this. For another: does your __getitem__ properly
>> handle *all* the cases that regular ndarray.__getitem__ handles? (I'm
>> not sure anyone actually knows what this complete list is, there are a
>> lot of potential corner cases.) What happens if one of your objects is
>> passed to third-party code that uses __getitem__? What happens if your
>> array is accidentally stripped of its magic properties by passing
>> through np.asarray() at the top of some function? Have you thought
>> about how your special attributes are affected by, say, swapaxes? Have
>> you applied your tweaks to item() and setitem()?
>>
>> I'm just guessing randomly here of course, since I have no idea what
>> you've done. And I've subclassed ndarray myself at least three times,
>> for reasons that seemed good enough at the time, so I'm not saying
>> it's never doable. It's just that there are tons of these tiny little
>> details, any one of which can trip you up, and that means that people
>> tend to dive in and then discover the pitfalls later.
>
> I've also used subclasses of ndarray, and have stumbled across most (but
> not all) of the problems you mentioned above. In my case, my code has
> gradually evolved over a few years as I've become aware of each of these
> problems. I think it would be useful to have an example of a completely
> 'correctly' subclassed ndarray that handles all of these issues that
> people could use as a template when they want to subclass ndarray. I
> appreciate, though, that there's no-one who particularly wants to do
> this! :) I'd offer my code as an example, but Nathaniel's comment above
> shows that there's many things mine doesn't handle properly.
>
> Dan
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

The text Nathaniel wrote is already very useful (certainly to me).  It
seems like this text could be put almost verbatim (maybe with some
list items) in a subsection at the end of [1] titled "Caution" or
"Other considerations".

Thanks,
Tom

[1] http://docs.scipy.org/doc/numpy/user/basics.subclassing.html



More information about the NumPy-Discussion mailing list