[Numpy-discussion] Bug: extremely misleading array behavior

Larry Denneau larryd at pangalactic.com
Tue May 28 11:03:38 EDT 2002


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.

It appears n is aliased into a, but preserves its value when a is
deleted (with del(a)).  What happens to the "rest of" a?

I'm using Python 2.2, Numeric-21.0, on both Unix and Win32.

Thanks,
Larry




More information about the NumPy-Discussion mailing list