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
0d arrays are recognised as collections.abc.Sized instances but don't actually support len
#19833
Comments
The workaround here is to use try:
n = len(a)
except TypeError:
pass
else:
if n > 1:
... That is: it's better to ask forgiveness than permission. Fundamentally I don't think numpy has any power to fix this without creating more problems, and the above spelling is likely more idiomatic anyway. |
The EAFP pattern you described is precisely what I was initially trying to eliminate in yt where the construct is used so often that is has its own function. |
@neutrinoceros, I expect the only way to fix this would be to make 0-D arrays subclasses of |
Thanks for reminding me of the acronym EAFP. What's the reason for trying to avoid it in yt? |
mostly just trying to modernise the code base and start using
wait. Are they currently not subclasses of ndarray ? I though that was precisely the cause of this issue. |
Sure, but it's slower for the success path, doesn't actually work in all cases (as seen here!), and IMO less idiomatic. |
Well if a big player like numpy is ok with breaking the language's intended rules, it also negates the chances of this becoming idiomatic. In any case it feels like this can only be discovered by trial and error, and I assume that the current behavior is 100% accidental and surely not intented. |
Sorry, but those "language rules" were developed long after numpy arrays. |
No, they are |
They are In my opinion, the truly idiomatic thing would probably be to either refuse being a sequence, or making I would be happy to help petition Python to create an "idiotmatic way" that NumPy has a fighting chance to adhere to, but I am not exactly sure how it would look like in general, because I am more on the NumPy side of things, but this affects more the "user/library" side using NumPy. |
I don't think changes to numpy influence whether LBYL vs EAFP is more idiomatic; if anything, this provides a clear demonstration that LBYL is fragile and requires active work from third-party packages, while EAFP works out of the box. That's not to disagree that it would be nice if your code sample worked; just that I don't think your spelling is a particularly good idea in general. |
I'm well aware of that, and I mean no disrespect @rkern.
Thanks for the clarification ! I believe that
I think I need to emphasise that, at least in yt, having
sorry about the confusion @eric-wieser, this isn't what I meant either.
Truth be told my main motivation in yt was to move away from the EAFP form, not because of a preference of LBYL (if anything I'm more of an advocate for the EAFP pattern), but because I see this check in many places where the |
Numpy implements it with something equivalent to: def __len__(self):
if self.ndim == 0:
raise TypeError
else:
return self.shape[0] # this would be an IndexError if `ndim == 0` |
Nor I. Sorry, I didn't intend those to be sneer-quotes, just quote-quotes. :-)
"Inherit whatever makes it One more wrinkle is that
So a LBYL type check per se just doesn't really fit with the dynamism numpy has implemented against. I think there is a case to be made for deprecating and removing this amount of dynamism, if we decide to really clean up our act to really make these type checks express everything we want them to. Using |
Closing. There is some interesting discussion in the thread, but I really don't think there is anything actionable. If anyone disagrees, please reopen/or speak up. |
Reproducing code example:
The first condition normally guards against calling
len
on an object that doesn't have a__len__
implementation, however this fails with 0d ndarrays.Error message:
NumPy/Python version information:
numpy 1.21.0, Python 3.9.6
somewhat related to #2776
The text was updated successfully, but these errors were encountered: