Discussion:
[Numpy-discussion] changing ValueError to KeyError for bad field access
Allan Haldane
2015-06-12 17:46:39 UTC
Permalink
Hi all,

I think it would be very nice to make access to invalid fields of a
a = np.ones(3, dtype=[('a', 'f4'), ('b', 'f4')])
a['c']
KeyError: 'c'

A commit in my PR https://github.com/numpy/numpy/pull/5636 does this. As
discussed there, backwards compatibility is a problem but it seems like
the impact might be fairly small.

Any opinions?

Allan
Sturla Molden
2015-06-12 17:59:54 UTC
Permalink
Post by Allan Haldane
a = np.ones(3, dtype=[('a', 'f4'), ('b', 'f4')])
a['c']
KeyError: 'c'
Any opinions?
Sounds good to me. But it will probably break someones code.


Sturla
Phil Hodge
2015-06-12 18:06:44 UTC
Permalink
Post by Allan Haldane
I think it would be very nice to make access to invalid fields of a
a = np.ones(3, dtype=[('a', 'f4'), ('b', 'f4')])
a['c']
KeyError: 'c'
This will break code, but it should do so in a way that's visible.
KeyError does seem like a more appropriate exception to me.

Phil

Loading...