From documentation,
iterator.__next__():
Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.
For setting and getting values for container class, you need to implement __getitem__ and __setitem__.
For your sample code
class ABC():
def __init__(self):
self.ary = np.zeros(50)
self.index = self.ary.shape[0]
def __iter__(self):
return np.nditer(self.ary)
def next(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
def _check_indx(self, idx):
if abs(idx) >= self.ary.shape[0]:
raise IndexError(f"Invalid Index {idx} for array with shape {self.ary.shape}")
def __setitem__(self, idx, value):
self._check_indx(idx)
self.ary[idx] = value
def __getitem__(self, idx):
self._check_indx(idx)
return self.ary[idx]
next. Your class should work, so if it isn't working, you need to be specific about what doesn't work. "It doesn't work" isn't an adequate problem description. To make a class iterable you need to define__iter__. You only define__next__(note, it is__next__notnextin Python 3) only if you want your class to be an iterator, in which case,__iter__must returnself. You don't want to define an iterator, I think, but an iterable. Which you have. As to item-based assignment, please, also, avoid asking multiple questions.nditer. It's too difficult to use right, and doesn't offer any speed benefits.list?