[Numpy-discussion] Bug: extremely misleading array behavior
Pearu Peterson
pearu at cens.ioc.ee
Tue May 28 12:41:02 EDT 2002
On Tue, 28 May 2002, Larry Denneau wrote:
> Hello,
>
> I recently discovered the following behavior when fetching values
> from a Numeric array. Can somebody offer some insight?
>
> #1)
>
> import Numeric
>
> a = Numeric.zeros((2, 2), 'i')
> n = a[1, 1] # fetch interesting value from array
> print n
> a[1, 1] = 10 # change array
> print n # blam
> print type(n) # huh
>
> [bash]$ python 1.py
> 0
> 10
> <type 'array'>
>
> but
>
> #2)
>
> import Numeric
>
> a = Numeric.zeros((2,), 'i')
> n = a[1]
> print n
> a[1] = 10
> print n
> print type(n)
>
> [bash]$ python 2.py
> 0
> 0
> <type 'int'>
>
> #2 works the way one would expect, and #1 does not (n changes).
> They should at least both behave the same. :-) At a minimum, naive
> use of arrays can lead to confusing or disastrous results, since
> a single value fetched from an array can change behind your back.
Use
a[1][1] = 10
and the output will be
0
0
<type 'int'>
I find it is an useful feature in Numeric to have both behaviours of
either using a[1,1] or a[1][1]. You may want to dig into Numeric's
userguide to get a more detailed explanation of the differences.
Regards,
Pearu
More information about the NumPy-Discussion
mailing list