0

How do you make class to iterate by its internal numpy array : Just idea which does not work :

class ABC:

  def __init__(self):
     self.ary = np.zeros(50)

  def __iter__(self): return np.nditer(self.ary)
  def next(self): ...??..

Also how to make assignment work too :

abc = ABC()
abc[5] = 12
abc[7:9] 0
4
  • you don't need to define 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__ not next in Python 3) only if you want your class to be an iterator, in which case, __iter__ must return self. 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. Commented Mar 18, 2020 at 21:07
  • 1
    Don't use nditer. It's too difficult to use right, and doesn't offer any speed benefits. Commented Mar 18, 2020 at 22:03
  • 1
    What do you hope to do with thus class? Have you considered sublassing list? Commented Mar 18, 2020 at 22:41
  • What is this for? Commented Mar 19, 2020 at 1:05

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

why does it return arrays !! [i for i in t] Out[15]: [array(277, dtype=uint16), array(709, dtype=uint16), array(776, dtype=uint16), .......
@sten This is because of return np.diter(self.ary) in __iter__(self):, If you are planning to use this for 1-d array, you can change the return statement to return iter(self.ary) which will give the expected behaviour. For, Multi-dimensional cases np.diter would be better choice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.