setting the attributes of an array of object
![](https://secure.gravatar.com/avatar/211f6ffc1d92d0091415be4891cdb1ba.jpg?s=120&d=mm&r=g)
Hi, I'm wondering whether i can re-write the following idiom with numpy arrays: for i in some_list: i.some_attr = some_value it would be wonderful if one was able to write this idiom as arr[just_these].some_attr = some_value or setattr(arr[just_these], 'some_attr', some_value) since often expensive loops through lists of object could be avoided. any thoughts on this are much appreciated, thanks, -jelle
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
Hi, there is a way of doing this. As far as I know, you have to create your own "version of numpy arrays". E. g. try this: class myNumpy(numpy.ndarray): pass Then creating an instance as in a = myNumpy(otherNumpyArray) would make `a` behave just like any other array, except that you CAN attach attributes to it. Be carefull that some (many / most ?) operations on that array will return you a normal numpy array again. I don't understand the reasons behind all of this. HTH, Sebastian Haase On 9/29/07, jelle <jelleferinga@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/56b215661867f3b4f4a3b28077de66b3.jpg?s=120&d=mm&r=g)
On Tuesday 16 October 2007 11:47:35 Sebastian Haase wrote:
Maybe http://www.scipy.org/Subclasses could give you some hints ?
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
Hi, there is a way of doing this. As far as I know, you have to create your own "version of numpy arrays". E. g. try this: class myNumpy(numpy.ndarray): pass Then creating an instance as in a = myNumpy(otherNumpyArray) would make `a` behave just like any other array, except that you CAN attach attributes to it. Be carefull that some (many / most ?) operations on that array will return you a normal numpy array again. I don't understand the reasons behind all of this. HTH, Sebastian Haase On 9/29/07, jelle <jelleferinga@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/56b215661867f3b4f4a3b28077de66b3.jpg?s=120&d=mm&r=g)
On Tuesday 16 October 2007 11:47:35 Sebastian Haase wrote:
Maybe http://www.scipy.org/Subclasses could give you some hints ?
participants (4)
-
jelle
-
Pierre GM
-
Sebastian Haase
-
Travis E. Oliphant