python技巧(8)
2017-01-30 17:32
281 查看
生成器函数(不是迭代器)生成可迭代对象,迭代器对象
使用yeild方法,把iter接口实现,可以保留程序的执行状态,并实现了Iterator,Iterable
使用yeild方法,把iter接口实现,可以保留程序的执行状态,并实现了Iterator,Iterable
def PrimeNumber(): def __init__(self,start,end): self.start=start self.end=end def isPrimeNumber(self,k): if k<2: return False for i in xrange(2,k): if k%i==0: return False return True def __iter__(self): for x in xrange(self.start,self.end+1): if self.isPrimeNumber(x): yeild x
相关文章推荐
- Python天天美味(10) - 除法小技巧
- 懒人的Python技巧#20060706
- Python技巧汇总
- Python编程技巧-使用状态机
- 在讨论群看到的一个python技巧
- zt:Python编程技巧笔记
- python技巧31[对象相等性|dictionary模拟switchcase]
- 手把手教您Python多线程应用技巧
- 懒人的Python技巧#20060708
- Python 文件操作技巧(File operation) 实例代码分析
- Python 学习笔记 - 14.技巧(Tips)
- Python 一些细节技巧汇总
- Python 过滤字符串的技巧,map与itertools.imap
- 懒人的Python技巧#20060708
- 懒人的Python技巧#20060706
- Python 文件操作技巧(File operation) 实例代码分析
- python技巧(3)——下划线,私有变量
- 在讨论群看到的一个python技巧
- Python技巧_处理带参数的超长URL