程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern behavioral pattern iterator pattern

編輯:Python

Catalog

List of articles

  • Catalog
  • Iterator pattern
  • Application scenarios
  • Code example

Iterator pattern

Iterator pattern , Provide an external interface , Achieve sequential access to aggregated data , But the internal mechanism of this data is not shown .

Python The iterator pattern is well supported by itself , A few lines of code can be implemented .

stay Python in :

  • generator : For a data set , The generator does not remember every element value , But record the element position in the loop and calculate the value according to the element generation rules , This form of side loop and side calculation is generator .
  • iterator : Is a way to access collections , Remember the traversal position , Access... From the first element , Until the last element , And can only move forward, not backward . All can pass through next() The objects accessed are all iterator types , That is to say, a generator is a kind of iterator ;
  • Iteratable object : image List、Set、Str This can be done through for The type of loop traversal is an iteratable object , This traversal order can be from end to end . Iteratable objects can pass through iter() Convert to iterator .

Application scenarios

Code example


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved