Skip to content
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

0-dimensional ndarrays should support len #7281

Closed
gerritholl opened this issue Feb 18, 2016 · 6 comments
Closed

0-dimensional ndarrays should support len #7281

gerritholl opened this issue Feb 18, 2016 · 6 comments
Labels
57 - Close? Issues which may be closable unless discussion continued

Comments

@gerritholl
Copy link
Contributor

When asking for len(X) on a 0-d ndarray, a TypeError is raised because the object is reportedly unsized. This despite X.size being equal to 1. If X were unsized, it wouldn't have a '.size' attribute?

In [88]: X = array(0.0, dtype=float32)

In [89]: X.shape
Out[89]: ()

In [90]: X.size
Out[90]: 1

In [91]: len(X)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-91-a6c197cecae8> in <module>()
----> 1 len(X)

Interestingly, a void does accept len:

In [80]: X = zeros(shape=(), dtype=dtype([('x', '<f4')]))

In [81]: X.size
Out[81]: 1

In [82]: len(X)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-82-a6c197cecae8> in <module>()
----> 1 len(X)

TypeError: len() of unsized object

In [83]: len(X[()])
Out[83]: 1
@rkern
Copy link
Member

rkern commented Feb 18, 2016

It does not report a __len__ because it cannot be indexed by integers, sliced, or iterated over, at least one of which should be true for things with a __len__. .size is a numpy thing talking about how much memory it takes up (in units of .itemsize bytes), more or less.

@gerritholl
Copy link
Contributor Author

Or maybe not. I just thought the second example was odd. If it has a size, doesn't that sort of mean it has a length?

@gerritholl
Copy link
Contributor Author

It makes sense, on second thought. But then why does void have a __len__?

@njsmith
Copy link
Member

njsmith commented Feb 18, 2016

Another way to think about it: a.size is prod(a.shape), and len(a) is a.shape[0]. So for a 0d array, the former is 1 and the latter is an error.

@rkern
Copy link
Member

rkern commented Feb 18, 2016

This void is a record with fields. You can access those fields by indexing in addition to using the name.

@seberg
Copy link
Member

seberg commented Mar 2, 2016

I guess this can be closed, or is there still something to discuss here @gerritholl?

@seberg seberg added the 57 - Close? Issues which may be closable unless discussion continued label Mar 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

4 participants