I'm not sure if this title is good..Hope someone could help to change it.
I'm trying to define an molecule
class, and hope it can iterate over its atoms.
I searched how people define an iterable class, and it may look like this:
class molecule(object):
def __init__(self,name):
self.__pointer=0
# ...something else
def __iter__(self):
self.__pointer=0
return self
def __next__(self):
self.__pointer+=1
if self.__pointer<=self.natoms: # self.natoms is defined somewhere
return self[self.__pointer] # suppose __getitem__ is defined to return an atom object
else:
raise StopIteration("Exceeded all atoms")
It works well:
>>> ben=molecule('ben') #defined a molecule
>>> ben.addatom(...) # and defined some atoms
>>> ite=iter(ben)
>>> for atom in ite:
... print(atom)
...
# atom objects are correctly printed here
However, I found it cannot work in two iterators, if they exist simultaneously.
>>> ite=iter(ben)
>>> next(ite)
# atom1
>>> next(ite)
# atom2
>>> ite2=iter(ben)
>>> next(ite)
# atom1 again, where atom3 is expected
>>> next(ite2)
# atom2, where atom1 is expected
This is not a surprise because the two iterators share a same self.__pointer
, so defining a new iterator will renew the pointer to zero.
I had a look at this page How to make class iterable?, and most of them use a self.__pointer
within the class, which raises my question.
I guess if the pointer
be an attribute of the iterator(ite
or ite2
) but not of the iterated object itself (molecule
), then this problem could be solved.
Hope someone can give some help:) Thanks.