#类的序列构造
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_序列构造
最新推荐文章于 2022-05-20 15:20:57 发布