[Numpy-discussion] Recarray attributes writeable
Andrew Straw
strawman at astraw.com
Fri Jun 16 17:46:19 EDT 2006
Erin Sheldon wrote:
>Anyway - Recarrays have convenience attributes such that
>fields may be accessed through "." in additioin to
>the "field()" method. These attributes are designed for
>read only; one cannot alter the data through them.
>Yet they are writeable:
>
>
>
>>>>tr=numpy.recarray(10, formats='i4,f8,f8', names='id,ra,dec')
>>>>tr.field('ra')[:] = 0.0
>>>>tr.ra
>>>>
>>>>
>array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>
>
>
>>>>tr.ra = 3
>>>>tr.ra
>>>>
>>>>
>3
>
>
>>>>tr.field('ra')
>>>>
>>>>
>array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>
>I feel this should raise an exception, just as with trying to write
>to the "size" attribute. Any thoughts?
>
>
I have not used recarrays much, so take this with the appropriate
measure of salt.
I'd prefer to drop the entire pseudo-attribute thing completely before
it gets entrenched. (Perhaps it's too late.)
I've used a similar system in pytables which, although it is convenient
in the short term and for interactive use, there are corner cases that
result in long term headaches. I think you point out one such issue for
recarrays. There will be more. For example:
In [1]:import numpy
In [2]:tr=numpy.recarray(10, formats='i4,f8,f8', names='id,ra,dec')
In [3]:tr.field('ra')[:] = 0.0
In [4]:tr.ra
Out[4]:array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [5]:del tr.ra
---------------------------------------------------------------------------
exceptions.AttributeError Traceback (most
recent call last)
/home/astraw/<console>
AttributeError: 'recarray' object has no attribute 'ra'
The above seems completely counterintuitive -- an attribute error for
something I just accessed? Yes, I know what's going on, but it certainly
makes life more confusing than it need be, IMO.
Another issue is that it is possible to have field names that are not
valid Python identifier strings.
More information about the NumPy-Discussion
mailing list