Iterator mode (Python)

Source: Internet
Author: User

Iterator Mode:Provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.

Python provides built-in support for this mode, so you do not need to write it yourself,

# Encoding = UTF-8 ## by Panda # iterator mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # iterator abstract class iterator: def first (Self): Pass def next (Self): Pass def isdone (Self): Pass def currentitem (Self ): pass # Set abstract class aggregate: def createiterator (Self): pass # specific iterator class: Class concreteiterator (iterator ): aggregate = none current = 0 def _ init _ (self, aggregate): Self. aggregate = aggrega Te self. current = 0 def first (Self): return self. aggregate [0] def next (Self): ret = none self. current + = 1 If (self. current <Len (self. aggregate): ret = self. aggregate [self. current] return ret def isdone (Self): If (self. current <Len (self. aggregate): Return false else: Return true def currentitem (Self): ret = none if (self. current <Len (self. aggregate): ret = self. aggregate [self. current] Return re T # specific collection class concreteaggregate (aggregate): items = none def _ init _ (Self): Self. items = [] def clientui (): A = concreteaggregate (). items. append ('Big Bird'). items. append ('food'). items. append ('luggage '). items. append ('foreigner '). items. append ('bus internal employee '). items. append ('thief ') printinfo (' --------- iterator mode ------------- ') I = concreteiterator (. items) item = I. first () while (false = I. isdone (): printinfo ("% s buy a ticket! "% I. currentitem (); I. next () printinfo ('\ n --------- internal Python iteration -------------') for item in. items: printinfo ("% s buy a ticket! "% Item); returnif _ name _ = '_ main _': clientui ();

Class Diagram:

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.