This article is mainly to share with you the Python iteration model examples, mainly in the form of code and we share, hope to help everyone.
#-*-Coding:utf-8-*-"" "Created on Thu Mar 11:43:05 2018@author:mz" "" Class Iterator (object): Def Next (self): Pass Def Hasnext (self): Pass def first: Pass Def Forward (self): Passclass cocre Teiterator (Iterator): Def __init__ (self, aggregate): Self._aggregate = aggregate def Next (self): R Eturn self._aggregate. Next () def hasnext (self): return self._aggregate. Hasnext () def first (self): return self._aggregate. First () def Forward (self): return self._aggregate. Forward () class Aggregate (object): Def createiterator (self): Pass def Next (self): Pass def H Asnext (self): Pass def First (self): Pass def Attach (self, obj): Pass def for Ward (self): Pass class Concreteaggregate (object): Def __init__ (self): Self._lst = [] self. _index = 0 def createiterator (self): RetuRN Cocreteiterator (self) def Next (self): return Self._lst[self._index] def hasnext (self): Retu RN Self._index < Len (SELF._LST) def first (self): Self._index = 0 return self._lst[0] def at Tach (self, obj): Self._lst.append (obj) def Forward (self): Self._index + = 1 if "__main_ _ "= = __name__: aggregate = Concreteaggregate () aggregate. Attach (1) aggregate. Attach ("2") aggregate. Attach ("a") aggregate. Attach ("B") aggregate. Attach ("C") aggregate. Attach ("the") it = aggregate. Createiterator () while it. Hasnext (): Print (it. Next ()) it. Forward ()
Operation Result:
12abc45