[Numpy-discussion] Rank-0 arrays - reprise

Sebastian Berg sebastian@sipsolutions....
Sun Jan 6 03:41:16 CST 2013

On Sun, 2013-01-06 at 08:58 +0100, Dag Sverre Seljebotn wrote:
> On 01/05/2013 10:31 PM, Nathaniel Smith wrote:
> > On 5 Jan 2013 12:16, "Matthew Brett" <matthew.brett@gmail.com> wrote:
> >>
> >> Hi,
> >>
> >> Following on from Nathaniel's explorations of the scalar - array
> >> casting rules, some resources on rank-0 arrays.
> >>


> > Q: Yeah. I mean, I remember that seemed weird when I first learned
> > Python, but when have you ever felt the Python was really missing a
> > "character" type like C has?
> str is immutable which makes this a lot easier to deal with without 
> getting confused. So basically you have:
> a[0:1] # read-write view
> a[[0]] # read-write copy
> a[0] # read-only view
> AND, += are allowed on all read-only arrays, they just transparently 
> create a copy instead of doing the operation in-place.
> Try to enumerate all the fundamentally different things (if you count 
> memory use/running time) that can happen for ndarrays a, b, and 
> arbitrary x here:
> a += b[x]
> That's already quite a lot, your proposal adds even more options. It's 
> certainly a lot more complicated than str.
> To me it all sounds like a lot of rules introduced just to have the 
> result of a[0] be "kind of a scalar" without actually choosing that option.

Yes, but I don't think there is an option to making the elements of an
array being immutable. Firstly if you switch normal python code to numpy
code you suddenly get numpy data types spilled into your code, and
mutable objects are simply very different (also true for code updating
to this new version). Do you expect:

array = np.zeros(10, dtype=np.intp)
b = arr[5]
while condition:
    # might change the array?!    
    b += 1
# This would not be possible and break:
dictionary[b] = b**2

Because mutable objects are not hashable which important considering
that dictionaries are a very central data type, making an element return
mutable would be a bad idea.
One could argue about structured datatypes, but maybe then it should be
a datatype property whether its mutable or not, and even then the
element should probably be a copy (though I did not check what happens
here right now).

> BUT I should read up on that thread you posted on why that won't work, 
> didn't have time yet...
> Dag Sverre
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list