[Numpy-discussion] don't understand nditer

Neal Becker ndbecker2@gmail....
Thu Apr 5 12:20:12 CDT 2012


Along the lines of my question about apply getitem to each element...

If I try to use nditer, I seem to run into trouble:

 for d in np.nditer (y, ['refs_ok'],['readwrite']):
   ....:     y[...].w = 2
   ....: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/nbecker/hn-8psk/<ipython-input-73-713fc95a44d2> in <module>()
      1 for d in np.nditer (y, ['refs_ok'],['readwrite']):
----> 2         y[...].w = 2
      3 

AttributeError: 'numpy.ndarray' object has no attribute 'w'

y is a 2D array of 'noop' class instances:

class noop:
  pass
 
y
Out[75]: 
array([[<__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>],
       [<__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>,
        <__main__.noop instance at 0x4241098>]], dtype=object)

Any idea how to do this setattr per element using nditer?



More information about the NumPy-Discussion mailing list