New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ndarray should implement Sequence abc #7315
Comments
IIRC there some discussion of this in the archives already, and there's some reason why this isn't totally trivial, like ndarray doesn't actually implement all the Sequence api. |
Perhaps the concern was 0d arrays, which don't have a length? |
I think we may also be missing reversed, index, and count On Tue, Feb 23, 2016 at 8:19 PM, Stephan Hoyer notifications@github.com
Nathaniel J. Smith -- https://vorpus.org http://vorpus.org |
Is it so? collections.abc.Sequence.__abstractmethods__
|
I think you are missing the mixin methods, @rahuldave, that come from inherited ABCs. ( https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes ) |
@rahuldave: a sequence has to provide more methods than that -- that's just the minimal set that are sufficient to implement the others, and the sequence ABC contains default implementations of the others in terms of those two. ndarray doesn't inherit from sequence and probably doesn't want those default implementations. (For example, I guess the index and count implementations assume that sequence elements support == turning a bool, which is not going to work for multidimensional arrays.) |
Unless I am missing something, most of these could be (or are already) implemented. |
By traversing the parents (Sized, Iterable, Container) and their mro's I get |
I think it is from |
Yeah, that appears to be the case. Basically the same story for Python 2. ( https://github.com/python/cpython/blob/1fe0fd9feb6a4472a9a1b186502eb9c0b2366326/Lib/_collections_abc.py#L827-L829 ) |
Just to link it, this has been discussed here before, first sight sounds like it went into the direction of "just add the abc": gh-2776 |
Just to throw some ideas out there to see if they sound reasonable,
Thoughts? |
To fully comply with the
The handling of slices though is really..... weird:
|
Contains has long been broken, got a long standing issue open to at one point make it purely element wise for everyones sanity. EDIT: Of course it is true, that in some sense it shouldn't be element wise when comparing to the sequence ABC. |
I was wonder if someone might say this. :) I suppose this would make an argument towards making |
@seberg: that does seem like a good idea... right now I can't even tell what the semantics are :-) @jakirkham: my personal feeling is that in a perfect world ndarray would not even attempt to implement the sequence protocol for non-1d-arrays -- e.g. if |
I was once thinking about |
A totally different thought would be to provide a function in Numpy to replace random sample (unless I am completely overlooking an existing one). Considering both issues have been related to this problem in particular, this would provide a practical manner forward (possibly with a more efficient implementation than Python's) for users who want this feature. In the meantime, these larger issues of following the |
BTW, there is a |
Should this be closed? Reading over the discussion it looks like Sequence is not a good fit for all ndarrays (just some of them). |
Going to close this one in favor of gh-2776 in any case, which has also a lot of discussion. We may want to close that as well, I suppose. I doubt we will add it, although there may be things that could be done in the general direction. |
numpy arrays don't (and likely won't) fulfill the Sequence interface numpy/numpy#2776 numpy/numpy#7315 The array gets converted down to a tuple any way, and each element must be an integer too. Unit test modified to allow this behavior.
Some third-party functions may explicit check for Sequence registration, e.g.:
The text was updated successfully, but these errors were encountered: