Q1: class cqIdxed: def __init__(self,q): self.q = q self.idx = 0 self.lenQ = len(self.q) def __iter__(self): return self def next(self): tmp = self.q[self.idx] self.idx += 1 if self.idx >= self.lenQ: self.idx = 0 return tmp myQ = cqIdxed([5,12,13]) i = 0 for qElement in myQ: print qElement i += 1 if i >= 5: break Q2: class dirColl(list): def __init__(self,dirList): self.dirList = dirList self.numDirs = len(dirList) self.i = 0 # indexes the collection def __iter__(self): return self def next(self): if self.i == self.numDirs: raise StopIteration thisDir = self.dirList[self.i] if not os.path.isdir(thisDir): print thisDir, ' is not a directory' raise StopIteration self.i += 1 return thisDir import os try: os.mkdir('x') except: pass try: os.mkdir('y') except: pass try: f = open('z','w') f.writelines('123\n') f.close() except: pass dc = dirColl(['x','y']) for d in dc: print d # prints 'x', 'y' dc1 = dirColl(['x','z','y']) for d in dc1: print d # prints 'x', then error