Python Iteration Pattern Examples

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.