Francesc Altet wrote:
> Hello,
>
> Is the next a bug a feature?
>
> In [102]: f4=numpy.ndarray(buffer="a\x00b"*4, dtype="f4", shape=3)
>
> In [103]: f4
> Out[103]: array([ 2.60561966e+20, 8.94319890e-39, 5.92050103e+20],
> dtype=float32)
>
> In [104]: f4[2] = 2
> ---------------------------------------------------------------------------
> <type 'exceptions.RuntimeError'> Traceback (most recent call last)
>
> /home/faltet/python.nobackup/numpy/<ipython console> in <module>()
>
> <type 'exceptions.RuntimeError'>: array is not writeable
>
> In [105]: f4.flags.writeable = True
>
> In [106]: f4[2] = 2
>
> In [107]: f4
> Out[107]: array([ 2.60561966e+20, 8.94319890e-39, 2.00000000e+00],
> dtype=float32)
>
>
> i.e. in an array built from ndarray, the default is that it has to be
> read-only?
>
It's not that the it's being built from ndarray, it's that the buffer
that you are passing it is read only. In fact, I'd argue that allowing
the writeable flag to be set to True in this case is actually a bug.
Consider this slightly modified example:
>>> a = "12345"*4
>>> f4=numpy.ndarray(buffer=a, dtype="f4", shape=3)
>>> f4[2] = 99
Traceback (most recent call last):
File "<stdin>", line 1, in ?
RuntimeError: array is not writeable
>>> f4.flags.writeable = True
>>> a
'12345123451234512345'
>>> f4.flags.writeable = True
>>> f4[2] = 99
>>> a
'12345123\x00\x00\xc6B34512345'
The original, *immutable* string has been mutated. This could get you
into real trouble in certain situations.
-tim
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Numpy-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/numpy-discussion