Sebastian Berg
2016-09-10 10:01:41 UTC
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.
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
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.
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