python_序列构造
2015-12-16 22:17
471 查看
#类的序列构造 class MySeq: def __init__(self): self.lseq = ["I","II","III","IV"] def __len__(self): return len(self.lseq) def __getitem__(self, item): if 0 <= item <4: return self.lseq[item] if __name__ == '__main__': m = MySeq() for i in range(4): print(m[i]) #构造迭代器 class MyIter: def __init__(self,start,end): self.count = start self.end = end def __iter__(self): return self def __next__(self): if self.count < self.end: r=self.count self.count += 1 return r else: raise StopIteration if __name__ == '__main__': for i in MyIter(1,9): print(i)
相关文章推荐
- python_类的继承
- python_类的实例化
- python基础知识-字典
- python 正则表达式
- Python Excel操作之一
- python外部模块安装
- python 汉子转拼音库
- python爬虫实战之爬取知乎帖子
- python scrapy安装中的一点问题
- 电子邮件发送-python
- udp单播-广播-组播-python例子
- python-简单的makefile生成程序-没有注释
- 聚类算法(一):k-均值 (k-means)算法
- python(1)
- python之字符串格式化
- Python入门-1
- python --正则学习
- python函数的属性
- XGBoost:在Python中使用XGBoost
- 解决python3使用cx_Freeze打包成exe后不能运行