[Numpy-discussion] Re: Trying out Numeric3

Scott Gilbert xscottg at yahoo.com
Fri Mar 25 23:16:08 EST 2005

--- Stephen Walton <stephen.walton at csun.edu> wrote:
> Travis Oliphant wrote:
> > Well, rank-0 arrays are and forever will be mutable.  But, Python 
> > scalars (and the new Array-like Scalars) are not mutable.
> This is a really minor point, and only slightly relevant to the 
> discussion, and perhaps I'm just revealing my Python ignorance again, 
> but:  what does it mean for a scalar to be mutable?  I can understand 
> that one wants a[0]=7 to be allowed when a is a rank-0 array, and I also 
> understand that str[k]='b' where str is a string is not allowed because 
> strings are immutable.  But if I type "b=7" followed by "b=3", do I 
> really care whether the 3 gets stuck in the same memory location 
> previously occupied by the 7 (mutable) or the symbol b points to a new 
> location containing a 3 (immutable)?  What are some circumstances where 
> this might matter?

It's nice because it fits with the rest of the array semantics and creates
a consistant system:

    Array3D = zeros((1, 1, 1))

    Array2D = Array3D[0]
    Array1D = Array2D[0]
    Array0D = Array1D[0]

That each is mutable is shown by:

    Array3D[0, 0, 0] = 1
    Array2D[0, 0] = 1
    Array1D[0] = 1
    Array0D[] = 1 # whoops!

Unfortunately that last one, while it follows the pattern, doesn't work for
Python's parser so you're stuck with:

    Array0D[()] = 1

This becomes useful when you start writing generic routines that want to
work with *any* dimensional arrays:


Python's immutable scalar types could not change in this case.  More
complicated examples are more interesting, but a simple implementation of
the above would be:

    def zero_all_elements(any):
        any[...] = 0


More information about the NumPy-Discussion mailing list