[Numpy-discussion] Continued New Indexing Methods Revival #N (subclasses!)

Sebastian Berg sebastian at sipsolutions.net
Sat Sep 10 09:49:10 EDT 2016


On Sa, 2016-09-10 at 12:01 +0200, Sebastian Berg wrote:
> Hi all,
> 
> from the discussion, I was thinking maybe something like this:
> 
> class B():
>    def __numpy_getitem__(self, index, indexing_method="plain"):
>        # do magic.
>        return super().__numpy_getitem__(
>            index, indexing_method=indexing_method)
> 
> as new API. There are some issues, though. An old subclass may define
> `__getitem__`. Now the behaviour that would seem nice to me is:
> 
> 1. No new attribute (no `__numpy_getitem__`) and also no
>    `__getitem__`/`__setitem__`: Should just work
> 2. No new attribute but old attributes defined: Should at
>    least give a warning (or an error) when using the new
>    attributes, since the behaviour might buggy.
> 3. `__numpy_getitem__` defined: Will channel all indexing through it
>    (except maybe some edge cases in python 2). Best, also avoid that
>    use getitem in setitem trick.... If you define both (which might
>    make sense for some edge case stuff), you should just channel it
>    through this yourself.
> 

Maybe in shorter; I would like to know if a subclass:

1. requires no fixup
2. may need fixup
3. supports everything.

And I am not sure how to approach this.


> Now the issue I have is that for 1. and 2. to work correctly, I need
> to
> know which methods are overloaded by the subclass. Checking is a bit
> tedious and the method I hacked first for getitem and setitem does
> not
> work for a normal method.
> 
> Can anyone think of a nicer way to do this trick that does not
> require
> quite as much hackery. Or is there an easy way to do the overloading
> check?
> 
> - Sebastian
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> https://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160910/987074e3/attachment.sig>


More information about the NumPy-Discussion mailing list