[Numpy-discussion] Bug: extremely misleading array behavior

eric eric at enthought.com
Tue May 28 23:38:01 EDT 2002


Hey Larry,

I actually thought, as you did, that indexing the array returns an element
converted to a scalar -- and it does in the "default" cases when you don't
specify a non-standard typecode.

After testing, it looks like values that are representable as native Python
types ('l', 'd', and 'D') are returned as actual values while non-standard types
are returned as views into the array.  Is this intentional?  It is dangerous to
have the behavior change based on the type.  It seems they should all be views
or they should all  be converted to a scalar.

Here is your test code modified to test all Numeric types:

    import Numeric

    def test_index(typecode):
        print 'typcode:', typecode
        a = Numeric.zeros((2, 2), typecode)
        n = a[1, 1]     # fetch interesting value from array
        print n
        a[1, 1] = 10    # change array
        print n         # blam
        print type(n)   # huh
        print

    print 'Numeric version:', Numeric.__version__
    for t in ['i','1','s','l','f','d','F','D']:
        test_index(t)

And here is the output.  Look at the types returned.

C:\home\ej\wrk\junk>python num_index.py
Numeric version: 21.0
typcode: i
0
10
<type 'array'>

typcode: 1
0
10
<type 'array'>

typcode: s
0
10
<type 'array'>

typcode: l
0
0
<type 'int'>

typcode: f
0.0
10.0
<type 'array'>

typcode: d
0.0
0.0
<type 'float'>

typcode: F
0j
(10+0j)
<type 'array'>

typcode: D
0j
0j
<type 'complex'>

eric

----- Original Message -----
From: "Larry Denneau" <larryd at pangalactic.com>
To: <numpy-discussion at lists.sourceforge.net>
Sent: Tuesday, May 28, 2002 1:13 PM
Subject: [Numpy-discussion] Bug: extremely misleading array behavior


> 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
>
> _______________________________________________________________
>
> Don't miss the 2002 Sprint PCS Application Developer's Conference
> August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>






More information about the NumPy-Discussion mailing list